|
2 | 2 |
|
3 | 3 | set -e |
4 | 4 |
|
| 5 | +# Find all directories (under start_dir) that contain a CMakeLists.txt |
5 | 6 | find_cmake_subdirs() { |
6 | 7 | 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" |
11 | 11 | } |
12 | 12 |
|
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 | +} |
22 | 20 |
|
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 |
27 | 25 | } |
28 | 26 |
|
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 |
35 | 46 |
|
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 |
38 | 51 |
|
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 |
42 | 55 |
|
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 | + } |
46 | 60 |
|
| 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 |
47 | 66 |
|
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 |
50 | 69 |
|
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 |
52 | 72 |
|
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" |
55 | 77 |
|
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" |
59 | 81 |
|
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)) |
62 | 88 |
|
63 | | -} |
| 89 | + total_passed_tests=$((total_passed_tests + cpp_passed_tests)) |
| 90 | + total_failed_tests=$((total_failed_tests + cpp_failed_tests)) |
64 | 91 |
|
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" |
71 | 94 | 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" |
72 | 98 | } |
73 | 99 |
|
74 | 100 | 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 |
77 | 119 |
|
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 |
80 | 124 |
|
81 | 125 | local total_passed_tests=0 |
82 | 126 | local total_failed_tests=0 |
| 127 | + local python_test_log |
83 | 128 |
|
84 | | - for subdir in $subdirs; do |
| 129 | + echo "Running tests for modified Python projects:" |
| 130 | + for subdir in $modified_subdirs; do |
85 | 131 | echo -e "\nRunning tests for Python project at: $subdir" |
86 | 132 | 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 | + |
88 | 137 | python3 -m unittest discover -v 2>&1 | tee -a "$python_test_log" |
89 | 138 | cd "$current_dir" |
90 | 139 |
|
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) |
94 | 145 | local python_failed_tests=$((python_total_tests - python_passed_tests)) |
95 | 146 |
|
96 | | - # add the passed and failed tests to the total |
97 | 147 | total_passed_tests=$((total_passed_tests + python_passed_tests)) |
98 | 148 | total_failed_tests=$((total_failed_tests + python_failed_tests)) |
99 | 149 |
|
100 | 150 | 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" |
102 | 152 | done |
103 | 153 |
|
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" |
107 | 156 | } |
108 | 157 |
|
109 | | - |
110 | 158 | main() { |
111 | 159 | 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 ----" |
114 | 162 | test_python_projects |
115 | | - echo "Running tests for C++ projects" |
| 163 | + echo "---- C++ Projects ----" |
116 | 164 | test_cpp_projects |
| 165 | + exit 0 |
117 | 166 | fi |
118 | 167 |
|
119 | 168 | 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 |
140 | 196 | fi |
141 | 197 |
|
142 | 198 | if [ "$#" -gt 1 ]; then |
143 | 199 | echo "Too many arguments" |
| 200 | + echo "Usage: run_tests.sh [OPTION]" |
144 | 201 | exit 1 |
145 | 202 | fi |
146 | | - |
147 | 203 | } |
148 | 204 |
|
149 | 205 | main "$@" |
150 | | - |
|
0 commit comments