Skip to content

Commit 7f19f91

Browse files
committed
Refactor common code to function and add missing test
1 parent c6f0297 commit 7f19f91

6 files changed

Lines changed: 137 additions & 27 deletions

File tree

lib/config-utils.js

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

lib/config-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.

lib/config-utils.test.js

Lines changed: 43 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.

src/config-utils.test.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,58 @@ test("default queries are used", async t => {
254254
});
255255
});
256256

257+
test("Queries can be specified in config file", async t => {
258+
return await util.withTmpDir(async tmpDir => {
259+
process.env['RUNNER_TEMP'] = tmpDir;
260+
process.env['GITHUB_WORKSPACE'] = tmpDir;
261+
262+
const inputFileContents = `
263+
name: my config
264+
queries:
265+
- uses: ./foo`;
266+
267+
fs.writeFileSync(path.join(tmpDir, 'input'), inputFileContents, 'utf8');
268+
setInput('config-file', 'input');
269+
270+
fs.mkdirSync(path.join(tmpDir, 'foo'));
271+
272+
const resolveQueriesArgs: {queries: string[], extraSearchPath: string | undefined}[] = [];
273+
CodeQL.setCodeQL({
274+
resolveQueries: async function(queries: string[], extraSearchPath: string | undefined) {
275+
resolveQueriesArgs.push({queries, extraSearchPath});
276+
// Return what we're given, just in the right format for a resolved query
277+
// This way we can test by seeing which returned items are in the final
278+
// configuration.
279+
const dummyResolvedQueries = {};
280+
queries.forEach(q => { dummyResolvedQueries[q] = {}; });
281+
return {
282+
byLanguage: {
283+
'javascript': dummyResolvedQueries,
284+
},
285+
noDeclaredLanguage: {},
286+
multipleDeclaredLanguages: {},
287+
};
288+
},
289+
});
290+
291+
setInput('languages', 'javascript');
292+
293+
const config = await configUtils.initConfig();
294+
295+
// Check resolveQueries was called correctly
296+
// It'll be called once for the default queries
297+
// and once for `./foo` from the config file.
298+
t.deepEqual(resolveQueriesArgs.length, 2);
299+
t.deepEqual(resolveQueriesArgs[1].queries.length, 1);
300+
t.regex(resolveQueriesArgs[1].queries[0], /.*\/foo$/);
301+
302+
// Now check that the end result contains the default queries and the query from config
303+
t.deepEqual(config.queries['javascript'].length, 2);
304+
t.regex(config.queries['javascript'][0], /javascript-code-scanning.qls$/);
305+
t.regex(config.queries['javascript'][1], /.*\/foo$/);
306+
});
307+
});
308+
257309
test("Queries from config file can be overridden in workflow file", async t => {
258310
return await util.withTmpDir(async tmpDir => {
259311
process.env['RUNNER_TEMP'] = tmpDir;

src/config-utils.ts

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -472,19 +472,34 @@ async function getLanguages(): Promise<string[]> {
472472
}
473473

474474
/**
475-
* Get the default config for when the user has not supplied one.
475+
* Returns true if queries were provided in the workflow file
476+
* (and thus added), otherwise false
476477
*/
477-
export async function getDefaultConfig(): Promise<Config> {
478-
const languages = await getLanguages();
479-
const queries = {};
480-
await addDefaultQueries(languages, queries);
478+
function addQueriesFromWorkflowIfRequired(
479+
configFile: string,
480+
languages: string[],
481+
resultMap: { [language: string]: string[] }
482+
): boolean {
481483
const queryUses = core.getInput('queries');
482484
if (queryUses) {
483485
queryUses.split(',').forEach(async query => {
484-
await parseQueryUses('', languages, queries, query);
486+
await parseQueryUses(configFile, languages, resultMap, query);
485487
});
488+
return true;
486489
}
487490

491+
return false;
492+
}
493+
494+
/**
495+
* Get the default config for when the user has not supplied one.
496+
*/
497+
export async function getDefaultConfig(): Promise<Config> {
498+
const languages = await getLanguages();
499+
const queries = {};
500+
await addDefaultQueries(languages, queries);
501+
addQueriesFromWorkflowIfRequired('', languages, queries);
502+
488503
return {
489504
languages: languages,
490505
queries: queries,
@@ -545,12 +560,8 @@ async function loadConfig(configFile: string): Promise<Config> {
545560

546561
// If queries were provided using `with` in the action configuration,
547562
// they should take precedence over the queries in the config file
548-
const queryUses = core.getInput('queries');
549-
if (queryUses) {
550-
queryUses.split(',').forEach(async query => {
551-
await parseQueryUses(configFile, languages, queries, query);
552-
});
553-
} else if (QUERIES_PROPERTY in parsedYAML) {
563+
const addedQueriesFromAction = addQueriesFromWorkflowIfRequired(configFile, languages, queries);
564+
if (!addedQueriesFromAction && QUERIES_PROPERTY in parsedYAML) {
554565
if (!(parsedYAML[QUERIES_PROPERTY] instanceof Array)) {
555566
throw new Error(getQueriesInvalid(configFile));
556567
}

0 commit comments

Comments
 (0)