Refactor common code to function and add missing test

This commit is contained in:
Sam Partington 2020-08-24 15:37:07 +01:00
parent c6f02973ac
commit 7f19f9198a
6 changed files with 137 additions and 27 deletions

30
lib/config-utils.js generated
View file

@ -343,6 +343,20 @@ async function getLanguages() {
}
return languages;
}
/**
* Returns true if queries were provided in the workflow file
* (and thus added), otherwise false
*/
function addQueriesFromWorkflowIfRequired(configFile, languages, resultMap) {
const queryUses = core.getInput('queries');
if (queryUses) {
queryUses.split(',').forEach(async (query) => {
await parseQueryUses(configFile, languages, resultMap, query);
});
return true;
}
return false;
}
/**
* Get the default config for when the user has not supplied one.
*/
@ -350,12 +364,7 @@ async function getDefaultConfig() {
const languages = await getLanguages();
const queries = {};
await addDefaultQueries(languages, queries);
const queryUses = core.getInput('queries');
if (queryUses) {
queryUses.split(',').forEach(async (query) => {
await parseQueryUses('', languages, queries, query);
});
}
addQueriesFromWorkflowIfRequired('', languages, queries);
return {
languages: languages,
queries: queries,
@ -410,13 +419,8 @@ async function loadConfig(configFile) {
}
// If queries were provided using `with` in the action configuration,
// they should take precedence over the queries in the config file
const queryUses = core.getInput('queries');
if (queryUses) {
queryUses.split(',').forEach(async (query) => {
await parseQueryUses(configFile, languages, queries, query);
});
}
else if (QUERIES_PROPERTY in parsedYAML) {
const addedQueriesFromAction = addQueriesFromWorkflowIfRequired(configFile, languages, queries);
if (!addedQueriesFromAction && QUERIES_PROPERTY in parsedYAML) {
if (!(parsedYAML[QUERIES_PROPERTY] instanceof Array)) {
throw new Error(getQueriesInvalid(configFile));
}

File diff suppressed because one or more lines are too long

View file

@ -224,6 +224,49 @@ ava_1.default("default queries are used", async (t) => {
t.deepEqual(resolveQueriesArgs[0].extraSearchPath, undefined);
});
});
ava_1.default("Queries can be specified in config file", async (t) => {
return await util.withTmpDir(async (tmpDir) => {
process.env['RUNNER_TEMP'] = tmpDir;
process.env['GITHUB_WORKSPACE'] = tmpDir;
const inputFileContents = `
name: my config
queries:
- uses: ./foo`;
fs.writeFileSync(path.join(tmpDir, 'input'), inputFileContents, 'utf8');
setInput('config-file', 'input');
fs.mkdirSync(path.join(tmpDir, 'foo'));
const resolveQueriesArgs = [];
CodeQL.setCodeQL({
resolveQueries: async function (queries, extraSearchPath) {
resolveQueriesArgs.push({ queries, extraSearchPath });
// Return what we're given, just in the right format for a resolved query
// This way we can test by seeing which returned items are in the final
// configuration.
const dummyResolvedQueries = {};
queries.forEach(q => { dummyResolvedQueries[q] = {}; });
return {
byLanguage: {
'javascript': dummyResolvedQueries,
},
noDeclaredLanguage: {},
multipleDeclaredLanguages: {},
};
},
});
setInput('languages', 'javascript');
const config = await configUtils.initConfig();
// Check resolveQueries was called correctly
// It'll be called once for the default queries
// and once for `./foo` from the config file.
t.deepEqual(resolveQueriesArgs.length, 2);
t.deepEqual(resolveQueriesArgs[1].queries.length, 1);
t.regex(resolveQueriesArgs[1].queries[0], /.*\/foo$/);
// Now check that the end result contains the default queries and the query from config
t.deepEqual(config.queries['javascript'].length, 2);
t.regex(config.queries['javascript'][0], /javascript-code-scanning.qls$/);
t.regex(config.queries['javascript'][1], /.*\/foo$/);
});
});
ava_1.default("Queries from config file can be overridden in workflow file", async (t) => {
return await util.withTmpDir(async (tmpDir) => {
process.env['RUNNER_TEMP'] = tmpDir;

File diff suppressed because one or more lines are too long

View file

@ -254,6 +254,58 @@ test("default queries are used", async t => {
});
});
test("Queries can be specified in config file", async t => {
return await util.withTmpDir(async tmpDir => {
process.env['RUNNER_TEMP'] = tmpDir;
process.env['GITHUB_WORKSPACE'] = tmpDir;
const inputFileContents = `
name: my config
queries:
- uses: ./foo`;
fs.writeFileSync(path.join(tmpDir, 'input'), inputFileContents, 'utf8');
setInput('config-file', 'input');
fs.mkdirSync(path.join(tmpDir, 'foo'));
const resolveQueriesArgs: {queries: string[], extraSearchPath: string | undefined}[] = [];
CodeQL.setCodeQL({
resolveQueries: async function(queries: string[], extraSearchPath: string | undefined) {
resolveQueriesArgs.push({queries, extraSearchPath});
// Return what we're given, just in the right format for a resolved query
// This way we can test by seeing which returned items are in the final
// configuration.
const dummyResolvedQueries = {};
queries.forEach(q => { dummyResolvedQueries[q] = {}; });
return {
byLanguage: {
'javascript': dummyResolvedQueries,
},
noDeclaredLanguage: {},
multipleDeclaredLanguages: {},
};
},
});
setInput('languages', 'javascript');
const config = await configUtils.initConfig();
// Check resolveQueries was called correctly
// It'll be called once for the default queries
// and once for `./foo` from the config file.
t.deepEqual(resolveQueriesArgs.length, 2);
t.deepEqual(resolveQueriesArgs[1].queries.length, 1);
t.regex(resolveQueriesArgs[1].queries[0], /.*\/foo$/);
// Now check that the end result contains the default queries and the query from config
t.deepEqual(config.queries['javascript'].length, 2);
t.regex(config.queries['javascript'][0], /javascript-code-scanning.qls$/);
t.regex(config.queries['javascript'][1], /.*\/foo$/);
});
});
test("Queries from config file can be overridden in workflow file", async t => {
return await util.withTmpDir(async tmpDir => {
process.env['RUNNER_TEMP'] = tmpDir;

View file

@ -471,6 +471,26 @@ async function getLanguages(): Promise<string[]> {
return languages;
}
/**
* Returns true if queries were provided in the workflow file
* (and thus added), otherwise false
*/
function addQueriesFromWorkflowIfRequired(
configFile: string,
languages: string[],
resultMap: { [language: string]: string[] }
): boolean {
const queryUses = core.getInput('queries');
if (queryUses) {
queryUses.split(',').forEach(async query => {
await parseQueryUses(configFile, languages, resultMap, query);
});
return true;
}
return false;
}
/**
* Get the default config for when the user has not supplied one.
*/
@ -478,12 +498,7 @@ export async function getDefaultConfig(): Promise<Config> {
const languages = await getLanguages();
const queries = {};
await addDefaultQueries(languages, queries);
const queryUses = core.getInput('queries');
if (queryUses) {
queryUses.split(',').forEach(async query => {
await parseQueryUses('', languages, queries, query);
});
}
addQueriesFromWorkflowIfRequired('', languages, queries);
return {
languages: languages,
@ -545,12 +560,8 @@ async function loadConfig(configFile: string): Promise<Config> {
// If queries were provided using `with` in the action configuration,
// they should take precedence over the queries in the config file
const queryUses = core.getInput('queries');
if (queryUses) {
queryUses.split(',').forEach(async query => {
await parseQueryUses(configFile, languages, queries, query);
});
} else if (QUERIES_PROPERTY in parsedYAML) {
const addedQueriesFromAction = addQueriesFromWorkflowIfRequired(configFile, languages, queries);
if (!addedQueriesFromAction && QUERIES_PROPERTY in parsedYAML) {
if (!(parsedYAML[QUERIES_PROPERTY] instanceof Array)) {
throw new Error(getQueriesInvalid(configFile));
}