3
3
4
4
# Determine base branch by looking at the upstream. If no upstream is set, fall back to "master".
5
5
detect_base_branch () {
6
- # Try to get the upstream name, e.g. " origin/master" or " origin/main"
6
+ # If there’s an upstream ( e.g. origin/master or origin/main), use that.
7
7
if base=" $( git rev-parse --abbrev-ref --symbolic-full-name @{u} 2> /dev/null) " ; then
8
8
echo " $base "
9
9
else
10
10
echo " master"
11
11
fi
12
12
}
13
13
14
- # Find all directories under start_dir that contain a CMakeLists.txt
14
+ # Find all directories ( under start_dir) that contain a CMakeLists.txt
15
15
find_cmake_subdirs () {
16
16
local start_dir=" $1 "
17
17
find " $start_dir " -type f -name ' CMakeLists.txt' -printf ' %h\n' | sort -u
18
18
}
19
19
20
- # Find all directories under start_dir that contain a __init__.py
20
+ # Find only those Python-package roots that actually have a tests/ subdirectory.
21
+ # In other words, look for “*/tests” and return the parent directory.
21
22
find_python_subdirs () {
22
23
local start_dir=" $1 "
23
- find " $start_dir " -type f -name ' __init__.py ' -printf ' %h\n' | sort -u
24
+ find " $start_dir " -type d -name ' tests ' -printf ' %h\n' | sort -u
24
25
}
25
26
26
27
# Get the list of files changed since the last commit on base branch
@@ -34,15 +35,15 @@ test_cpp_projects() {
34
35
local current_dir
35
36
current_dir=$( pwd)
36
37
37
- # Find every directory that has a CMakeLists.txt
38
+ # All C++ project directories (where CMakeLists.txt lives)
38
39
local all_subdirs
39
40
all_subdirs=$( find_cmake_subdirs .)
40
41
41
- # Which files changed in this PR ( relative to base)
42
+ # Files modified in this PR relative to base
42
43
local modified_files
43
44
modified_files=$( get_modified_files)
44
45
45
- # Filter to only those C++ subdirs where at least one file was modified
46
+ # Filter to only those C++ subdirs in which at least one file was modified
46
47
local modified_subdirs=" "
47
48
for subdir in $all_subdirs ; do
48
49
# strip leading "./" for comparison against git output
@@ -85,14 +86,20 @@ test_cpp_projects() {
85
86
cleanup
86
87
cd " $current_dir "
87
88
89
+ # Safely extract “<number> tests from” (if any) or default to 0
88
90
local cpp_total_tests
89
91
cpp_total_tests=$( grep -oP ' \d+(?= tests from)' " $cpp_test_log " | tail -1 || echo 0)
92
+ cpp_total_tests=" ${cpp_total_tests:- 0} "
93
+
94
+ # Safely extract passed count (if any) or default to 0
90
95
local cpp_passed_tests
91
96
cpp_passed_tests=$( grep -oP ' (?<=\[ *PASSED *\] )\d+' " $cpp_test_log " | tail -1 || echo 0)
92
- local cpp_failed_tests=$(( cpp_total_tests - cpp_passed_tests))
97
+ cpp_passed_tests=" ${cpp_passed_tests:- 0} "
98
+
99
+ local cpp_failed_tests=$(( cpp_total_tests - cpp_passed_tests ))
93
100
94
- total_passed_tests=$(( total_passed_tests + cpp_passed_tests))
95
- total_failed_tests=$(( total_failed_tests + cpp_failed_tests))
101
+ total_passed_tests=$(( total_passed_tests + cpp_passed_tests ))
102
+ total_failed_tests=$(( total_failed_tests + cpp_failed_tests ))
96
103
97
104
echo " C++ Tests summary for $subdir :"
98
105
echo -e " Passed: \e[32m$cpp_passed_tests \e[0m, Failed: \e[31m$cpp_failed_tests \e[0m"
@@ -106,15 +113,15 @@ test_python_projects() {
106
113
local current_dir
107
114
current_dir=$( pwd)
108
115
109
- # Find every directory that has an __init__.py
116
+ # Only pick up directories that actually have a “tests/” folder
110
117
local all_subdirs
111
118
all_subdirs=$( find_python_subdirs .)
112
119
113
120
# Which files changed in this PR (relative to base)
114
121
local modified_files
115
122
modified_files=$( get_modified_files)
116
123
117
- # Filter to only those Python subdirs where at least one file was modified
124
+ # Filter to only those Python-root dirs where at least one file was modified
118
125
local modified_subdirs=" "
119
126
for subdir in $all_subdirs ; do
120
127
local sub=" ${subdir# ./ } "
@@ -140,17 +147,25 @@ test_python_projects() {
140
147
python_test_log=" $current_dir /python_test_$( echo " $subdir " | tr ' /' ' _' ) .log"
141
148
: > " $python_test_log "
142
149
150
+ # Run unittest discovery; any output goes into the log
143
151
python3 -m unittest discover -v 2>&1 | tee -a " $python_test_log "
144
152
cd " $current_dir "
145
153
154
+ # Safely grab “Ran X tests” (default to 0 if none found)
146
155
local python_total_tests
147
156
python_total_tests=$( grep -oP ' (?<=Ran )\d+' " $python_test_log " | head -1 || echo 0)
157
+ python_total_tests=" ${python_total_tests:- 0} "
158
+
159
+ # Count how many “... ok” lines (default to 0)
148
160
local python_passed_tests
149
161
python_passed_tests=$( grep -c ' \.\.\. ok' " $python_test_log " || echo 0)
150
- local python_failed_tests=$(( python_total_tests - python_passed_tests))
162
+ python_passed_tests=" ${python_passed_tests:- 0} "
163
+
164
+ # Compute failures
165
+ local python_failed_tests=$(( python_total_tests - python_passed_tests ))
151
166
152
- total_passed_tests=$(( total_passed_tests + python_passed_tests))
153
- total_failed_tests=$(( total_failed_tests + python_failed_tests))
167
+ total_passed_tests=$(( total_passed_tests + python_passed_tests ))
168
+ total_failed_tests=$(( total_failed_tests + python_failed_tests ))
154
169
155
170
echo " Python Tests summary for $subdir :"
156
171
echo -e " Passed: \e[32m$python_passed_tests \e[0m, Failed: \e[31m$python_failed_tests \e[0m"
0 commit comments