Skip to content

Commit

Permalink
Showing 12 changed files with 40 additions and 52 deletions.
5 changes: 2 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.

16 changes: 6 additions & 10 deletions lib/error-matcher.test.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.test.js.map

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

10 changes: 5 additions & 5 deletions lib/toolrunner-error-catcher.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/toolrunner-error-catcher.js.map

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

9 changes: 4 additions & 5 deletions lib/toolrunner-error-catcher.test.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/toolrunner-error-catcher.test.js.map
18 changes: 7 additions & 11 deletions src/error-matcher.test.ts
@@ -18,16 +18,12 @@ test('noSourceCodeFound matches against example javascript output', async t => {

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}`);
}
if (!(matcherName in namedMatchersForTesting)) {
throw new Error(`Unknown matcher ${matcherName}`);
}

const regex = namedMatchersForTesting[matcherName][1];
if (regex === null) {
throw new Error(`Cannot test matcher ${matcherName} with null regex`);
}
return regex.test(logSample);
}
5 changes: 2 additions & 3 deletions src/error-matcher.ts
@@ -5,13 +5,12 @@ export type ErrorMatcher = [number|null, RegExp|null, string];
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,
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`
"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"
],
};

9 changes: 4 additions & 5 deletions src/toolrunner-error-catcher.test.ts
@@ -1,5 +1,4 @@
import * as exec from '@actions/exec';
import * as toolrunnner from '@actions/exec/lib/toolrunner';
import test from 'ava';

import { ErrorMatcher } from './error-matcher';
@@ -139,13 +138,13 @@ function buildDummyArgs(stdoutContents: string, stderrContents: string,

let command = '';

if (stdoutContents) command += 'console.log(\\"' + stdoutContents + '\\");';
if (stderrContents) command += 'console.error(\\"' + stderrContents + '\\");';
if (stdoutContents) command += 'console.log("' + stdoutContents + '");';
if (stderrContents) command += 'console.error("' + stderrContents + '");';

if (command.length === 0) throw new Error("Must provide contents for either stdout or stderr");

if (desiredErrorMessage) command += 'throw new Error(\\"' + desiredErrorMessage + '\\");';
if (desiredErrorMessage) command += 'throw new Error("' + desiredErrorMessage + '");';
if (desiredExitCode) command += 'process.exitCode = ' + desiredExitCode + ';';

return toolrunnner.argStringToArray('-e "' + command + '"');
return ["-e", command];
}
12 changes: 6 additions & 6 deletions src/toolrunner-error-catcher.ts
@@ -4,13 +4,13 @@ import * as toolrunnner from '@actions/exec/lib/toolrunner';
import {ErrorMatcher} from './error-matcher';

/**
* Wrapper for exec.exec which checks for specific return code and/or regex matches in console output.
* Wrapper for toolrunner.Toolrunner which checks for specific return code and/or regex matches in console output.
* Output will be streamed to the live console as well as captured for subsequent processing.
* Returns promise with return code
*
* @param commandLine command to execute (can include additional args). Must be correctly escaped.
* @param matchers defines specific codes and/or regexes that should lead to return of a custom error
* @param commandLine command to execute
* @param args optional arguments for tool. Escaping is handled by the lib.
* @param matchers defines specific codes and/or regexes that should lead to return of a custom error
* @param options optional exec options. See ExecOptions
* @returns Promise<number> exit code
*/
@@ -27,7 +27,7 @@ export async function toolrunnerErrorCatcher(commandLine: string, args?: string[
if (options?.listeners?.stdout !== undefined) {
options.listeners.stdout(data);
} else {
// if no stdout listener was originally defined then we match default behavior of exec.exec
// if no stdout listener was originally defined then we match default behavior of Toolrunner
process.stdout.write(data);
}

@@ -37,7 +37,7 @@ export async function toolrunnerErrorCatcher(commandLine: string, args?: string[
if (options?.listeners?.stderr !== undefined) {
options.listeners.stderr(data);
} else {
// if no stderr listener was originally defined then we match default behavior of exec.exec
// if no stderr listener was originally defined then we match default behavior of Toolrunner
process.stderr.write(data);
}
}
@@ -50,7 +50,7 @@ export async function toolrunnerErrorCatcher(commandLine: string, args?: string[
commandLine,
args,
{
...options, // pass original options first in order to override below
...options, // we want to override the original options, so include them first
listeners: listeners,
ignoreReturnCode: true, // so we can check for specific codes using the matchers
}

0 comments on commit 1fb7c81

Please sign in to comment.