Skip to content

Commit

Permalink
Merge pull request #935 from snyk/chore/refactor-project-count-test
Browse files Browse the repository at this point in the history
Refactor project count test & test formatters
  • Loading branch information
lili2311 committed Dec 31, 2019
2 parents 738095e + bff57f3 commit 96cff85
Show file tree
Hide file tree
Showing 14 changed files with 266 additions and 189 deletions.
13 changes: 1 addition & 12 deletions src/cli/commands/monitor/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import * as analytics from '../../../lib/analytics';
import { MonitorError } from '../../../lib/errors';
import { legacyPlugin as pluginApi } from '@snyk/cli-interface';
import { formatMonitorOutput } from './formatters/format-monitor-response';
import { getSubProjectCount } from '../../../lib/plugins/get-sub-project-count';

const SEPARATOR = '\n-------------------------------------------------------\n';

Expand Down Expand Up @@ -259,18 +260,6 @@ function convertMultiPluginResultToSingle(
}));
}

function getSubProjectCount(inspectResult): number | null {
if (
inspectResult.plugin.meta &&
inspectResult.plugin.meta.allSubProjectNames &&
inspectResult.plugin.meta.allSubProjectNames.length > 1
) {
return inspectResult.plugin.meta.allSubProjectNames.length;
}

return null;
}

async function validateMonitorPath(path, isDocker) {
const exists = await fs.exists(path);
if (!exists && !isDocker) {
Expand Down
38 changes: 38 additions & 0 deletions src/cli/commands/test/formatters/docker/format-docker-advice.ts
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;
}
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',
)
: '';
}
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;
}
3 changes: 3 additions & 0 deletions src/cli/commands/test/formatters/docker/index.ts
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 src/cli/commands/test/formatters/format-error-result-summary.ts
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 '';
}
72 changes: 72 additions & 0 deletions src/cli/commands/test/formatters/format-test-meta.ts
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');
}
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.';
}
10 changes: 10 additions & 0 deletions src/cli/commands/test/formatters/index.ts
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';

0 comments on commit 96cff85

Please sign in to comment.