Skip to content

Commit

Permalink
feat: auto detect snyk test
Browse files Browse the repository at this point in the history
Adding auto detect logic to snyk test.
Auto detecting npm and ruby in same directory to begin with.
Added tests, moved mono-repo-project test fixture to
acceptance test workspace.
Propagate target file and project name to display meta
Throwing if docker and all-project options used together
  • Loading branch information
gitphill authored and orsagie committed Dec 24, 2019
1 parent 74a6c33 commit 6c1db27
Show file tree
Hide file tree
Showing 95 changed files with 717 additions and 6,000 deletions.
1 change: 1 addition & 0 deletions src/cli/args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ export function args(rawArgv: string[]): Args {
'skip-unresolved',
'scan-all-unmanaged',
'fail-on',
'all-projects',
]) {
if (argv[dashedArg]) {
const camelCased = dashToCamelCase(dashedArg);
Expand Down
25 changes: 15 additions & 10 deletions src/cli/commands/test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,13 @@ import {
SEVERITY,
GroupedVuln,
VulnMetaData,
TestResult,
} from '../../../lib/snyk-test/legacy';
import { formatIssues } from './formatters/legacy-format-issue';
import { WIZARD_SUPPORTED_PACKAGE_MANAGERS } from '../../../lib/package-managers';
import {
WIZARD_SUPPORTED_PACKAGE_MANAGERS,
SupportedPackageManagers,
} from '../../../lib/package-managers';
import {
formatIssuesWithRemediation,
getSeverityValue,
Expand Down Expand Up @@ -86,7 +90,7 @@ async function test(...args: MethodArgs): Promise<string> {
let res;

try {
res = (await snyk.test(path, testOpts)) as LegacyVulnApiResult;
res = await snyk.test(path, testOpts);
} catch (error) {
// Possible error cases:
// - the test found some vulns. `error.message` is a
Expand Down Expand Up @@ -349,10 +353,11 @@ function summariseErrorResults(errorResults) {
return '';
}

function displayResult(res, options: Options & TestOptions) {
function displayResult(res: TestResult, options: Options & TestOptions) {
const meta = metaForDisplay(res, options);
const dockerAdvice = dockerRemediationForDisplay(res);
const packageManager = options.packageManager;
const packageManager =
(res.packageManager as SupportedPackageManagers) || options.packageManager;
const localPackageTest = isLocalFolder(options.path);
const prefix = chalk.bold.white('\nTesting ' + options.path + '...\n\n');

Expand Down Expand Up @@ -569,23 +574,23 @@ function rightPadWithSpaces(s, desiredLength) {

function metaForDisplay(res, options) {
const padToLength = 19; // chars to align
const packageManager = options.packageManager || res.packageManager;
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 (options.file) {
if (targetFile) {
meta.push(
chalk.bold(rightPadWithSpaces('Target file: ', padToLength)) +
options.file,
chalk.bold(rightPadWithSpaces('Target file: ', padToLength)) + targetFile,
);
}
if (options.projectName) {
if (res.projectName) {
meta.push(
chalk.bold(rightPadWithSpaces('Project name: ', padToLength)) +
options.projectName,
res.projectName,
);
}
if (options.docker) {
Expand Down
13 changes: 13 additions & 0 deletions src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,19 @@ async function main() {
'scan-all-unmanaged',
]);
}

if (
(args.options['project-name'] ||
args.options.file ||
args.options.packageManager ||
args.options.docker) &&
args.options.allProjects
) {
throw new UnsupportedOptionCombinationError([
'project-name or file or package-manager or docker',
'all-projects',
]);
}
if (
args.options.file &&
typeof args.options.file === 'string' &&
Expand Down
14 changes: 12 additions & 2 deletions src/lib/detect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ const DETECTABLE_FILES: string[] = [
'Podfile.lock',
];

export const AUTO_DETECTABLE_FILES: string[] = [
'package-lock.json',
'yarn.lock',
'package.json',
'Gemfile',
'Gemfile.lock',
];

// when file is specified with --file, we look it up here
const DETECTABLE_PACKAGE_MANAGERS: {
[name: string]: SupportedPackageManagers;
Expand Down Expand Up @@ -73,7 +81,7 @@ export function isPathToPackageFile(path) {
return false;
}

export function detectPackageManager(root, options) {
export function detectPackageManager(root: string, options) {
// If user specified a package manager let's use it.
if (options.packageManager) {
return options.packageManager;
Expand Down Expand Up @@ -142,7 +150,9 @@ export function detectPackageFile(root) {
debug('no package file found in ' + root);
}

export function detectPackageManagerFromFile(file) {
export function detectPackageManagerFromFile(
file: string,
): SupportedPackageManagers {
let key = pathLib.basename(file);

// TODO: fix this to use glob matching instead
Expand Down
2 changes: 1 addition & 1 deletion src/lib/module-info/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export function ModuleInfo(plugin, policy) {
root,
targetFile,
options,
): Promise<pluginApi.SinglePackageResult> {
): Promise<pluginApi.SinglePackageResult | pluginApi.MultiProjectResult> {
const pluginOptions = _.merge(
{
args: options._doubleDashArgs,
Expand Down
2 changes: 1 addition & 1 deletion src/lib/plugins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { SupportedPackageManagers } from '../package-managers';
import { UnsupportedPackageManagerError } from '../errors';

export function loadPlugin(
packageManager: SupportedPackageManagers,
packageManager: SupportedPackageManagers | undefined,
options: types.Options = {},
): types.Plugin {
if (options.docker) {
Expand Down
3 changes: 1 addition & 2 deletions src/lib/snyk-test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@ const chalk = require('chalk');
const pm = require('../package-managers');
const { UnsupportedPackageManagerError } = require('../errors');

function test(root, options, callback) {
async function test(root, options, callback) {
if (typeof options === 'function') {
callback = options;
options = {};
}
if (!options) {
options = {};
}

const promise = executeTest(root, options);
if (callback) {
promise
Expand Down
4 changes: 4 additions & 0 deletions src/lib/snyk-test/legacy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ export interface LegacyVulnApiResult {
remediation?: RemediationChanges;
}

export interface TestResult extends LegacyVulnApiResult {
targetFile?: string;
}

interface UpgradePathItem {
name: string;
version: string;
Expand Down
Loading

0 comments on commit 6c1db27

Please sign in to comment.