make ErrorMatcher an object rather than a tuple
This commit is contained in:
parent
1fb7c81099
commit
b3b99014eb
12 changed files with 58 additions and 48 deletions
|
|
@ -21,8 +21,8 @@ function testErrorMatcher(matcherName: string, logSample: string): boolean {
|
|||
if (!(matcherName in namedMatchersForTesting)) {
|
||||
throw new Error(`Unknown matcher ${matcherName}`);
|
||||
}
|
||||
const regex = namedMatchersForTesting[matcherName][1];
|
||||
if (regex === null) {
|
||||
const regex = namedMatchersForTesting[matcherName].outputRegex;
|
||||
if (regex === undefined) {
|
||||
throw new Error(`Cannot test matcher ${matcherName} with null regex`);
|
||||
}
|
||||
return regex.test(logSample);
|
||||
|
|
|
|||
|
|
@ -1,17 +1,23 @@
|
|||
|
||||
export type ErrorMatcher = [number|null, RegExp|null, string];
|
||||
// defines properties to match against the result of executed commands,
|
||||
// and a custom error to return when a match is found
|
||||
export interface ErrorMatcher {
|
||||
exitCode?: number; // exit code of the run process
|
||||
outputRegex?: RegExp; // pattern to match against either stdout or stderr
|
||||
message: string; // the error message that will be thrown for a matching process
|
||||
}
|
||||
|
||||
// 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.
|
||||
*/
|
||||
noSourceCodeFound: [
|
||||
32,
|
||||
new RegExp("No JavaScript or TypeScript code found\\."),
|
||||
"No code found during the build. Please see:\n" +
|
||||
"https://docs.github.com/en/github/finding-security-vulnerabilities-and-errors-in-your-code/troubleshooting-code-scanning#no-code-found-during-the-build"
|
||||
],
|
||||
noSourceCodeFound: {
|
||||
exitCode: 32,
|
||||
outputRegex: new RegExp("No JavaScript or TypeScript code found\\."),
|
||||
message: "No code found during the build. Please see:\n" +
|
||||
"https://docs.github.com/en/github/finding-security-vulnerabilities-and-errors-in-your-code/troubleshooting-code-scanning#no-code-found-during-the-build"
|
||||
},
|
||||
};
|
||||
|
||||
// we collapse the matches into an array for use in execErrorCatcher
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@ test('matchers are never applied if non-error exit', async t => {
|
|||
|
||||
const testArgs = buildDummyArgs("foo bar\\nblort qux", "foo bar\\nblort qux", '', 0);
|
||||
|
||||
const matchers: ErrorMatcher[] = [[123, new RegExp("foo bar"), 'error!!!']];
|
||||
const matchers: ErrorMatcher[] = [{exitCode: 123, outputRegex: new RegExp("foo bar"), message: 'error!!!' }];
|
||||
|
||||
t.deepEqual(await exec.exec('node', testArgs), 0);
|
||||
|
||||
|
|
@ -23,7 +23,7 @@ test('regex matchers are applied to stdout for non-zero exit code', async t => {
|
|||
|
||||
const testArgs = buildDummyArgs("foo bar\\nblort qux", '', '', 1);
|
||||
|
||||
const matchers: ErrorMatcher[] = [[123, new RegExp("foo bar"), '🦄']];
|
||||
const matchers: ErrorMatcher[] = [{exitCode: 123, outputRegex: new RegExp("foo bar"), message: '🦄' }];
|
||||
|
||||
await t.throwsAsync(exec.exec('node', testArgs), {instanceOf: Error, message: 'The process \'node\' failed with exit code 1'});
|
||||
|
||||
|
|
@ -38,7 +38,7 @@ test('regex matchers are applied to stderr for non-zero exit code', async t => {
|
|||
|
||||
const testArgs = buildDummyArgs("non matching string", 'foo bar\\nblort qux', '', 1);
|
||||
|
||||
const matchers: ErrorMatcher[] = [[123, new RegExp("foo bar"), '🦄']];
|
||||
const matchers: ErrorMatcher[] = [{exitCode: 123, outputRegex: new RegExp("foo bar"), message: '🦄' }];
|
||||
|
||||
await t.throwsAsync(exec.exec('node', testArgs), {instanceOf: Error, message: 'The process \'node\' failed with exit code 1'});
|
||||
|
||||
|
|
@ -53,9 +53,9 @@ test('matcher returns correct error message when multiple matchers defined', asy
|
|||
|
||||
const testArgs = buildDummyArgs("non matching string", 'foo bar\\nblort qux', '', 1);
|
||||
|
||||
const matchers: ErrorMatcher[] = [[456, new RegExp("lorem ipsum"), '😩'],
|
||||
[123, new RegExp("foo bar"), '🦄'],
|
||||
[789, new RegExp("blah blah"), '🤦♂️']];
|
||||
const matchers: ErrorMatcher[] = [{exitCode: 456, outputRegex: new RegExp("lorem ipsum"), message: '😩'},
|
||||
{exitCode: 123, outputRegex: new RegExp("foo bar"), message: '🦄'},
|
||||
{exitCode: 789, outputRegex: new RegExp("blah blah"), message: '🤦♂️'}];
|
||||
|
||||
await t.throwsAsync(exec.exec('node', testArgs), {instanceOf: Error, message: 'The process \'node\' failed with exit code 1'});
|
||||
|
||||
|
|
@ -70,9 +70,9 @@ test('matcher returns first match to regex when multiple matches', async t => {
|
|||
|
||||
const testArgs = buildDummyArgs("non matching string", 'foo bar\\nblort qux', '', 1);
|
||||
|
||||
const matchers: ErrorMatcher[] = [[123, new RegExp("foo bar"), '🦄'],
|
||||
[789, new RegExp("blah blah"), '🤦♂️'],
|
||||
[987, new RegExp("foo bar"), '🚫']];
|
||||
const matchers: ErrorMatcher[] = [{exitCode: 123, outputRegex: new RegExp("foo bar"), message: '🦄'},
|
||||
{exitCode: 789, outputRegex: new RegExp("blah blah"), message: '🤦♂️'},
|
||||
{exitCode: 987, outputRegex: new RegExp("foo bar"), message: '🚫'}];
|
||||
|
||||
await t.throwsAsync(exec.exec('node', testArgs), {instanceOf: Error, message: 'The process \'node\' failed with exit code 1'});
|
||||
|
||||
|
|
@ -87,7 +87,7 @@ test('exit code matchers are applied', async t => {
|
|||
|
||||
const testArgs = buildDummyArgs("non matching string", 'foo bar\\nblort qux', '', 123);
|
||||
|
||||
const matchers: ErrorMatcher[] = [[123, new RegExp("this will not match"), '🦄']];
|
||||
const matchers: ErrorMatcher[] = [{exitCode: 123, outputRegex: new RegExp("this will not match"), message: '🦄' }];
|
||||
|
||||
await t.throwsAsync(exec.exec('node', testArgs), {instanceOf: Error, message: 'The process \'node\' failed with exit code 123'});
|
||||
|
||||
|
|
|
|||
|
|
@ -63,9 +63,13 @@ export async function toolrunnerErrorCatcher(commandLine: string, args?: string[
|
|||
if (returnState === 0) return returnState;
|
||||
|
||||
if (matchers) {
|
||||
for (const [customCode, regex, message] of matchers) {
|
||||
if (customCode === returnState || regex && (regex.test(stderr) || regex.test(stdout)) ) {
|
||||
throw new Error(message);
|
||||
for (const matcher of matchers) {
|
||||
if (
|
||||
matcher.exitCode === returnState ||
|
||||
matcher.outputRegex?.test(stderr) ||
|
||||
matcher.outputRegex?.test(stdout)
|
||||
) {
|
||||
throw new Error(matcher.message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue