BATS: revamp test `setup()` and `setup_test_fixture()`
parent
fdff1d81cd
commit
014c102b71
|
|
@ -137,7 +137,6 @@ plugins/available/zoxide.plugin.bash
|
|||
test/plugins/alias-completion.plugin.bats
|
||||
test/run
|
||||
test/test_helper.bash
|
||||
test/test_helper_libs.bash
|
||||
|
||||
# themes
|
||||
#
|
||||
|
|
|
|||
18
test/run
18
test/run
|
|
@ -2,17 +2,23 @@
|
|||
test_directory="$(cd "$(dirname "$0")" && pwd)"
|
||||
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
|
||||
|
||||
if [[ -z "${BASH_IT}" ]]; then
|
||||
BASH_IT="$(cd "${test_directory}" && dirname "${PWD}")"
|
||||
export BASH_IT
|
||||
# Warn user that tests run from the current git HEAD
|
||||
if ! git diff --quiet; then
|
||||
echo "${BASH_SOURCE##*/}: your worktree is dirty; uncommitted changes will *not* be tested!"
|
||||
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})
|
||||
else
|
||||
test_dirs=("$1")
|
||||
test_dirs=("$@")
|
||||
fi
|
||||
|
||||
# Make sure that the `parallel` command is installed,
|
||||
|
|
@ -41,5 +47,5 @@ if command -v parallel &> /dev/null \
|
|||
"${test_dirs[@]}"
|
||||
else
|
||||
# Run `bats` in single-threaded mode.
|
||||
exec "$bats_executable" ${CI:+--tap} "${test_dirs[@]}"
|
||||
exec "$bats_executable" "${CI:+--tap}" "${test_dirs[@]}"
|
||||
fi
|
||||
|
|
|
|||
|
|
@ -1,105 +1,117 @@
|
|||
#!/usr/bin/env bats
|
||||
load "${BASH_IT}/vendor/github.com/erichs/composure/composure.sh"
|
||||
# shellcheck shell=bash
|
||||
|
||||
unset BASH_IT_THEME
|
||||
unset GIT_HOSTING
|
||||
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
|
||||
function setup_file() {
|
||||
common_setup_file
|
||||
}
|
||||
|
||||
local_teardown() {
|
||||
true
|
||||
}
|
||||
function common_setup_file() {
|
||||
# export *everything* to subshells, needed to support tests
|
||||
set -a
|
||||
|
||||
# This function 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.
|
||||
# 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/../../../.."
|
||||
# Locate ourselves for easy reference.
|
||||
TEST_MAIN_DIR="${MAIN_BASH_IT_DIR:-${BATS_TEST_DIRNAME?}/../..}/test"
|
||||
TEST_DEPS_DIR="${MAIN_BASH_IT_DIR:-${TEST_MAIN_DIR}/..}/test_lib"
|
||||
|
||||
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=helper.bash --exclude=enabled "$src_topdir" "$BASH_IT"
|
||||
else
|
||||
rm -rf "$BASH_IT"
|
||||
mkdir -p "$BASH_IT"
|
||||
# Load the BATS modules we use:
|
||||
load "${TEST_DEPS_DIR}/bats-support/load.bash"
|
||||
load "${TEST_DEPS_DIR}/bats-assert/load.bash"
|
||||
load "${TEST_DEPS_DIR}/bats-file/load.bash"
|
||||
|
||||
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
|
||||
|
||||
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}"
|
||||
# shellcheck disable=SC2034 # Clear any inherited environment:
|
||||
XDG_DUMMY="" BASH_IT_DUMMY="" # avoid possible invalid reference:
|
||||
unset "${!XDG_@}" "${!BASH_IT@}" # unset all BASH_IT* and XDG_* variables
|
||||
unset GIT_HOSTING NGINX_PATH IRC_CLIENT TODO SCM_CHECK
|
||||
|
||||
# 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.
|
||||
# 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}"
|
||||
readonly HOME="${BATS_SUITE_TMPDIR?}"
|
||||
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"
|
||||
git config --global user.name "Bash It BATS Runner"
|
||||
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
|
||||
}
|
||||
|
||||
teardown() {
|
||||
function teardown() {
|
||||
unset GIT_CONFIG_NOSYSTEM
|
||||
local_teardown
|
||||
|
||||
rm -rf "${BASH_IT_TEST_DIR}"
|
||||
temp_del "${TEST_TEMP_DIR}"
|
||||
clean_test_fixture
|
||||
}
|
||||
|
||||
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?}"
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
Loading…
Reference in New Issue