Skip to content

Commit

Permalink
feat: improve help command
Browse files Browse the repository at this point in the history
  • Loading branch information
Avishag Israeli committed Nov 12, 2020
1 parent 312ca91 commit 1237e7d
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ jobs:
echo "Checkout the README in test/smoke folder for more details about this step"
unset SNYK_API
unset SNYK_API_KEY
shellspec -f d
shellspec -f d -e REGRESSION_TEST=1
test-windows:
<<: *defaults
Expand Down
10 changes: 8 additions & 2 deletions src/cli/args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,17 @@ export function args(rawArgv: string[]): Args {
if (argv.help === true || command === 'help') {
argv.help = 'help';
}
command = 'help';

// If command has a value prior to running it over with “help” and argv.help contains "help", save the command in argv._
// so that no argument gets deleted or ignored. This ensures `snyk --help [command]` and `snyk [command] --help` return the
// specific help page instead of the generic one.
// This change also covers the scenario of 'snyk [mode] [command] --help' and 'snyk --help [mode] [command]`.
if (!argv._.length) {
argv._.unshift((argv.help as string) || 'help');
command && argv.help === 'help'
? argv._.unshift(command)
: argv._.unshift((argv.help as string) || 'help');
}
command = 'help';
}

if (command && command.indexOf('config:') === 0) {
Expand Down
13 changes: 5 additions & 8 deletions src/cli/commands/help.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import * as fs from 'fs';
import * as path from 'path';
import * as Debug from 'debug';
const debug = Debug('snyk');

export = async function help(item: string | boolean) {
if (!item || item === true || typeof item !== 'string') {
Expand All @@ -12,11 +10,10 @@ export = async function help(item: string | boolean) {
// aka: /\W/g but figured this was easier to read
item = item.replace(/[^a-z-]/gi, '');

const filename = path.resolve(__dirname, '../../../help', item + '.txt');
try {
return fs.readFileSync(filename, 'utf8');
} catch (error) {
debug(error);
return `'${item}' help can't be found at location: ${filename}`;
if (!fs.existsSync(path.resolve(__dirname, '../../../help', item + '.txt'))) {
item = 'help';
}

const filename = path.resolve(__dirname, '../../../help', item + '.txt');
return fs.readFileSync(filename, 'utf8');
};
60 changes: 60 additions & 0 deletions test/smoke/spec/snyk_basic_spec.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,66 @@ Describe "Snyk CLI basics"
The stderr should equal ""
End
End

Describe "extensive snyk help"
Skip if "execute only in regression test" check_if_regression_test

It "prints help info when called with unknown argument"
When run snyk help hello
The output should include "$ snyk [command] [options] [package]"
The status should be success
# TODO: unusable with our current docker issues
The stderr should equal ""
End

It "prints help info when called with flag and unknown argument"
When run snyk --help hello
The output should include "$ snyk [command] [options] [package]"
The status should be success
# TODO: unusable with our current docker issues
The stderr should equal ""
End

It "prints specific help info"
When run snyk file --help
The output should include "you can specify the file that Snyk should inspect"
The status should be success
# TODO: unusable with our current docker issues
The stderr should equal ""
End

It "prints specific help info for container"
When run snyk -h container
The output should include "$ snyk container [command] [options] [image]"
The status should be success
# TODO: unusable with our current docker issues
The stderr should equal ""
End

It "prints specific help info for iac"
When run snyk iac -help
The output should include "$ snyk iac [command] [options] <path>"
The status should be success
# TODO: unusable with our current docker issues
The stderr should equal ""
End

It "prints specific help info when called with flag and equals sign"
When run snyk --help=file
The output should include "you can specify the file that Snyk should inspect"
The status should be success
# TODO: unusable with our current docker issues
The stderr should equal ""
End

It "prints help info for argument with mode"
When run snyk --help container test
The output should include "$ snyk container [command] [options] [image]"
The status should be success
# TODO: unusable with our current docker issues
The stderr should equal ""
End
End

Describe "snyk config"
It "prints config"
Expand Down
2 changes: 2 additions & 0 deletions test/smoke/spec/spec_helper.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,5 @@ restore_is_ci_flags() {
if [ -n "${CI}" ]; then CI=$CI_BACKUP_VALUE; unset CI_BACKUP_VALUE; fi
if [ -n "${CIRCLECI}" ]; then CIRCLECI=$CIRCLECI_BACKUP_VALUE; unset CIRCLECI_BACKUP_VALUE; fi
}

check_if_regression_test() { ! [ "${REGRESSION_TEST}" = "1" ]; }

0 comments on commit 1237e7d

Please sign in to comment.