-
Notifications
You must be signed in to change notification settings - Fork 573
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor: set default test options #1704
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import * as config from '../../../lib/config'; | ||
import { Options, ShowVulnPaths, TestOptions } from '../../../lib/types'; | ||
|
||
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 | ||
org: options.org || config.org, | ||
// making `show-vulnerable-paths` 'some' by default. | ||
showVulnPaths: showVulnPathsMapping[svpSupplied] || 'some', | ||
}; | ||
} | ||
|
||
const showVulnPathsMapping: Record<string, ShowVulnPaths> = { | ||
false: 'none', | ||
none: 'none', | ||
true: 'some', | ||
some: 'some', | ||
all: 'all', | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,8 +13,7 @@ export type DepTree = legacyApi.DepTree; | |
export type ShowVulnPaths = 'none' | 'some' | 'all'; | ||
|
||
export interface TestOptions { | ||
traverseNodeModules: boolean; | ||
interactive: boolean; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is a protect option for wizard only |
||
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', | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just curious, in the jest's equals logic, is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it does indeed! |
||
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', | ||
}); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this essentially adds support for
--show-vulnerable-paths
to be treated as--show-vulnerable-paths=true
WDYT?Otherwise if you run test with
snyk test --show-vulnerable-paths
you get a bad error that saystoLowerCase is not a function
because it is not a string. Or alternative is to add validation for this options and throw outside of this function somewhere probablyThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 this sounds in line with how arg parsing libraries should treat this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@gitphill while @aviadhahami is off, could you help 👀 ?