diff --git a/lib/bats/helper-function b/lib/bats/helper-function index dfe21fe..1a4781b 100644 --- a/lib/bats/helper-function +++ b/lib/bats/helper-function @@ -52,14 +52,15 @@ restore_bats_shell_options() { local result="${1:-0}" local target_stack_item_pattern=" ${FUNCNAME[1]} ${BASH_SOURCE[1]}$" - # After removing our caller from BATS_CURRENT_STACK_TRACE and restoring the - # Bats shell options, the `return` call at the end of the function will fire - # `bats_debug_trap`, which sets BATS_CURRENT_STACK_TRACE to - # BATS_PREVIOUS_STACK_TRACE. - # - # If `result` is nonzero, `bats_error_trap` will fire and set - # BATS_ERROR_STACK_TRACE to BATS_PREVIOUS_STACK_TRACE and fail the test. if [[ "${BATS_CURRENT_STACK_TRACE[0]}" =~ $target_stack_item_pattern ]]; then + # After removing our caller from BATS_CURRENT_STACK_TRACE and restoring the + # Bats shell options, the `return` call at the end of the function will fire + # `bats_debug_trap`, which sets BATS_CURRENT_STACK_TRACE to + # BATS_PREVIOUS_STACK_TRACE. + # + # Then, if `result` is nonzero, `return` will fire `bats_error_trap`, which + # sets BATS_ERROR_STACK_TRACE to BATS_PREVIOUS_STACK_TRACE, and the error + # response will fail the test. unset 'BATS_CURRENT_STACK_TRACE[0]' set -eET fi diff --git a/lib/bats/helpers b/lib/bats/helpers index 223c22d..e1a5fc5 100644 --- a/lib/bats/helpers +++ b/lib/bats/helpers @@ -41,6 +41,8 @@ # This is good practice even if you call `remove_bats_test_dirs` in your # `environment.bash` file. +. "${BASH_SOURCE%/*}/helper-function" + # A subdirectory of BATS_TMPDIR that contains a space. # # Using this path instead of BATS_TMPDIR directly helps ensure that shell @@ -61,8 +63,9 @@ BATS_TEST_BINDIR="$BATS_TEST_ROOTDIR/bin" # Arguments: # $1: Path to the project's top-level test directory set_bats_test_suite_name() { - local test_rootdir="$(cd "$1" && echo "$PWD")" - local relative_filename="${BATS_TEST_FILENAME#$test_rootdir/}" + cd "$1" + local relative_filename="${BATS_TEST_FILENAME#$PWD/}" + cd - &>/dev/null readonly SUITE="${relative_filename%.bats}" } @@ -74,18 +77,9 @@ set_bats_test_suite_name() { # Arguments: # $@: Paths of subdirectories relative to BATS_TEST_ROOTDIR create_bats_test_dirs() { - local dirs_to_create=() - local test_dir - - for test_dir in "${@/#/$BATS_TEST_ROOTDIR/}"; do - if [[ ! -d "$test_dir" ]]; then - dirs_to_create+=("$test_dir") - fi - done - - if [[ "${#dirs_to_create[@]}" -ne '0' ]]; then - mkdir -p "${dirs_to_create[@]}" - fi + set "$DISABLE_BATS_SHELL_OPTIONS" + __create_bats_test_dirs "$@" + restore_bats_shell_options "$?" } # Creates a test script relative to BATS_TEST_ROOTDIR @@ -100,26 +94,9 @@ create_bats_test_dirs() { # $1: Path of the script relative to BATS_TEST_ROOTDIR # ...: Lines comprising the script create_bats_test_script() { - local script="$1" - shift - local script_dir="${script%/*}" - - if [[ -z "$script" ]]; then - echo "No test script specified" >&2 - exit 1 - elif [[ "$script_dir" == "$script" ]]; then - script_dir='' - fi - - create_bats_test_dirs "$script_dir" - script="$BATS_TEST_ROOTDIR/$script" - rm -f "$script" - - if [[ "${1:0:2}" != '#!' ]]; then - echo "#! /usr/bin/env bash" >"$script" - fi - printf '%s\n' "$@" >>"$script" - chmod 700 "$script" + set "$DISABLE_BATS_SHELL_OPTIONS" + __create_bats_test_script "$@" + restore_bats_shell_options "$?" } # Recursively removes `BATS_TEST_ROOTDIR` and its subdirectories @@ -183,18 +160,15 @@ skip_if_cannot_trigger_file_permission_failure() { # Arguments: # ...: System programs that must be present for the test case to proceed skip_if_system_missing() { - local missing=() - local program + local __missing=() - for program in "$@"; do - if ! command -v "$program" >/dev/null; then - missing+=("$program") - fi - done + set "$DISABLE_BATS_SHELL_OPTIONS" + __search_for_missing_programs "$@" + restore_bats_shell_options "$?" - if [[ "${#missing[@]}" -ne '0' ]]; then - printf -v missing '%s, ' "${missing[@]}" - skip "${missing%, } not installed on the system" + if [[ "${#__missing[@]}" -ne '0' ]]; then + printf -v __missing '%s, ' "${__missing[@]}" + skip "${__missing%, } not installed on the system" fi } @@ -311,12 +285,9 @@ test_filter() { # compare the exact lines of `output` using `assert_lines_equal` and other # `lines`-based assertions. split_bats_output_into_lines() { - local line - lines=() - - while IFS= read -r line; do - lines+=("${line%$'\r'}") - done <<<"$output" + set "$DISABLE_BATS_SHELL_OPTIONS" + __split_bats_output_into_lines + restore_bats_shell_options "$?" } # Creates a stub program in PATH for testing purposes @@ -335,3 +306,80 @@ stub_program_in_path() { fi create_bats_test_script "${BATS_TEST_BINDIR#$BATS_TEST_ROOTDIR/}/$1" "${@:2}" } + +# -------------------------------- +# IMPLEMENTATION - HERE BE DRAGONS +# +# None of the functions below this line are part of the public interface. +# -------------------------------- + +# Implementation for `create_bats_test_dirs` +# +# Arguments: +# $@: Paths of subdirectories relative to BATS_TEST_ROOTDIR +__create_bats_test_dirs() { + local dirs_to_create=() + local test_dir + + for test_dir in "${@/#/$BATS_TEST_ROOTDIR/}"; do + if [[ ! -d "$test_dir" ]]; then + dirs_to_create+=("$test_dir") + fi + done + + if [[ "${#dirs_to_create[@]}" -ne '0' ]]; then + mkdir -p "${dirs_to_create[@]}" + fi +} + +# Implementation for `create_bats_test_script` +# +# Arguments: +# $1: Path of the script relative to BATS_TEST_ROOTDIR +# ...: Lines comprising the script +__create_bats_test_script() { + local script="$1" + shift + local script_dir="${script%/*}" + + if [[ -z "$script" ]]; then + echo "No test script specified" >&2 + exit 1 + elif [[ "$script_dir" == "$script" ]]; then + script_dir='' + fi + + create_bats_test_dirs "$script_dir" + script="$BATS_TEST_ROOTDIR/$script" + rm -f "$script" + + if [[ "${1:0:2}" != '#!' ]]; then + echo "#! /usr/bin/env bash" >"$script" + fi + printf '%s\n' "$@" >>"$script" + chmod 700 "$script" +} + +# Enumerates programs not installed on the system for `skip_if_system_missing` +# +# Arguments: +# ...: System programs that must be present for the test case to proceed +__search_for_missing_programs() { + local program + + for program in "$@"; do + if ! command -v "$program" >/dev/null; then + __missing+=("$program") + fi + done +} + +# Implementation for `split_bats_output_into_lines` +__split_bats_output_into_lines() { + local line + lines=() + + while IFS= read -r line; do + lines+=("${line%$'\r'}") + done <<<"$output" +} diff --git a/lib/testing/environment b/lib/testing/environment index cc4866d..ad8124a 100644 --- a/lib/testing/environment +++ b/lib/testing/environment @@ -106,15 +106,7 @@ test-go() { # ...: Names of "child" command scripts @go.create_parent_and_subcommands() { set "$DISABLE_BATS_SHELL_OPTIONS" - local parent="$1" - shift - local subcommand - - @go.create_test_command_script "$parent" - - for subcommand in "$@"; do - @go.create_test_command_script "$parent.d/$subcommand" - done + __@go.create_parent_and_subcommands "$@" restore_bats_shell_options "$?" } @@ -132,3 +124,26 @@ test-go() { @go.split $'\n' "$(@go.compgen "${@:2}")" "$1" restore_bats_shell_options "$?" } + +# -------------------------------- +# IMPLEMENTATION - HERE BE DRAGONS +# +# None of the functions below this line are part of the public interface. +# -------------------------------- + +# Implementation for `@go.create_parent_and_subcommands` +# +# Arguments: +# parent_name: Name of the "parent" command script +# ...: Names of "child" command scripts +__@go.create_parent_and_subcommands() { + local parent="$1" + shift + local subcommand + + @go.create_test_command_script "$parent" + + for subcommand in "$@"; do + @go.create_test_command_script "$parent.d/$subcommand" + done +} diff --git a/lib/testing/stubbing b/lib/testing/stubbing index 624af1d..c7b7743 100644 --- a/lib/testing/stubbing +++ b/lib/testing/stubbing @@ -11,6 +11,8 @@ # You must import `_GO_CORE_DIR/lib/testing/environment` before importing this # script, and make sure to call `@go.remove_test_go_rootdir` from `teardown`. +. "$_GO_CORE_DIR/lib/bats/helper-function" + export _GO_INJECT_SEARCH_PATH="$TEST_GO_ROOTDIR/test-bin" export _GO_INJECT_MODULE_PATH="$TEST_GO_ROOTDIR/test-lib" @@ -20,8 +22,10 @@ export _GO_INJECT_MODULE_PATH="$TEST_GO_ROOTDIR/test-lib" # module_name: Name of the command script to stub # ...: Lines comprising the stubbed command script implementation @go.create_command_script_test_stub() { + set "$DISABLE_BATS_SHELL_OPTIONS" local script_path="$_GO_INJECT_SEARCH_PATH/$1" create_bats_test_script "${script_path#$BATS_TEST_ROOTDIR/}" "${@:2}" + restore_bats_shell_options "$?" } # Creates a stub module implementation in `_GO_INJECT_MODULE_PATH` @@ -30,7 +34,9 @@ export _GO_INJECT_MODULE_PATH="$TEST_GO_ROOTDIR/test-lib" # module_name: Name of the module to stub # ...: Lines comprising the stubbed module implementation @go.create_module_test_stub() { + set "$DISABLE_BATS_SHELL_OPTIONS" local module_path="$_GO_INJECT_MODULE_PATH/$1" create_bats_test_script "${module_path#$BATS_TEST_ROOTDIR/}" "${@:2}" chmod 600 "$module_path" + restore_bats_shell_options "$?" } diff --git a/tests/assertion-test-helpers.bats b/tests/assertion-test-helpers.bats index f72c03b..ec10986 100644 --- a/tests/assertion-test-helpers.bats +++ b/tests/assertion-test-helpers.bats @@ -24,6 +24,13 @@ emit_debug_info() { } run_assertion_test() { + set "$DISABLE_BATS_SHELL_OPTIONS" + setup_assertion_test "$@" + restore_bats_shell_options + run "$BATS_TEST_ROOTDIR/$EXPECT_ASSERTION_TEST_SCRIPT" +} + +setup_assertion_test() { local expected_output=("${@:2}") local expected_output_line @@ -43,11 +50,15 @@ run_assertion_test() { "@test \"$BATS_TEST_DESCRIPTION\" {" \ " $ASSERTION" \ '}' - run "$BATS_TEST_ROOTDIR/$EXPECT_ASSERTION_TEST_SCRIPT" } check_failure_output() { - set +eET + set "$DISABLE_BATS_SHELL_OPTIONS" + __check_failure_output "$@" + restore_bats_shell_options "$?" +} + +__check_failure_output() { local test_script="$BATS_TEST_ROOTDIR/$EXPECT_ASSERTION_TEST_SCRIPT" local assertion_line="${ASSERTION%%$'\n'*}" local expected_output @@ -69,7 +80,6 @@ check_failure_output() { result='1' fi unset 'BATS_CURRENT_STACK_TRACE[0]' 'BATS_PREVIOUS_STACK_TRACE[0]' - set -eET return "$result" } diff --git a/tests/bats-helpers.bats b/tests/bats-helpers.bats index 73a8337..572f677 100644 --- a/tests/bats-helpers.bats +++ b/tests/bats-helpers.bats @@ -18,32 +18,55 @@ teardown() { assert_matches ' ' "$BATS_TEST_ROOTDIR" "BATS_TEST_ROOTDIR" } +check_dirs_do_not_exist() { + set "$DISABLE_BATS_SHELL_OPTIONS" + __check_dirs_do_not_exist "$@" + restore_bats_shell_options "$?" +} + +__check_dirs_do_not_exist() { + local test_dir + + for test_dir in "$@"; do + if [[ -d "$BATS_TEST_ROOTDIR/$test_dir" ]]; then + printf "'$test_dir' already present in '$BATS_TEST_ROOTDIR'\n" >&2 + return 1 + fi + done +} + +check_dirs_exist() { + set "$DISABLE_BATS_SHELL_OPTIONS" + __check_dirs_exist "$@" + restore_bats_shell_options "$?" +} + +__check_dirs_exist() { + local test_dir + + for test_dir in "${test_dirs[@]}"; do + if [[ ! -d "$BATS_TEST_ROOTDIR/$test_dir" ]]; then + printf "Failed to create '$test_dir' in '$BATS_TEST_ROOTDIR'\n" >&2 + return 1 + fi + done +} + @test "$SUITE: {create,remove}_bats_test_dirs" { local test_dirs=('foo' 'bar/baz' 'quux/xyzzy' 'quux/plugh' ) - local test_dir if [[ -d "$BATS_TEST_ROOTDIR" ]]; then fail "'$BATS_TEST_ROOTDIR' already exists" fi - - for test_dir in "${test_dirs[@]}"; do - if [[ -d "$BATS_TEST_ROOTDIR/$test_dir" ]]; then - fail "'$test_dir' already present in '$BATS_TEST_ROOTDIR'" - fi - done + check_dirs_do_not_exist "${test_dirs[@]}" run create_bats_test_dirs "${test_dirs[@]}" assert_success - - for test_dir in "${test_dirs[@]}"; do - if [[ ! -d "$BATS_TEST_ROOTDIR/$test_dir" ]]; then - fail "Failed to create '$test_dir' in '$BATS_TEST_ROOTDIR'" - fi - done + check_dirs_exist "${test_dirs[@]}" run remove_bats_test_dirs assert_success diff --git a/tests/builtins.bats b/tests/builtins.bats index 23c35d3..e102696 100644 --- a/tests/builtins.bats +++ b/tests/builtins.bats @@ -11,13 +11,12 @@ load environment @test "$SUITE: tab completions" { local expected=('--exists' '--summaries') - local IFS=$'\n' run ./go complete 1 builtins '' - assert_success "${expected[*]}" + assert_success "${expected[@]}" run ./go complete 1 builtins - - assert_success "${expected[*]}" + assert_success "${expected[@]}" run ./go complete 2 builtins --exists assert_failure '' @@ -58,7 +57,7 @@ load environment assert_failure 'ERROR: unknown flag: --foobar' } -@test "$SUITE: list builtin command summaries" { +setup_list_builtin_command_summaries() { local builtins=($(./go builtins)) local longest_name_len=0 local cmd_name @@ -69,23 +68,32 @@ load environment fi done - run ./go builtins --summaries - assert_success - . lib/internal/command_descriptions local __go_cmd_desc='' local first_cmd="${builtins[0]}" local last_cmd="${builtins[$((${#builtins[@]} - 1))]}" _@go.command_summary "libexec/$first_cmd" - assert_line_equals 0 \ - "$(_@go.format_summary "$first_cmd" "$__go_cmd_desc" "$longest_name_len")" \ - "first builtin summary" + __expected_first_cmd_summary="$(_@go.format_summary "$first_cmd" \ + "$__go_cmd_desc" "$longest_name_len")" _@go.command_summary "libexec/$last_cmd" - assert_line_equals -1 \ - "$(_@go.format_summary "$last_cmd" "$__go_cmd_desc" "$longest_name_len")" \ - "last builtin summary" + __expected_last_cmd_summary="$(_@go.format_summary "$last_cmd" \ + "$__go_cmd_desc" "$longest_name_len")" +} + +@test "$SUITE: list builtin command summaries" { + set "$DISABLE_BATS_SHELL_OPTIONS" + local __expected_first_cmd_summary + local __expected_last_cmd_summary + setup_list_builtin_command_summaries + restore_bats_shell_options "$?" + + run ./go builtins --summaries + assert_success + + assert_line_equals 0 "$__expected_first_cmd_summary" + assert_line_equals -1 "$__expected_last_cmd_summary" } @test "$SUITE: help filter" { diff --git a/tests/commands/helpers.bash b/tests/commands/helpers.bash index f8fb507..6a1f4ec 100644 --- a/tests/commands/helpers.bash +++ b/tests/commands/helpers.bash @@ -7,6 +7,12 @@ declare BUILTIN_SCRIPTS declare LONGEST_BUILTIN_NAME find_builtins() { + set "$DISABLE_BATS_SHELL_OPTIONS" + __find_builtins + restore_bats_shell_options "$?" +} + +__find_builtins() { local cmd_script local cmd_name @@ -25,6 +31,12 @@ find_builtins() { } merge_scripts() { + set "$DISABLE_BATS_SHELL_OPTIONS" + __merge_scripts "$@" + restore_bats_shell_options "$?" +} + +__merge_scripts() { local args=("$@") local i=0 local j=0 @@ -53,6 +65,12 @@ merge_scripts() { } add_scripts() { + set "$DISABLE_BATS_SHELL_OPTIONS" + __add_scripts "$@" + restore_bats_shell_options "$?" +} + +__add_scripts() { local script_names=("$@") merge_scripts "${script_names[@]/#/$TEST_GO_SCRIPTS_DIR/}" diff --git a/tests/commands/main.bats b/tests/commands/main.bats index 723c9fd..f9e8e5e 100644 --- a/tests/commands/main.bats +++ b/tests/commands/main.bats @@ -46,22 +46,18 @@ teardown() { } @test "$SUITE: tab complete subcommand" { + set "$DISABLE_BATS_SHELL_OPTIONS" @go.create_test_command_script 'foo' - mkdir "$TEST_GO_SCRIPTS_DIR/foo.d" - - local expected=('bar' 'baz' 'quux') - local subcommand - - for subcommand in "${expected[@]}"; do - @go.create_test_command_script "foo.d/$subcommand" - done + @go.create_test_command_script 'foo.d/bar' + @go.create_test_command_script 'foo.d/baz' + @go.create_test_command_script 'foo.d/quux' + restore_bats_shell_options "$?" run "$TEST_GO_SCRIPT" complete 2 commands foo - assert_success "${expected[@]}" + assert_success 'bar' 'baz' 'quux' run "$TEST_GO_SCRIPT" complete 2 commands foo b - expected=('bar' 'baz') - assert_success "${expected[@]}" + assert_success 'bar' 'baz' run "$TEST_GO_SCRIPT" complete 2 commands foo g assert_failure @@ -71,19 +67,15 @@ teardown() { } @test "$SUITE: only tab complete flags before other args" { + set "$DISABLE_BATS_SHELL_OPTIONS" @go.create_test_command_script 'foo' - mkdir "$TEST_GO_SCRIPTS_DIR/foo.d" - - local subcommands=('bar' 'baz' 'quux') - local subcommand - - for subcommand in "${subcommands[@]}"; do - @go.create_test_command_script "foo.d/$subcommand" - done + @go.create_test_command_script 'foo.d/bar' + @go.create_test_command_script 'foo.d/baz' + @go.create_test_command_script 'foo.d/quux' + restore_bats_shell_options "$?" run "$TEST_GO_SCRIPT" complete 1 commands '' foo - expected=('--paths' '--summaries') - assert_success "${expected[@]}" + assert_success '--paths' '--summaries' run "$TEST_GO_SCRIPT" complete 2 commands foo '' bar assert_failure @@ -160,6 +152,12 @@ teardown() { } generate_expected_paths() { + set "$DISABLE_BATS_SHELL_OPTIONS" + __generate_expected_paths + restore_bats_shell_options "$?" +} + +__generate_expected_paths() { local script local cmd_name local longest_cmd_name_len @@ -222,23 +220,19 @@ create_script_with_description() { } @test "$SUITE: command summaries" { - local user_commands=('bar' 'baz' 'foo') - - for cmd_name in "${user_commands[@]}"; do - create_script_with_description "$cmd_name" - done + set "$DISABLE_BATS_SHELL_OPTIONS" + create_script_with_description 'foo' + create_script_with_description 'bar' + create_script_with_description 'baz' + restore_bats_shell_options "$?" run "$TEST_GO_SCRIPT" commands --summaries "$TEST_GO_SCRIPTS_DIR" - local expected=( - ' bar Does bar stuff' - ' baz Does baz stuff' - ' foo Does foo stuff') - assert_success "${expected[@]}" + assert_success ' bar Does bar stuff' \ + ' baz Does baz stuff' \ + ' foo Does foo stuff' } -@test "$SUITE: subcommand list, paths, and summaries" { - local top_level_commands=('bar' 'baz' 'foo') - local subcommands=('plugh' 'quux' 'xyzzy') +create_top_level_and_subcommand_scripts() { local cmd_name local subcmd_dir local subcmd_name @@ -252,6 +246,15 @@ create_script_with_description() { create_script_with_description "$cmd_name.d/$subcmd_name" done done +} + +@test "$SUITE: subcommand list, paths, and summaries" { + local top_level_commands=('bar' 'baz' 'foo') + local subcommands=('plugh' 'quux' 'xyzzy') + + set "$DISABLE_BATS_SHELL_OPTIONS" + create_top_level_and_subcommand_scripts + restore_bats_shell_options "$?" run "$TEST_GO_SCRIPT" commands 'foo' assert_success "${subcommands[@]}" diff --git a/tests/complete.bats b/tests/complete.bats index 8e6f7a8..a77c0d6 100644 --- a/tests/complete.bats +++ b/tests/complete.bats @@ -2,12 +2,12 @@ load environment load commands/helpers +. "$_GO_USE_MODULES" 'complete' setup() { test_filter @go.create_test_go_script '@go "$@"' find_builtins - . "$_GO_USE_MODULES" 'complete' } teardown() { @@ -66,11 +66,8 @@ teardown() { assert_success 'scripts/' local expected=() - local item - - while IFS= read -r item; do - expected+=("${item#$TEST_GO_ROOTDIR/}") - done < <(@go.compgen -d "$TEST_GO_SCRIPTS_DIR/") + @go.test_compgen expected -d "$TEST_GO_SCRIPTS_DIR/" + expected=("${expected[@]#$TEST_GO_ROOTDIR/}") run "$TEST_GO_SCRIPT" complete 1 cd 'scripts/' assert_success "${expected[@]}" diff --git a/tests/complete/compgen.bats b/tests/complete/compgen.bats index 5632777..f83c771 100644 --- a/tests/complete/compgen.bats +++ b/tests/complete/compgen.bats @@ -58,17 +58,23 @@ teardown() { assert_success "${expected[@]/%//}" } -@test "$SUITE: adds trailing slashes when called with -f" { - mkdir -p "${TEST_GO_ROOTDIR}"/{foo,bar,baz} - run "$TEST_GO_SCRIPT" -f -- '' - - # Remember that `compgen` won't add trailing slashes by itself. - local expected=($(cd "$TEST_GO_ROOTDIR"; compgen -f -- '')) +add_trailing_slashes() { local i for ((i=0; i != "${#expected[@]}"; ++i)); do if [[ -d "$TEST_GO_ROOTDIR/${expected[$i]}" ]]; then expected[$i]+='/' fi done +} + +@test "$SUITE: adds trailing slashes when called with -f" { + mkdir -p "${TEST_GO_ROOTDIR}"/{foo,bar,baz} + run "$TEST_GO_SCRIPT" -f -- '' + + set "$DISABLE_BATS_SHELL_OPTIONS" + # Remember that `compgen` won't add trailing slashes by itself. + local expected=($(cd "$TEST_GO_ROOTDIR"; compgen -f -- '')) + add_trailing_slashes + restore_bats_shell_options "$?" assert_success "${expected[@]}" } diff --git a/tests/core/search-plugins.bats b/tests/core/search-plugins.bats index 5ef51de..3f5aee1 100644 --- a/tests/core/search-plugins.bats +++ b/tests/core/search-plugins.bats @@ -119,18 +119,13 @@ teardown() { @test "$SUITE: /plugins/ in _GO_ROOTDIR, _GO_SCRIPTS_DIR (pathological)" { local test_rootdir="$TEST_GO_ROOTDIR/plugins/plugins" local test_go_script="$test_rootdir/go" + local test_go_script_impl="$(< "$TEST_GO_SCRIPT")" local test_scripts_dir="$test_rootdir/plugins" local test_plugins_dir="$test_scripts_dir/plugins" mkdir -p "$test_plugins_dir/foo/bin/plugins/bar/bin/plugins" - local line - while IFS= read -r line; do - line="${line%$'\r'}" - if [[ "$line" =~ go-core\.bash ]]; then - line=". '$_GO_CORE_DIR/go-core.bash' 'plugins'" - fi - printf '%s\n' "$line" >> "$test_go_script" - done < "$TEST_GO_SCRIPT" + printf '%s\n' "${test_go_script_impl/$TEST_GO_SCRIPTS_RELATIVE_DIR/plugins}" \ + >"$test_go_script" chmod 700 "$test_go_script" local foo_path="${test_plugins_dir#$BATS_TEST_ROOTDIR/}/foo/bin/foo" @@ -141,10 +136,6 @@ teardown() { create_bats_test_script "$foo_path" '@go bar' create_bats_test_script "$bar_path" 'collect_dirs' - test_printf 'test_go_script: %s\n' "$test_go_script" >&2 - test_printf 'test_plugins_dir: %s\n' "$test_plugins_dir" >&2 - test_printf 'foo_path: %s\n' "${foo_path}" >&2 - test_printf 'bar_path: %s\n' "${bar_path}" >&2 COLLECT_DIRS_SUCCESS_AFTER_NUM_ITERATIONS='10' run "$test_go_script" 'foo' assert_failure "$test_plugins_dir/foo/bin/plugins/bar/bin/plugins" \ "$test_plugins_dir/foo/bin/plugins" \ diff --git a/tests/fullpath.bats b/tests/fullpath.bats index ad456f3..385eb78 100644 --- a/tests/fullpath.bats +++ b/tests/fullpath.bats @@ -11,17 +11,16 @@ teardown() { } @test "$SUITE: tab completions" { - . "$_GO_USE_MODULES" 'complete' - local expected=('--existing') - expected+=($(@go.compgen -f)) + local expected=() + @go.test_compgen expected -f run ./go complete 1 fullpath '' - assert_success "${expected[@]}" + assert_success '--existing' "${expected[@]}" run ./go complete 1 fullpath '-' assert_success '--existing ' - expected=($(@go.compgen -f -- 'li')) + @go.test_compgen expected -f -- 'li' fail_if equal '0' "${#expected[@]}" run ./go complete 1 fullpath 'li' assert_success "${expected[@]}" diff --git a/tests/glob/arg-completion.bats b/tests/glob/arg-completion.bats index d1d9b3d..6f91d93 100644 --- a/tests/glob/arg-completion.bats +++ b/tests/glob/arg-completion.bats @@ -1,13 +1,13 @@ #! /usr/bin/env bats load ../environment +. "$_GO_USE_MODULES" 'complete' TESTS_DIR="$TEST_GO_ROOTDIR/tests" setup() { test_filter mkdir -p "$TESTS_DIR" - . "$_GO_USE_MODULES" 'complete' } teardown() { diff --git a/tests/kcov.bats b/tests/kcov.bats index 808e48b..8595bc2 100644 --- a/tests/kcov.bats +++ b/tests/kcov.bats @@ -22,7 +22,16 @@ KCOV_ARGV_START=( setup() { test_filter + set "$DISABLE_BATS_SHELL_OPTIONS" + setup_fake_binaries + restore_bats_shell_options "$?" +} + +teardown() { + @go.remove_test_go_rootdir +} +setup_fake_binaries() { local fake_binaries=( 'apt-get' 'cmake' @@ -38,10 +47,6 @@ setup() { done } -teardown() { - @go.remove_test_go_rootdir -} - write_kcov_go_script() { @go.create_test_go_script \ ". \"\$_GO_USE_MODULES\" 'kcov-ubuntu'" \ diff --git a/tests/modules/helpers.bash b/tests/modules/helpers.bash index 48006df..ffb327d 100644 --- a/tests/modules/helpers.bash +++ b/tests/modules/helpers.bash @@ -20,6 +20,12 @@ TEST_PLUGIN_MODULES_PATHS=() TOTAL_NUM_MODULES=0 setup_test_modules() { + set "$DISABLE_BATS_SHELL_OPTIONS" + __setup_test_modules + restore_bats_shell_options "$?" +} + +__setup_test_modules() { local module local module_file diff --git a/tests/modules/use.bats b/tests/modules/use.bats index 715cd1f..395512d 100644 --- a/tests/modules/use.bats +++ b/tests/modules/use.bats @@ -46,7 +46,17 @@ GO_USE_MODULES_STACK_ITEM="$(@go.stack_trace_item "$_GO_USE_MODULES" source \ setup() { test_filter + set "$DISABLE_BATS_SHELL_OPTIONS" + do_setup + restore_bats_shell_options "$?" +} + +teardown() { + rm -rf "$_GO_CORE_DIR/lib/"{builtin-test,go-use-modules-test} + @go.remove_test_go_rootdir +} +do_setup() { local core_test_module for core_test_module in 'builtin-test' 'go-use-modules-test'; do if [[ -e "$_GO_CORE_DIR/lib/$core_test_module" ]]; then @@ -71,11 +81,6 @@ setup() { done } -teardown() { - rm -rf "$_GO_CORE_DIR/lib/"{builtin-test,go-use-modules-test} - @go.remove_test_go_rootdir -} - @test "$SUITE: no modules imported by default" { @go.create_test_go_script 'printf -- "%s\n" "${_GO_IMPORTED_MODULES[@]}"' run "$TEST_GO_SCRIPT" @@ -192,6 +197,7 @@ teardown() { } @test "$SUITE: import order: injected; core; internal; exported; plugin" { + set "$DISABLE_BATS_SHELL_OPTIONS" local module_dir='go-use-modules-test' local module_name='test-module' local module_path="$module_dir/$module_name" @@ -210,6 +216,8 @@ teardown() { >"$TEST_GO_PLUGINS_DIR/${module_dir}/lib/${module_name}" @go.create_test_go_script ". \"\$_GO_USE_MODULES\" '${module_path}'" + restore_bats_shell_options "$?" + run "$TEST_GO_SCRIPT" assert_success 'INJECTED' diff --git a/tests/path/list-available-commands.bats b/tests/path/list-available-commands.bats index 9eb4640..3b6e492 100644 --- a/tests/path/list-available-commands.bats +++ b/tests/path/list-available-commands.bats @@ -12,25 +12,29 @@ teardown() { @go.remove_test_go_rootdir } -@test "$SUITE: list available commands" { - # Since we aren't creating any new commands, and _@go.find_commands is already - # thoroughly tested in isolation, we only check that builtins are available. +setup_list_available_commands() { local builtin_cmd - local expected=() - for builtin_cmd in "$_GO_ROOTDIR"/libexec/*; do if [[ -f "$builtin_cmd" && -x "$builtin_cmd" ]]; then expected+=("${builtin_cmd[@]##*/}") fi done +} + +@test "$SUITE: list available commands" { + set "$DISABLE_BATS_SHELL_OPTIONS" + # Since we aren't creating any new commands, and _@go.find_commands is already + # thoroughly tested in isolation, we only check that builtins are available. + local expected=() + setup_list_available_commands + restore_bats_shell_options "$?" run "$TEST_GO_SCRIPT" "$_GO_ROOTDIR/libexec" assert_success assert_line_equals 0 'Available commands are:' - unset 'lines[0]' - local IFS=$'\n' - assert_equal "${expected[*]/#/ }" "${lines[*]}" 'available commands' + lines=("${lines[@]:1}") + assert_lines_equal "${expected[@]/#/ }" } @test "$SUITE: error if no commands available" { diff --git a/tests/plugins.bats b/tests/plugins.bats index ca48e32..ba82093 100644 --- a/tests/plugins.bats +++ b/tests/plugins.bats @@ -22,8 +22,7 @@ teardown() { @go.create_test_command_script 'plugins/foo/bin/foo' run "$TEST_GO_SCRIPT" complete 1 plugins '' - local expected=('--paths' '--summaries') - assert_success "${expected[@]}" + assert_success '--paths' '--summaries' run "$TEST_GO_SCRIPT" complete 1 plugins '--paths' assert_success '--paths ' @@ -39,7 +38,7 @@ teardown() { assert_failure '' } -@test "$SUITE: show plugin info" { +setup_show_plugin_info() { mkdir -p "$TEST_GO_PLUGINS_DIR/bar/bin" "$TEST_GO_PLUGINS_DIR/plugh/bin" local plugins=( @@ -62,24 +61,24 @@ teardown() { longest_plugin_len="${#plugin}" fi done +} + +@test "$SUITE: show plugin info" { + set "$DISABLE_BATS_SHELL_OPTIONS" + setup_show_plugin_info + restore_bats_shell_options "$?" # Note that only `/bin` scripts from each plugin directory are included. run "$TEST_GO_SCRIPT" plugins assert_success 'bar' 'baz' 'plugh' - local paths=( - 'bar scripts/plugins/bar/bin/bar' - 'baz scripts/plugins/bar/bin/baz' - 'plugh scripts/plugins/plugh/bin/plugh') - run "$TEST_GO_SCRIPT" plugins --paths - assert_success "${paths[@]}" - - local summaries=( - ' bar Does bar stuff' - ' baz Does baz stuff' - ' plugh Does plugh stuff') + assert_success 'bar scripts/plugins/bar/bin/bar' \ + 'baz scripts/plugins/bar/bin/baz' \ + 'plugh scripts/plugins/plugh/bin/plugh' run "$TEST_GO_SCRIPT" plugins --summaries - assert_success "${summaries[@]}" + assert_success ' bar Does bar stuff' \ + ' baz Does baz stuff' \ + ' plugh Does plugh stuff' } diff --git a/tests/subcommands.bats b/tests/subcommands.bats index d6088eb..436b321 100644 --- a/tests/subcommands.bats +++ b/tests/subcommands.bats @@ -9,12 +9,14 @@ EXPECTED_SUBCOMMAND_LISTING=('Available subcommands of "foo" are:' ' quux Do quux stuff') setup() { + set "$DISABLE_BATS_SHELL_OPTIONS" @go.create_test_go_script '@go "$@"' @go.create_test_command_script 'foo' '. "$_GO_USE_MODULES" subcommands' \ '@go.show_subcommands' @go.create_test_command_script 'foo.d/bar' '# Do bar stuff' @go.create_test_command_script 'foo.d/baz' '# Do baz stuff' @go.create_test_command_script 'foo.d/quux' '# Do quux stuff' + restore_bats_shell_options "$?" } teardown() { diff --git a/tests/testing/environment.bats b/tests/testing/environment.bats index 9350319..1f689f4 100644 --- a/tests/testing/environment.bats +++ b/tests/testing/environment.bats @@ -94,27 +94,32 @@ teardown() { assert_success '_GO_CMD: test-go' } -@test "$SUITE: @go.test_compgen" { +setup_go_test_compgen() { + local item + mkdir -p "$TEST_GO_ROOTDIR/lib" printf 'foo' >"$TEST_GO_ROOTDIR/lib/foo" printf 'bar' >"$TEST_GO_ROOTDIR/lib/bar" printf 'baz' >"$TEST_GO_ROOTDIR/lib/baz" - local expected=() - local item - . "$_GO_USE_MODULES" 'complete' while IFS= read -r item; do - expected+=("${item#$TEST_GO_ROOTDIR/}") + __expected+=("${item#$TEST_GO_ROOTDIR/}") done < <(@go.compgen -f -- "$TEST_GO_ROOTDIR/lib/") +} + +@test "$SUITE: @go.test_compgen" { + set "$DISABLE_BATS_SHELL_OPTIONS" + local __expected=() + setup_go_test_compgen + restore_bats_shell_options "$?" export -f @go.test_compgen @go.create_test_go_script \ - '. "$_GO_USE_MODULES" "complete"' \ 'declare results=()' \ '@go.test_compgen "results" -f -- lib/' \ 'printf "%s\n" "${results[@]}"' run "$TEST_GO_SCRIPT" - assert_success "${expected[@]}" + assert_success "${__expected[@]}" } diff --git a/tests/vars.bats b/tests/vars.bats index 1abda87..9eedb0c 100644 --- a/tests/vars.bats +++ b/tests/vars.bats @@ -27,6 +27,7 @@ quotify_expected() { run "$TEST_GO_SCRIPT" vars assert_success + set "$DISABLE_BATS_SHELL_OPTIONS" local search_paths=("[0]=\"$_GO_CORE_DIR/libexec\"" "[1]=\"$TEST_GO_SCRIPTS_DIR\"") @@ -59,10 +60,12 @@ quotify_expected() { "declare -rx _GO_USE_MODULES=\"$_GO_CORE_DIR/lib/internal/use\"") quotify_expected + restore_bats_shell_options "$?" assert_lines_equal "${expected[@]}" } @test "$SUITE: all _GO_* variables for Bash subcommand contain values" { + set "$DISABLE_BATS_SHELL_OPTIONS" @go.create_test_command_script 'test-command.d/test-subcommand' \ '. "$_GO_USE_MODULES" "module_0" "module_1"' \ '@go vars' @@ -131,6 +134,7 @@ quotify_expected() { "declare -rx _GO_USE_MODULES=\"$_GO_CORE_DIR/lib/internal/use\"") quotify_expected + restore_bats_shell_options "$?" assert_lines_equal "${expected[@]}" }