From 64c31322c85ad4b49dc6558c7dac642200ecf2c4 Mon Sep 17 00:00:00 2001 From: Andrew Kesterson Date: Thu, 29 Aug 2013 08:54:00 -0400 Subject: [PATCH] Added shunit helper script and updated README --- Makefile | 3 ++- README.md | 19 ++++++++++++++++++- shunit | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 shunit diff --git a/Makefile b/Makefile index dcf2b21..9a47d48 100644 --- a/Makefile +++ b/Makefile @@ -4,6 +4,7 @@ all: install: install lib/junit.sh /usr/lib/junit.sh install lib/tunit.sh /usr/lib/tunit.sh + install shunit /usr/bin/shunit uninstall: - rm /usr/lib/[tj]unit.sh + rm /usr/lib/[tj]unit.sh /usr/bin/shunit diff --git a/README.md b/README.md index 768043b..a0e3f65 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,21 @@ -shunit +The shunit script +====== + +shunit is a bash script for running tests scripts that are written with the shunit library. To use it, fir install it: + + make install + +... then run it with the name of a single bash test script or a whole directory of scripts: + + shunit -t test_script.sh + shunit -t test_directory/ + +... The default output format is junit. You can select tunit with the '-f' flag: + + shunit -t test_script.sh -f tunit + + +The shunit library ====== shunit is just a set of bash functions for producing unit test output from bash scripts. I wrote this library because I often found myself writing things in bash whose output I wanted to consume as discrete pass/fail tasks into Bamboo, so I wrote the junit functions. Then later on, I got sick and tired of (as a human) reading junit output, so I wrote the tunit functions, so my scripts could output tunit or junit depending on which flags I passed. diff --git a/shunit b/shunit new file mode 100644 index 0000000..c86d06d --- /dev/null +++ b/shunit @@ -0,0 +1,49 @@ +#!/bin/bash + +source /usr/lib/cmdarg.sh + +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' 'echo ${OPTARG} | grep -E "^junit$|^tunit$" >/dev/null 2>&1' +cmdarg 't:' 'tests' 'Directory or single file to test.' + +cmdarg_parse "$@" + +FORMATTER=${cmdarg_cfg['format']} + +set -e +source /usr/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 'test_') + 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 + done + unset tests +done + +${FORMATTER}_footer