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"
|
|
|
|
|
2016-01-25 06:31:42 +00:00
|
|
|
export HOSTS_PATH="$(mktemp /tmp/hosts_test.XXXXXX)" || exit 1
|
|
|
|
cat "${BATS_TEST_DIRNAME}/fixtures/hosts" > "${HOSTS_PATH}"
|
2016-01-25 05:05:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
teardown() {
|
2016-01-25 06:31:42 +00:00
|
|
|
if [[ -n "${HOSTS_PATH}" ]] &&
|
|
|
|
[[ -e "${HOSTS_PATH}" ]] &&
|
|
|
|
[[ "${HOSTS_PATH}" =~ ^/tmp ]]
|
|
|
|
then
|
|
|
|
rm "${HOSTS_PATH}"
|
|
|
|
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:-}"
|
|
|
|
printf "expected:\n%s\n" "${_expected}"
|
|
|
|
printf "actual:\n%s\n" "${_actual}"
|
|
|
|
}
|