Skip to content

Commit

Permalink
Merge pull request #198 from mbland/skip-if-none-present-#186
Browse files Browse the repository at this point in the history
bats/helpers: Add skip_if_none_present_on_system
  • Loading branch information
mbland committed Sep 2, 2017
2 parents f273ac2 + f5c7434 commit f5c7fca
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 19 deletions.
23 changes: 23 additions & 0 deletions lib/bats/helpers
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,29 @@ skip_if_system_missing() {
fi
}

# Skips the current test if zero of the listed system programs are present.
#
# Useful for testing behavior that can use any one of a number of system
# programs based on what's available, such as `./go get file` being able to use
# `curl`, `fetch`, or `wget`.
#
# Arguments:
# $@: System programs of which at least one must be present to run the test
skip_if_none_present_on_system() {
local missing

if ! command -v "$@" >/dev/null; then
if [[ "$#" -eq '1' ]]; then
skip "$1 isn't installed on the system"
elif [[ "$#" -eq '2' ]]; then
skip "Neither $1 nor $2 is installed on the system"
else
printf -v missing '%s, ' "${@:1:$(($# - 1))}"
skip "None of ${missing% } or ${!#} are installed on the system"
fi
fi
}

# Joins lines using a delimiter into a user-defined variable
#
# Just like `@go.join` from `lib/strings`, except that it doesn't depend on any
Expand Down
28 changes: 28 additions & 0 deletions tests/bats-helpers.bats
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,34 @@ __check_dirs_exist() {
'ok 1 # skip (foo, bar, baz not installed on the system) skip if missing'
}

@test "$SUITE: skip_if_none_present_on_system" {
stub_program_in_path 'quux'
run_bats_test_suite_in_isolation 'skip-if-none-present-on-system-test.bats' \
"load '$_GO_CORE_DIR/lib/bats/helpers'" \
'@test "should not skip if at least one present" {' \
' skip_if_none_present_on_system foo bar baz quux' \
'}'\
'@test "single program missing" {' \
' skip_if_none_present_on_system foo' \
'}' \
'@test "two programs missing" {' \
' skip_if_none_present_on_system foo bar' \
'}' \
'@test "three programs missing" {' \
' skip_if_none_present_on_system foo bar baz' \
'}'
assert_success

local expected_messages=("foo isn't installed on the system"
'Neither foo nor bar is installed on the system'
'None of foo, bar, or baz are installed on the system')
assert_lines_equal '1..4' \
'ok 1 should not skip if at least one present' \
"ok 2 # skip (${expected_messages[0]}) single program missing" \
"ok 3 # skip (${expected_messages[1]}) two programs missing" \
"ok 4 # skip (${expected_messages[2]}) three programs missing"
}

@test "$SUITE: test_join fails if result variable name is invalid" {
create_bats_test_script test-script \
". '$_GO_CORE_DIR/lib/bats/helpers'" \
Expand Down
19 changes: 0 additions & 19 deletions tests/template.bats
Original file line number Diff line number Diff line change
Expand Up @@ -75,25 +75,6 @@ assert_go_core_unpacked() {
restore_bats_shell_options "$result"
}

# Skips the current test if zero of the listed system programs are present.
#
# Arguments:
# $@: System programs of which at least one must be present to run the test
skip_if_none_present_on_system() {
local missing

if ! command -v "$@" >/dev/null; then
if [[ "$#" -eq '1' ]]; then
skip "$1 isn't installed on the system"
elif [[ "$#" -eq '2' ]]; then
skip "Neither $1 nor $2 are installed on the system"
else
printf -v missing '%s, ' "${@:1:$(($# - 1))}"
skip "None of ${missing% } or ${@:$#} are installed on the system"
fi
fi
}

# Converts a Unix path or 'file://' URL to a Git for Windows native path.
#
# This is useful when passing file paths or URLs to native programs on Git for
Expand Down

0 comments on commit f5c7fca

Please sign in to comment.