diff --git a/Makefile b/Makefile index 2608ebc..56b688f 100644 --- a/Makefile +++ b/Makefile @@ -32,7 +32,7 @@ run: boot.img .PHONY: test test: - cd tests && make clean && shunit.sh -f $(TEST_FORMAT) -t test.sh + cd tests && make clean && ../dependencies/bin/shunit.sh -f $(TEST_FORMAT) -t test.sh .PHONY: clean clean: diff --git a/dependencies/bin/shunit.sh b/dependencies/bin/shunit.sh new file mode 100755 index 0000000..a2cac8a --- /dev/null +++ b/dependencies/bin/shunit.sh @@ -0,0 +1,56 @@ +#!/bin/bash + +source $(dirname ${BASH_SOURCE[0]})/../lib/cmdarg.sh + +function validate_format +{ + echo $1 | grep -E "^junit$|^tunit$" >/dev/null 2>&1 + return $? +} + +cmdarg_info author "Andrew Kesterson " +cmdarg_info header "A bash script for unit testing other bash scripts in JUNIT or human-friendly formats" +cmdarg_info copyright "(MIT License)" +cmdarg 'f:' 'format' 'Format to print results in. Valid options are: [junit, tunit]' 'junit' validate_format +cmdarg 't:' 'tests' 'Directory or single file to test.' + +cmdarg_parse "$@" + +FORMATTER=${cmdarg_cfg['format']} + +set -e +source $(dirname ${BASH_SOURCE[0]})/../lib/${FORMATTER}.sh +set +e + +${FORMATTER}_header + +if [[ -d ${cmdarg_cfg[tests]} ]]; then + FILES=${cmdarg_cfg[tests]}/*sh +elif [[ -e ${cmdarg_cfg[tests]} ]]; then + FILES=${cmdarg_cfg[tests]} +fi + +for file in $FILES; +do + declare -A tests + source $file + for key in $(declare -F | grep 'shunittest_') + do + if [[ "$(type -t $key)" == "function" ]]; then + start=$(date "+%s") + ERR=$($key 2>&1) + ERRFLAG=$? + delta=$(($(date "+%s") - $start)) + if [[ $ERRFLAG -eq 0 ]]; then + ${FORMATTER}_testcase "$file" "$key" "$delta" + else + SHORTERR=$(echo "$ERR" | head -n 1) + ${FORMATTER}_testcase "$file" "$key" "$delta" "Exit ${ERRFLAG}" "$SHORTERR" "${ERR}" + fi + fi + unset -f $key + done + unset tests +done + +${FORMATTER}_footer diff --git a/dependencies/lib/cmdarg.sh b/dependencies/lib/cmdarg.sh new file mode 100644 index 0000000..282b05c --- /dev/null +++ b/dependencies/lib/cmdarg.sh @@ -0,0 +1,459 @@ +#!/bin/bash + +if (( BASH_VERSINFO[0] < 4 )); then + echo "cmdarg is incompatible with bash versions < 4, please upgrade bash" >&2 + exit 1 +fi + +CMDARG_ERROR_BEHAVIOR=return + +CMDARG_FLAG_NOARG=0 +CMDARG_FLAG_REQARG=2 +CMDARG_FLAG_OPTARG=4 + +CMDARG_TYPE_ARRAY=1 +CMDARG_TYPE_HASH=2 +CMDARG_TYPE_STRING=3 +CMDARG_TYPE_BOOLEAN=4 + +function cmdarg +{ + # cmdarg