Skip to content

Commit 2a9cb48

Browse files
authored
Merge pull request #7 from djeada/djeada-patch-3
Update run_tests.sh
2 parents 606227b + 9409238 commit 2a9cb48

File tree

2 files changed

+159
-91
lines changed

2 files changed

+159
-91
lines changed

.github/workflows/all-tests.yml

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,24 @@ jobs:
77
runs-on: ubuntu-latest
88

99
steps:
10-
- name: Checkout repository
10+
- name: Checkout repository (full history)
1111
uses: actions/checkout@v2
12+
with:
13+
fetch-depth: 0 # ← fetch all branches and commits
14+
15+
- name: Ensure master/main exist locally
16+
run: |
17+
# Try to fetch both master and main so that one of them is guaranteed to exist
18+
git fetch origin master:master || true
19+
git fetch origin main:main || true
1220
1321
- name: Setup Python
1422
uses: actions/setup-python@v2
1523
with:
1624
python-version: '3.x'
1725

1826
- name: Install CMake and Make
19-
run: sudo apt-get install -y cmake make
27+
run: sudo apt-get update && sudo apt-get install -y cmake make
2028

2129
- name: Run tests
2230
run: |

run_tests.sh

Lines changed: 149 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -1,150 +1,210 @@
11
#!/bin/bash
2-
32
set -e
43

4+
# Determine base branch by looking at the upstream. If no upstream is set, fall back to "master".
5+
detect_base_branch() {
6+
# Try to get the upstream name, e.g. "origin/master" or "origin/main"
7+
if base="$(git rev-parse --abbrev-ref --symbolic-full-name @{u} 2>/dev/null)"; then
8+
echo "$base"
9+
else
10+
echo "master"
11+
fi
12+
}
13+
14+
# Find all directories under start_dir that contain a CMakeLists.txt
515
find_cmake_subdirs() {
616
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
17+
find "$start_dir" -type f -name 'CMakeLists.txt' -printf '%h\n' | sort -u
1118
}
1219

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
22-
23-
cleanup() {
24-
echo "Cleaning up..."
25-
cd ..
26-
rm -rf build
20+
# Find all directories under start_dir that contain a __init__.py
21+
find_python_subdirs() {
22+
local start_dir="$1"
23+
find "$start_dir" -type f -name '__init__.py' -printf '%h\n' | sort -u
2724
}
2825

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
26+
# Get the list of files changed since the last commit on base branch
27+
get_modified_files() {
28+
local base_branch
29+
base_branch="$(detect_base_branch)"
30+
git diff --name-only "$base_branch"...HEAD
31+
}
3532

36-
cmake .. 1>/dev/null 2>&1 && make 1>/dev/null 2>&1
37-
ctest --verbose 2>&1 | tee -a "$cpp_test_log"
33+
test_cpp_projects() {
34+
local current_dir
35+
current_dir=$(pwd)
36+
37+
# Find every directory that has a CMakeLists.txt
38+
local all_subdirs
39+
all_subdirs=$(find_cmake_subdirs .)
40+
41+
# Which files changed in this PR (relative to base)
42+
local modified_files
43+
modified_files=$(get_modified_files)
44+
45+
# Filter to only those C++ subdirs where at least one file was modified
46+
local modified_subdirs=""
47+
for subdir in $all_subdirs; do
48+
# strip leading "./" for comparison against git output
49+
local sub="${subdir#./}"
50+
if echo "$modified_files" | grep -q "^$sub/"; then
51+
modified_subdirs="$modified_subdirs $subdir"
52+
fi
53+
done
3854

39-
trap - EXIT
40-
cleanup
41-
cd "$current_dir"
55+
if [ -z "$modified_subdirs" ]; then
56+
echo "No modified C++ projects to test."
57+
return
58+
fi
4259

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}')
60+
local total_passed_tests=0
61+
local total_failed_tests=0
62+
local cpp_test_log
4663

64+
cleanup() {
65+
cd "$current_dir"
66+
rm -rf build
67+
}
4768

48-
echo "cpp_total_tests: $cpp_total_tests"
49-
echo "cpp_passed_tests: $cpp_passed_tests"
69+
echo "Running tests for modified C++ projects:"
70+
for subdir in $modified_subdirs; do
71+
echo -e "\nRunning tests for C++ project at: $subdir"
72+
cd "$subdir"
5073

51-
local cpp_failed_tests=$((cpp_total_tests - cpp_passed_tests))
74+
mkdir -p build && cd build
75+
trap cleanup EXIT
5276

53-
total_passed_tests=$((total_passed_tests + cpp_passed_tests))
54-
total_failed_tests=$((total_failed_tests + cpp_failed_tests))
77+
# Hide cmake/make output
78+
cmake .. 1>/dev/null 2>&1 && make 1>/dev/null 2>&1
5579

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
80+
cpp_test_log="$current_dir/cpp_test_$(echo "$subdir" | tr '/' '_').log"
81+
: > "$cpp_test_log"
82+
ctest --verbose 2>&1 | tee -a "$cpp_test_log"
5983

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"
84+
trap - EXIT
85+
cleanup
86+
cd "$current_dir"
6287

63-
}
88+
local cpp_total_tests
89+
cpp_total_tests=$(grep -oP '\d+(?= tests from)' "$cpp_test_log" | tail -1 || echo 0)
90+
local cpp_passed_tests
91+
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))
6493

94+
total_passed_tests=$((total_passed_tests + cpp_passed_tests))
95+
total_failed_tests=$((total_failed_tests + cpp_failed_tests))
6596

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"
97+
echo "C++ Tests summary for $subdir:"
98+
echo -e " Passed: \e[32m$cpp_passed_tests\e[0m, Failed: \e[31m$cpp_failed_tests\e[0m"
7199
done
100+
101+
echo -e "\nTotal C++ Tests summary for modified projects:"
102+
echo -e " Total Passed: \e[32m$total_passed_tests\e[0m, Total Failed: \e[31m$total_failed_tests\e[0m"
72103
}
73104

74105
test_python_projects() {
75-
local subdirs=$(find_python_subdirs .)
76-
local current_dir=$(pwd)
106+
local current_dir
107+
current_dir=$(pwd)
108+
109+
# Find every directory that has an __init__.py
110+
local all_subdirs
111+
all_subdirs=$(find_python_subdirs .)
112+
113+
# Which files changed in this PR (relative to base)
114+
local modified_files
115+
modified_files=$(get_modified_files)
116+
117+
# Filter to only those Python subdirs where at least one file was modified
118+
local modified_subdirs=""
119+
for subdir in $all_subdirs; do
120+
local sub="${subdir#./}"
121+
if echo "$modified_files" | grep -q "^$sub/"; then
122+
modified_subdirs="$modified_subdirs $subdir"
123+
fi
124+
done
77125

78-
local python_test_log="$current_dir/python_test_log"
79-
: > "$python_test_log" # truncate the log file
126+
if [ -z "$modified_subdirs" ]; then
127+
echo "No modified Python projects to test."
128+
return
129+
fi
80130

81131
local total_passed_tests=0
82132
local total_failed_tests=0
133+
local python_test_log
83134

84-
for subdir in $subdirs; do
135+
echo "Running tests for modified Python projects:"
136+
for subdir in $modified_subdirs; do
85137
echo -e "\nRunning tests for Python project at: $subdir"
86138
cd "$subdir"
87-
: > "$python_test_log" # truncate the log file
139+
140+
python_test_log="$current_dir/python_test_$(echo "$subdir" | tr '/' '_').log"
141+
: > "$python_test_log"
142+
88143
python3 -m unittest discover -v 2>&1 | tee -a "$python_test_log"
89144
cd "$current_dir"
90145

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)
146+
local python_total_tests
147+
python_total_tests=$(grep -oP '(?<=Ran )\d+' "$python_test_log" | head -1 || echo 0)
148+
local python_passed_tests
149+
python_passed_tests=$(grep -c '\.\.\. ok' "$python_test_log" || echo 0)
94150
local python_failed_tests=$((python_total_tests - python_passed_tests))
95151

96-
# add the passed and failed tests to the total
97152
total_passed_tests=$((total_passed_tests + python_passed_tests))
98153
total_failed_tests=$((total_failed_tests + python_failed_tests))
99154

100155
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"
156+
echo -e " Passed: \e[32m$python_passed_tests\e[0m, Failed: \e[31m$python_failed_tests\e[0m"
102157
done
103158

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-
159+
echo -e "\nTotal Python Tests summary for modified projects:"
160+
echo -e " Total Passed: \e[32m$total_passed_tests\e[0m, Total Failed: \e[31m$total_failed_tests\e[0m"
107161
}
108162

109-
110163
main() {
111164
if [ "$#" -eq 0 ]; then
112-
echo "Running tests for all projects"
113-
echo "Running tests for Python projects"
165+
echo "Running tests only for projects modified since last commit on base branch."
166+
echo "---- Python Projects ----"
114167
test_python_projects
115-
echo "Running tests for C++ projects"
168+
echo "---- C++ Projects ----"
116169
test_cpp_projects
170+
exit 0
117171
fi
118172

119173
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
174+
case "$1" in
175+
-h|--help)
176+
echo "Usage: run_tests.sh [OPTION]"
177+
echo "Run tests for projects modified since base branch."
178+
echo ""
179+
echo "Options:"
180+
echo " -h, --help Show this help message and exit"
181+
echo " -p, --python Run tests for modified Python projects only"
182+
echo " -c, --cpp Run tests for modified C++ projects only"
183+
exit 0
184+
;;
185+
-p|--python)
186+
echo "Running tests for modified Python projects:"
187+
test_python_projects
188+
exit 0
189+
;;
190+
-c|--cpp)
191+
echo "Running tests for modified C++ projects:"
192+
test_cpp_projects
193+
exit 0
194+
;;
195+
*)
196+
echo "Unknown option: $1"
197+
echo "Usage: run_tests.sh [OPTION]"
198+
exit 1
199+
;;
200+
esac
140201
fi
141202

142203
if [ "$#" -gt 1 ]; then
143204
echo "Too many arguments"
205+
echo "Usage: run_tests.sh [OPTION]"
144206
exit 1
145207
fi
146-
147208
}
148209

149210
main "$@"
150-

0 commit comments

Comments
 (0)