From 4c3fbf98851886c864bb79c8747e2b93151b570f Mon Sep 17 00:00:00 2001 From: ghe Date: Wed, 10 Mar 2021 11:56:57 +0000 Subject: [PATCH 1/3] refactor: set default test options --- src/cli/commands/test/index.ts | 21 +++------------- .../commands/test/set-default-test-options.ts | 24 +++++++++++++++++++ 2 files changed, 27 insertions(+), 18 deletions(-) create mode 100644 src/cli/commands/test/set-default-test-options.ts diff --git a/src/cli/commands/test/index.ts b/src/cli/commands/test/index.ts index 11797dd661..2f963dcd2d 100644 --- a/src/cli/commands/test/index.ts +++ b/src/cli/commands/test/index.ts @@ -4,13 +4,11 @@ const cloneDeep = require('lodash.clonedeep'); const assign = require('lodash.assign'); import chalk from 'chalk'; import * as snyk from '../../../lib'; -import * as config from '../../../lib/config'; import { isCI } from '../../../lib/is-ci'; import * as Debug from 'debug'; import * as pathLib from 'path'; import { Options, - ShowVulnPaths, SupportedProjectTypes, TestOptions, } from '../../../lib/types'; @@ -54,30 +52,17 @@ import * as iacLocalExecution from './iac-local-execution'; import { validateCredentials } from './validate-credentials'; import { generateSnykTestError } from './generate-snyk-test-error'; import { validateTestOptions } from './validate-test-options'; +import { setDefaultTestOptions } from './set-default-test-options'; import { processCommandArgs } from '../process-command-args'; const debug = Debug('snyk-test'); const SEPARATOR = '\n-------------------------------------------------------\n'; -const showVulnPathsMapping: Record = { - false: 'none', - none: 'none', - true: 'some', - some: 'some', - all: 'all', -}; - // TODO: avoid using `as any` whenever it's possible async function test(...args: MethodArgs): Promise { - const { options, paths } = processCommandArgs(...args); - // org fallback to config unless specified - options.org = options.org || config.org; - - // making `show-vulnerable-paths` 'some' by default. - const svpSupplied = (options['show-vulnerable-paths'] || '').toLowerCase(); - options.showVulnPaths = showVulnPathsMapping[svpSupplied] || 'some'; - + const { options: originalOptions, paths } = processCommandArgs(...args); + const options = setDefaultTestOptions(originalOptions); validateTestOptions(options); validateCredentials(options); diff --git a/src/cli/commands/test/set-default-test-options.ts b/src/cli/commands/test/set-default-test-options.ts new file mode 100644 index 0000000000..8ddf3ae519 --- /dev/null +++ b/src/cli/commands/test/set-default-test-options.ts @@ -0,0 +1,24 @@ +import * as config from '../../../lib/config'; +import { Options, ShowVulnPaths, TestOptions } from '../../../lib/types'; + +export function setDefaultTestOptions( + options: Options & TestOptions, +): Options & TestOptions { + const svpSupplied = (options['show-vulnerable-paths'] || '').toLowerCase(); + + return { + ...options, + // org fallback to config unless specified + org: options.org || config.org, + // making `show-vulnerable-paths` 'some' by default. + showVulnPaths: showVulnPathsMapping[svpSupplied] || 'some', + }; +} + +const showVulnPathsMapping: Record = { + false: 'none', + none: 'none', + true: 'some', + some: 'some', + all: 'all', +}; From 15dce36d1cc9282f86cc818d390f445b78e72c02 Mon Sep 17 00:00:00 2001 From: ghe Date: Thu, 11 Mar 2021 18:02:44 +0000 Subject: [PATCH 2/3] chore: update Protect types --- src/cli/commands/protect/index.ts | 5 ++++- src/cli/commands/protect/wizard.ts | 4 ++-- src/lib/types.ts | 9 +++++---- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/cli/commands/protect/index.ts b/src/cli/commands/protect/index.ts index 857cb7aa53..a3cc5b10fe 100644 --- a/src/cli/commands/protect/index.ts +++ b/src/cli/commands/protect/index.ts @@ -12,7 +12,10 @@ import * as errors from '../../../lib/errors'; const debug = debugModule('snyk'); async function protectFunc( - options: types.PolicyOptions & types.Options & types.TestOptions, + options: types.PolicyOptions & + types.Options & + types.TestOptions & + types.ProtectOptions, ) { const protectOptions = { ...options }; protectOptions.loose = true; // replace missing policies with empty ones diff --git a/src/cli/commands/protect/wizard.ts b/src/cli/commands/protect/wizard.ts index 01698691c7..a156f56118 100644 --- a/src/cli/commands/protect/wizard.ts +++ b/src/cli/commands/protect/wizard.ts @@ -43,8 +43,8 @@ import { Options, MonitorMeta, MonitorResult, - WizardOptions, PackageJson, + ProtectOptions, } from '../../../lib/types'; import { LegacyVulnApiResult } from '../../../lib/snyk-test/legacy'; import { MultiProjectResult } from '@snyk/cli-interface/legacy/plugin'; @@ -82,7 +82,7 @@ async function processPackageManager(options: Options) { return Promise.resolve(options); } -async function loadOrCreatePolicyFile(options: Options & WizardOptions) { +async function loadOrCreatePolicyFile(options: Options & ProtectOptions) { let policyFile; try { policyFile = await snyk.policy.load(options['policy-path'], options); diff --git a/src/lib/types.ts b/src/lib/types.ts index d838ca09f7..71752ad5de 100644 --- a/src/lib/types.ts +++ b/src/lib/types.ts @@ -13,8 +13,7 @@ export type DepTree = legacyApi.DepTree; export type ShowVulnPaths = 'none' | 'some' | 'all'; export interface TestOptions { - traverseNodeModules: boolean; - interactive: boolean; + traverseNodeModules?: boolean; pruneRepeatedSubdependencies?: boolean; showVulnPaths: ShowVulnPaths; failOn?: FailOn; @@ -27,7 +26,8 @@ export interface TestOptions { iacDirFiles?: IacFileInDirectory[]; } -export interface WizardOptions { +export interface ProtectOptions { + interactive?: boolean; newPolicy: boolean; } @@ -184,7 +184,8 @@ export type SupportedUserReachableFacingCliArgs = | 'reachable-vulns-timeout' | 'init-script' | 'integration-name' - | 'integration-version'; + | 'integration-version' + | 'show-vulnerable-paths'; export enum SupportedCliCommands { version = 'version', From 442e37f22fd9a477e0f7066cefff2ca367145e0d Mon Sep 17 00:00:00 2001 From: ghe Date: Thu, 11 Mar 2021 18:03:51 +0000 Subject: [PATCH 3/3] feat: delete vuln paths option once transformed Add tests for the setDefaultTestOptions() --- .../commands/test/set-default-test-options.ts | 9 +- test/run-test.spec.ts | 3 - test/set-default-test-options.spec.ts | 99 +++++++++++++++++++ test/snyk-code-test.spec.ts | 2 - 4 files changed, 104 insertions(+), 9 deletions(-) create mode 100644 test/set-default-test-options.spec.ts diff --git a/src/cli/commands/test/set-default-test-options.ts b/src/cli/commands/test/set-default-test-options.ts index 8ddf3ae519..a6106fe7fd 100644 --- a/src/cli/commands/test/set-default-test-options.ts +++ b/src/cli/commands/test/set-default-test-options.ts @@ -1,11 +1,12 @@ import * as config from '../../../lib/config'; import { Options, ShowVulnPaths, TestOptions } from '../../../lib/types'; -export function setDefaultTestOptions( - options: Options & TestOptions, -): Options & TestOptions { - const svpSupplied = (options['show-vulnerable-paths'] || '').toLowerCase(); +export function setDefaultTestOptions(options: Options): Options & TestOptions { + const svpSupplied = (options['show-vulnerable-paths'] || '') + .toString() + .toLowerCase(); + delete options['show-vulnerable-paths']; return { ...options, // org fallback to config unless specified diff --git a/test/run-test.spec.ts b/test/run-test.spec.ts index dd70a2fcf3..942d55fc62 100644 --- a/test/run-test.spec.ts +++ b/test/run-test.spec.ts @@ -15,7 +15,6 @@ describe('CLI runTest - propagate correct user error', () => { const options: Options & TestOptions = { path: '', traverseNodeModules: false, - interactive: false, showVulnPaths: 'none', }; @@ -37,7 +36,6 @@ describe('CLI runTest - propagate correct user error', () => { const options: Options & TestOptions = { path: '', traverseNodeModules: false, - interactive: false, showVulnPaths: 'none', }; @@ -59,7 +57,6 @@ describe('CLI runTest - propagate correct user error', () => { const options: Options & TestOptions = { path: '', traverseNodeModules: false, - interactive: false, showVulnPaths: 'none', }; diff --git a/test/set-default-test-options.spec.ts b/test/set-default-test-options.spec.ts new file mode 100644 index 0000000000..895f405d5e --- /dev/null +++ b/test/set-default-test-options.spec.ts @@ -0,0 +1,99 @@ +import { setDefaultTestOptions } from '../src/cli/commands/test/set-default-test-options'; + +describe('setDefaultTestOptions', () => { + it('defaults to show-vulnerable-paths:some & org from config when no options passed in', () => { + const updated = setDefaultTestOptions({ path: '/' }); + expect(updated).toEqual({ + org: undefined, + path: '/', + showVulnPaths: 'some', + }); + }); + + it('with show-vulnerable-paths set to `false` => `none`', () => { + const options = { + path: '/', + 'show-vulnerable-paths': 'false', + }; + const updated = setDefaultTestOptions(options); + expect(updated).toEqual({ + org: undefined, + path: '/', + showVulnPaths: 'none', + }); + }); + + it('with show-vulnerable-paths as boolean => `some`', () => { + const options = { + path: '/', + 'show-vulnerable-paths': true, + }; + const updated = setDefaultTestOptions(options as any); + expect(updated).toEqual({ + org: undefined, + path: '/', + showVulnPaths: 'some', + }); + }); + + it('with show-vulnerable-paths set to `none` => `none`', () => { + const options = { + path: '/', + 'show-vulnerable-paths': 'none', + }; + const updated = setDefaultTestOptions(options); + expect(updated).toEqual({ + org: undefined, + path: '/', + showVulnPaths: 'none', + }); + }); + + it('with show-vulnerable-paths set to `true` => `some`', () => { + const options = { + path: '/', + 'show-vulnerable-paths': 'true', + }; + const updated = setDefaultTestOptions(options); + expect(updated).toEqual({ + org: undefined, + path: '/', + showVulnPaths: 'some', + }); + }); + + it('with show-vulnerable-paths set to `some` => `some`', () => { + const options = { + path: '/', + 'show-vulnerable-paths': 'some', + }; + const updated = setDefaultTestOptions(options); + expect(updated).toEqual({ + org: undefined, + path: '/', + showVulnPaths: 'some', + }); + }); + + it('with show-vulnerable-paths set to `all` => `all`', () => { + const options = { + path: '/', + 'show-vulnerable-paths': 'all', + }; + const updated = setDefaultTestOptions(options); + expect(updated).toEqual({ + org: undefined, + path: '/', + showVulnPaths: 'all', + }); + }); + + it('with org set', () => { + const updated = setDefaultTestOptions({ path: '/', org: 'my-org' }); + expect(updated).toEqual({ + org: 'my-org', + path: '/', + showVulnPaths: 'some', + }); + }); +}); diff --git a/test/snyk-code-test.spec.ts b/test/snyk-code-test.spec.ts index 786542530b..55c15cdae3 100644 --- a/test/snyk-code-test.spec.ts +++ b/test/snyk-code-test.spec.ts @@ -63,7 +63,6 @@ describe('Test snyk code', () => { const options: Options & TestOptions = { path: '', traverseNodeModules: false, - interactive: false, showVulnPaths: 'none', }; isFeatureFlagSupportedForOrgSpy.mockResolvedValueOnce({ code: 401 }); @@ -82,7 +81,6 @@ describe('Test snyk code', () => { const options: Options & TestOptions = { path: '', traverseNodeModules: false, - interactive: false, showVulnPaths: 'none', code: true, };