BATS: revamp test `setup()` and `setup_test_fixture()`

pull/2031/head
John D Pell 2022-01-30 16:31:01 -08:00 committed by John D Pell
parent fdff1d81cd
commit 014c102b71
4 changed files with 109 additions and 100 deletions

View File

@ -137,7 +137,6 @@ plugins/available/zoxide.plugin.bash
test/plugins/alias-completion.plugin.bats test/plugins/alias-completion.plugin.bats
test/run test/run
test/test_helper.bash test/test_helper.bash
test/test_helper_libs.bash
# themes # themes
# #

View File

@ -2,17 +2,23 @@
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"
# Locate ourselves for easy reference.
export MAIN_BASH_IT_DIR="${test_directory%/*}"
export MAIN_BASH_IT_GITDIR="${MAIN_BASH_IT_DIR}/.git"
# Make sure BATS is available:
git submodule init && git submodule update git submodule init && git submodule update
if [[ -z "${BASH_IT}" ]]; then # Warn user that tests run from the current git HEAD
BASH_IT="$(cd "${test_directory}" && dirname "${PWD}")" if ! git diff --quiet; then
export BASH_IT echo "${BASH_SOURCE##*/}: your worktree is dirty; uncommitted changes will *not* be tested!"
fi fi
if [[ -z "$1" ]]; then # Which tests do we run?
if [[ $# -eq '0' ]]; 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=("$@")
fi fi
# Make sure that the `parallel` command is installed, # Make sure that the `parallel` command is installed,
@ -41,5 +47,5 @@ if command -v parallel &> /dev/null \
"${test_dirs[@]}" "${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

182
test/test_helper.bash 100755 → 100644
View File

@ -1,105 +1,117 @@
#!/usr/bin/env bats # shellcheck shell=bash
load "${BASH_IT}/vendor/github.com/erichs/composure/composure.sh"
unset BASH_IT_THEME function setup_file() {
unset GIT_HOSTING common_setup_file
unset NGINX_PATH
unset IRC_CLIENT
unset TODO
unset SCM_CHECK
unset BASH_IT_AUTOMATIC_RELOAD_AFTER_CONFIG_CHANGE
export TEST_MAIN_DIR="${BATS_TEST_DIRNAME}/.."
export TEST_DEPS_DIR="${TEST_DEPS_DIR-${TEST_MAIN_DIR}/../test_lib}"
# be independent of git's system configuration
export GIT_CONFIG_NOSYSTEM
load "${TEST_DEPS_DIR}/bats-support/load.bash"
load "${TEST_DEPS_DIR}/bats-assert/load.bash"
load "${TEST_DEPS_DIR}/bats-file/load.bash"
# support 'plumbing' metadata
cite _about _param _example _group _author _version
cite about-alias about-plugin about-completion
local_setup() {
true
} }
local_teardown() { function common_setup_file() {
true # export *everything* to subshells, needed to support tests
} set -a
# This function sets up a local test fixture, i.e. a completely # Locate ourselves for easy reference.
# fresh and isolated Bash-it directory. This is done to avoid TEST_MAIN_DIR="${MAIN_BASH_IT_DIR:-${BATS_TEST_DIRNAME?}/../..}/test"
# messing with your own Bash-it source directory. TEST_DEPS_DIR="${MAIN_BASH_IT_DIR:-${TEST_MAIN_DIR}/..}/test_lib"
# 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/../../../.."
if command -v rsync &> /dev/null; then # Load the BATS modules we use:
# Use rsync to copy Bash-it to the temp folder load "${TEST_DEPS_DIR}/bats-support/load.bash"
rsync -qavrKL -d --delete-excluded --exclude=.git --exclude=helper.bash --exclude=enabled "$src_topdir" "$BASH_IT" load "${TEST_DEPS_DIR}/bats-assert/load.bash"
else load "${TEST_DEPS_DIR}/bats-file/load.bash"
rm -rf "$BASH_IT"
mkdir -p "$BASH_IT"
find "$src_topdir" \ # shellcheck disable=SC2034 # Clear any inherited environment:
-mindepth 1 -maxdepth 1 \ XDG_DUMMY="" BASH_IT_DUMMY="" # avoid possible invalid reference:
-not -name .git \ unset "${!XDG_@}" "${!BASH_IT@}" # unset all BASH_IT* and XDG_* variables
-exec cp -r {} "$BASH_IT" \; unset GIT_HOSTING NGINX_PATH IRC_CLIENT TODO SCM_CHECK
fi
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
# Some tests use the BASH_IT_TEST_HOME variable, e.g. install/uninstall
export BASH_IT_TEST_HOME="$TEST_TEMP_DIR"
}
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
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
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
# seems to set HOME explicitly to a separate location. # this 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 readonly HOME="${BATS_SUITE_TMPDIR?}"
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 "Bash It BATS Runner"
git config --global user.email "johndoe@example.com" git config --global user.email "bats@bash.it"
git config --global advice.detachedHead false
git config --global init.defaultBranch "master"
# Locate the temporary folder, avoid double-slash.
BASH_IT="${BATS_FILE_TMPDIR//\/\///}/.bash_it"
# This sets up a local test fixture, i.e. a completely fresh and isolated Bash-it directory. This is done to avoid messing with your own Bash-it source directory.
git --git-dir="${MAIN_BASH_IT_GITDIR?}" worktree add -d "${BASH_IT}"
load "${BASH_IT?}/vendor/github.com/erichs/composure/composure.sh"
# support 'plumbing' metadata
cite _about _param _example _group _author _version
cite about-alias about-plugin about-completion
# Run any local test setup
local_setup_file
set +a # not needed, but symetiric!
}
# Load standard _Bash It_ libraries
function setup_libs() {
local lib
# Use a loop to allow convenient short-circuiting for some test files
for lib in "log" "utilities" "helpers" "search" "preexec" "colors"; do
load "${BASH_IT?}/lib/${lib}.bash" || return
# shellcheck disable=SC2015 # short-circuit if we've reached the requested library
[[ "${lib}" == "${1:-}" ]] && return 0 || true
done
return 0
}
function local_setup_file() {
true
}
function local_setup() {
true
}
function local_teardown() {
true
}
function clean_test_fixture() {
rm -rf "${BASH_IT_CONFIG?}/enabled"
rm -rf "${BASH_IT_CONFIG?}/aliases/enabled"
rm -rf "${BASH_IT_CONFIG?}/completion/enabled"
rm -rf "${BASH_IT_CONFIG?}/plugins/enabled"
rm -rf "${BASH_IT_CONFIG?}/tmp/cache"
rm -rf "${BASH_IT_CONFIG?}/profiles"/test*.bash_it
}
function setup_test_fixture() {
mkdir -p "${BASH_IT_CONFIG?}/enabled"
mkdir -p "${BASH_IT_CONFIG?}/aliases/enabled"
mkdir -p "${BASH_IT_CONFIG?}/completion/enabled"
mkdir -p "${BASH_IT_CONFIG?}/plugins/enabled"
}
function setup() {
# be independent of git's system configuration
export GIT_CONFIG_NOSYSTEM
# Locate the temporary folder:
BASH_IT_CONFIG="${BASH_IT?}" #"${BATS_TEST_TMPDIR//\/\///}"
export XDG_CACHE_HOME="${BATS_TEST_TMPDIR?}"
setup_test_fixture
local_setup local_setup
} }
teardown() { function teardown() {
unset GIT_CONFIG_NOSYSTEM
local_teardown local_teardown
clean_test_fixture
rm -rf "${BASH_IT_TEST_DIR}" }
temp_del "${TEST_TEMP_DIR}"
function teardown_file() {
# This only serves to clean metadata from the real git repo.
git --git-dir="${MAIN_BASH_IT_GITDIR?}" worktree remove -f "${BASH_IT?}"
} }

View File

@ -1,8 +0,0 @@
# shellcheck shell=bash
load "${BASH_IT}/lib/log.bash"
load "${BASH_IT}/lib/utilities.bash"
load "${BASH_IT}/lib/helpers.bash"
load "${BASH_IT}/lib/search.bash"
load "${BASH_IT}/lib/preexec.bash"
load "${BASH_IT}/lib/colors.bash"