From 34df01d123f8b8f7846918c09fdf9e525079912f Mon Sep 17 00:00:00 2001 From: ghe Date: Fri, 12 Mar 2021 15:55:53 +0000 Subject: [PATCH] feat: output the fixSummary provided by @snyk/fix --- .../test/unit/__snapshots__/fix.spec.ts.snap | 42 ++++++++------ src/cli/commands/fix/index.ts | 9 ++- src/cli/commands/test/index.ts | 5 -- src/lib/errors/command-not-supported.ts | 2 +- src/lib/errors/not-supported-by-ecosystem.ts | 2 +- test/__snapshots__/cli-fix.spec.ts.snap | 8 +-- .../workspaces/pip-app-custom/base.txt | 1 + test/cli-fix.spec.ts | 55 ++++++++++++++----- 8 files changed, 79 insertions(+), 45 deletions(-) create mode 100644 test/acceptance/workspaces/pip-app-custom/base.txt diff --git a/packages/snyk-fix/test/unit/__snapshots__/fix.spec.ts.snap b/packages/snyk-fix/test/unit/__snapshots__/fix.spec.ts.snap index c9aed4a536..3523d535b0 100644 --- a/packages/snyk-fix/test/unit/__snapshots__/fix.spec.ts.snap +++ b/packages/snyk-fix/test/unit/__snapshots__/fix.spec.ts.snap @@ -2,7 +2,7 @@ exports[`Error handling Snyk fix returns error when called with unsupported type 1`] = ` Object { - "exceptionsByScanType": Object { + "exceptions": Object { "npm": Object { "originals": Array [ Object { @@ -57,18 +57,22 @@ Object { "userMessage": "npm is not supported.", }, }, - "fixSummary": "✖ No successful fixes + "fixSummary": "✖ No successful fixes -Unresolved items: +Unresolved items: package.json - ✖ npm is not supported. + ✖ npm is not supported. -Summary: +Summary: - 1 items were not fixed - 0 items were successfully fixed", - "resultsByPlugin": Object {}, + 1 items were not fixed + 0 items were successfully fixed", + "meta": Object { + "failed": 1, + "fixed": 0, + }, + "results": Object {}, } `; @@ -157,22 +161,26 @@ Summary: exports[`Snyk fix Snyk fix returns results for supported & unsupported type 1`] = ` Object { - "exceptionsByScanType": Object {}, - "fixSummary": "Successful fixes: + "exceptions": Object {}, + "fixSummary": "Successful fixes: requirements.txt - ✔ Pinned django from 1.6.1 to 2.0.1 + ✔ Pinned django from 1.6.1 to 2.0.1 -Unresolved items: +Unresolved items: Pipfile - ✖ Pipfile is not supported + ✖ Pipfile is not supported -Summary: +Summary: - 1 items were not fixed - 1 items were successfully fixed", - "resultsByPlugin": Object { + 1 items were not fixed + 1 items were successfully fixed", + "meta": Object { + "failed": 1, + "fixed": 1, + }, + "results": Object { "python": Object { "failed": Array [], "skipped": Array [ diff --git a/src/cli/commands/fix/index.ts b/src/cli/commands/fix/index.ts index 4068927701..6e58bcce2d 100644 --- a/src/cli/commands/fix/index.ts +++ b/src/cli/commands/fix/index.ts @@ -32,9 +32,12 @@ async function fix(...args: MethodArgs): Promise { debug( `Organization has ${snykFixFeatureFlag} feature flag enabled for experimental Snyk fix functionality`, ); - await snykFix.fix(results); - // TODO: what is being returned if anything? - return ''; + const { fixSummary, meta } = await snykFix.fix(results); + + if (meta.fixed === 0) { + throw new Error(fixSummary); + } + return fixSummary; } /* @deprecated diff --git a/src/cli/commands/test/index.ts b/src/cli/commands/test/index.ts index b27848e1e8..ab6a540cdc 100644 --- a/src/cli/commands/test/index.ts +++ b/src/cli/commands/test/index.ts @@ -52,15 +52,10 @@ import { import { test as iacTest } from './iac-test-shim'; import { validateCredentials } from './validate-credentials'; -<<<<<<< HEAD -import { generateSnykTestError } from './generate-snyk-test-error'; import { validateTestOptions } from './validate-test-options'; import { setDefaultTestOptions } from './set-default-test-options'; -======= ->>>>>>> feat: introduce `snyk fix` CLI command import { processCommandArgs } from '../process-command-args'; import { formatTestError } from './format-test-error'; -import { validateTestOptions } from './validate-test-options'; const debug = Debug('snyk-test'); const SEPARATOR = '\n-------------------------------------------------------\n'; diff --git a/src/lib/errors/command-not-supported.ts b/src/lib/errors/command-not-supported.ts index f4ec9c5451..09a75a1647 100644 --- a/src/lib/errors/command-not-supported.ts +++ b/src/lib/errors/command-not-supported.ts @@ -10,7 +10,7 @@ export class CommandNotSupportedError extends CustomError { this.command = command; this.org = org; - this.userMessage = `\`${command}\`' is not supported ${ + this.userMessage = `\`${command}\` is not supported ${ org ? `for org '${org}'` : '' }`; } diff --git a/src/lib/errors/not-supported-by-ecosystem.ts b/src/lib/errors/not-supported-by-ecosystem.ts index e260b12dc4..5ac9e2afa5 100644 --- a/src/lib/errors/not-supported-by-ecosystem.ts +++ b/src/lib/errors/not-supported-by-ecosystem.ts @@ -13,6 +13,6 @@ export class FeatureNotSupportedByEcosystemError extends CustomError { this.code = 422; this.feature = feature; - this.userMessage = `'${feature}' is not supported for ecosystem '${ecosystem}'`; + this.userMessage = `\`${feature}\` is not supported for ecosystem '${ecosystem}'`; } } diff --git a/test/__snapshots__/cli-fix.spec.ts.snap b/test/__snapshots__/cli-fix.spec.ts.snap index 0bc491704c..aaac1bf228 100644 --- a/test/__snapshots__/cli-fix.spec.ts.snap +++ b/test/__snapshots__/cli-fix.spec.ts.snap @@ -1,7 +1,7 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP exports[`snyk fix (system tests) \`shows expected response when Python project was skipped because of missing remediation data --all-projects\` 1`] = ` -"✔ Done +"✖ Done ✖ No successful fixes Unresolved items: @@ -21,7 +21,7 @@ exports[`snyk fix (system tests) \`shows expected response when Python project w "- Looking for supported Python items ✔ Looking for supported Python items ⠋ Processing 1 requirements.txt items.✔ Processing 1 requirements.txt items. -✔ Done +✖ Done ✖ No successful fixes Unresolved items: @@ -41,7 +41,7 @@ exports[`snyk fix (system tests) \`shows expected response when Python project w "- Looking for supported Python items ✔ Looking for supported Python items ⠋ Processing 1 requirements.txt items.✔ Processing 1 requirements.txt items. -✔ Done +✖ Done ✖ No successful fixes Unresolved items: @@ -58,7 +58,7 @@ Summary: `; exports[`snyk fix (system tests) \`shows expected response when nothing could be fixed + returns exit code 2\` 1`] = ` -"✔ Done +"✖ Done ✖ No successful fixes Unresolved items: diff --git a/test/acceptance/workspaces/pip-app-custom/base.txt b/test/acceptance/workspaces/pip-app-custom/base.txt new file mode 100644 index 0000000000..c3eeef4fb7 --- /dev/null +++ b/test/acceptance/workspaces/pip-app-custom/base.txt @@ -0,0 +1 @@ +Jinja2==2.7.2 diff --git a/test/cli-fix.spec.ts b/test/cli-fix.spec.ts index 0d9055e9d3..5ad348e498 100644 --- a/test/cli-fix.spec.ts +++ b/test/cli-fix.spec.ts @@ -1,5 +1,6 @@ import { exec } from 'child_process'; import * as pathLib from 'path'; +import stripAnsi from 'strip-ansi'; import { fakeServer } from './acceptance/fake-server'; import cli = require('../src/cli/commands'); @@ -81,7 +82,13 @@ describe('snyk fix (system tests)', () => { SNYK_HOST, }, }, - (err, stdout) => { + (err, stdout, stderr) => { + if (!err) { + throw new Error('Test expected to return an error'); + } + expect(stderr).toBe(''); + expect(err.message).toMatch('Command failed'); + expect(err.code).toEqual(2); expect(stdout).toMatch( "`snyk fix` is not supported for org 'no-flag'", ); @@ -104,7 +111,13 @@ describe('snyk fix (system tests)', () => { SNYK_HOST, }, }, - (err, stdout) => { + (err, stdout, stderr) => { + if (!err) { + throw new Error('Test expected to return an error'); + } + expect(stderr).toBe(''); + expect(err.message).toMatch('Command failed'); + expect(err.code).toEqual(2); expect(stdout).toMatch( "`snyk fix` is not supported for ecosystem 'cpp'", ); @@ -128,7 +141,13 @@ describe('snyk fix (system tests)', () => { SNYK_HOST, }, }, - (err, stdout) => { + (err, stdout, stderr) => { + if (!err) { + throw new Error('Test expected to return an error'); + } + expect(stderr).toBe(''); + expect(err.message).toMatch('Command failed'); + expect(err.code).toEqual(2); expect(stdout).toMatch( "`snyk fix` is not supported for ecosystem 'docker'", ); @@ -139,8 +158,10 @@ describe('snyk fix (system tests)', () => { testTimeout, ); - // TODO: this is only showing help when fails? - it.skip( + /* this command is different + * it shows help text not an error when command is not supported + */ + it( '`shows error when called with container (deprecated)`', (done) => { exec( @@ -153,10 +174,10 @@ describe('snyk fix (system tests)', () => { SNYK_HOST, }, }, - (err, stdout) => { - expect(stdout).toMatch( - "`snyk fix` is not supported for ecosystem 'docker'", - ); + (err, stdout, stderr) => { + expect(stderr).toBe(''); + expect(stdout).toMatch('COMMANDS'); + expect(err).toBe(null); done(); }, ); @@ -176,7 +197,13 @@ describe('snyk fix (system tests)', () => { SNYK_HOST, }, }, - (err, stdout) => { + (err, stdout, stderr) => { + if (!err) { + throw new Error('Test expected to return an error'); + } + expect(stderr).toBe(''); + expect(err.message).toMatch('Command failed'); + expect(err.code).toEqual(2); expect(stdout).toMatch( "`snyk fix` is not supported for ecosystem 'code'", ); @@ -205,7 +232,7 @@ describe('snyk fix (system tests)', () => { throw new Error('Test expected to return an error'); } expect(stderr).toBe(''); - expect(stdout).toMatchSnapshot(); + expect(stripAnsi(stdout)).toMatchSnapshot(); expect(err.message).toMatch('Command failed'); expect(err.code).toBe(2); done(); @@ -231,7 +258,7 @@ describe('snyk fix (system tests)', () => { }, }, (err, stdout) => { - expect(stdout).toMatchSnapshot(); + expect(stripAnsi(stdout)).toMatchSnapshot(); done(); }, ); @@ -252,7 +279,7 @@ describe('snyk fix (system tests)', () => { }, }, (err, stdout) => { - expect(stdout).toMatchSnapshot(); + expect(stripAnsi(stdout)).toMatchSnapshot(); done(); }, ); @@ -273,7 +300,7 @@ describe('snyk fix (system tests)', () => { }, }, (err, stdout) => { - expect(stdout).toMatchSnapshot(); + expect(stripAnsi(stdout)).toMatchSnapshot(); done(); }, );