From dc0c6457235bd6b0a005fe6396fd4f6c43178565 Mon Sep 17 00:00:00 2001 From: Quentin Neill Date: Mon, 27 Nov 2017 12:15:36 -0600 Subject: [PATCH] Add bad command line option errors --- libexec/bats | 6 ++++-- libexec/bats-exec-test | 2 +- test/bats.bats | 12 ++++++++++-- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/libexec/bats b/libexec/bats index 71f392f..a6fdf80 100755 --- a/libexec/bats +++ b/libexec/bats @@ -6,7 +6,9 @@ version() { } usage() { + local errmsg="$1" version + [ -n "$errmsg" ] && echo "Error: $errmsg" echo "Usage: bats [-c] [-p | -t] [ ...]" } @@ -100,14 +102,14 @@ for option in "${options[@]}"; do pretty="1" ;; * ) - usage >&2 + usage "Bad command line option '-$option'" >&2 exit 1 ;; esac done if [ "${#arguments[@]}" -eq 0 ]; then - usage >&2 + usage "Must specify at least one " >&2 exit 1 fi diff --git a/libexec/bats-exec-test b/libexec/bats-exec-test index 8f3bd51..bebcb09 100755 --- a/libexec/bats-exec-test +++ b/libexec/bats-exec-test @@ -17,7 +17,7 @@ fi BATS_TEST_FILENAME="$1" if [ -z "$BATS_TEST_FILENAME" ]; then - echo "usage: bats-exec " >&2 + echo "usage: bats-exec-test " >&2 exit 1 elif [ ! -f "$BATS_TEST_FILENAME" ]; then echo "bats: $BATS_TEST_FILENAME does not exist" >&2 diff --git a/test/bats.bats b/test/bats.bats index f1aff29..364aa35 100755 --- a/test/bats.bats +++ b/test/bats.bats @@ -3,10 +3,18 @@ load test_helper fixtures bats -@test "no arguments prints usage instructions" { +@test "no arguments prints message and usage instructions" { run bats [ $status -eq 1 ] - [ $(expr "${lines[1]}" : "Usage:") -ne 0 ] + [ $(expr "${lines[1]}" : "Error: Must specify at least one") -ne 0 ] + [ $(expr "${lines[2]}" : "Usage:") -ne 0 ] +} + +@test "invalid option prints message and usage instructions" { + run bats --invalid-option + [ $status -eq 1 ] + [ $(expr "${lines[1]}" : "Error: Bad command line option") -ne 0 ] + [ $(expr "${lines[2]}" : "Usage:") -ne 0 ] } @test "-v and --version print version number" {