9623f1b2f1a12dcfebf2a207d36a2ea6faad27ca
[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         FAILED=$( echo "${OUT}" | grep '\*\*\*Test Failed\*\*\*' | wc -l )
82         if [ $FAILED == 0 ]; then
83             echo "OK"
84         else
85             echo -e "${FAILED}"
86             FAILURES=$((FAILURES+1))
87         fi
88     done
89 fi
90
91 if [ ${UNITTEST} -eq 1 ]; then
92     for test in $(find ${ROOT} -name "*_test.py" -print); do
93         BASE=$(basename ${test})
94         BASE="${BASE} (unittest)"
95         make_header "${BASE}" "${GREEN}"
96         ${test}
97         if [ $? -ne 0 ]; then
98             FAILURES=$((FAILURES+1))
99         fi
100     done
101 fi
102
103 if [ ${INTEGRATION} -eq 1 ]; then
104     for test in $(find ${ROOT} -name "*_itest.py" -print); do
105         BASE=$(basename ${test})
106         BASE="${BASE} (integration test)"
107         make_header "${BASE}" "${ORANGE}"
108         ${test}
109         if [ $? -ne 0 ]; then
110             FAILURES=$((FAILURES+1))
111         fi
112     done
113 fi
114
115 if [ ${FAILURES} -ne 0 ]; then
116     if [ ${FAILURES} -eq 1 ]; then
117         echo -e "${RED}There was ${FAILURES} failure.${NC}"
118     else
119         echo -e "${RED}There were ${FAILURES} failures.${NC}"
120     fi
121 else
122     echo -e "${GREEN}Everything looks good.${NC}"
123 fi