Skip to content

Commit

Permalink
chore: log errors in acceptance tests (#5180)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
cmars authored Apr 22, 2024
1 parent 4446b9d commit d564470
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
5 changes: 5 additions & 0 deletions test/jest/acceptance/cli-args.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,7 @@ describe('cli args', () => {
{
env,
cwd: project.path(),
logErrors: true,
},
);

Expand All @@ -401,6 +402,7 @@ describe('cli args', () => {
{
env,
cwd: project.path(),
logErrors: true,
},
);

Expand All @@ -419,6 +421,7 @@ describe('cli args', () => {
{
env,
cwd: project.path(),
logErrors: true,
},
);

Expand All @@ -436,6 +439,7 @@ describe('cli args', () => {
{
env,
cwd: project.path(),
logErrors: true,
},
);

Expand All @@ -458,6 +462,7 @@ describe('cli args', () => {
{
env,
cwd: project.path(),
logErrors: true,
},
);
const jsonOutput = JSON.parse(stdout);
Expand Down
9 changes: 8 additions & 1 deletion test/jest/util/runCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
});
});
Expand Down

0 comments on commit d564470

Please sign in to comment.