diff --git a/lib/bats/helpers b/lib/bats/helpers index edef0ba..76a281a 100644 --- a/lib/bats/helpers +++ b/lib/bats/helpers @@ -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 diff --git a/tests/bats-helpers.bats b/tests/bats-helpers.bats index 3b7f7d3..658b227 100644 --- a/tests/bats-helpers.bats +++ b/tests/bats-helpers.bats @@ -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'" \ diff --git a/tests/template.bats b/tests/template.bats index 5291934..4ea913e 100644 --- a/tests/template.bats +++ b/tests/template.bats @@ -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