|
1 | 1 | #!/bin/bash
|
2 |
| - |
3 | 2 | set -e
|
4 | 3 |
|
| 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 |
5 | 15 | find_cmake_subdirs() {
|
6 | 16 | 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 |
11 | 18 | }
|
12 | 19 |
|
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 |
27 | 24 | }
|
28 | 25 |
|
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 | +} |
35 | 32 |
|
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 |
38 | 54 |
|
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 |
42 | 59 |
|
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 |
46 | 63 |
|
| 64 | + cleanup() { |
| 65 | + cd "$current_dir" |
| 66 | + rm -rf build |
| 67 | + } |
47 | 68 |
|
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" |
50 | 73 |
|
51 |
| -local cpp_failed_tests=$((cpp_total_tests - cpp_passed_tests)) |
| 74 | + mkdir -p build && cd build |
| 75 | + trap cleanup EXIT |
52 | 76 |
|
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 |
55 | 79 |
|
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" |
59 | 83 |
|
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" |
62 | 87 |
|
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)) |
64 | 93 |
|
| 94 | + total_passed_tests=$((total_passed_tests + cpp_passed_tests)) |
| 95 | + total_failed_tests=$((total_failed_tests + cpp_failed_tests)) |
65 | 96 |
|
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" |
71 | 99 | 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" |
72 | 103 | }
|
73 | 104 |
|
74 | 105 | 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 |
77 | 125 |
|
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 |
80 | 130 |
|
81 | 131 | local total_passed_tests=0
|
82 | 132 | local total_failed_tests=0
|
| 133 | + local python_test_log |
83 | 134 |
|
84 |
| - for subdir in $subdirs; do |
| 135 | + echo "Running tests for modified Python projects:" |
| 136 | + for subdir in $modified_subdirs; do |
85 | 137 | echo -e "\nRunning tests for Python project at: $subdir"
|
86 | 138 | 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 | + |
88 | 143 | python3 -m unittest discover -v 2>&1 | tee -a "$python_test_log"
|
89 | 144 | cd "$current_dir"
|
90 | 145 |
|
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) |
94 | 150 | local python_failed_tests=$((python_total_tests - python_passed_tests))
|
95 | 151 |
|
96 |
| - # add the passed and failed tests to the total |
97 | 152 | total_passed_tests=$((total_passed_tests + python_passed_tests))
|
98 | 153 | total_failed_tests=$((total_failed_tests + python_failed_tests))
|
99 | 154 |
|
100 | 155 | 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" |
102 | 157 | done
|
103 | 158 |
|
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" |
107 | 161 | }
|
108 | 162 |
|
109 |
| - |
110 | 163 | main() {
|
111 | 164 | 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 ----" |
114 | 167 | test_python_projects
|
115 |
| - echo "Running tests for C++ projects" |
| 168 | + echo "---- C++ Projects ----" |
116 | 169 | test_cpp_projects
|
| 170 | + exit 0 |
117 | 171 | fi
|
118 | 172 |
|
119 | 173 | 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 |
140 | 201 | fi
|
141 | 202 |
|
142 | 203 | if [ "$#" -gt 1 ]; then
|
143 | 204 | echo "Too many arguments"
|
| 205 | + echo "Usage: run_tests.sh [OPTION]" |
144 | 206 | exit 1
|
145 | 207 | fi
|
146 |
| - |
147 | 208 | }
|
148 | 209 |
|
149 | 210 | main "$@"
|
150 |
| - |
|
0 commit comments