Extract more common test code to function

This commit is contained in:
Sam Partington 2020-08-28 17:20:40 +01:00
parent 23cf700e38
commit abf5854149
3 changed files with 22 additions and 29 deletions

View file

@ -33,6 +33,10 @@ function setInput(name, value) {
delete process.env[envVar];
}
}
function setConfigFile(inputFileContents, tmpDir) {
fs.writeFileSync(path.join(tmpDir, 'input'), inputFileContents, 'utf8');
setInput('config-file', 'input');
}
function mockGetContents(content) {
// Passing an auth token is required, so we just use a dummy value
let client = new github.GitHub('123');
@ -175,6 +179,7 @@ ava_1.default("load non-empty input", async (t) => {
- b
paths:
- c/d`;
setConfigFile(inputFileContents, tmpDir);
fs.mkdirSync(path.join(tmpDir, 'foo'));
// And the config we expect it to parse to
const expectedConfig = {
@ -193,8 +198,6 @@ ava_1.default("load non-empty input", async (t) => {
toolCacheDir: tmpDir,
codeQLCmd: codeQL.getPath(),
};
fs.writeFileSync(path.join(tmpDir, 'input'), inputFileContents, 'utf8');
setInput('config-file', 'input');
setInput('languages', 'javascript');
const actualConfig = await configUtils.initConfig(tmpDir, tmpDir, codeQL);
// Should exactly equal the object we constructed earlier
@ -231,9 +234,8 @@ ava_1.default("default queries are used", async (t) => {
const inputFileContents = `
paths:
- foo`;
setConfigFile(inputFileContents, tmpDir);
fs.mkdirSync(path.join(tmpDir, 'foo'));
fs.writeFileSync(path.join(tmpDir, 'input'), inputFileContents, 'utf8');
setInput('config-file', 'input');
setInput('languages', 'javascript');
await configUtils.initConfig(tmpDir, tmpDir, codeQL);
// Check resolve queries was called correctly
@ -266,8 +268,7 @@ ava_1.default("Queries can be specified in config file", async (t) => {
name: my config
queries:
- uses: ./foo`;
fs.writeFileSync(path.join(tmpDir, 'input'), inputFileContents, 'utf8');
setInput('config-file', 'input');
setConfigFile(inputFileContents, tmpDir);
fs.mkdirSync(path.join(tmpDir, 'foo'));
const resolveQueriesArgs = [];
const codeQL = codeql_1.setCodeQL({
@ -298,8 +299,7 @@ ava_1.default("Queries from config file can be overridden in workflow file", asy
name: my config
queries:
- uses: ./foo`;
fs.writeFileSync(path.join(tmpDir, 'input'), inputFileContents, 'utf8');
setInput('config-file', 'input');
setConfigFile(inputFileContents, tmpDir);
// This config item should take precedence over the config file but shouldn't affect the default queries.
setInput('queries', './override');
fs.mkdirSync(path.join(tmpDir, 'foo'));
@ -332,8 +332,7 @@ ava_1.default("Queries in workflow file can be used in tandem with the 'disable
const inputFileContents = `
name: my config
disable-default-queries: true`;
fs.writeFileSync(path.join(tmpDir, 'input'), inputFileContents, 'utf8');
setInput('config-file', 'input');
setConfigFile(inputFileContents, tmpDir);
setInput('queries', './workflow-query');
fs.mkdirSync(path.join(tmpDir, 'workflow-query'));
const resolveQueriesArgs = [];
@ -395,8 +394,7 @@ ava_1.default("Queries in workflow file can be added to the set of queries witho
name: my config
queries:
- uses: ./foo`;
fs.writeFileSync(path.join(tmpDir, 'input'), inputFileContents, 'utf8');
setInput('config-file', 'input');
setConfigFile(inputFileContents, tmpDir);
// These queries shouldn't override anything, because the value is prefixed with "+"
setInput('queries', '+./additional1,./additional2');
fs.mkdirSync(path.join(tmpDir, 'foo'));

File diff suppressed because one or more lines are too long

View file

@ -24,6 +24,11 @@ function setInput(name: string, value: string | undefined) {
}
}
function setConfigFile(inputFileContents: string, tmpDir: string) {
fs.writeFileSync(path.join(tmpDir, 'input'), inputFileContents, 'utf8');
setInput('config-file', 'input');
}
type GetContentsResponse = { content?: string; } | {}[];
function mockGetContents(content: GetContentsResponse): sinon.SinonStub<any, any> {
@ -192,6 +197,7 @@ test("load non-empty input", async t => {
- b
paths:
- c/d`;
setConfigFile(inputFileContents, tmpDir);
fs.mkdirSync(path.join(tmpDir, 'foo'));
@ -213,8 +219,6 @@ test("load non-empty input", async t => {
codeQLCmd: codeQL.getPath(),
};
fs.writeFileSync(path.join(tmpDir, 'input'), inputFileContents, 'utf8');
setInput('config-file', 'input');
setInput('languages', 'javascript');
const actualConfig = await configUtils.initConfig(tmpDir, tmpDir, codeQL);
@ -257,11 +261,10 @@ test("default queries are used", async t => {
const inputFileContents = `
paths:
- foo`;
setConfigFile(inputFileContents, tmpDir);
fs.mkdirSync(path.join(tmpDir, 'foo'));
fs.writeFileSync(path.join(tmpDir, 'input'), inputFileContents, 'utf8');
setInput('config-file', 'input');
setInput('languages', 'javascript');
await configUtils.initConfig(tmpDir, tmpDir, codeQL);
@ -299,9 +302,7 @@ test("Queries can be specified in config file", async t => {
name: my config
queries:
- uses: ./foo`;
fs.writeFileSync(path.join(tmpDir, 'input'), inputFileContents, 'utf8');
setInput('config-file', 'input');
setConfigFile(inputFileContents, tmpDir);
fs.mkdirSync(path.join(tmpDir, 'foo'));
@ -340,9 +341,7 @@ test("Queries from config file can be overridden in workflow file", async t => {
name: my config
queries:
- uses: ./foo`;
fs.writeFileSync(path.join(tmpDir, 'input'), inputFileContents, 'utf8');
setInput('config-file', 'input');
setConfigFile(inputFileContents, tmpDir);
// This config item should take precedence over the config file but shouldn't affect the default queries.
setInput('queries', './override');
@ -384,9 +383,7 @@ test("Queries in workflow file can be used in tandem with the 'disable default q
const inputFileContents = `
name: my config
disable-default-queries: true`;
fs.writeFileSync(path.join(tmpDir, 'input'), inputFileContents, 'utf8');
setInput('config-file', 'input');
setConfigFile(inputFileContents, tmpDir);
setInput('queries', './workflow-query');
fs.mkdirSync(path.join(tmpDir, 'workflow-query'));
@ -464,9 +461,7 @@ test("Queries in workflow file can be added to the set of queries without overri
name: my config
queries:
- uses: ./foo`;
fs.writeFileSync(path.join(tmpDir, 'input'), inputFileContents, 'utf8');
setInput('config-file', 'input');
setConfigFile(inputFileContents, tmpDir);
// These queries shouldn't override anything, because the value is prefixed with "+"
setInput('queries', '+./additional1,./additional2');