add tdd for a better reloader

pull/1761/head
cornfeedhobo 2021-01-02 22:51:27 -06:00
parent 1ce93fd60e
commit 83f4c70e81
No known key found for this signature in database
GPG Key ID: 724357093F994B26
9 changed files with 203 additions and 91 deletions

View File

@ -1,20 +1,10 @@
#!/usr/bin/env bats #!/usr/bin/env bats
load ../test_helper load ../test_helper
load ../../lib/composure
function local_setup { function local_setup {
setup_test_fixture setup_test_fixture
copy_test_fixtures
# 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
} }
@test "bash-it: verify that the test fixture is available" { @test "bash-it: verify that the test fixture is available" {

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -0,0 +1,4 @@
# shellcheck shell=bash
about-plugin 'here to load last. a canary test for silent failures.'
printf 'You found me!'

View File

@ -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!'
}

View File

@ -1,19 +1,20 @@
#!/usr/bin/env bash #!/usr/bin/env bash
test_directory="$(cd "$(dirname "$0")" && pwd)" test_directory="$(cd "$(dirname "$0")" && pwd)"
bats_executable="${test_directory}/../test_lib/bats-core/bin/bats" bats_executable="${test_directory}/../test_lib/bats-core/bin/bats"
git submodule init && git submodule update git submodule init && git submodule update
if [ -z "${BASH_IT}" ]; then if [ -z "${BASH_IT}" ]; then
declare BASH_IT declare BASH_IT
BASH_IT=$(cd ${test_directory} && dirname "$(pwd)") BASH_IT=$(cd "${test_directory}" && dirname "$(pwd)")
export BASH_IT export BASH_IT
fi fi
if [ -z "$1" ]; then 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 else
test_dirs=( "$1" ) test_dirs=("$1")
fi fi
# Make sure that the `parallel` command is installed, # 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, # 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. # so we can run `bats` in parallel processing mode, which is a lot faster.
if command -v parallel &> /dev/null \ if command -v parallel &> /dev/null \
&& parallel -V &> /dev/null \ && parallel -V &> /dev/null \
&& { parallel -V 2> /dev/null | grep -q '^GNU\>'; } && { parallel -V 2> /dev/null | grep -q '^GNU\>'; }; then
then # Expect to run at least on a dual-core CPU; slightly degraded performance
# Expect to run at least on a dual-core CPU; slightly degraded performance # shouldn't matter otherwise.
# shouldn't matter otherwise. declare -i -r test_jobs_default=2
declare -i -r test_jobs_default=2 declare -i -r test_jobs_effective="$(
declare -i -r test_jobs_effective="$( if [ "${TEST_JOBS:-detect}" = "detect" ] \
if [ "${TEST_JOBS:-detect}" = "detect" ] \ && command -v nproc &> /dev/null; then
&& command -v nproc &> /dev/null nproc
then elif [ -n "${TEST_JOBS}" ] \
nproc && [ "${TEST_JOBS}" != "detect" ]; then
elif [ -n "${TEST_JOBS}" ] \ echo "${TEST_JOBS}"
&& [ "${TEST_JOBS}" != "detect" ] else
then echo ${test_jobs_default}
echo "${TEST_JOBS}" fi
else )"
echo ${test_jobs_default} exec "$bats_executable" ${CI:+--tap} --jobs "${test_jobs_effective}" "${test_dirs[@]}"
fi
)"
exec "$bats_executable" ${CI:+--tap} --jobs ${test_jobs_effective} \
"${test_dirs[@]}"
else else
# Run `bats` in single-threaded mode. # Run `bats` in single-threaded mode.
exec "$bats_executable" ${CI:+--tap} "${test_dirs[@]}" exec "$bats_executable" ${CI:+--tap} "${test_dirs[@]}"
fi fi

View File

@ -1,5 +1,4 @@
#!/usr/bin/env bats #!/usr/bin/env bats
load ../../lib/composure
unset BASH_IT_THEME unset BASH_IT_THEME
unset GIT_HOSTING unset GIT_HOSTING
@ -20,14 +19,16 @@ load "${TEST_DEPS_DIR}/bats-assert/load.bash"
load "${TEST_DEPS_DIR}/bats-file/load.bash" load "${TEST_DEPS_DIR}/bats-file/load.bash"
# support 'plumbing' metadata # support 'plumbing' metadata
load ../../lib/composure
cite _about _param _example _group _author _version cite _about _param _example _group _author _version
cite about-alias about-completion about-plugin
local_setup() { local_setup() {
true true
} }
local_teardown() { local_teardown() {
true true
} }
# This function sets up a local test fixture, i.e. a completely # 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. # messing with your own Bash-it source directory.
# If you need this, call it in your .bats file's `local_setup` function. # If you need this, call it in your .bats file's `local_setup` function.
setup_test_fixture() { setup_test_fixture() {
mkdir -p "$BASH_IT" mkdir -p "$BASH_IT"
lib_directory="$(cd "$(dirname "$0")" && pwd)" lib_directory="$(cd "$(dirname "$0")" && pwd)"
local src_topdir="$lib_directory/../../../.." local src_topdir="$lib_directory/../../../.."
if command -v rsync &> /dev/null if command -v rsync &> /dev/null; then
then # Use rsync to copy Bash-it to the temp folder
# Use rsync to copy Bash-it to the temp folder rsync -qavrKL -d --delete-excluded --exclude=.git --exclude=enabled "$src_topdir" "$BASH_IT"
rsync -qavrKL -d --delete-excluded --exclude=.git --exclude=enabled "$src_topdir" "$BASH_IT" else
else rm -rf "$BASH_IT"
rm -rf "$BASH_IT" mkdir -p "$BASH_IT"
mkdir -p "$BASH_IT"
find "$src_topdir" \ find "$src_topdir" \
-mindepth 1 -maxdepth 1 \ -mindepth 1 -maxdepth 1 \
-not -name .git \ -not -name .git \
-exec cp -r {} "$BASH_IT" \; -exec cp -r {} "$BASH_IT" \;
fi fi
rm -rf "$BASH_IT"/enabled rm -rf "$BASH_IT"/enabled
rm -rf "$BASH_IT"/aliases/enabled rm -rf "$BASH_IT"/aliases/enabled
rm -rf "$BASH_IT"/completion/enabled rm -rf "$BASH_IT"/completion/enabled
rm -rf "$BASH_IT"/plugins/enabled rm -rf "$BASH_IT"/plugins/enabled
mkdir -p "$BASH_IT"/enabled mkdir -p "$BASH_IT"/enabled
mkdir -p "$BASH_IT"/aliases/enabled mkdir -p "$BASH_IT"/aliases/enabled
mkdir -p "$BASH_IT"/completion/enabled mkdir -p "$BASH_IT"/completion/enabled
mkdir -p "$BASH_IT"/plugins/enabled mkdir -p "$BASH_IT"/plugins/enabled
# Some tests use the BASH_IT_TEST_HOME variable, e.g. install/uninstall # Some tests use the BASH_IT_TEST_HOME variable, e.g. install/uninstall
export BASH_IT_TEST_HOME="$TEST_TEMP_DIR" 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() { setup() {
# The `temp_make` function from "bats-file" requires the tralston/bats-file fork, # 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. # since the original ztombol/bats-file's `temp_make` does not work on macOS.
TEST_TEMP_DIR="$(temp_make --prefix 'bash-it-test-')" TEST_TEMP_DIR="$(temp_make --prefix 'bash-it-test-')"
export TEST_TEMP_DIR 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_ROOT="${BASH_IT_TEST_DIR}/root"
export BASH_IT=$BASH_IT_TEST_DIR 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, # 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 # which interferes with our tests. The only way to keep `git` from doing this
# seems to set HOME explicitly to a separate location. # seems to set HOME explicitly to a separate location.
# Refer to https://git-scm.com/docs/git-config#FILES. # Refer to https://git-scm.com/docs/git-config#FILES.
unset XDG_CONFIG_HOME unset XDG_CONFIG_HOME
export HOME="${TEST_TEMP_DIR}" export HOME="${TEST_TEMP_DIR}"
mkdir -p "${HOME}" mkdir -p "${HOME}"
# For `git` tests to run well, user name and email need to be set. # 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. # Refer to https://git-scm.com/docs/git-commit#_commit_information.
# This goes to the test-specific config, due to the $HOME overridden above. # This goes to the test-specific config, due to the $HOME overridden above.
git config --global user.name "John Doe" git config --global user.name "John Doe"
git config --global user.email "johndoe@example.com" git config --global user.email "johndoe@example.com"
local_setup local_setup
} }
teardown() { teardown() {
local_teardown local_teardown
rm -rf "${BASH_IT_TEST_DIR}" rm -rf "${BASH_IT_TEST_DIR}"
temp_del "${TEST_TEMP_DIR}" temp_del "${TEST_TEMP_DIR}"
} }