From c380e0e1932f3ad541be58c6df59a0386b009828 Mon Sep 17 00:00:00 2001 From: Andrew Kesterson Date: Wed, 19 Dec 2012 22:54:17 -0500 Subject: [PATCH] Initial add with a couple of tests --- README | 33 ++++++++++++++ lib/junit.sh | 116 +++++++++++++++++++++++++++++++++++++++++++++++++ lib/tunit.sh | 107 +++++++++++++++++++++++++++++++++++++++++++++ tests/junit.sh | 10 +++++ tests/tunit.sh | 10 +++++ 5 files changed, 276 insertions(+) create mode 100644 README create mode 100644 lib/junit.sh create mode 100644 lib/tunit.sh create mode 100644 tests/junit.sh create mode 100644 tests/tunit.sh diff --git a/README b/README new file mode 100644 index 0000000..b10730a --- /dev/null +++ b/README @@ -0,0 +1,33 @@ += What is shunit + +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. + +Complete documentation is inside the functions (each function has --help) in ./lib/*unit.sh. A pair of complete examples are in tests/*unit.sh. + += Example output + + akesterson@akesterson-pc ~/source/upstream/git/shunit + $ bash tests/tunit.sh + + ==== 0 TESTS in 0 SECONDS : 0 ERRORS, 0 FAILURES ==== + [super::class] someTest .... [OK] + [super::class] someOtherTest .... [FAILED] + generic failure : Yo dawg, I heard you like failures + This is some raw data, please read it + + + akesterson@akesterson-pc ~/source/upstream/git/shunit + $ bash tests/junit.sh + + + + + + + + + + + diff --git a/lib/junit.sh b/lib/junit.sh new file mode 100644 index 0000000..56ff091 --- /dev/null +++ b/lib/junit.sh @@ -0,0 +1,116 @@ +#!/bin/bash + +JUNIT_FAILURES=0 +JUNIT_ERRORS=0 +JUNIT_TESTS=0 +JUNIT_TIMERSTART=$(date "+%s") + +function junit_header() +{ + if [ "$1" == "--help" ]; then + cat < /tmp/$$.junit +} + +function junit_header_real() +{ + if [ "$1" == "--help" ]; then + cat <' + echo '' + echo ' ' +} + +function junit_footer() +{ + if [ "$1" == "--help" ]; then + cat <' +} + +function junit_testcase() +{ + if [ "$1" == "--help" ]; then + cat <' >> /tmp/$$.junit + if [ "$failtype" != "" ]; then + JUNIT_ERRORS=$(expr $JUNIT_ERRORS + 1) + JUNIT_FAILURES=$(expr $JUNIT_FAILURES + 1) + echo ' ' >> /tmp/$$.junit + echo ' > /tmp/$$.junit + echo "${cdata}" >> /tmp/$$.junit + echo ' ]]>' >> /tmp/$$.junit + echo ' ' >> /tmp/$$.junit + fi + echo ' ' >> /tmp/$$.junit +} diff --git a/lib/tunit.sh b/lib/tunit.sh new file mode 100644 index 0000000..d8fdb95 --- /dev/null +++ b/lib/tunit.sh @@ -0,0 +1,107 @@ +#!/bin/bash + +# tunit is short for "text unit", which is something I completely made +# up. Every unit test is printed in the following format: +# +# [ TEST NAME ] ... [ OK | FAIL ] +# +# If COLOR=on, then the OK/FAIL bits are in RED or GREEN. + +COLOR=${COLOR:-on} + +if [ "$COLOR" == "on" ]; then + COLOR_GREEN=$(echo -e '\033[0;32;40m'); + COLOR_RED=$(echo -e '\033[0;31;40m'); + COLOR_NORMAL=$(echo -e '\033[0m'); +else + COLOR_GREEN="" + COLOR_RED="" + COLOR_NORMAL="" +fi + + +function tunit_header() +{ + if [ "$1" == "--help" ]; then + cat <