Skip to content

Commit 7127435

Browse files
authored
Update run_tests.sh
1 parent 606227b commit 7127435

File tree

1 file changed

+143
-88
lines changed

1 file changed

+143
-88
lines changed

run_tests.sh

Lines changed: 143 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -2,149 +2,204 @@
22

33
set -e
44

5+
# Find all directories (under start_dir) that contain a CMakeLists.txt
56
find_cmake_subdirs() {
67
local start_dir="$1"
7-
local cmakedirs=$(find "$start_dir" -type d -exec test -e '{}/CMakeLists.txt' \; -print -prune)
8-
for cmakedir in $cmakedirs; do
9-
echo "$cmakedir"
10-
done
8+
local cmakedirs
9+
cmakedirs=$(find "$start_dir" -type f -name 'CMakeLists.txt' -printf '%h\n' | sort -u)
10+
echo "$cmakedirs"
1111
}
1212

13-
test_cpp_projects() {
14-
local subdirs=$(find_cmake_subdirs .)
15-
local current_dir=$(pwd)
16-
17-
local cpp_test_log="$current_dir/cpp_test.log"
18-
: > "$cpp_test_log" # truncate the log file
19-
20-
local total_passed_tests=0
21-
local total_failed_tests=0
13+
# Find all directories (under start_dir) that contain a __init__.py (i.e., Python packages)
14+
find_python_subdirs() {
15+
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"
19+
}
2220

23-
cleanup() {
24-
echo "Cleaning up..."
25-
cd ..
26-
rm -rf build
21+
# Get list of files changed since last commit on master
22+
get_modified_files() {
23+
# Compare against master branch; adjust "master" to "origin/master" if needed
24+
git diff --name-only master...HEAD
2725
}
2826

29-
for subdir in $subdirs; do
30-
: > "$cpp_test_log" # truncate the log file
31-
echo -e "\nRunning tests for C++ project at: $subdir"
32-
cd "$subdir"
33-
mkdir -p build && cd build
34-
trap cleanup EXIT
27+
test_cpp_projects() {
28+
local current_dir
29+
current_dir=$(pwd)
30+
31+
# All C++ project directories (where CMakeLists.txt lives)
32+
local all_subdirs
33+
all_subdirs=$(find_cmake_subdirs .)
34+
35+
# Files modified in this PR relative to master
36+
local modified_files
37+
modified_files=$(get_modified_files)
38+
39+
# Filter to only those C++ subdirs in which at least one file was modified
40+
local modified_subdirs=""
41+
for subdir in $all_subdirs; do
42+
if echo "$modified_files" | grep -q "^${subdir#./}/"; then
43+
modified_subdirs="$modified_subdirs $subdir"
44+
fi
45+
done
3546

36-
cmake .. 1>/dev/null 2>&1 && make 1>/dev/null 2>&1
37-
ctest --verbose 2>&1 | tee -a "$cpp_test_log"
47+
if [ -z "$modified_subdirs" ]; then
48+
echo "No modified C++ projects to test."
49+
return
50+
fi
3851

39-
trap - EXIT
40-
cleanup
41-
cd "$current_dir"
52+
local total_passed_tests=0
53+
local total_failed_tests=0
54+
local cpp_test_log
4255

43-
# count the number of passed and total tests
44-
cpp_total_tests=$(grep -oP '\d+ tests from' "$cpp_test_log" | tail -1 | awk '{print $1}')
45-
cpp_passed_tests=$(grep -oP '\[\s*PASSED\s*\] \d+' "$cpp_test_log" | tail -1 | awk '{print $4}')
56+
cleanup() {
57+
cd "$current_dir"
58+
rm -rf build
59+
}
4660

61+
echo "Running tests for modified C++ projects:"
62+
for subdir in $modified_subdirs; do
63+
echo -e "\nRunning tests for C++ project at: $subdir"
64+
cd "$subdir"
65+
mkdir -p build && cd build
4766

48-
echo "cpp_total_tests: $cpp_total_tests"
49-
echo "cpp_passed_tests: $cpp_passed_tests"
67+
# Ensure cleanup on exit from this block
68+
trap cleanup EXIT
5069

51-
local cpp_failed_tests=$((cpp_total_tests - cpp_passed_tests))
70+
# Redirect cmake/make output to /dev/null
71+
cmake .. 1>/dev/null 2>&1 && make 1>/dev/null 2>&1
5272

53-
total_passed_tests=$((total_passed_tests + cpp_passed_tests))
54-
total_failed_tests=$((total_failed_tests + cpp_failed_tests))
73+
# Run tests and capture output
74+
cpp_test_log="$current_dir/cpp_test_$(echo "$subdir" | tr '/' '_').log"
75+
: > "$cpp_test_log"
76+
ctest --verbose 2>&1 | tee -a "$cpp_test_log"
5577

56-
echo "C++ Tests summary for $subdir:"
57-
echo -e "Passed: \e[32m$cpp_passed_tests\e[0m, Failed: \e[31m$cpp_failed_tests\e[0m"
58-
done
78+
trap - EXIT
79+
cleanup
80+
cd "$current_dir"
5981

60-
echo "Total C++ Tests summary:"
61-
echo -e "Total Passed: \e[32m$total_passed_tests\e[0m, Total Failed: \e[31m$total_failed_tests\e[0m"
82+
# Count total and passed tests in this subdir
83+
local cpp_total_tests
84+
cpp_total_tests=$(grep -oP '\d+(?= tests from)' "$cpp_test_log" | tail -1 || echo 0)
85+
local cpp_passed_tests
86+
cpp_passed_tests=$(grep -oP '(?<=\[ *PASSED *\] )\d+' "$cpp_test_log" | tail -1 || echo 0)
87+
local cpp_failed_tests=$((cpp_total_tests - cpp_passed_tests))
6288

63-
}
89+
total_passed_tests=$((total_passed_tests + cpp_passed_tests))
90+
total_failed_tests=$((total_failed_tests + cpp_failed_tests))
6491

65-
66-
find_python_subdirs() {
67-
local start_dir="$1"
68-
local pydirs=$(find "$start_dir" -type d -exec test -e '{}/__init__.py' \; -print -prune)
69-
for pydir in $pydirs; do
70-
echo "$pydir"
92+
echo "C++ Tests summary for $subdir:"
93+
echo -e " Passed: \e[32m$cpp_passed_tests\e[0m, Failed: \e[31m$cpp_failed_tests\e[0m"
7194
done
95+
96+
echo -e "\nTotal C++ Tests summary for modified projects:"
97+
echo -e " Total Passed: \e[32m$total_passed_tests\e[0m, Total Failed: \e[31m$total_failed_tests\e[0m"
7298
}
7399

74100
test_python_projects() {
75-
local subdirs=$(find_python_subdirs .)
76-
local current_dir=$(pwd)
101+
local current_dir
102+
current_dir=$(pwd)
103+
104+
# All Python project directories (where __init__.py lives)
105+
local all_subdirs
106+
all_subdirs=$(find_python_subdirs .)
107+
108+
# Files modified in this PR relative to master
109+
local modified_files
110+
modified_files=$(get_modified_files)
111+
112+
# Filter to only those Python subdirs in which at least one file was modified
113+
local modified_subdirs=""
114+
for subdir in $all_subdirs; do
115+
if echo "$modified_files" | grep -q "^${subdir#./}/"; then
116+
modified_subdirs="$modified_subdirs $subdir"
117+
fi
118+
done
77119

78-
local python_test_log="$current_dir/python_test_log"
79-
: > "$python_test_log" # truncate the log file
120+
if [ -z "$modified_subdirs" ]; then
121+
echo "No modified Python projects to test."
122+
return
123+
fi
80124

81125
local total_passed_tests=0
82126
local total_failed_tests=0
127+
local python_test_log
83128

84-
for subdir in $subdirs; do
129+
echo "Running tests for modified Python projects:"
130+
for subdir in $modified_subdirs; do
85131
echo -e "\nRunning tests for Python project at: $subdir"
86132
cd "$subdir"
87-
: > "$python_test_log" # truncate the log file
133+
134+
python_test_log="$current_dir/python_test_$(echo "$subdir" | tr '/' '_').log"
135+
: > "$python_test_log"
136+
88137
python3 -m unittest discover -v 2>&1 | tee -a "$python_test_log"
89138
cd "$current_dir"
90139

91-
# count the number of passed and total tests
92-
local python_total_tests=$(grep -oP 'Ran \K\d+' "$python_test_log")
93-
local python_passed_tests=$(grep -o '.*... ok' "$python_test_log" | wc -l)
140+
# Count total and passed tests
141+
local python_total_tests
142+
python_total_tests=$(grep -oP '(?<=Ran )\d+' "$python_test_log" | head -1 || echo 0)
143+
local python_passed_tests
144+
python_passed_tests=$(grep -c '\.\.\. ok' "$python_test_log" || echo 0)
94145
local python_failed_tests=$((python_total_tests - python_passed_tests))
95146

96-
# add the passed and failed tests to the total
97147
total_passed_tests=$((total_passed_tests + python_passed_tests))
98148
total_failed_tests=$((total_failed_tests + python_failed_tests))
99149

100150
echo "Python Tests summary for $subdir:"
101-
echo -e "Passed: \e[32m$python_passed_tests\e[0m, Failed: \e[31m$python_failed_tests\e[0m"
151+
echo -e " Passed: \e[32m$python_passed_tests\e[0m, Failed: \e[31m$python_failed_tests\e[0m"
102152
done
103153

104-
echo -e "\nTotal Python Tests summary:"
105-
echo -e "Total Passed: \e[32m$total_passed_tests\e[0m, Total Failed: \e[31m$total_failed_tests\e[0m"
106-
154+
echo -e "\nTotal Python Tests summary for modified projects:"
155+
echo -e " Total Passed: \e[32m$total_passed_tests\e[0m, Total Failed: \e[31m$total_failed_tests\e[0m"
107156
}
108157

109-
110158
main() {
111159
if [ "$#" -eq 0 ]; then
112-
echo "Running tests for all projects"
113-
echo "Running tests for Python projects"
160+
echo "Running tests only for projects modified since last master commit."
161+
echo "---- Python Projects ----"
114162
test_python_projects
115-
echo "Running tests for C++ projects"
163+
echo "---- C++ Projects ----"
116164
test_cpp_projects
165+
exit 0
117166
fi
118167

119168
if [ "$#" -eq 1 ]; then
120-
if [ "$1" == "-h" ] || [ "$1" == "--help" ]; then
121-
echo "Usage: run_tests.sh [OPTION]"
122-
echo "Run tests for all projects."
123-
echo ""
124-
echo "Options:"
125-
echo " -h, --help Show this help message and exit"
126-
echo " -p, --python Run tests for Python projects"
127-
echo " -c, --cpp Run tests for C++ projects"
128-
exit 0
129-
fi
130-
if [ "$1" == "-p" ] || [ "$1" == "--python" ]; then
131-
echo "Running tests for Python projects"
132-
test_python_projects
133-
exit 0
134-
fi
135-
if [ "$1" == "-c" ] || [ "$1" == "--cpp" ]; then
136-
echo "Running tests for C++ projects"
137-
test_cpp_projects
138-
exit 0
139-
fi
169+
case "$1" in
170+
-h|--help)
171+
echo "Usage: run_tests.sh [OPTION]"
172+
echo "Run tests for projects modified since last master commit."
173+
echo ""
174+
echo "Options:"
175+
echo " -h, --help Show this help message and exit"
176+
echo " -p, --python Run tests for modified Python projects only"
177+
echo " -c, --cpp Run tests for modified C++ projects only"
178+
exit 0
179+
;;
180+
-p|--python)
181+
echo "Running tests for modified Python projects:"
182+
test_python_projects
183+
exit 0
184+
;;
185+
-c|--cpp)
186+
echo "Running tests for modified C++ projects:"
187+
test_cpp_projects
188+
exit 0
189+
;;
190+
*)
191+
echo "Unknown option: $1"
192+
echo "Usage: run_tests.sh [OPTION]"
193+
exit 1
194+
;;
195+
esac
140196
fi
141197

142198
if [ "$#" -gt 1 ]; then
143199
echo "Too many arguments"
200+
echo "Usage: run_tests.sh [OPTION]"
144201
exit 1
145202
fi
146-
147203
}
148204

149205
main "$@"
150-

0 commit comments

Comments
 (0)