-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_tests.sh
executable file
·165 lines (129 loc) · 4.81 KB
/
run_tests.sh
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/bin/bash
set -e
find_cmake_subdirs() {
local start_dir="$1"
local cmakedirs=$(find "$start_dir" -type d -exec test -e '{}/CMakeLists.txt' \; -print -prune)
for cmakedir in $cmakedirs; do
echo "$cmakedir"
done
}
test_cpp_projects() {
local subdirs=$(find_cmake_subdirs .)
local current_dir=$(pwd)
local cpp_test_log="$current_dir/cpp_test.log"
: > "$cpp_test_log" # truncate the log file
local total_passed_tests=0
local total_failed_tests=0
cleanup() {
echo "Cleaning up..."
cd ..
rm -rf build
}
for subdir in $subdirs; do
: > "$cpp_test_log" # truncate the log file
echo -e "\nRunning tests for C++ project at: $subdir"
cd "$subdir"
mkdir -p build && cd build
trap cleanup EXIT
cmake .. 1>/dev/null 2>&1 && make 1>/dev/null 2>&1
ctest --verbose 2>&1 | tee -a "$cpp_test_log"
trap - EXIT
cleanup
cd "$current_dir"
# count the number of passed and total tests
cpp_total_tests=$(grep -oP '\d+ tests from' "$cpp_test_log" | tail -1 | awk '{print $1}')
cpp_passed_tests=$(grep -oP '\[\s*PASSED\s*\] \d+' "$cpp_test_log" | tail -1 | awk '{print $4}')
echo "cpp_total_tests: $cpp_total_tests"
echo "cpp_passed_tests: $cpp_passed_tests"
local cpp_failed_tests=$((cpp_total_tests - cpp_passed_tests))
total_passed_tests=$((total_passed_tests + cpp_passed_tests))
total_failed_tests=$((total_failed_tests + cpp_failed_tests))
echo "C++ Tests summary for $subdir:"
echo -e "Passed: \033[32m$cpp_passed_tests\033[0m, Failed: \033[31m$cpp_failed_tests\033[0m"
done
echo "Total C++ Tests summary:"
echo -e "Total Passed: \033[32m$total_passed_tests\033[0m, Total Failed: \033[31m$total_failed_tests\033[0m"
}
find_python_subdirs() {
local start_dir="$1"
local pydirs=$(find "$start_dir" -type d -exec test -e '{}/__init__.py' \; -print -prune)
for pydir in $pydirs; do
echo "$pydir"
done
}
test_python_projects() {
local subdirs=$(find_python_subdirs .)
local current_dir=$(pwd)
local python_test_log="$current_dir/python_test_log"
: > "$python_test_log" # truncate the log file
local total_passed_tests=0
local total_failed_tests=0
for subdir in $subdirs; do
if [ -n "$1" ]; then # if exercise is not empty
if [ $(basename "$subdir") != "$1" ]; then # skip all other exercises
continue
fi
fi
echo -e "\nRunning tests for Python project at: $subdir"
cd "$subdir"
: > "$python_test_log" # truncate the log file
python3 -m unittest discover -v 2>&1 | tee -a "$python_test_log"
cd "$current_dir"
# count the number of passed and total tests
local python_total_tests=$(awk '/Ran [0-9]+ test/ {print $2}' "$python_test_log")
local python_passed_tests=$(grep -o '.*... ok' "$python_test_log" | wc -l | tr -d ' ')
local python_failed_tests=$((python_total_tests - python_passed_tests))
# add the passed and failed tests to the total
total_passed_tests=$((total_passed_tests + python_passed_tests))
total_failed_tests=$((total_failed_tests + python_failed_tests))
echo "Python Tests summary for $subdir:"
echo -e "Passed: \033[32m$python_passed_tests\033[0m, Failed: \033[31m$python_failed_tests\033[0m"
done
echo -e "\nTotal Python Tests summary:"
echo -e "Total Passed: \033[32m$total_passed_tests\033[0m, Total Failed: \033[31m$total_failed_tests\033[0m"
}
main() {
if [ "$#" -gt 2 ]; then
echo "Too many arguments"
exit 1
fi
if [ "$#" -eq 0 ]; then
echo "Running tests for all projects"
echo "Running tests for Python projects"
test_python_projects
echo "Running tests for C++ projects"
test_cpp_projects
fi
if [ "$#" -ge 1 ]; then
if [ "$1" == "-h" ] || [ "$1" == "--help" ]; then
echo "Usage: run_tests.sh [OPTION]"
echo "Run tests for all projects."
echo ""
echo "Options:"
echo " -h, --help Show this help message and exit"
echo " -p, --python Run tests for Python projects"
echo " -c, --cpp Run tests for C++ projects"
exit 0
fi
if [ "$1" == "-p" ] || [ "$1" == "--python" ]; then
local exercise=""
echo "Running tests for Python projects"
for arg in "$@"; do
case $arg in
--exercise=*)
exercise="${arg#*=}"
shift
;;
esac
done
test_python_projects "$exercise"
exit 0
fi
if [ "$1" == "-c" ] || [ "$1" == "--cpp" ]; then
echo "Running tests for C++ projects"
test_cpp_projects
exit 0
fi
fi
}
main "$@"