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
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" {

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,4 +1,5 @@
#!/usr/bin/env bash
test_directory="$(cd "$(dirname "$0")" && pwd)"
bats_executable="${test_directory}/../test_lib/bats-core/bin/bats"
@ -6,14 +7,14 @@ git submodule init && git submodule update
if [ -z "${BASH_IT}" ]; then
declare BASH_IT
BASH_IT=$(cd ${test_directory} && dirname "$(pwd)")
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,
@ -22,26 +23,22 @@ fi
# 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
&& { 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
&& command -v nproc &> /dev/null; then
nproc
elif [ -n "${TEST_JOBS}" ] \
&& [ "${TEST_JOBS}" != "detect" ]
then
&& [ "${TEST_JOBS}" != "detect" ]; then
echo "${TEST_JOBS}"
else
echo ${test_jobs_default}
fi
)"
exec "$bats_executable" ${CI:+--tap} --jobs ${test_jobs_effective} \
"${test_dirs[@]}"
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[@]}"

View File

@ -1,5 +1,4 @@
#!/usr/bin/env bats
load ../../lib/composure
unset BASH_IT_THEME
unset GIT_HOSTING
@ -20,7 +19,9 @@ 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
@ -39,8 +40,7 @@ setup_test_fixture() {
lib_directory="$(cd "$(dirname "$0")" && pwd)"
local src_topdir="$lib_directory/../../../.."
if command -v rsync &> /dev/null
then
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
@ -67,6 +67,17 @@ setup_test_fixture() {
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.