From b7447095296fb4bfdc93b75068c87e6021b34144 Mon Sep 17 00:00:00 2001 From: Or Sagie Date: Wed, 23 Oct 2019 22:07:39 +0300 Subject: [PATCH] fix: throw error when cli runs with multiple paths or sln and project-name option --- src/cli/index.ts | 20 +++++++++-- .../unsupported-option-combination-error.ts | 15 ++++++++ test/acceptance/cli-args.test.ts | 36 +++++++++++++++++++ 3 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 src/lib/errors/unsupported-option-combination-error.ts diff --git a/src/cli/index.ts b/src/cli/index.ts index 264767803d..58ab5685c5 100755 --- a/src/cli/index.ts +++ b/src/cli/index.ts @@ -16,6 +16,7 @@ import ansiEscapes = require('ansi-escapes'); import { isPathToPackageFile } from '../lib/detect'; import { updateCheck } from '../lib/updater'; import { MissingTargetFileError, FileFlagBadInputError } from '../lib/errors'; +import { UnsupportedOptionCombinationError } from '../lib/errors/unsupported-option-combination-error'; const debug = Debug('snyk'); const EXIT_CODES = { @@ -120,12 +121,20 @@ function checkRuntime() { } } -// Check if user specify package file name as part of path -// and throw error if so. +// Throw error if user specifies package file name as part of path, +// and if user specifies multiple paths and used project-name option. function checkPaths(args) { + let count = 0; for (const path of args.options._) { if (typeof path === 'string' && isPathToPackageFile(path)) { throw MissingTargetFileError(path); + } else if (typeof path === 'string') { + if (++count > 1 && args.options['project-name']) { + throw new UnsupportedOptionCombinationError([ + 'multiple paths', + 'project-name', + ]); + } } } } @@ -144,12 +153,19 @@ async function main() { typeof args.options.file === 'string' && (args.options.file as string).match(/\.sln$/) ) { + if (args.options['project-name']) { + throw new UnsupportedOptionCombinationError([ + 'file=*.sln', + 'project-name', + ]); + } sln.updateArgs(args); } else if (typeof args.options.file === 'boolean') { throw new FileFlagBadInputError(); } checkPaths(args); + res = await runCommand(args); } catch (error) { failed = true; diff --git a/src/lib/errors/unsupported-option-combination-error.ts b/src/lib/errors/unsupported-option-combination-error.ts new file mode 100644 index 0000000000..d84d562055 --- /dev/null +++ b/src/lib/errors/unsupported-option-combination-error.ts @@ -0,0 +1,15 @@ +import { CustomError } from './custom-error'; + +export class UnsupportedOptionCombinationError extends CustomError { + private static ERROR_MESSAGE = + 'The following option combination is not currently supported: '; + + constructor(options: string[]) { + super( + UnsupportedOptionCombinationError.ERROR_MESSAGE + JSON.stringify(options), + ); + this.code = 422; + this.userMessage = + UnsupportedOptionCombinationError.ERROR_MESSAGE + JSON.stringify(options); + } +} diff --git a/test/acceptance/cli-args.test.ts b/test/acceptance/cli-args.test.ts index cb1462923c..042bae2125 100644 --- a/test/acceptance/cli-args.test.ts +++ b/test/acceptance/cli-args.test.ts @@ -34,3 +34,39 @@ test('snyk test command should fail when --packageManager is not specified corre ); }); }); + +test('`test multiple paths with --project-name=NAME`', (t) => { + t.plan(1); + + exec( + `node ${main} test pathA pathB --project-name=NAME`, + (err, stdout, stderr) => { + if (err) { + throw err; + } + t.equals( + stdout.trim(), + 'The following option combination is not currently supported: ["multiple paths","project-name"]', + 'correct error output', + ); + }, + ); +}); + +test('`test --file=file.sln --project-name=NAME`', (t) => { + t.plan(1); + + exec( + `node ${main} test --file=file.sln --project-name=NAME`, + (err, stdout, stderr) => { + if (err) { + throw err; + } + t.equals( + stdout.trim(), + 'The following option combination is not currently supported: ["file=*.sln","project-name"]', + 'correct error output', + ); + }, + ); +});