Skip to content

Commit

Permalink
reset environment variables between tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert committed Jul 21, 2020
1 parent ee63f4e commit 29cf065
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 3 deletions.
4 changes: 4 additions & 0 deletions lib/config-utils.test.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/config-utils.test.js.map

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions lib/testing-utils.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/testing-utils.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/config-utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ test("load non-empty input", async t => {

fs.writeFileSync(path.join(tmpDir, 'input'), inputFileContents, 'utf8');
setInput('config-file', 'input');
setInput('languages', 'javascript');

const actualConfig = await configUtils.initConfig();

Expand Down Expand Up @@ -233,6 +234,7 @@ test("default queries are used", async t => {

fs.writeFileSync(path.join(tmpDir, 'input'), inputFileContents, 'utf8');
setInput('config-file', 'input');
setInput('languages', 'javascript');

await configUtils.initConfig();

Expand Down Expand Up @@ -279,6 +281,8 @@ test("API client used when reading remote config", async t => {
fs.mkdirSync(path.join(tmpDir, 'foo/bar'), { recursive: true });

setInput('config-file', 'octo-org/codeql-config/config.yaml@main');
setInput('languages', 'javascript');

await configUtils.initConfig();
t.assert(spyGetContents.called);
});
Expand Down Expand Up @@ -347,6 +351,7 @@ function doInvalidInputTest(
const inputFile = path.join(tmpDir, 'input');
fs.writeFileSync(inputFile, inputFileContents, 'utf8');
setInput('config-file', 'input');
setInput('languages', 'javascript');

try {
await configUtils.initConfig();
Expand Down
11 changes: 10 additions & 1 deletion src/testing-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import sinon from 'sinon';

import * as CodeQL from './codeql';

type TestContext = {stdoutWrite: any, stderrWrite: any, testOutput: string};
type TestContext = {stdoutWrite: any, stderrWrite: any, testOutput: string, env: NodeJS.ProcessEnv};

function wrapOutput(context: TestContext) {
// Function signature taken from Socket.write.
Expand Down Expand Up @@ -49,6 +49,12 @@ export function setupTests(test: TestInterface<any>) {
const processStderrWrite = process.stderr.write.bind(process.stderr);
t.context.stderrWrite = processStderrWrite;
process.stderr.write = wrapOutput(t.context) as any;

// Many tests modify environment variables. Take a copy now so that
// We reset them after the test to keep tests independent of each other.
// process.env only has strings fields, so a shallow copy is fine.
t.context.env = {};
Object.assign(process.env, t.context.env);
});

typedTest.afterEach.always(t => {
Expand All @@ -62,5 +68,8 @@ export function setupTests(test: TestInterface<any>) {

// Undo any modifications made by sinon
sinon.restore();

// Undo any modifications to the env
process.env = t.context.env;
});
}

0 comments on commit 29cf065

Please sign in to comment.