Skip to content

Commit

Permalink
libexec/bats: Consolidate, update usage
Browse files Browse the repository at this point in the history
Per @sublimino's suggestion in sstephenson#126, `usage()` and `help()` have been
combined into `usage()`. Also updates the format of the `Usage:` stanza
to make it more readable.

Also adds the `BATS_VERSION` environment variable, which seems like a
reasonable item to export. This implementation is in conflict with
the change @jasonkarns made in sstephenson#124, though as small change to that
PR should bring it in line.
  • Loading branch information
Bland, Mike committed Jul 21, 2018
1 parent 4bacb99 commit 04f9cb6
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
22 changes: 11 additions & 11 deletions libexec/bats-core/bats
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
#!/usr/bin/env bash
set -e

version() {
printf 'Bats 1.1.0\n'
}
export BATS_VERSION='1.1.0'

usage() {
version
printf '%s\n' \
'Usage: bats [-h] [-c] [-f <regex>] [-r] [-p | -t] <test> [<test> ...]'
version() {
printf 'Bats %s\n' "$BATS_VERSION"
}

abort() {
Expand All @@ -17,15 +13,18 @@ abort() {
exit 1
}

help() {
usage() {
local cmd="${0##*/}"
local line
usage

while IFS= read -r line; do
printf '%s\n' "$line"
done <<END_OF_HELP_TEXT
Usage: $cmd [-cr] [-f <regex>] [-p | -t] <test>...
$cmd [-h | -v]
<test> is the path to a Bats test file, or the path to a directory
containing Bats test files.
containing Bats test files (ending with ".bats").
-c, --count Count the number of test cases without running any tests
-f, --filter Filter test cases by names matching the regular expression
Expand Down Expand Up @@ -92,7 +91,8 @@ fi
while [[ "$#" -ne 0 ]]; do
case "$1" in
-h|--help)
help
version
usage
exit 0
;;
-v|--version)
Expand Down
4 changes: 2 additions & 2 deletions test/bats.bats
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ fixtures bats
run bats
[ $status -eq 1 ]
[ "${lines[0]}" == 'Error: Must specify at least one <test>' ]
[ "${lines[2]%% *}" == 'Usage:' ]
[ "${lines[1]%% *}" == 'Usage:' ]
}

@test "invalid option prints message and usage instructions" {
run bats --invalid-option
[ $status -eq 1 ]
emit_debug_output
[ "${lines[0]}" == "Error: Bad command line option '--invalid-option'" ]
[ "${lines[2]%% *}" == 'Usage:' ]
[ "${lines[1]%% *}" == 'Usage:' ]
}

@test "-v and --version print version number" {
Expand Down

0 comments on commit 04f9cb6

Please sign in to comment.