Skip to content

Commit

Permalink
feat: output the fixSummary provided by @snyk/fix
Browse files Browse the repository at this point in the history
  • Loading branch information
lili2311 committed Mar 29, 2021
1 parent cbeeeaf commit 34df01d
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 45 deletions.
42 changes: 25 additions & 17 deletions packages/snyk-fix/test/unit/__snapshots__/fix.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -57,18 +57,22 @@ Object {
"userMessage": "npm is not supported.",
},
},
"fixSummary": "[31m✖ No successful fixes[39m
"fixSummary": "✖ No successful fixes
[1mUnresolved items:[22m
Unresolved items:
package.json
[31m✖[39m [31mnpm is not supported.[39m
npm is not supported.
[1mSummary:[22m
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 {},
}
`;

Expand Down Expand Up @@ -157,22 +161,26 @@ Summary:

exports[`Snyk fix Snyk fix returns results for supported & unsupported type 1`] = `
Object {
"exceptionsByScanType": Object {},
"fixSummary": "[1mSuccessful fixes:[22m
"exceptions": Object {},
"fixSummary": "Successful fixes:
requirements.txt
[32m✔[39m Pinned django from 1.6.1 to 2.0.1
Pinned django from 1.6.1 to 2.0.1
[1mUnresolved items:[22m
Unresolved items:
Pipfile
[31m✖[39m [31mPipfile is not supported[39m
Pipfile is not supported
[1mSummary:[22m
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 [
Expand Down
9 changes: 6 additions & 3 deletions src/cli/commands/fix/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,12 @@ async function fix(...args: MethodArgs): Promise<string> {
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
Expand Down
5 changes: 0 additions & 5 deletions src/cli/commands/test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
2 changes: 1 addition & 1 deletion src/lib/errors/command-not-supported.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}'` : ''
}`;
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib/errors/not-supported-by-ecosystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}'`;
}
}
8 changes: 4 additions & 4 deletions test/__snapshots__/cli-fix.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand Down
1 change: 1 addition & 0 deletions test/acceptance/workspaces/pip-app-custom/base.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Jinja2==2.7.2
55 changes: 41 additions & 14 deletions test/cli-fix.spec.ts
Original file line number Diff line number Diff line change
@@ -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');
Expand Down Expand Up @@ -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'",
);
Expand All @@ -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'",
);
Expand All @@ -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'",
);
Expand All @@ -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(
Expand All @@ -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();
},
);
Expand All @@ -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'",
);
Expand Down Expand Up @@ -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();
Expand All @@ -231,7 +258,7 @@ describe('snyk fix (system tests)', () => {
},
},
(err, stdout) => {
expect(stdout).toMatchSnapshot();
expect(stripAnsi(stdout)).toMatchSnapshot();
done();
},
);
Expand All @@ -252,7 +279,7 @@ describe('snyk fix (system tests)', () => {
},
},
(err, stdout) => {
expect(stdout).toMatchSnapshot();
expect(stripAnsi(stdout)).toMatchSnapshot();
done();
},
);
Expand All @@ -273,7 +300,7 @@ describe('snyk fix (system tests)', () => {
},
},
(err, stdout) => {
expect(stdout).toMatchSnapshot();
expect(stripAnsi(stdout)).toMatchSnapshot();
done();
},
);
Expand Down

0 comments on commit 34df01d

Please sign in to comment.