Update tests / test harness.
[python_utils.git] / tests / run_tests.sh
1 #!/bin/bash
2
3 source /home/scott/bin/color_vars.sh
4
5 ROOT=/home/scott/lib/python_modules
6 DOCTEST=0
7 UNITTEST=0
8 INTEGRATION=0
9 FAILURES=0
10
11 dup() {
12     if [ $# -ne 2 ]; then
13         echo "Usage: dup <string> <count>"
14         return
15     fi
16     local times=$(seq 1 $2)
17     for x in ${times}; do
18         echo -n "$1"
19     done
20 }
21
22 make_header() {
23     if [ $# -ne 2 ]; then
24         echo "Usage: make_header <required title> <color>"
25         return
26     fi
27     local title="$1"
28     local title_len=${#title}
29     title_len=$((title_len + 4))
30     local width=70
31     local left=4
32     local right=$(($width-($title_len+$left)))
33     local color="$2"
34     dup '-' $left
35     echo -ne "[ ${color}${title}${NC} ]"
36     dup '-' $right
37     echo
38 }
39
40
41 while [[ $# -gt 0 ]]; do
42     key="$1"
43     case $key in
44         -a|--all)
45             DOCTEST=1
46             UNITTEST=1
47             INTEGRATION=1
48             ;;
49         -d|--doctests)
50             DOCTEST=1
51             ;;
52         -u|--unittests)
53             UNITTEST=1
54             ;;
55         -i|--integration)
56             INTEGRATION=1
57             ;;
58         *)    # unknown option
59             echo "Usage: $0 [-a]|[-i][-u][-d]"
60             echo
61             echo "Runs tests under ${ROOT}.  Options control which test types:"
62             echo
63             echo "    -a | --all . . . . . . . . . . . . Run all types of tests"
64             echo "    -d | --doctests  . . . . . . . . . Run doctests"
65             echo "    -u | --unittests . . . . . . . . . Run unittests"
66             echo "    -i | --integration . . . . . . . . Run integration tests"
67             echo
68             echo "Argument $key was not recognized."
69             exit 1
70             ;;
71     esac
72     shift
73 done
74
75 if [ ${DOCTEST} -eq 1 ]; then
76     for doctest in $(grep -lR doctest ${ROOT}/*.py); do
77         BASE=$(basename ${doctest})
78         BASE="${BASE} (doctest)"
79         make_header "${BASE}" "${CYAN}"
80         OUT=$( python3 ${doctest} 2>&1 )
81         if [ "$OUT" == "" ]; then
82             echo "OK"
83         else
84             echo -e "${OUT}"
85             FAILURES=$((FAILURES+1))
86         fi
87     done
88 fi
89
90 if [ ${UNITTEST} -eq 1 ]; then
91     for test in $(find ${ROOT} -name "*_test.py" -print); do
92         BASE=$(basename ${test})
93         BASE="${BASE} (unittest)"
94         make_header "${BASE}" "${GREEN}"
95         ${test}
96         if [ $? -ne 0 ]; then
97             FAILURES=$((FAILURES+1))
98         fi
99     done
100 fi
101
102 if [ ${INTEGRATION} -eq 1 ]; then
103     for test in $(find ${ROOT} -name "*_itest.py" -print); do
104         BASE=$(basename ${test})
105         BASE="${BASE} (integration test)"
106         make_header "${BASE}" "${ORANGE}"
107         ${test}
108         if [ $? -ne 0 ]; then
109             FAILURES=$((FAILURES+1))
110         fi
111     done
112 fi
113
114 if [ ${FAILURES} -ne 0 ]; then
115     echo -e "${RED}There were ${FAILURES} failure(s).${NC}"
116 fi