diff --git a/test/bash_it/bash_it.bats b/test/bash_it/bash_it.bats index 6867c7fb..418719b4 100644 --- a/test/bash_it/bash_it.bats +++ b/test/bash_it/bash_it.bats @@ -1,20 +1,10 @@ #!/usr/bin/env bats load ../test_helper -load ../../lib/composure function local_setup { setup_test_fixture - - # Copy the test fixture to the Bash-it folder - if command -v rsync &> /dev/null - then - rsync -a "$BASH_IT/test/fixtures/bash_it/" "$BASH_IT/" - else - find "$BASH_IT/test/fixtures/bash_it" \ - -mindepth 1 -maxdepth 1 \ - -exec cp -r {} "$BASH_IT/" \; - fi + copy_test_fixtures } @test "bash-it: verify that the test fixture is available" { diff --git a/test/fixtures/bash_it/plugins/available/exit-nonzero.bash b/test/fixtures/bash_it/plugins/available/exit-nonzero.bash new file mode 100644 index 00000000..dad3bfaa --- /dev/null +++ b/test/fixtures/bash_it/plugins/available/exit-nonzero.bash @@ -0,0 +1,7 @@ +# shellcheck shell=bash +about-plugin 'fixture plugin for testing plugins that exit with non-0 exit codes' + +printf 'exit-nonzero: stdout message\n' +printf 'exit-nonzero: stderr message\n' >&2 + +exit 123 diff --git a/test/fixtures/bash_it/plugins/available/exit-zero.bash b/test/fixtures/bash_it/plugins/available/exit-zero.bash new file mode 100644 index 00000000..87e168a5 --- /dev/null +++ b/test/fixtures/bash_it/plugins/available/exit-zero.bash @@ -0,0 +1,7 @@ +# shellcheck shell=bash +about-plugin 'fixture plugin for testing plugins that exit with a 0 exit code' + +printf 'exit-zero: stdout message\n' +printf 'exit-zero: stderr message\n' >&2 + +exit 0 diff --git a/test/fixtures/bash_it/plugins/available/return-nonzero.bash b/test/fixtures/bash_it/plugins/available/return-nonzero.bash new file mode 100644 index 00000000..46dcc91e --- /dev/null +++ b/test/fixtures/bash_it/plugins/available/return-nonzero.bash @@ -0,0 +1,7 @@ +# shellcheck shell=bash +about-plugin 'fixture plugin for testing plugins that return with a non-0 return code' + +printf 'return-nonzero: stdout message\n' +printf 'return-nonzero: stderr message\n' >&2 + +return 123 diff --git a/test/fixtures/bash_it/plugins/available/return-zero.bash b/test/fixtures/bash_it/plugins/available/return-zero.bash new file mode 100644 index 00000000..79df15dd --- /dev/null +++ b/test/fixtures/bash_it/plugins/available/return-zero.bash @@ -0,0 +1,7 @@ +# shellcheck shell=bash +about-plugin 'fixture plugin for testing plugins that return with a 0 return code' + +printf 'return-zero: stdout message\n' +printf 'return-zero: stderr message\n' >&2 + +return 0 diff --git a/test/fixtures/bash_it/plugins/available/zz.plugin.bash b/test/fixtures/bash_it/plugins/available/zz.plugin.bash new file mode 100644 index 00000000..9b7b1581 --- /dev/null +++ b/test/fixtures/bash_it/plugins/available/zz.plugin.bash @@ -0,0 +1,4 @@ +# shellcheck shell=bash +about-plugin 'here to load last. a canary test for silent failures.' + +printf 'You found me!' diff --git a/test/lib/reloader.bats b/test/lib/reloader.bats new file mode 100644 index 00000000..0cb736c8 --- /dev/null +++ b/test/lib/reloader.bats @@ -0,0 +1,82 @@ + +#!/usr/bin/env bats + +load ../test_helper + +function local_setup { + setup_test_fixture + copy_test_fixtures + + # log everything to verify output + BASH_IT_LOG_LEVEL=3 +} + +@test "reloader: exit-nonzero" { + # use bash-it to enable the fixture + load "$BASH_IT/bash_it.sh" + + run bash-it enable plugin exit-nonzero + assert_success + + run bash-it enable plugin zz + assert_success + + run source "${BASH_IT}/scripts/reloader.bash" + assert_success +} + +@test "reloader: exit-zero" { + # use bash-it to enable the fixture + load "$BASH_IT/bash_it.sh" + + run bash-it enable plugin exit-zero + assert_success + + run bash-it enable plugin zz + assert_success + + run source "${BASH_IT}/scripts/reloader.bash" + assert_success +} + +@test "reloader: return-nonzero" { + # use bash-it to enable the fixture + load "$BASH_IT/bash_it.sh" + + run bash-it enable plugin return-nonzero + assert_success + + run bash-it enable plugin zz + assert_success + + run source "${BASH_IT}/scripts/reloader.bash" + assert_success + + assert_line 'DEBUG: bash: return-nonzero.bash: Loading component...' + assert_line 'return-nonzero: stdout message' + assert_line 'return-nonzero: stderr message' + + assert_line 'DEBUG: plugin: zz: Loading component...' + assert_line 'You found me!' +} + +@test "reloader: return-zero" { + # use bash-it to enable the fixture + load "$BASH_IT/bash_it.sh" + + run bash-it enable plugin return-zero + assert_success + + run bash-it enable plugin zz + assert_success + + run source "${BASH_IT}/scripts/reloader.bash" + assert_success + + assert_line 'DEBUG: bash: return-zero.bash: Loading component...' + assert_line 'return-zero: stdout message' + assert_line 'return-zero: stderr message' + + assert_line 'DEBUG: plugin: zz: Loading component...' + assert_line 'You found me!' +} diff --git a/test/run b/test/run index 88202916..c2fe080e 100755 --- a/test/run +++ b/test/run @@ -1,19 +1,20 @@ #!/usr/bin/env bash + test_directory="$(cd "$(dirname "$0")" && pwd)" bats_executable="${test_directory}/../test_lib/bats-core/bin/bats" git submodule init && git submodule update if [ -z "${BASH_IT}" ]; then - declare BASH_IT - BASH_IT=$(cd ${test_directory} && dirname "$(pwd)") - export BASH_IT + declare BASH_IT + BASH_IT=$(cd "${test_directory}" && dirname "$(pwd)") + export BASH_IT fi if [ -z "$1" ]; then - test_dirs=( ${test_directory}/{bash_it,completion,install,lib,plugins,themes} ) + test_dirs=("${test_directory}"/{bash_it,completion,install,lib,plugins,themes}) else - test_dirs=( "$1" ) + test_dirs=("$1") fi # Make sure that the `parallel` command is installed, @@ -21,28 +22,24 @@ fi # If that is the case, try to guess the number of CPU cores, # so we can run `bats` in parallel processing mode, which is a lot faster. if command -v parallel &> /dev/null \ - && parallel -V &> /dev/null \ - && { parallel -V 2> /dev/null | grep -q '^GNU\>'; } -then - # Expect to run at least on a dual-core CPU; slightly degraded performance - # shouldn't matter otherwise. - declare -i -r test_jobs_default=2 - declare -i -r test_jobs_effective="$( - if [ "${TEST_JOBS:-detect}" = "detect" ] \ - && command -v nproc &> /dev/null - then - nproc - elif [ -n "${TEST_JOBS}" ] \ - && [ "${TEST_JOBS}" != "detect" ] - then - echo "${TEST_JOBS}" - else - echo ${test_jobs_default} - fi - )" - exec "$bats_executable" ${CI:+--tap} --jobs ${test_jobs_effective} \ - "${test_dirs[@]}" + && parallel -V &> /dev/null \ + && { parallel -V 2> /dev/null | grep -q '^GNU\>'; }; then + # Expect to run at least on a dual-core CPU; slightly degraded performance + # shouldn't matter otherwise. + declare -i -r test_jobs_default=2 + declare -i -r test_jobs_effective="$( + if [ "${TEST_JOBS:-detect}" = "detect" ] \ + && command -v nproc &> /dev/null; then + nproc + elif [ -n "${TEST_JOBS}" ] \ + && [ "${TEST_JOBS}" != "detect" ]; then + echo "${TEST_JOBS}" + else + echo ${test_jobs_default} + fi + )" + exec "$bats_executable" ${CI:+--tap} --jobs "${test_jobs_effective}" "${test_dirs[@]}" else - # Run `bats` in single-threaded mode. - exec "$bats_executable" ${CI:+--tap} "${test_dirs[@]}" + # Run `bats` in single-threaded mode. + exec "$bats_executable" ${CI:+--tap} "${test_dirs[@]}" fi diff --git a/test/test_helper.bash b/test/test_helper.bash index 4e077090..99addd43 100644 --- a/test/test_helper.bash +++ b/test/test_helper.bash @@ -1,5 +1,4 @@ #!/usr/bin/env bats -load ../../lib/composure unset BASH_IT_THEME unset GIT_HOSTING @@ -20,14 +19,16 @@ load "${TEST_DEPS_DIR}/bats-assert/load.bash" load "${TEST_DEPS_DIR}/bats-file/load.bash" # support 'plumbing' metadata +load ../../lib/composure cite _about _param _example _group _author _version +cite about-alias about-completion about-plugin local_setup() { - true + true } local_teardown() { - true + true } # This function sets up a local test fixture, i.e. a completely @@ -35,71 +36,81 @@ local_teardown() { # messing with your own Bash-it source directory. # If you need this, call it in your .bats file's `local_setup` function. setup_test_fixture() { - mkdir -p "$BASH_IT" - lib_directory="$(cd "$(dirname "$0")" && pwd)" - local src_topdir="$lib_directory/../../../.." + mkdir -p "$BASH_IT" + lib_directory="$(cd "$(dirname "$0")" && pwd)" + local src_topdir="$lib_directory/../../../.." - if command -v rsync &> /dev/null - then - # Use rsync to copy Bash-it to the temp folder - rsync -qavrKL -d --delete-excluded --exclude=.git --exclude=enabled "$src_topdir" "$BASH_IT" - else - rm -rf "$BASH_IT" - mkdir -p "$BASH_IT" + if command -v rsync &> /dev/null; then + # Use rsync to copy Bash-it to the temp folder + rsync -qavrKL -d --delete-excluded --exclude=.git --exclude=enabled "$src_topdir" "$BASH_IT" + else + rm -rf "$BASH_IT" + mkdir -p "$BASH_IT" - find "$src_topdir" \ - -mindepth 1 -maxdepth 1 \ - -not -name .git \ - -exec cp -r {} "$BASH_IT" \; - fi + find "$src_topdir" \ + -mindepth 1 -maxdepth 1 \ + -not -name .git \ + -exec cp -r {} "$BASH_IT" \; + fi - rm -rf "$BASH_IT"/enabled - rm -rf "$BASH_IT"/aliases/enabled - rm -rf "$BASH_IT"/completion/enabled - rm -rf "$BASH_IT"/plugins/enabled + rm -rf "$BASH_IT"/enabled + rm -rf "$BASH_IT"/aliases/enabled + rm -rf "$BASH_IT"/completion/enabled + rm -rf "$BASH_IT"/plugins/enabled - mkdir -p "$BASH_IT"/enabled - mkdir -p "$BASH_IT"/aliases/enabled - mkdir -p "$BASH_IT"/completion/enabled - mkdir -p "$BASH_IT"/plugins/enabled + mkdir -p "$BASH_IT"/enabled + mkdir -p "$BASH_IT"/aliases/enabled + mkdir -p "$BASH_IT"/completion/enabled + mkdir -p "$BASH_IT"/plugins/enabled - # Some tests use the BASH_IT_TEST_HOME variable, e.g. install/uninstall - export BASH_IT_TEST_HOME="$TEST_TEMP_DIR" + # Some tests use the BASH_IT_TEST_HOME variable, e.g. install/uninstall + export BASH_IT_TEST_HOME="$TEST_TEMP_DIR" +} + +copy_test_fixtures() { + # Copy the test fixture to the Bash-it folder + if command -v rsync &> /dev/null; then + rsync -qa "$BASH_IT/test/fixtures/bash_it/" "$BASH_IT/" + else + find "$BASH_IT/test/fixtures/bash_it" \ + -mindepth 1 -maxdepth 1 \ + -exec cp -r {} "$BASH_IT/" \; + fi } setup() { - # The `temp_make` function from "bats-file" requires the tralston/bats-file fork, - # since the original ztombol/bats-file's `temp_make` does not work on macOS. - TEST_TEMP_DIR="$(temp_make --prefix 'bash-it-test-')" - export TEST_TEMP_DIR + # The `temp_make` function from "bats-file" requires the tralston/bats-file fork, + # since the original ztombol/bats-file's `temp_make` does not work on macOS. + TEST_TEMP_DIR="$(temp_make --prefix 'bash-it-test-')" + export TEST_TEMP_DIR - export BASH_IT_TEST_DIR="${TEST_TEMP_DIR}/.bash_it" + export BASH_IT_TEST_DIR="${TEST_TEMP_DIR}/.bash_it" - export BASH_IT_ROOT="${BASH_IT_TEST_DIR}/root" - export BASH_IT=$BASH_IT_TEST_DIR + export BASH_IT_ROOT="${BASH_IT_TEST_DIR}/root" + export BASH_IT=$BASH_IT_TEST_DIR - mkdir -p -- "${BASH_IT_ROOT}" + mkdir -p -- "${BASH_IT_ROOT}" - # Some tools, e.g. `git` use configuration files from the $HOME directory, - # which interferes with our tests. The only way to keep `git` from doing this - # seems to set HOME explicitly to a separate location. - # Refer to https://git-scm.com/docs/git-config#FILES. - unset XDG_CONFIG_HOME - export HOME="${TEST_TEMP_DIR}" - mkdir -p "${HOME}" + # Some tools, e.g. `git` use configuration files from the $HOME directory, + # which interferes with our tests. The only way to keep `git` from doing this + # seems to set HOME explicitly to a separate location. + # Refer to https://git-scm.com/docs/git-config#FILES. + unset XDG_CONFIG_HOME + export HOME="${TEST_TEMP_DIR}" + mkdir -p "${HOME}" - # For `git` tests to run well, user name and email need to be set. - # Refer to https://git-scm.com/docs/git-commit#_commit_information. - # This goes to the test-specific config, due to the $HOME overridden above. - git config --global user.name "John Doe" - git config --global user.email "johndoe@example.com" + # For `git` tests to run well, user name and email need to be set. + # Refer to https://git-scm.com/docs/git-commit#_commit_information. + # This goes to the test-specific config, due to the $HOME overridden above. + git config --global user.name "John Doe" + git config --global user.email "johndoe@example.com" - local_setup + local_setup } teardown() { - local_teardown + local_teardown - rm -rf "${BASH_IT_TEST_DIR}" - temp_del "${TEST_TEMP_DIR}" + rm -rf "${BASH_IT_TEST_DIR}" + temp_del "${TEST_TEMP_DIR}" }