reset environment variables between tests

This commit is contained in:
Robert 2020-07-21 11:24:37 +01:00
parent ee63f4ee4b
commit 29cf06569d
6 changed files with 28 additions and 3 deletions

View file

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

View file

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