diff options
author | Junichi Uekawa <dancer@netfort.gr.jp> | 2008-08-11 23:39:31 +0900 |
---|---|---|
committer | Junichi Uekawa <dancer@netfort.gr.jp> | 2008-08-11 23:39:31 +0900 |
commit | 98960e2003834913df870d96a4f7ceabc6fac2c9 (patch) | |
tree | 15d3184bce11b843538e7ce35acce8ba86b89b85 | |
parent | 2fce0e3ac0cdacc3adbaa6f1d6a72153b22a5b85 (diff) | |
download | pbuilder-98960e2003834913df870d96a4f7ceabc6fac2c9.tar pbuilder-98960e2003834913df870d96a4f7ceabc6fac2c9.tar.gz |
implement a bash functional unit-testing testsuite, testlib.
-rwxr-xr-x | test_pbuilder-satisfydepends-checkparams | 24 | ||||
-rwxr-xr-x | test_testlib.sh | 22 | ||||
-rw-r--r-- | testlib.sh | 76 |
3 files changed, 122 insertions, 0 deletions
diff --git a/test_pbuilder-satisfydepends-checkparams b/test_pbuilder-satisfydepends-checkparams new file mode 100755 index 0000000..d0ef2dc --- /dev/null +++ b/test_pbuilder-satisfydepends-checkparams @@ -0,0 +1,24 @@ +#!/bin/bash + +. ./testlib.sh + +# testsuite to test pbuilder-satisfydepends-checkparams. + +# mock function +checkbuilddep_internal() { + : # do nothing function +} + +test_chrootexec1() { + set -- --chroot /tmp --internal-chrootexec 'chroot /tmp-hoge ' + . ./pbuilder-satisfydepends-checkparams +} + +test_chrootexec2() { + set -- --internal-chrootexec 'chroot /tmp-hoge ' --chroot /tmp + . ./pbuilder-satisfydepends-checkparams +} + +expect_success test_chrootexec1 +expect_fail test_chrootexec2 +testlib_summary diff --git a/test_testlib.sh b/test_testlib.sh new file mode 100755 index 0000000..bfaecaa --- /dev/null +++ b/test_testlib.sh @@ -0,0 +1,22 @@ +#!/bin/bash +# testsuite for testlib + +. ./testlib.sh + +test_success() { + exit 0 +} + +test_fail() { + exit 1 +} + +test_options() { + echo "$@" + exit 1 +} + +expect_success test_success +expect_fail test_fail +expect_fail test_options "hello world" +testlib_summary diff --git a/testlib.sh b/testlib.sh new file mode 100644 index 0000000..459c630 --- /dev/null +++ b/testlib.sh @@ -0,0 +1,76 @@ +#!/bin/bash +# 2008 Junichi Uekawa <dancer@debian.org> + +set -e + +# library for functional unit-testing in bash. + +TESTLIB_FAILS=0 +TESTLIB_TESTS=0 + +testlib_echo() { + case "$1" in + OK) + shift + echo "[OK ]" "$@" >&2 + ;; + FAIL) + shift + echo "[FAIL]" "$@" >&2 + TESTLIB_FAILS=$((TESTLIB_FAILS+1)) + ;; + esac + TESTLIB_TESTS=$((TESTLIB_TESTS+1)) +} + +testlib_summary() { + echo "Ran ${TESTLIB_TESTS} tests and ${TESTLIB_FAILS} failed" + if [ $TESTLIB_FAILS != 0 ]; then + echo '=================' + echo 'Testsuite FAILED!' + echo '=================' + exit 1 + fi + exit 0 +} + +expect_success() { + # run the test in subshell + if ( + "$@" + ); then + testlib_echo "OK" "$1" + else + testlib_echo "FAIL" "$1" + fi +} + +expect_fail() { + # run the test in subshell + if ( + "$@" + ); then + testlib_echo "FAIL" "$1" + else + testlib_echo "OK" "$1" + fi +} + +expect_output() { + # run the test in subshell + local val + val="$1"; + shift + if [ $( "$@" ) = "$1" ]; then + testlib_echo "OK" "$1" + else + testlib_echo "FAIL" "$1" + fi +} + +# Write your functions test_xxxx and call them at the end with their expected result code: +# expect_success test_success +# expect_success test_fail +# expect_success test_options "hello world" +# testlib_summary + |