From ecdd86cade5218d19a04b586a7d5143444e253bd Mon Sep 17 00:00:00 2001 From: Casey Marshall Date: Thu, 18 Apr 2024 15:28:20 -0500 Subject: [PATCH] chore: log errors in acceptance tests When a Snyk CLI command is expected to exit 0 in acceptance tests, it would be useful for debugging to see the stderr from the process. This adds an option to runSnykCLI (logErrors) to do this, opt-in. That way we don't pollute the log with expected non-zero exits in tests that expect them. --- test/jest/acceptance/cli-args.spec.ts | 5 +++++ test/jest/util/runCommand.ts | 9 ++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/test/jest/acceptance/cli-args.spec.ts b/test/jest/acceptance/cli-args.spec.ts index b50ced9383..494103ca6a 100644 --- a/test/jest/acceptance/cli-args.spec.ts +++ b/test/jest/acceptance/cli-args.spec.ts @@ -380,6 +380,7 @@ describe('cli args', () => { { env, cwd: project.path(), + logErrors: true, }, ); @@ -401,6 +402,7 @@ describe('cli args', () => { { env, cwd: project.path(), + logErrors: true, }, ); @@ -419,6 +421,7 @@ describe('cli args', () => { { env, cwd: project.path(), + logErrors: true, }, ); @@ -436,6 +439,7 @@ describe('cli args', () => { { env, cwd: project.path(), + logErrors: true, }, ); @@ -458,6 +462,7 @@ describe('cli args', () => { { env, cwd: project.path(), + logErrors: true, }, ); const jsonOutput = JSON.parse(stdout); diff --git a/test/jest/util/runCommand.ts b/test/jest/util/runCommand.ts index 084dea896c..a95e009d33 100644 --- a/test/jest/util/runCommand.ts +++ b/test/jest/util/runCommand.ts @@ -11,7 +11,10 @@ type RunCommandResult = { // bufferOutput sets the RunCommandResult stdoutBuffer and stderrBuffer // useful if the stdout or stderr string output is too large for the v8 engine -type RunCommandOptions = SpawnOptionsWithoutStdio & { bufferOutput?: boolean }; +type RunCommandOptions = SpawnOptionsWithoutStdio & { + bufferOutput?: boolean; + logErrors?: boolean; +}; const runCommand = ( command: string, @@ -50,6 +53,10 @@ const runCommand = ( result.stderr = Buffer.concat(stderr).toString('utf-8'); } + if (options?.logErrors && result.code !== 0) { + console.log('stderr:', result.stderr); + } + resolve(result); }); });