Skip to content

Commit

Permalink
add test for initial error matcher
Browse files Browse the repository at this point in the history
  • Loading branch information
Nick Fyson committed Sep 7, 2020
1 parent 7dbaff0 commit 3e6d239
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 16 deletions.
4 changes: 2 additions & 2 deletions lib/codeql.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/codeql.js.map

Large diffs are not rendered by default.

18 changes: 15 additions & 3 deletions lib/error_matcher.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/error_matcher.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 33 additions & 0 deletions lib/error_matcher.test.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions lib/error_matcher.test.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 33 additions & 0 deletions src/error_matcher.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@

import test from 'ava';

import { namedMatchersForTesting } from './error_matcher';

/*
NB We test the regexes for all the matchers against example log output snippets.
*/

test('noSourceCodeFound matches against example javascript output', async t => {
t.assert(testErrorMatcher("noSourceCodeFound", `
2020-09-07T17:39:53.9050522Z [2020-09-07 17:39:53] [build] Done extracting /opt/hostedtoolcache/CodeQL/0.0.0-20200630/x64/codeql/javascript/tools/data/externs/web/ie_vml.js (3 ms)
2020-09-07T17:39:53.9051849Z [2020-09-07 17:39:53] [build-err] No JavaScript or TypeScript code found.
2020-09-07T17:39:53.9052444Z [2020-09-07 17:39:53] [build-err] No JavaScript or TypeScript code found.
2020-09-07T17:39:53.9251124Z [2020-09-07 17:39:53] [ERROR] Spawned process exited abnormally (code 255; tried to run: [/opt/hostedtoolcache/CodeQL/0.0.0-20200630/x64/codeql/javascript/tools/autobuild.sh])
`));
});

function testErrorMatcher(matcherName: string, logSample: string): boolean {

const regex = namedMatchersForTesting[matcherName] ? namedMatchersForTesting[matcherName][1] : null;

if (regex) {
return regex.test(logSample);
} else {
if (namedMatchersForTesting[matcherName]) {
throw new Error(`Cannot test matcher ${matcherName} with null regex`);
} else {
throw new Error(`Unknown matcher ${matcherName}`);
}
}

}
18 changes: 9 additions & 9 deletions src/error_matcher.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@

export type ErrorMatcher = [number|null, RegExp|null, string];

const namedMatchers: { [key: string]: ErrorMatcher } = {
// exported only for testing purposes
export const namedMatchersForTesting: { [key: string]: ErrorMatcher } = {
/*
In due course it may be possible to remove the regex, if/when javascript also exits with code 32.
For context see https://github.com/github/semmle-code/pull/36921
*/
noSourceCodeFound: [
32,
null,
`No source code was seen during the build. Please see...
https://docs.github.com/en/github/finding-security-vulnerabilities-and-errors-in-your-code/troubleshooting-code-scanning#no-code-found-during-the-build`
],
noSourceCodeFoundJavascript: [
null,
new RegExp("No JavaScript or TypeScript code found\\."),
`No source code was seen during the build. Please see...
https://docs.github.com/en/github/finding-security-vulnerabilities-and-errors-in-your-code/troubleshooting-code-scanning#no-code-found-during-the-build`
]
],
};

export const errorMatchers = Object.values(namedMatchers);
// we collapse the matches into an array for use in exec_wrapper
export const errorMatchers = Object.values(namedMatchersForTesting);

0 comments on commit 3e6d239

Please sign in to comment.