summaryrefslogtreecommitdiff
path: root/tests/run_tests.sh
blob: 6e0c30cf1b3c5ddb9ec6da18b2fb3c5f0553be0e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/bin/bash

source /home/scott/bin/color_vars.sh

ROOT=/home/scott/lib/python_modules
DOCTEST=0
UNITTEST=0
INTEGRATION=0
FAILURES=0

dup() {
    if [ $# -ne 2 ]; then
        echo "Usage: dup <string> <count>"
        return
    fi
    local times=$(seq 1 $2)
    for x in ${times}; do
        echo -n "$1"
    done
}

make_header() {
    if [ $# -ne 2 ]; then
        echo "Usage: make_header <required title> <color>"
        return
    fi
    local title="$1"
    local title_len=${#title}
    title_len=$((title_len + 4))
    local width=70
    local left=4
    local right=$(($width-($title_len+$left)))
    local color="$2"
    dup '-' $left
    echo -ne "[ ${color}${title}${NC} ]"
    dup '-' $right
    echo
}


while [[ $# -gt 0 ]]; do
    key="$1"
    case $key in
        -a|--all)
            DOCTEST=1
            UNITTEST=1
            INTEGRATION=1
            ;;
        -d|--doctests)
            DOCTEST=1
            ;;
        -u|--unittests)
            UNITTEST=1
            ;;
        -i|--integration)
            INTEGRATION=1
            ;;
        *)    # unknown option
            echo "Usage: $0 [-a]|[-i][-u][-d]"
            echo
            echo "Runs tests under ${ROOT}.  Options control which test types:"
            echo
            echo "    -a | --all . . . . . . . . . . . . Run all types of tests"
            echo "    -d | --doctests  . . . . . . . . . Run doctests"
            echo "    -u | --unittests . . . . . . . . . Run unittests"
            echo "    -i | --integration . . . . . . . . Run integration tests"
            echo
            echo "Argument $key was not recognized."
            exit 1
            ;;
    esac
    shift
done

if [ ${DOCTEST} -eq 1 ]; then
    for doctest in $(grep -lR doctest ${ROOT}/*.py); do
        BASE=$(basename ${doctest})
        BASE="${BASE} (doctest)"
        make_header "${BASE}" "${CYAN}"
        OUT=$( python3 ${doctest} 2>&1 )
        if [ "$OUT" == "" ]; then
            echo "OK"
        else
            echo -e "${OUT}"
            FAILURES=$((FAILURES+1))
        fi
    done
fi

if [ ${UNITTEST} -eq 1 ]; then
    for test in $(find ${ROOT} -name "*_test.py" -print); do
        BASE=$(basename ${test})
        BASE="${BASE} (unittest)"
        make_header "${BASE}" "${GREEN}"
        ${test}
        if [ $? -ne 0 ]; then
            FAILURES=$((FAILURES+1))
        fi
    done
fi

if [ ${INTEGRATION} -eq 1 ]; then
    for test in $(find ${ROOT} -name "*_itest.py" -print); do
        BASE=$(basename ${test})
        BASE="${BASE} (integration test)"
        make_header "${BASE}" "${ORANGE}"
        ${test}
        if [ $? -ne 0 ]; then
            FAILURES=$((FAILURES+1))
        fi
    done
fi

if [ ${FAILURES} -ne 0 ]; then
    echo -e "${RED}There were ${FAILURES} failure(s).${NC}"
fi