Add test script
This commit is contained in:
parent
ecc668938d
commit
511397c955
1 changed files with 129 additions and 0 deletions
129
test.sh
Executable file
129
test.sh
Executable file
|
@ -0,0 +1,129 @@
|
|||
#!/usr/bin/env bash
|
||||
set -uo pipefail
|
||||
IFS=$'\n'
|
||||
|
||||
TESTS="
|
||||
# Logging
|
||||
logger_diag
|
||||
logger_verb
|
||||
logger_sarn
|
||||
logger_info
|
||||
logger_warn
|
||||
logger_error
|
||||
logger_crash
|
||||
|
||||
# Checks
|
||||
check_command_alias
|
||||
check_command_keyword
|
||||
check_command_function
|
||||
check_command_builtin
|
||||
check_command_file
|
||||
check_command_defined
|
||||
"
|
||||
|
||||
# Utility methods
|
||||
function invalid() { echo ":: Error: Invalid test ${TEST}, skipping"; }
|
||||
function fail() {
|
||||
echo -e ":: Test ${TEST} failed\n ${*//\\n/\\n }"
|
||||
[ -n "${FAILFAST}" ] && exit 1
|
||||
}
|
||||
## Assertions
|
||||
function assert_equals() {
|
||||
EXPECTED="${1}"
|
||||
shift
|
||||
ACTUAL="${*}"
|
||||
|
||||
[[ "${ACTUAL}" != "${EXPECTED}" ]] && fail "${1}" "String does not match expected string\nExpected: ${EXPECTED}\nActual: ${ACTUAL}"
|
||||
}
|
||||
function assert_contains() {
|
||||
EXPECTED="${1}"
|
||||
shift
|
||||
ACTUAL="${*}"
|
||||
|
||||
[[ "${ACTUAL}" != *"${EXPECTED}"* ]] && fail "${1}" "String does not contain expected string\nExpected: ${EXPECTED}\nActual: ${ACTUAL}"
|
||||
}
|
||||
|
||||
# Test loop
|
||||
for TEST in ${TESTS}; do
|
||||
[ -z "${TEST}" ] || [[ "${TEST}" == "#"* ]] && continue
|
||||
|
||||
echo ":: Running test ${TEST}"
|
||||
export TEST
|
||||
|
||||
# Logging
|
||||
if [[ "${TEST}" == "logger_"* ]]; then
|
||||
if [ "${TEST}" == "logger_crash" ]; then
|
||||
assert_contains "It crashed!" "$( (
|
||||
source "bashutils.sh"
|
||||
crash "true" "It crashed!"
|
||||
) &> /dev/stdout)"
|
||||
else
|
||||
assert_contains "This is a test message" "$( (
|
||||
source "bashutils.sh"
|
||||
# shellcheck disable=SC2034
|
||||
BASHUTILS_LOGLEVEL=0
|
||||
${TEST//logger_/} "This is a test message"
|
||||
) &> /dev/stdout)"
|
||||
|
||||
fi
|
||||
|
||||
# Checks
|
||||
elif [[ "${TEST}" == "check_"* ]]; then
|
||||
# Command checks
|
||||
if [[ "${TEST}" == "check_command_"* ]]; then
|
||||
case "${TEST//check_command/}" in
|
||||
"alias")
|
||||
assert_equals "0" "$(
|
||||
source "bashutils.sh"
|
||||
alias "verynicealias=ls"
|
||||
is_command_alias "verynicealias"
|
||||
echo "${?}"
|
||||
)"
|
||||
;;
|
||||
"keyword")
|
||||
assert_equals "0" "$(
|
||||
source "bashutils.sh"
|
||||
is_command_keyword "continue"
|
||||
echo "${?}"
|
||||
)"
|
||||
;;
|
||||
"function")
|
||||
assert_equals "0" "$(
|
||||
source "bashutils.sh"
|
||||
is_command_function "is_command_function"
|
||||
echo "${?}"
|
||||
)"
|
||||
;;
|
||||
"builtin")
|
||||
assert_equals "0" "$(
|
||||
source "bashutils.sh"
|
||||
is_command_builtin "set"
|
||||
echo "${?}"
|
||||
)"
|
||||
;;
|
||||
"file")
|
||||
assert_equals "0" "$(
|
||||
source "bashutils.sh"
|
||||
is_command_file "clear"
|
||||
echo "${?}"
|
||||
)"
|
||||
;;
|
||||
"defined")
|
||||
assert_equals "0" "$(
|
||||
source "bashutils.sh"
|
||||
is_command_defined "echo"
|
||||
echo "${?}"
|
||||
)"
|
||||
;;
|
||||
esac
|
||||
|
||||
# Invalid command
|
||||
else
|
||||
invalid
|
||||
fi
|
||||
|
||||
# Invalid command
|
||||
else
|
||||
invalid
|
||||
fi
|
||||
done
|
Loading…
Reference in a new issue