reset environment variables between tests
This commit is contained in:
parent
ee63f4ee4b
commit
29cf06569d
6 changed files with 28 additions and 3 deletions
4
lib/config-utils.test.js
generated
4
lib/config-utils.test.js
generated
|
|
@ -172,6 +172,7 @@ ava_1.default("load non-empty input", async (t) => {
|
||||||
};
|
};
|
||||||
fs.writeFileSync(path.join(tmpDir, 'input'), inputFileContents, 'utf8');
|
fs.writeFileSync(path.join(tmpDir, 'input'), inputFileContents, 'utf8');
|
||||||
setInput('config-file', 'input');
|
setInput('config-file', 'input');
|
||||||
|
setInput('languages', 'javascript');
|
||||||
const actualConfig = await configUtils.initConfig();
|
const actualConfig = await configUtils.initConfig();
|
||||||
// Should exactly equal the object we constructed earlier
|
// Should exactly equal the object we constructed earlier
|
||||||
t.deepEqual(actualConfig, expectedConfig);
|
t.deepEqual(actualConfig, expectedConfig);
|
||||||
|
|
@ -206,6 +207,7 @@ ava_1.default("default queries are used", async (t) => {
|
||||||
fs.mkdirSync(path.join(tmpDir, 'foo'));
|
fs.mkdirSync(path.join(tmpDir, 'foo'));
|
||||||
fs.writeFileSync(path.join(tmpDir, 'input'), inputFileContents, 'utf8');
|
fs.writeFileSync(path.join(tmpDir, 'input'), inputFileContents, 'utf8');
|
||||||
setInput('config-file', 'input');
|
setInput('config-file', 'input');
|
||||||
|
setInput('languages', 'javascript');
|
||||||
await configUtils.initConfig();
|
await configUtils.initConfig();
|
||||||
// Check resolve queries was called correctly
|
// Check resolve queries was called correctly
|
||||||
t.deepEqual(resolveQueriesArgs.length, 1);
|
t.deepEqual(resolveQueriesArgs.length, 1);
|
||||||
|
|
@ -245,6 +247,7 @@ ava_1.default("API client used when reading remote config", async (t) => {
|
||||||
// Create checkout directory for remote queries repository
|
// Create checkout directory for remote queries repository
|
||||||
fs.mkdirSync(path.join(tmpDir, 'foo/bar'), { recursive: true });
|
fs.mkdirSync(path.join(tmpDir, 'foo/bar'), { recursive: true });
|
||||||
setInput('config-file', 'octo-org/codeql-config/config.yaml@main');
|
setInput('config-file', 'octo-org/codeql-config/config.yaml@main');
|
||||||
|
setInput('languages', 'javascript');
|
||||||
await configUtils.initConfig();
|
await configUtils.initConfig();
|
||||||
t.assert(spyGetContents.called);
|
t.assert(spyGetContents.called);
|
||||||
});
|
});
|
||||||
|
|
@ -302,6 +305,7 @@ function doInvalidInputTest(testName, inputFileContents, expectedErrorMessageGen
|
||||||
const inputFile = path.join(tmpDir, 'input');
|
const inputFile = path.join(tmpDir, 'input');
|
||||||
fs.writeFileSync(inputFile, inputFileContents, 'utf8');
|
fs.writeFileSync(inputFile, inputFileContents, 'utf8');
|
||||||
setInput('config-file', 'input');
|
setInput('config-file', 'input');
|
||||||
|
setInput('languages', 'javascript');
|
||||||
try {
|
try {
|
||||||
await configUtils.initConfig();
|
await configUtils.initConfig();
|
||||||
throw new Error('initConfig did not throw error');
|
throw new Error('initConfig did not throw error');
|
||||||
|
|
|
||||||
File diff suppressed because one or more lines are too long
7
lib/testing-utils.js
generated
7
lib/testing-utils.js
generated
|
|
@ -51,6 +51,11 @@ function setupTests(test) {
|
||||||
const processStderrWrite = process.stderr.write.bind(process.stderr);
|
const processStderrWrite = process.stderr.write.bind(process.stderr);
|
||||||
t.context.stderrWrite = processStderrWrite;
|
t.context.stderrWrite = processStderrWrite;
|
||||||
process.stderr.write = wrapOutput(t.context);
|
process.stderr.write = wrapOutput(t.context);
|
||||||
|
// 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 => {
|
typedTest.afterEach.always(t => {
|
||||||
// Restore stdout and stderr
|
// Restore stdout and stderr
|
||||||
|
|
@ -62,6 +67,8 @@ function setupTests(test) {
|
||||||
}
|
}
|
||||||
// Undo any modifications made by sinon
|
// Undo any modifications made by sinon
|
||||||
sinon_1.default.restore();
|
sinon_1.default.restore();
|
||||||
|
// Undo any modifications to the env
|
||||||
|
process.env = t.context.env;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
exports.setupTests = setupTests;
|
exports.setupTests = setupTests;
|
||||||
|
|
|
||||||
|
|
@ -1 +1 @@
|
||||||
{"version":3,"file":"testing-utils.js","sourceRoot":"","sources":["../src/testing-utils.ts"],"names":[],"mappings":";;;;;;;;;;;;AACA,kDAA0B;AAE1B,iDAAmC;AAInC,SAAS,UAAU,CAAC,OAAoB;IACtC,8CAA8C;IAC9C,gCAAgC;IAChC,2EAA2E;IAC3E,2FAA2F;IAC3F,OAAO,CAAC,KAA0B,EAAE,QAAiB,EAAE,EAA0B,EAAW,EAAE;QAC5F,2CAA2C;QAC3C,IAAI,EAAE,KAAK,SAAS,IAAI,OAAO,QAAQ,KAAK,UAAU,EAAE;YACtD,EAAE,GAAG,QAAQ,CAAC;YACd,QAAQ,GAAG,SAAS,CAAC;SACtB;QAED,oBAAoB;QACpB,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE;YAC7B,OAAO,CAAC,UAAU,IAAI,KAAK,CAAC;SAC7B;aAAM;YACL,OAAO,CAAC,UAAU,IAAI,IAAI,WAAW,CAAC,QAAQ,IAAI,OAAO,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;SAC1E;QAED,iDAAiD;QACjD,IAAI,EAAE,KAAK,SAAS,IAAI,OAAO,EAAE,KAAK,UAAU,EAAE;YAChD,EAAE,EAAE,CAAC;SACN;QAED,OAAO,IAAI,CAAC;IACd,CAAC,CAAC;AACJ,CAAC;AAED,SAAgB,UAAU,CAAC,IAAwB;IACjD,MAAM,SAAS,GAAG,IAAkC,CAAC;IAErD,SAAS,CAAC,UAAU,CAAC,CAAC,CAAC,EAAE;QACvB,gEAAgE;QAChE,0CAA0C;QAC1C,MAAM,CAAC,SAAS,CAAC,EAAE,CAAC,CAAC;QAErB,iEAAiE;QACjE,CAAC,CAAC,OAAO,CAAC,UAAU,GAAG,EAAE,CAAC;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AW,GAAG,kBAAkB,CAAC;QAC3C,OAAO,CAAC,MAAM,CAAC,KAAK,GAAG,UAAU,CAAC,CAAC,CAAC,OAAO,CAAQ,CAAC;QAEpD,mEAAmE;QACnE,wEAAwE;QACxE,kEAAkE;QAClE,CAAC,CAAC,OAAO,CAAC,GAAG,GAAG,EAAE,CAAC;QACnB,MAAM,CAAC,MAAM,CAAC,OAAO,CAAC,GAAG,EAAE,CAAC,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;IAC5C,CAAC,CAAC,CAAC;IAEH,SAAS,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE;QAC7B,4BAA4B;QAC5B,0DAA0D;QAC1D,OAAO,CAAC,MAAM,CAAC,KAAK,GAAG,CAAC,CAAC,OAAO,CAAC,WAAW,CAAC;QAC7C,OAAO,CAAC,MAAM,CAAC,KAAK,GAAG,CAAC,CAAC,OAAO,CAAC,WAAW,CAAC;QAC7C,IAAI,CAAC,CAAC,CAAC,MAAM,EAAE;YACb,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,OAAO,CAAC,UAAU,CAAC,CAAC;SAC5C;QAED,uCAAuC;QACvC,eAAK,CAAC,OAAO,EAAE,CAAC;QAEhB,oCAAoC;QACpC,OAAO,CAAC,GAAG,GAAG,CAAC,CAAC,OAAO,CAAC,GAAG,CAAC;IAC9B,CAAC,CAAC,CAAC;AACL,CAAC;AAvCD,gCAuCC"}
|
||||||
|
|
@ -191,6 +191,7 @@ test("load non-empty input", async t => {
|
||||||
|
|
||||||
fs.writeFileSync(path.join(tmpDir, 'input'), inputFileContents, 'utf8');
|
fs.writeFileSync(path.join(tmpDir, 'input'), inputFileContents, 'utf8');
|
||||||
setInput('config-file', 'input');
|
setInput('config-file', 'input');
|
||||||
|
setInput('languages', 'javascript');
|
||||||
|
|
||||||
const actualConfig = await configUtils.initConfig();
|
const actualConfig = await configUtils.initConfig();
|
||||||
|
|
||||||
|
|
@ -233,6 +234,7 @@ test("default queries are used", async t => {
|
||||||
|
|
||||||
fs.writeFileSync(path.join(tmpDir, 'input'), inputFileContents, 'utf8');
|
fs.writeFileSync(path.join(tmpDir, 'input'), inputFileContents, 'utf8');
|
||||||
setInput('config-file', 'input');
|
setInput('config-file', 'input');
|
||||||
|
setInput('languages', 'javascript');
|
||||||
|
|
||||||
await configUtils.initConfig();
|
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 });
|
fs.mkdirSync(path.join(tmpDir, 'foo/bar'), { recursive: true });
|
||||||
|
|
||||||
setInput('config-file', 'octo-org/codeql-config/config.yaml@main');
|
setInput('config-file', 'octo-org/codeql-config/config.yaml@main');
|
||||||
|
setInput('languages', 'javascript');
|
||||||
|
|
||||||
await configUtils.initConfig();
|
await configUtils.initConfig();
|
||||||
t.assert(spyGetContents.called);
|
t.assert(spyGetContents.called);
|
||||||
});
|
});
|
||||||
|
|
@ -347,6 +351,7 @@ function doInvalidInputTest(
|
||||||
const inputFile = path.join(tmpDir, 'input');
|
const inputFile = path.join(tmpDir, 'input');
|
||||||
fs.writeFileSync(inputFile, inputFileContents, 'utf8');
|
fs.writeFileSync(inputFile, inputFileContents, 'utf8');
|
||||||
setInput('config-file', 'input');
|
setInput('config-file', 'input');
|
||||||
|
setInput('languages', 'javascript');
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await configUtils.initConfig();
|
await configUtils.initConfig();
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,7 @@ import sinon from 'sinon';
|
||||||
|
|
||||||
import * as CodeQL from './codeql';
|
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 wrapOutput(context: TestContext) {
|
||||||
// Function signature taken from Socket.write.
|
// Function signature taken from Socket.write.
|
||||||
|
|
@ -49,6 +49,12 @@ export function setupTests(test: TestInterface<any>) {
|
||||||
const processStderrWrite = process.stderr.write.bind(process.stderr);
|
const processStderrWrite = process.stderr.write.bind(process.stderr);
|
||||||
t.context.stderrWrite = processStderrWrite;
|
t.context.stderrWrite = processStderrWrite;
|
||||||
process.stderr.write = wrapOutput(t.context) as any;
|
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 => {
|
typedTest.afterEach.always(t => {
|
||||||
|
|
@ -62,5 +68,8 @@ export function setupTests(test: TestInterface<any>) {
|
||||||
|
|
||||||
// Undo any modifications made by sinon
|
// Undo any modifications made by sinon
|
||||||
sinon.restore();
|
sinon.restore();
|
||||||
|
|
||||||
|
// Undo any modifications to the env
|
||||||
|
process.env = t.context.env;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue