Skip to content

Commit

Permalink
Merge pull request #1697 from snyk/feat/validate-test-options
Browse files Browse the repository at this point in the history
feat: extract validate test options
  • Loading branch information
lili2311 authored Mar 9, 2021
2 parents 3ed3db4 + e091054 commit f5786bf
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 22 deletions.
25 changes: 3 additions & 22 deletions src/cli/commands/test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import {
mapIacTestResult,
} from '../../../lib/snyk-test/iac-test-result';

import { FailOnError } from '../../../lib/errors/fail-on-error.ts';
import {
dockerRemediationForDisplay,
formatTestMeta,
Expand All @@ -43,7 +42,7 @@ import {
TEST_SUPPORTED_IAC_PROJECTS,
} from '../../../lib/iac/constants';
import { hasFixes, hasPatches, hasUpgrades } from './vuln-helpers';
import { FAIL_ON, FailOn, SEVERITIES } from '../../../lib/snyk-test/common';
import { FailOn } from '../../../lib/snyk-test/common';
import {
createErrorMappedResultsForJsonOutput,
dockerUserCTA,
Expand All @@ -54,6 +53,7 @@ import {
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';

const debug = Debug('snyk-test');
const SEPARATOR = '\n-------------------------------------------------------\n';
Expand Down Expand Up @@ -86,18 +86,7 @@ async function test(...args: MethodArgs): Promise<TestCommandResult> {
const svpSupplied = (options['show-vulnerable-paths'] || '').toLowerCase();
options.showVulnPaths = showVulnPathsMapping[svpSupplied] || 'some';

if (
options.severityThreshold &&
!validateSeverityThreshold(options.severityThreshold)
) {
return Promise.reject(new Error('INVALID_SEVERITY_THRESHOLD'));
}

if (options.failOn && !validateFailOn(options.failOn)) {
const error = new FailOnError();
return Promise.reject(chalk.red.bold(error.message));
}

validateTestOptions(options);
validateCredentials(options);

const ecosystem = getEcosystemForTest(options);
Expand Down Expand Up @@ -349,14 +338,6 @@ function shouldFail(vulnerableResults: any[], failOn: FailOn) {
return vulnerableResults.length > 0;
}

function validateSeverityThreshold(severityThreshold) {
return SEVERITIES.map((s) => s.verboseName).indexOf(severityThreshold) > -1;
}

function validateFailOn(arg: FailOn) {
return Object.keys(FAIL_ON).includes(arg);
}

function displayResult(
res: TestResult,
options: Options & TestOptions,
Expand Down
27 changes: 27 additions & 0 deletions src/cli/commands/test/validate-test-options.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import chalk from 'chalk';

import { TestOptions, Options } from '../../../lib/types';
import { FAIL_ON, FailOn, SEVERITIES } from '../../../lib/snyk-test/common';
import { FailOnError } from '../../../lib/errors/fail-on-error.ts';

export function validateTestOptions(options: TestOptions & Options) {
if (
options.severityThreshold &&
!validateSeverityThreshold(options.severityThreshold)
) {
throw new Error('INVALID_SEVERITY_THRESHOLD');
}

if (options.failOn && !validateFailOn(options.failOn)) {
const error = new FailOnError();
throw chalk.red.bold(error.message);
}
}

function validateSeverityThreshold(severityThreshold) {
return SEVERITIES.map((s) => s.verboseName).indexOf(severityThreshold) > -1;
}

function validateFailOn(arg: FailOn) {
return Object.keys(FAIL_ON).includes(arg);
}

0 comments on commit f5786bf

Please sign in to comment.