Skip to content

Commit 29cf065

Browse files
reset environment variables between tests
1 parent ee63f4e commit 29cf065

File tree

6 files changed

+28
-3
lines changed

6 files changed

+28
-3
lines changed

lib/config-utils.test.js

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/config-utils.test.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/testing-utils.js

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/testing-utils.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/config-utils.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ test("load non-empty input", async t => {
191191

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

195196
const actualConfig = await configUtils.initConfig();
196197

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

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

237239
await configUtils.initConfig();
238240

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

281283
setInput('config-file', 'octo-org/codeql-config/config.yaml@main');
284+
setInput('languages', 'javascript');
285+
282286
await configUtils.initConfig();
283287
t.assert(spyGetContents.called);
284288
});
@@ -347,6 +351,7 @@ function doInvalidInputTest(
347351
const inputFile = path.join(tmpDir, 'input');
348352
fs.writeFileSync(inputFile, inputFileContents, 'utf8');
349353
setInput('config-file', 'input');
354+
setInput('languages', 'javascript');
350355

351356
try {
352357
await configUtils.initConfig();

src/testing-utils.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import sinon from 'sinon';
33

44
import * as CodeQL from './codeql';
55

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

88
function wrapOutput(context: TestContext) {
99
// Function signature taken from Socket.write.
@@ -49,6 +49,12 @@ export function setupTests(test: TestInterface<any>) {
4949
const processStderrWrite = process.stderr.write.bind(process.stderr);
5050
t.context.stderrWrite = processStderrWrite;
5151
process.stderr.write = wrapOutput(t.context) as any;
52+
53+
// Many tests modify environment variables. Take a copy now so that
54+
// We reset them after the test to keep tests independent of each other.
55+
// process.env only has strings fields, so a shallow copy is fine.
56+
t.context.env = {};
57+
Object.assign(process.env, t.context.env);
5258
});
5359

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

6369
// Undo any modifications made by sinon
6470
sinon.restore();
71+
72+
// Undo any modifications to the env
73+
process.env = t.context.env;
6574
});
6675
}

0 commit comments

Comments
 (0)