2
2
3
3
set -e
4
4
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
+
5
17
# Find all directories (under start_dir) that contain a CMakeLists.txt
6
18
find_cmake_subdirs () {
7
19
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
11
22
}
12
23
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)
14
25
find_python_subdirs () {
15
26
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
19
28
}
20
29
21
- # Get list of files changed since last commit on master
30
+ # Get list of files changed since last base-branch commit
22
31
get_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
25
35
}
26
36
27
37
test_cpp_projects () {
@@ -32,14 +42,16 @@ test_cpp_projects() {
32
42
local all_subdirs
33
43
all_subdirs=$( find_cmake_subdirs .)
34
44
35
- # Files modified in this PR relative to master
45
+ # Files modified in this PR relative to base branch
36
46
local modified_files
37
47
modified_files=$( get_modified_files)
38
48
39
49
# Filter to only those C++ subdirs in which at least one file was modified
40
50
local modified_subdirs=" "
41
51
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
43
55
modified_subdirs=" $modified_subdirs $subdir "
44
56
fi
45
57
done
@@ -62,15 +74,13 @@ test_cpp_projects() {
62
74
for subdir in $modified_subdirs ; do
63
75
echo -e " \nRunning tests for C++ project at: $subdir "
64
76
cd " $subdir "
65
- mkdir -p build && cd build
66
77
67
- # Ensure cleanup on exit from this block
78
+ mkdir -p build && cd build
68
79
trap cleanup EXIT
69
80
70
- # Redirect cmake/make output to /dev/null
81
+ # hide cmake/make output
71
82
cmake .. 1> /dev/null 2>&1 && make 1> /dev/null 2>&1
72
83
73
- # Run tests and capture output
74
84
cpp_test_log=" $current_dir /cpp_test_$( echo " $subdir " | tr ' /' ' _' ) .log"
75
85
: > " $cpp_test_log "
76
86
ctest --verbose 2>&1 | tee -a " $cpp_test_log "
@@ -79,7 +89,6 @@ test_cpp_projects() {
79
89
cleanup
80
90
cd " $current_dir "
81
91
82
- # Count total and passed tests in this subdir
83
92
local cpp_total_tests
84
93
cpp_total_tests=$( grep -oP ' \d+(?= tests from)' " $cpp_test_log " | tail -1 || echo 0)
85
94
local cpp_passed_tests
@@ -105,14 +114,15 @@ test_python_projects() {
105
114
local all_subdirs
106
115
all_subdirs=$( find_python_subdirs .)
107
116
108
- # Files modified in this PR relative to master
117
+ # Files modified in this PR relative to base branch
109
118
local modified_files
110
119
modified_files=$( get_modified_files)
111
120
112
121
# Filter to only those Python subdirs in which at least one file was modified
113
122
local modified_subdirs=" "
114
123
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
116
126
modified_subdirs=" $modified_subdirs $subdir "
117
127
fi
118
128
done
@@ -137,7 +147,6 @@ test_python_projects() {
137
147
python3 -m unittest discover -v 2>&1 | tee -a " $python_test_log "
138
148
cd " $current_dir "
139
149
140
- # Count total and passed tests
141
150
local python_total_tests
142
151
python_total_tests=$( grep -oP ' (?<=Ran )\d+' " $python_test_log " | head -1 || echo 0)
143
152
local python_passed_tests
@@ -157,7 +166,7 @@ test_python_projects() {
157
166
158
167
main () {
159
168
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 ."
161
170
echo " ---- Python Projects ----"
162
171
test_python_projects
163
172
echo " ---- C++ Projects ----"
@@ -169,7 +178,7 @@ main() {
169
178
case " $1 " in
170
179
-h|--help)
171
180
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 ."
173
182
echo " "
174
183
echo " Options:"
175
184
echo " -h, --help Show this help message and exit"
0 commit comments