-
Notifications
You must be signed in to change notification settings - Fork 534
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #935 from snyk/chore/refactor-project-count-test
Refactor project count test & test formatters
- Loading branch information
Showing
14 changed files
with
266 additions
and
189 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
src/cli/commands/test/formatters/docker/format-docker-advice.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import chalk from 'chalk'; | ||
import { | ||
TestResult, | ||
BaseImageRemediationAdvice, | ||
} from '../../../../../lib/snyk-test/legacy'; | ||
|
||
export function dockerRemediationForDisplay(res: TestResult) { | ||
if (!res.docker || !res.docker.baseImageRemediation) { | ||
return ''; | ||
} | ||
const { advice, message } = res.docker.baseImageRemediation; | ||
const out = [] as any[]; | ||
|
||
if (advice) { | ||
for (const item of advice) { | ||
out.push(getTerminalStringFormatter(item)(item.message)); | ||
} | ||
} else if (message) { | ||
out.push(message); | ||
} else { | ||
return ''; | ||
} | ||
return `\n\n${out.join('\n')}`; | ||
} | ||
|
||
function getTerminalStringFormatter({ | ||
color, | ||
bold, | ||
}: BaseImageRemediationAdvice) { | ||
let formatter = chalk; | ||
if (color && formatter[color]) { | ||
formatter = formatter[color]; | ||
} | ||
if (bold) { | ||
formatter = formatter.bold; | ||
} | ||
return formatter; | ||
} |
16 changes: 16 additions & 0 deletions
16
src/cli/commands/test/formatters/docker/format-docker-binary-heading.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import * as _ from 'lodash'; | ||
import chalk from 'chalk'; | ||
|
||
export function createDockerBinaryHeading(pkgInfo): string { | ||
const binaryName = pkgInfo.pkg.name; | ||
const binaryVersion = pkgInfo.pkg.version; | ||
const numOfVulns = _.values(pkgInfo.issues).length; | ||
const vulnCountText = numOfVulns > 1 ? 'vulnerabilities' : 'vulnerability'; | ||
return numOfVulns | ||
? chalk.bold.white( | ||
`------------ Detected ${numOfVulns} ${vulnCountText}` + | ||
` for ${binaryName}@${binaryVersion} ------------`, | ||
'\n', | ||
) | ||
: ''; | ||
} |
23 changes: 23 additions & 0 deletions
23
src/cli/commands/test/formatters/docker/format-docker-binary-issues.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import * as _ from 'lodash'; | ||
import { createDockerBinaryHeading } from './format-docker-binary-heading'; | ||
import { Options, TestOptions } from '../../../../../lib/types'; | ||
import { formatIssues } from '../legacy-format-issue'; | ||
|
||
export function formatDockerBinariesIssues( | ||
dockerBinariesSortedGroupedVulns, | ||
binariesVulns, | ||
options: Options & TestOptions, | ||
): string[] { | ||
const binariesIssuesOutput = [] as string[]; | ||
for (const pkgInfo of _.values(binariesVulns.affectedPkgs)) { | ||
binariesIssuesOutput.push(createDockerBinaryHeading(pkgInfo)); | ||
const binaryIssues = dockerBinariesSortedGroupedVulns.filter( | ||
(vuln) => vuln.metadata.name === pkgInfo.pkg.name, | ||
); | ||
const formattedBinaryIssues = binaryIssues.map((vuln) => | ||
formatIssues(vuln, options), | ||
); | ||
binariesIssuesOutput.push(formattedBinaryIssues.join('\n\n')); | ||
} | ||
return binariesIssuesOutput; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export { dockerRemediationForDisplay } from './format-docker-advice'; | ||
export { formatDockerBinariesIssues } from './format-docker-binary-issues'; | ||
export { createDockerBinaryHeading } from './format-docker-binary-heading'; |
11 changes: 11 additions & 0 deletions
11
src/cli/commands/test/formatters/format-error-result-summary.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
export function summariseErrorResults(errorResultsLength: number): string { | ||
const projects = errorResultsLength > 1 ? 'projects' : 'project'; | ||
if (errorResultsLength > 0) { | ||
return ( | ||
` Failed to test ${errorResultsLength} ${projects}.\n` + | ||
'Run with `-d` for debug output and contact support@snyk.io' | ||
); | ||
} | ||
|
||
return ''; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import chalk from 'chalk'; | ||
import { rightPadWithSpaces } from '../../../../lib/right-pad'; | ||
import { TestOptions, Options } from '../../../../lib/types'; | ||
import { TestResult } from '../../../../lib/snyk-test/legacy'; | ||
|
||
export function formatTestMeta( | ||
res: TestResult, | ||
options: Options & TestOptions, | ||
): string { | ||
const padToLength = 19; // chars to align | ||
const packageManager = res.packageManager || options.packageManager; | ||
const targetFile = res.targetFile || options.file; | ||
const openSource = res.isPrivate ? 'no' : 'yes'; | ||
const meta = [ | ||
chalk.bold(rightPadWithSpaces('Organization: ', padToLength)) + res.org, | ||
chalk.bold(rightPadWithSpaces('Package manager: ', padToLength)) + | ||
packageManager, | ||
]; | ||
if (targetFile) { | ||
meta.push( | ||
chalk.bold(rightPadWithSpaces('Target file: ', padToLength)) + targetFile, | ||
); | ||
} | ||
if (res.projectName) { | ||
meta.push( | ||
chalk.bold(rightPadWithSpaces('Project name: ', padToLength)) + | ||
res.projectName, | ||
); | ||
} | ||
if (options.docker) { | ||
meta.push( | ||
chalk.bold(rightPadWithSpaces('Docker image: ', padToLength)) + | ||
options.path, | ||
); | ||
} else { | ||
meta.push( | ||
chalk.bold(rightPadWithSpaces('Open source: ', padToLength)) + openSource, | ||
); | ||
meta.push( | ||
chalk.bold(rightPadWithSpaces('Project path: ', padToLength)) + | ||
options.path, | ||
); | ||
} | ||
if (res.docker && res.docker.baseImage) { | ||
meta.push( | ||
chalk.bold(rightPadWithSpaces('Base image: ', padToLength)) + | ||
res.docker.baseImage, | ||
); | ||
} | ||
|
||
if (res.filesystemPolicy) { | ||
meta.push( | ||
chalk.bold(rightPadWithSpaces('Local Snyk policy: ', padToLength)) + | ||
chalk.green('found'), | ||
); | ||
if (res.ignoreSettings && res.ignoreSettings.disregardFilesystemIgnores) { | ||
meta.push( | ||
chalk.bold( | ||
rightPadWithSpaces('Local Snyk policy ignored: ', padToLength), | ||
) + chalk.red('yes'), | ||
); | ||
} | ||
} | ||
if (res.licensesPolicy) { | ||
meta.push( | ||
chalk.bold(rightPadWithSpaces('Licenses: ', padToLength)) + | ||
chalk.green('enabled'), | ||
); | ||
} | ||
|
||
return meta.join('\n'); | ||
} |
20 changes: 20 additions & 0 deletions
20
src/cli/commands/test/formatters/format-vulnerable-result-summary.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { TestOptions } from '../../../../lib/types'; | ||
|
||
export function summariseVulnerableResults( | ||
vulnerableResults, | ||
options: TestOptions, | ||
): string { | ||
const vulnsLength = vulnerableResults.length; | ||
if (vulnsLength) { | ||
if (options.showVulnPaths) { | ||
return `, ${vulnsLength} contained vulnerable paths.`; | ||
} | ||
return `, ${vulnsLength} had issues.`; | ||
} | ||
|
||
if (options.showVulnPaths) { | ||
return ', no vulnerable paths were found.'; | ||
} | ||
|
||
return ', no issues were found.'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
export { formatTestMeta } from './format-test-meta'; | ||
export { summariseVulnerableResults } from './format-vulnerable-result-summary'; | ||
export { summariseErrorResults } from './format-error-result-summary'; | ||
export { formatIssues } from './legacy-format-issue'; | ||
export { formatLegalInstructions } from './legal-license-instructions'; | ||
export { | ||
formatIssuesWithRemediation, | ||
getSeverityValue, | ||
} from './remediation-based-format-issues'; | ||
export * from './docker'; |
Oops, something went wrong.