Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
reset environment variables between tests
Robert committed Jul 21, 2020

Unverified

No user is associated with the committer email.
1 parent ee63f4e commit 29cf065
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
@@ -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();

@@ -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();

@@ -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);
});
@@ -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();
11 changes: 10 additions & 1 deletion src/testing-utils.ts
@@ -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.
@@ -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 => {
@@ -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.