handling no parms better
[feisty_meow.git] / scripts / shunit / shunit2_test.sh
1 #! /bin/sh
2 # $Id: shunit2_test.sh 322 2011-04-24 00:09:45Z kate.ward@forestent.com $
3 # vim:et:ft=sh:sts=2:sw=2
4 #
5 # Copyright 2008 Kate Ward. All Rights Reserved.
6 # Released under the LGPL (GNU Lesser General Public License)
7 # Author: kate.ward@forestent.com (Kate Ward)
8 #
9 # shUnit2 unit test suite runner.
10 #
11 # This script runs all the unit tests that can be found, and generates a nice
12 # report of the tests.
13
14 MY_NAME=`basename $0`
15 MY_PATH=`dirname $0`
16
17 PREFIX='shunit2_test_'
18 SHELLS='/bin/sh /bin/bash /bin/dash /bin/ksh /bin/pdksh /bin/zsh'
19 TESTS=''
20 for test in ${PREFIX}[a-z]*.sh; do
21   TESTS="${TESTS} ${test}"
22 done
23
24 # load common unit test functions
25 . ../lib/versions
26 . ./shunit2_test_helpers
27
28 usage()
29 {
30   echo "usage: ${MY_NAME} [-e key=val ...] [-s shell(s)] [-t test(s)]"
31 }
32
33 env=''
34
35 # process command line flags
36 while getopts 'e:hs:t:' opt; do
37   case ${opt} in
38     e)  # set an environment variable
39       key=`expr "${OPTARG}" : '\([^=]*\)='`
40       val=`expr "${OPTARG}" : '[^=]*=\(.*\)'`
41       if [ -z "${key}" -o -z "${val}" ]; then
42         usage
43         exit 1
44       fi
45       eval "${key}='${val}'"
46       export ${key}
47       env="${env:+${env} }${key}"
48       ;;
49     h) usage; exit 0 ;;  # output help
50     s) shells=${OPTARG} ;;  # list of shells to run
51     t) tests=${OPTARG} ;;  # list of tests to run
52     *) usage; exit 1 ;;
53   esac
54 done
55 shift `expr ${OPTIND} - 1`
56
57 # fill shells and/or tests
58 shells=${shells:-${SHELLS}}
59 tests=${tests:-${TESTS}}
60
61 # error checking
62 if [ -z "${tests}" ]; then
63   th_error 'no tests found to run; exiting'
64   exit 1
65 fi
66
67 cat <<EOF
68 #------------------------------------------------------------------------------
69 # System data
70 #
71
72 # test run info
73 shells: ${shells}
74 tests: ${tests}
75 EOF
76 for key in ${env}; do
77   eval "echo \"${key}=\$${key}\""
78 done
79 echo
80
81 # output system data
82 echo "# system info"
83 echo "$ date"
84 date
85 echo
86
87 echo "$ uname -mprsv"
88 uname -mprsv
89
90 #
91 # run tests
92 #
93
94 for shell in ${shells}; do
95   echo
96
97   # check for existance of shell
98   if [ ! -x ${shell} ]; then
99     th_warn "unable to run tests with the ${shell} shell"
100     continue
101   fi
102
103   cat <<EOF
104
105 #------------------------------------------------------------------------------
106 # Running the test suite with ${shell}
107 #
108 EOF
109
110   SHUNIT_SHELL=${shell}  # pass shell onto tests
111   shell_name=`basename ${shell}`
112   shell_version=`versions_shellVersion "${shell}"`
113
114   echo "shell name: ${shell_name}"
115   echo "shell version: ${shell_version}"
116
117   # execute the tests
118   for suite in ${tests}; do
119     suiteName=`expr "${suite}" : "${PREFIX}\(.*\).sh"`
120     echo
121     echo "--- Executing the '${suiteName}' test suite ---"
122     ( exec ${shell} ./${suite} 2>&1; )
123   done
124 done