33
44# Determine base branch by looking at the upstream. If no upstream is set, fall back to "master".
55detect_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.
77 if base=" $( git rev-parse --abbrev-ref --symbolic-full-name @{u} 2> /dev/null) " ; then
88 echo " $base "
99 else
1010 echo " master"
1111 fi
1212}
1313
14- # Find all directories under start_dir that contain a CMakeLists.txt
14+ # Find all directories ( under start_dir) that contain a CMakeLists.txt
1515find_cmake_subdirs () {
1616 local start_dir=" $1 "
1717 find " $start_dir " -type f -name ' CMakeLists.txt' -printf ' %h\n' | sort -u
1818}
1919
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.
2122find_python_subdirs () {
2223 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
2425}
2526
2627# Get the list of files changed since the last commit on base branch
@@ -34,15 +35,15 @@ test_cpp_projects() {
3435 local current_dir
3536 current_dir=$( pwd)
3637
37- # Find every directory that has a CMakeLists.txt
38+ # All C++ project directories (where CMakeLists.txt lives)
3839 local all_subdirs
3940 all_subdirs=$( find_cmake_subdirs .)
4041
41- # Which files changed in this PR ( relative to base)
42+ # Files modified in this PR relative to base
4243 local modified_files
4344 modified_files=$( get_modified_files)
4445
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
4647 local modified_subdirs=" "
4748 for subdir in $all_subdirs ; do
4849 # strip leading "./" for comparison against git output
@@ -85,14 +86,20 @@ test_cpp_projects() {
8586 cleanup
8687 cd " $current_dir "
8788
89+ # Safely extract “<number> tests from” (if any) or default to 0
8890 local cpp_total_tests
8991 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
9095 local cpp_passed_tests
9196 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 ))
93100
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 ))
96103
97104 echo " C++ Tests summary for $subdir :"
98105 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() {
106113 local current_dir
107114 current_dir=$( pwd)
108115
109- # Find every directory that has an __init__.py
116+ # Only pick up directories that actually have a “tests/” folder
110117 local all_subdirs
111118 all_subdirs=$( find_python_subdirs .)
112119
113120 # Which files changed in this PR (relative to base)
114121 local modified_files
115122 modified_files=$( get_modified_files)
116123
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
118125 local modified_subdirs=" "
119126 for subdir in $all_subdirs ; do
120127 local sub=" ${subdir# ./ } "
@@ -140,17 +147,25 @@ test_python_projects() {
140147 python_test_log=" $current_dir /python_test_$( echo " $subdir " | tr ' /' ' _' ) .log"
141148 : > " $python_test_log "
142149
150+ # Run unittest discovery; any output goes into the log
143151 python3 -m unittest discover -v 2>&1 | tee -a " $python_test_log "
144152 cd " $current_dir "
145153
154+ # Safely grab “Ran X tests” (default to 0 if none found)
146155 local python_total_tests
147156 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)
148160 local python_passed_tests
149161 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 ))
151166
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 ))
154169
155170 echo " Python Tests summary for $subdir :"
156171 echo -e " Passed: \e[32m$python_passed_tests \e[0m, Failed: \e[31m$python_failed_tests \e[0m"
0 commit comments