2016-01-25 06:38:55 +00:00
|
|
|
###############################################################################
|
|
|
|
# test_helper.bash
|
|
|
|
#
|
|
|
|
# Test helper for Bats: Bash Automated Testing System.
|
|
|
|
#
|
|
|
|
# https://github.com/sstephenson/bats
|
|
|
|
###############################################################################
|
|
|
|
|
2016-01-25 05:05:07 +00:00
|
|
|
setup() {
|
|
|
|
# `$_HOSTS`
|
|
|
|
#
|
|
|
|
# The location of the `hosts` script being tested.
|
|
|
|
export _HOSTS="${BATS_TEST_DIRNAME}/../hosts"
|
|
|
|
|
2020-04-13 02:04:39 +00:00
|
|
|
export _HOSTS_TEMP_DIR
|
|
|
|
_HOSTS_TEMP_DIR="$(mktemp -d)"
|
|
|
|
|
2018-05-06 21:44:16 +00:00
|
|
|
export _HOSTS_TEMP_PATH
|
2020-04-13 02:04:39 +00:00
|
|
|
_HOSTS_TEMP_PATH="${_HOSTS_TEMP_DIR}/hosts"
|
|
|
|
|
2020-05-17 18:25:28 +00:00
|
|
|
export _ERROR_PREFIX
|
|
|
|
_ERROR_PREFIX="$(tput setaf 1)!$(tput sgr0) "
|
|
|
|
|
2018-05-06 21:44:16 +00:00
|
|
|
cat "${BATS_TEST_DIRNAME}/fixtures/hosts" > "${_HOSTS_TEMP_PATH}"
|
|
|
|
|
|
|
|
export HOSTS_PATH="${_HOSTS_TEMP_PATH}"
|
2020-03-31 00:41:51 +00:00
|
|
|
|
|
|
|
# Use empty `hosts` script in environment to avoid depending on `hosts`
|
|
|
|
# being available in `$PATH`.
|
|
|
|
export PATH="${BATS_TEST_DIRNAME}/fixtures/bin:${PATH}"
|
2016-01-25 05:05:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
teardown() {
|
2018-05-06 21:44:16 +00:00
|
|
|
if [[ -n "${_HOSTS_TEMP_PATH}" ]] &&
|
|
|
|
[[ -e "${_HOSTS_TEMP_PATH}" ]] &&
|
|
|
|
[[ "${_HOSTS_TEMP_PATH}" =~ ^/tmp/hosts_test ]]
|
2016-01-25 06:31:42 +00:00
|
|
|
then
|
2020-03-16 01:42:16 +00:00
|
|
|
rm -f "${_HOSTS_TEMP_DIR}"/hosts_test.*
|
2016-01-25 06:31:42 +00:00
|
|
|
fi
|
2016-01-25 05:05:07 +00:00
|
|
|
}
|
2016-01-26 01:20:10 +00:00
|
|
|
|
|
|
|
###############################################################################
|
|
|
|
# Helpers
|
|
|
|
###############################################################################
|
|
|
|
|
|
|
|
# _compare()
|
|
|
|
#
|
|
|
|
# Usage:
|
|
|
|
# _compare <expected> <actual>
|
|
|
|
#
|
2016-01-26 01:25:10 +00:00
|
|
|
# Description:
|
2016-01-26 01:20:10 +00:00
|
|
|
# Compare the content of a variable against an expected value. When used
|
|
|
|
# within a `@test` block the output is only displayed when the test fails.
|
|
|
|
_compare() {
|
|
|
|
local _expected="${1:-}"
|
|
|
|
local _actual="${2:-}"
|
2018-04-15 21:55:26 +00:00
|
|
|
printf "expected:\\n%s\\n" "${_expected}"
|
|
|
|
printf "actual:\\n%s\\n" "${_actual}"
|
2016-01-26 01:20:10 +00:00
|
|
|
}
|