Skip to content
This repository was archived by the owner on Sep 1, 2022. It is now read-only.

Commit e195859

Browse files
author
Sam Lanning
authored
Merge pull request #9 from github/csv-todo
Allow specifying query files with no known expected CSV
2 parents 78cbc8c + 4f02c3c commit e195859

File tree

6 files changed

+42
-27
lines changed

6 files changed

+42
-27
lines changed

README.md

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ The file should look something like this:
7979
"expectedResults": {
8080
"step-01.ql": "step-01.csv",
8181
"step-02.ql": "step-02.csv",
82-
"step-03.ql": "step-03.csv",
82+
"step-03.ql": false,
8383
}
8484
}
8585
```
@@ -104,6 +104,12 @@ and csv files for the expected results.
104104
* `expectedResults` is an object that maps expected query filenames to a csv
105105
file detailing what the expected results for this query should be.
106106
Only the first expression for each row in the query results is checked.
107+
If instead of a CSV filename, `false` is used,
108+
then the checker will assume that the CSV file has simply
109+
never been generated,
110+
and will print out the resulting output from the query for you to copy into a
111+
new file.
112+
107113

108114
To simplify course creation,
109115
we recommend structuring your course folder like so:
@@ -200,18 +206,19 @@ and add a similar workflow file to the one mentioned above.
200206

201207
When testing the action ([as detailed above](#testing-the-action)),
202208
when a query that is run produces unexpected results,
209+
or it is specified as `false` in `config.yml` instead of listing a CSV filename,
203210
the actual results that it produces are printed out in the console.
204211
You can then store this output as the relevant CSV file.
205212

206213
So the workflow for adding a new query and CSV file looks like:
207214

208-
* add the query (`.ql` file) to `answers/`
209-
* add the query to `config.json`
210-
(remember that the path should be relative to `answers/`)
211-
* Test the action (whichever method you prefer)
212-
* Copy the CSV output to the appropriate file in `image/config/`
215+
* add the query (`.ql` file) to `answers/`.
216+
* add the query to the `expectedResults` property in `config.json`,
217+
with a starting value of `false`.
218+
* Test the action (whichever method you prefer).
219+
* Copy the CSV output to the appropriate file in `image/config/`.
213220
* Re-test the action to ensure it marks the query
214-
as producing the correct results
221+
as producing the correct results.
215222

216223
#### Publishing your action
217224

codeql-learninglab-check/package/src/check.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ export interface Result {
3838
}
3939

4040
export type ResultsCheck = {
41+
// CSV file has not been defined for this query yet
42+
status: 'undefined';
43+
} | {
4144
status: 'correct';
4245
count: number;
4346
} | {

codeql-learninglab-check/package/src/index.ts

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ type Config = {
5555
/**
5656
* Mapping from query filename to expected results csv file
5757
*/
58-
expectedResults: {[id: string]: string};
58+
expectedResults: {[id: string]: string | false};
5959
};
6060

6161
function isConfig(config: any): config is Config {
@@ -74,7 +74,7 @@ function isConfig(config: any): config is Config {
7474
throw new Error('Configuration property locationPaths must have the placeholder "{line-start}"');
7575
}
7676
for (const k of Object.keys(config.expectedResults)) {
77-
if (typeof config.expectedResults[k] !== 'string') {
77+
if (typeof config.expectedResults[k] !== 'string' && config.expectedResults[k] !== false) {
7878
throw new Error(`Confiuration property "expectedResults" -> "${k}" must be a string`);
7979
}
8080
}
@@ -319,8 +319,13 @@ function isConfig(config: any): config is Config {
319319
console.log(result.stdout);
320320
const csvOutput = csvPath(query);
321321
await execFile('codeql', ['bqrs', 'decode', '--entities=url,string', bqrsOutput, '--format=csv', `--output=${csvOutput}`]);
322-
const expectedCSV = path.join(CONFIG_PATH, config.expectedResults[query]);
323-
results.set(query, await checkResults(expectedCSV, csvOutput));
322+
const relativeExpectedCSV = config.expectedResults[query];
323+
if (relativeExpectedCSV) {
324+
const expectedCSV = path.join(CONFIG_PATH, relativeExpectedCSV);
325+
results.set(query, await checkResults(expectedCSV, csvOutput));
326+
} else {
327+
results.set(query, {status: 'undefined'});
328+
}
324329
}
325330

326331
for(const entry of results.entries()) {
@@ -330,18 +335,22 @@ function isConfig(config: any): config is Config {
330335
if (r.status === 'correct') {
331336
comment += ` (${pluralize(r.count, 'result')})`;
332337
} else {
333-
if (r.results) {
334-
comment += ` (${pluralize(r.results.actualCount, 'result')}):\n\n`;
335-
comment += r.explanation;
336-
comment += `\nExpected query to produce ${r.results.expectedCount} results`;
337-
comment += formatResults(config.locationPaths, r.results.missingResults, 'Missing results');
338-
comment += formatResults(config.locationPaths, r.results.unexpectedResults, 'Unexpected results');
339-
comment += formatResults(config.locationPaths, r.results.extraResults, 'Results selected too many times');
338+
if (r.status === 'incorrect') {
339+
if (r.results) {
340+
comment += ` (${pluralize(r.results.actualCount, 'result')}):\n\n`;
341+
comment += r.explanation;
342+
comment += `\nExpected query to produce ${r.results.expectedCount} results`;
343+
comment += formatResults(config.locationPaths, r.results.missingResults, 'Missing results');
344+
comment += formatResults(config.locationPaths, r.results.unexpectedResults, 'Unexpected results');
345+
comment += formatResults(config.locationPaths, r.results.extraResults, 'Results selected too many times');
346+
} else {
347+
comment += r.explanation;
348+
}
349+
core.setFailed(`Incorrect results for ${query}`);
340350
} else {
341-
comment += r.explanation;
351+
console.log(`No CSV defined for ${query}:`);
342352
}
343353
// Print CSV in console
344-
core.setFailed(`Incorrect results for ${query}`);
345354
core.startGroup('Actual Results CSV:');
346355
console.log((await (readFile(csvPath(query)))).toString());
347356
core.endGroup();

codeql-learninglab-check/publish.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ set -x
55

66
docker login docker.pkg.github.com -u github-actions -p ${GITHUB_TOKEN}
77

8-
PREV_IMAGE_VERSION=v0.0.6
9-
IMAGE_VERSION=v0.0.7
8+
PREV_IMAGE_VERSION=v0.0.7
9+
IMAGE_VERSION=v0.0.8
1010
IMAGE_PATH=docker.pkg.github.com/github/codeql-learninglab-actions/codeql-learninglab-check
1111
IMAGE_TAG=${IMAGE_PATH}:${IMAGE_VERSION}
1212

courses/template/image/config/config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
"locationPaths": "https://github.com/<org>/<repo>/blob/<sha>{path}#L{line-start}-L{line-end}",
44
"expectedResults": {
55
"step-01.ql": "step-01.csv",
6-
"step-02.ql": "step-02.csv"
6+
"step-02.ql": "step-01.csv"
77
}
88
}

courses/template/image/config/step-02.csv

Lines changed: 0 additions & 4 deletions
This file was deleted.

0 commit comments

Comments
 (0)