Skip to content

Commit

Permalink
fix: throw error when cli runs with multiple paths or sln and project…
Browse files Browse the repository at this point in the history
…-name option
  • Loading branch information
orsagie committed Oct 27, 2019
1 parent 80d460a commit b744709
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 2 deletions.
20 changes: 18 additions & 2 deletions src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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',
]);
}
}
}
}
Expand All @@ -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;
Expand Down
15 changes: 15 additions & 0 deletions src/lib/errors/unsupported-option-combination-error.ts
Original file line number Diff line number Diff line change
@@ -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);
}
}
36 changes: 36 additions & 0 deletions test/acceptance/cli-args.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
);
},
);
});

0 comments on commit b744709

Please sign in to comment.