22
33set -e
44
5+ # Determine which branch to compare against: master or main
6+ detect_base_branch () {
7+ if git rev-parse --verify master > /dev/null 2>&1 ; then
8+ echo " master"
9+ elif git rev-parse --verify main > /dev/null 2>&1 ; then
10+ echo " main"
11+ else
12+ echo " Error: neither 'master' nor 'main' exists in this repository." >&2
13+ exit 1
14+ fi
15+ }
16+
517# Find all directories (under start_dir) that contain a CMakeLists.txt
618find_cmake_subdirs () {
719 local start_dir=" $1 "
8- local cmakedirs
9- cmakedirs=$( find " $start_dir " -type f -name ' CMakeLists.txt' -printf ' %h\n' | sort -u)
10- echo " $cmakedirs "
20+ # print the parent directory of each CMakeLists.txt, remove duplicates, sort
21+ find " $start_dir " -type f -name ' CMakeLists.txt' -printf ' %h\n' | sort -u
1122}
1223
13- # Find all directories (under start_dir) that contain a __init__.py (i.e., Python packages)
24+ # Find all directories (under start_dir) that contain a __init__.py (Python packages)
1425find_python_subdirs () {
1526 local start_dir=" $1 "
16- local pydirs
17- pydirs=$( find " $start_dir " -type f -name ' __init__.py' -printf ' %h\n' | sort -u)
18- echo " $pydirs "
27+ find " $start_dir " -type f -name ' __init__.py' -printf ' %h\n' | sort -u
1928}
2029
21- # Get list of files changed since last commit on master
30+ # Get list of files changed since last base-branch commit
2231get_modified_files () {
23- # Compare against master branch; adjust "master" to "origin/master" if needed
24- git diff --name-only master...HEAD
32+ local base_branch
33+ base_branch=" $( detect_base_branch) "
34+ git diff --name-only " $base_branch " ...HEAD
2535}
2636
2737test_cpp_projects () {
@@ -32,14 +42,16 @@ test_cpp_projects() {
3242 local all_subdirs
3343 all_subdirs=$( find_cmake_subdirs .)
3444
35- # Files modified in this PR relative to master
45+ # Files modified in this PR relative to base branch
3646 local modified_files
3747 modified_files=$( get_modified_files)
3848
3949 # Filter to only those C++ subdirs in which at least one file was modified
4050 local modified_subdirs=" "
4151 for subdir in $all_subdirs ; do
42- if echo " $modified_files " | grep -q " ^${subdir# ./ } /" ; then
52+ # strip leading ./ if present when comparing to git output
53+ local sub=" ${subdir# ./ } "
54+ if echo " $modified_files " | grep -q " ^$sub /" ; then
4355 modified_subdirs=" $modified_subdirs $subdir "
4456 fi
4557 done
@@ -62,15 +74,13 @@ test_cpp_projects() {
6274 for subdir in $modified_subdirs ; do
6375 echo -e " \nRunning tests for C++ project at: $subdir "
6476 cd " $subdir "
65- mkdir -p build && cd build
6677
67- # Ensure cleanup on exit from this block
78+ mkdir -p build && cd build
6879 trap cleanup EXIT
6980
70- # Redirect cmake/make output to /dev/null
81+ # hide cmake/make output
7182 cmake .. 1> /dev/null 2>&1 && make 1> /dev/null 2>&1
7283
73- # Run tests and capture output
7484 cpp_test_log=" $current_dir /cpp_test_$( echo " $subdir " | tr ' /' ' _' ) .log"
7585 : > " $cpp_test_log "
7686 ctest --verbose 2>&1 | tee -a " $cpp_test_log "
@@ -79,7 +89,6 @@ test_cpp_projects() {
7989 cleanup
8090 cd " $current_dir "
8191
82- # Count total and passed tests in this subdir
8392 local cpp_total_tests
8493 cpp_total_tests=$( grep -oP ' \d+(?= tests from)' " $cpp_test_log " | tail -1 || echo 0)
8594 local cpp_passed_tests
@@ -105,14 +114,15 @@ test_python_projects() {
105114 local all_subdirs
106115 all_subdirs=$( find_python_subdirs .)
107116
108- # Files modified in this PR relative to master
117+ # Files modified in this PR relative to base branch
109118 local modified_files
110119 modified_files=$( get_modified_files)
111120
112121 # Filter to only those Python subdirs in which at least one file was modified
113122 local modified_subdirs=" "
114123 for subdir in $all_subdirs ; do
115- if echo " $modified_files " | grep -q " ^${subdir# ./ } /" ; then
124+ local sub=" ${subdir# ./ } "
125+ if echo " $modified_files " | grep -q " ^$sub /" ; then
116126 modified_subdirs=" $modified_subdirs $subdir "
117127 fi
118128 done
@@ -137,7 +147,6 @@ test_python_projects() {
137147 python3 -m unittest discover -v 2>&1 | tee -a " $python_test_log "
138148 cd " $current_dir "
139149
140- # Count total and passed tests
141150 local python_total_tests
142151 python_total_tests=$( grep -oP ' (?<=Ran )\d+' " $python_test_log " | head -1 || echo 0)
143152 local python_passed_tests
@@ -157,7 +166,7 @@ test_python_projects() {
157166
158167main () {
159168 if [ " $# " -eq 0 ]; then
160- echo " Running tests only for projects modified since last master commit."
169+ echo " Running tests only for projects modified since last commit on base branch ."
161170 echo " ---- Python Projects ----"
162171 test_python_projects
163172 echo " ---- C++ Projects ----"
@@ -169,7 +178,7 @@ main() {
169178 case " $1 " in
170179 -h|--help)
171180 echo " Usage: run_tests.sh [OPTION]"
172- echo " Run tests for projects modified since last master commit ."
181+ echo " Run tests for projects modified since base branch ."
173182 echo " "
174183 echo " Options:"
175184 echo " -h, --help Show this help message and exit"
0 commit comments