Michael Thiessen | 09c0e1d0 | 2020-03-23 18:44:50 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Avi Drissman | dfd88085 | 2022-09-15 20:11:09 | [diff] [blame] | 2 | # Copyright 2020 The Chromium Authors |
Michael Thiessen | 09c0e1d0 | 2020-03-23 18:44:50 | [diff] [blame] | 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | """Builds and runs a test by filename. |
| 6 | |
Edman Anjos | ad4625e | 2023-06-06 21:16:49 | [diff] [blame] | 7 | This script finds the appropriate test suites for the specified test files or |
| 8 | directories, builds it, then runs it with the (optionally) specified filter, |
Dan Harrington | 27d104d | 2020-09-08 18:30:14 | [diff] [blame] | 9 | passing any extra args on to the test runner. |
Michael Thiessen | 09c0e1d0 | 2020-03-23 18:44:50 | [diff] [blame] | 10 | |
| 11 | Examples: |
Dan Harrington | 27d104d | 2020-09-08 18:30:14 | [diff] [blame] | 12 | # Run the test target for bit_cast_unittest.cc. Use a custom test filter instead |
| 13 | # of the automatically generated one. |
| 14 | autotest.py -C out/Desktop bit_cast_unittest.cc --gtest_filter=BitCastTest* |
| 15 | |
| 16 | # Find and run UrlUtilitiesUnitTest.java's tests, pass remaining parameters to |
| 17 | # the test binary. |
Michael Thiessen | 09c0e1d0 | 2020-03-23 18:44:50 | [diff] [blame] | 18 | autotest.py -C out/Android UrlUtilitiesUnitTest --fast-local-dev -v |
Dan Harrington | 27d104d | 2020-09-08 18:30:14 | [diff] [blame] | 19 | |
Edman Anjos | ad4625e | 2023-06-06 21:16:49 | [diff] [blame] | 20 | # Run all tests under base/strings. |
Dan Harrington | 27d104d | 2020-09-08 18:30:14 | [diff] [blame] | 21 | autotest.py -C out/foo --run-all base/strings |
| 22 | |
Edman Anjos | ad4625e | 2023-06-06 21:16:49 | [diff] [blame] | 23 | # Run tests in multiple files or directories. |
| 24 | autotest.py -C out/foo base/strings base/pickle_unittest.cc |
| 25 | |
Dan Harrington | 27d104d | 2020-09-08 18:30:14 | [diff] [blame] | 26 | # Run only the test on line 11. Useful when running autotest.py from your text |
| 27 | # editor. |
| 28 | autotest.py -C out/foo --line 11 base/strings/strcat_unittest.cc |
Michael Thiessen | 09c0e1d0 | 2020-03-23 18:44:50 | [diff] [blame] | 29 | """ |
| 30 | |
| 31 | import argparse |
Erik Staab | dbdb3e5d | 2024-08-26 19:23:11 | [diff] [blame] | 32 | import json |
Michael Thiessen | 09c0e1d0 | 2020-03-23 18:44:50 | [diff] [blame] | 33 | import locale |
Michael Thiessen | 09c0e1d0 | 2020-03-23 18:44:50 | [diff] [blame] | 34 | import os |
| 35 | import re |
Andrew Grieve | 911128a | 2023-07-10 19:06:42 | [diff] [blame] | 36 | import shlex |
Michael Thiessen | 09c0e1d0 | 2020-03-23 18:44:50 | [diff] [blame] | 37 | import subprocess |
| 38 | import sys |
Terrence Reilly | eab6dc2 | 2025-06-03 02:45:25 | [diff] [blame] | 39 | import shutil |
Michael Thiessen | 09c0e1d0 | 2020-03-23 18:44:50 | [diff] [blame] | 40 | |
Mario Bianucci | 6b54500 | 2020-12-02 01:33:39 | [diff] [blame] | 41 | from enumimportEnum |
Michael Thiessen | 09c0e1d0 | 2020-03-23 18:44:50 | [diff] [blame] | 42 | from pathlibimportPath |
| 43 | |
Erik Staab | dbdb3e5d | 2024-08-26 19:23:11 | [diff] [blame] | 44 | # Don't write pyc files to the src tree, which show up in version control |
| 45 | # in some environments. |
| 46 | sys.dont_write_bytecode=True |
| 47 | |
Michael Thiessen | 09c0e1d0 | 2020-03-23 18:44:50 | [diff] [blame] | 48 | USE_PYTHON_3= f'This script will only run under python3.' |
| 49 | |
| 50 | SRC_DIR=Path(__file__).parent.parent.resolve() |
Andrew Grieve | 911128a | 2023-07-10 19:06:42 | [diff] [blame] | 51 | sys.path.append(str(SRC_DIR/'build')) |
| 52 | import gn_helpers |
| 53 | |
Peter Wen | 1b84b4b | 2021-03-11 18:12:22 | [diff] [blame] | 54 | sys.path.append(str(SRC_DIR/'build'/'android')) |
| 55 | from pylibimport constants |
| 56 | |
| 57 | DEPOT_TOOLS_DIR= SRC_DIR/'third_party'/'depot_tools' |
Michael Thiessen | 09c0e1d0 | 2020-03-23 18:44:50 | [diff] [blame] | 58 | DEBUG=False |
| 59 | |
Michael Thiessen | f46171e | 2020-03-31 17:29:38 | [diff] [blame] | 60 | # Some test suites use suffixes that would also match non-test-suite targets. |
| 61 | # Those test suites should be manually added here. |
Andrew Grieve | 5370b0a9 | 2023-07-06 21:43:20 | [diff] [blame] | 62 | _TEST_TARGET_ALLOWLIST=[ |
Michael Thiessen | 09c0e1d0 | 2020-03-23 18:44:50 | [diff] [blame] | 63 | |
Dan Harrington | 97d38e8 | 2025-05-21 01:05:30 | [diff] [blame] | 64 | # The tests below this line were output from the ripgrep command just below: |
| 65 | '//ash:ash_pixeltests', |
| 66 | '//build/rust/tests/test_serde_json_lenient:test_serde_json_lenient', |
| 67 | '//chrome/browser/apps/app_service/app_install:app_install_fuzztests', |
| 68 | '//chrome/browser/glic/e2e_test:glic_internal_e2e_interactive_ui_tests', |
| 69 | '//chrome/browser/mac:install_sh_test', |
| 70 | '//chrome/browser/metrics/perf:profile_provider_unittest', |
| 71 | '//chrome/browser/privacy_sandbox/notice:fuzz_tests', |
| 72 | '//chrome/browser/web_applications:web_application_fuzztests', |
| 73 | '//chromecast/media/base:video_plane_controller_test', |
| 74 | '//chromecast/metrics:cast_metrics_unittest', |
Dan Harrington | 97d38e8 | 2025-05-21 01:05:30 | [diff] [blame] | 75 | '//chrome/enterprise_companion:enterprise_companion_integration_tests', |
| 76 | '//chrome/enterprise_companion:enterprise_companion_tests', |
| 77 | '//chrome/installer/gcapi:gcapi_test', |
| 78 | '//chrome/installer/test:upgrade_test', |
| 79 | '//chromeos/ash/components/kiosk/vision:kiosk_vision_unit_tests', |
| 80 | '//chrome/test/android:chrome_public_apk_baseline_profile_generator', |
| 81 | '//chrome/test:unit_tests', |
| 82 | '//clank/javatests:chrome_apk_baseline_profile_generator', |
| 83 | '//clank/javatests:chrome_smoke_test', |
| 84 | '//clank/javatests:monochrome_bundle_smoke_test', |
| 85 | '//clank/javatests:trichrome_chrome_google_bundle_smoke_test', |
| 86 | '//components/chromeos_camera:jpeg_decode_accelerator_unittest', |
| 87 | '//components/exo/wayland:wayland_client_compatibility_tests', |
| 88 | '//components/exo/wayland:wayland_client_tests', |
| 89 | '//components/facilitated_payments/core/validation:pix_code_validator_fuzzer', |
| 90 | '//components/ip_protection:components_ip_protection_fuzztests', |
| 91 | '//components/minidump_uploader:minidump_uploader_test', |
| 92 | '//components/paint_preview/browser:paint_preview_browser_unit_tests', |
| 93 | '//components/paint_preview/common:paint_preview_common_unit_tests', |
| 94 | '//components/paint_preview/renderer:paint_preview_renderer_unit_tests', |
| 95 | '//components/services/paint_preview_compositor:paint_preview_compositor_unit_tests', |
| 96 | '//components/translate/core/language_detection:language_detection_util_fuzztest', |
| 97 | '//components/webcrypto:webcrypto_testing_fuzzer', |
| 98 | '//components/zucchini:zucchini_integration_test', |
| 99 | '//content/test/fuzzer:devtools_protocol_encoding_json_fuzzer', |
| 100 | '//fuchsia_web/runners:cast_runner_integration_tests', |
| 101 | '//fuchsia_web/webengine:web_engine_integration_tests', |
| 102 | '//google_apis/gcm:gcm_unit_tests', |
| 103 | '//gpu:gl_tests', |
| 104 | '//gpu:gpu_benchmark', |
| 105 | '//gpu/vulkan/android:vk_tests', |
| 106 | '//ios/web:ios_web_inttests', |
| 107 | '//ios/web_view:ios_web_view_inttests', |
| 108 | '//media/cdm:aes_decryptor_fuzztests', |
| 109 | '//media/formats:ac3_util_fuzzer', |
| 110 | '//media/gpu/chromeos:image_processor_test', |
| 111 | '//media/gpu/v4l2:v4l2_unittest', |
| 112 | '//media/gpu/vaapi/test/fake_libva_driver:fake_libva_driver_unittest', |
| 113 | '//media/gpu/vaapi:vaapi_unittest', |
| 114 | '//native_client/tests:large_tests', |
| 115 | '//native_client/tests:medium_tests', |
| 116 | '//native_client/tests:small_tests', |
| 117 | '//sandbox/mac:sandbox_mac_fuzztests', |
| 118 | '//sandbox/win:sbox_integration_tests', |
| 119 | '//sandbox/win:sbox_validation_tests', |
| 120 | '//testing/libfuzzer/fuzzers:libyuv_scale_fuzztest', |
| 121 | '//testing/libfuzzer/fuzzers:paint_vector_icon_fuzztest', |
| 122 | '//third_party/blink/renderer/controller:blink_perf_tests', |
| 123 | '//third_party/blink/renderer/core:css_parser_fuzzer', |
| 124 | '//third_party/blink/renderer/core:inspector_ghost_rules_fuzzer', |
| 125 | '//third_party/blink/renderer/platform/loader:unencoded_digest_fuzzer', |
| 126 | '//third_party/crc32c:crc32c_benchmark', |
| 127 | '//third_party/crc32c:crc32c_tests', |
| 128 | '//third_party/dawn/src/dawn/tests/benchmarks:dawn_benchmarks', |
| 129 | '//third_party/highway:highway_tests', |
| 130 | '//third_party/ipcz/src:ipcz_tests', |
| 131 | '//third_party/libaom:av1_encoder_fuzz_test', |
| 132 | '//third_party/libaom:test_libaom', |
| 133 | '//third_party/libvpx:test_libvpx', |
| 134 | '//third_party/libvpx:vp8_encoder_fuzz_test', |
| 135 | '//third_party/libvpx:vp9_encoder_fuzz_test', |
| 136 | '//third_party/libwebp:libwebp_advanced_api_fuzzer', |
| 137 | '//third_party/libwebp:libwebp_animation_api_fuzzer', |
| 138 | '//third_party/libwebp:libwebp_animencoder_fuzzer', |
| 139 | '//third_party/libwebp:libwebp_enc_dec_api_fuzzer', |
| 140 | '//third_party/libwebp:libwebp_huffman_fuzzer', |
| 141 | '//third_party/libwebp:libwebp_mux_demux_api_fuzzer', |
| 142 | '//third_party/libwebp:libwebp_simple_api_fuzzer', |
| 143 | '//third_party/opus:test_opus_api', |
| 144 | '//third_party/opus:test_opus_decode', |
| 145 | '//third_party/opus:test_opus_encode', |
| 146 | '//third_party/opus:test_opus_padding', |
| 147 | '//third_party/pdfium:pdfium_embeddertests', |
| 148 | '//third_party/pffft:pffft_unittest', |
| 149 | '//third_party/rapidhash:rapidhash_fuzztests', |
| 150 | '//ui/ozone:ozone_integration_tests', |
| 151 | ] |
Dan Harrington | 254f542 | 2025-05-21 16:05:19 | [diff] [blame] | 152 | r""" |
Dan Harrington | 97d38e8 | 2025-05-21 01:05:30 | [diff] [blame] | 153 | You can run this command to find test targets that do not match these regexes, |
| 154 | and use it to update _TEST_TARGET_ALLOWLIST. |
| 155 | rg '^(instrumentation_test_runner|test)\("([^"]*)' -o -g'BUILD.gn' -r'$2' -N \ |
| 156 | | rg -v '(_browsertests|_perftests|_wpr_tests|_unittests)$' \ |
| 157 | | rg '^(.*)/BUILD.gn(.*)$' -r'\'//$1$2\',' \ |
| 158 | | sort |
| 159 | |
| 160 | And you can use a command like this to find source_set targets that do match |
| 161 | the test target regex (ideally this is minimal). |
| 162 | rg '^source_set\("([^"]*)' -o -g'BUILD.gn' -r'$1' -N | \ |
| 163 | rg '(_browsertests|_perftests|_wpr_tests|_unittests)$' |
| 164 | """ |
Dan Harrington | 31f4eb58 | 2024-01-24 16:43:47 | [diff] [blame] | 165 | _TEST_TARGET_REGEX= re.compile( |
Dan Harrington | 97d38e8 | 2025-05-21 01:05:30 | [diff] [blame] | 166 | r'(_browsertests|_perftests|_wpr_tests|_unittests)$') |
Andrew Grieve | c2122d27 | 2021-02-10 16:22:29 | [diff] [blame] | 167 | |
Edman Anjos | 5617af5e | 2024-02-01 18:08:18 | [diff] [blame] | 168 | _PREF_MAPPING_FILE_PATTERN= re.escape( |
| 169 | str(Path('components')/'policy'/'test'/'data'/'pref_mapping')+ |
| 170 | r'/')+ r'.*\.json' |
| 171 | |
Edman Anjos | 2a7daff | 2025-03-14 14:38:27 | [diff] [blame] | 172 | TEST_FILE_NAME_REGEX= re.compile( |
| 173 | r'(.*Test\.java)'+ |
| 174 | r'|(.*_[a-z]*test(?:_win|_mac|_linux|_chromeos|_android)?\.cc)'+ r'|('+ |
| 175 | _PREF_MAPPING_FILE_PATTERN+ r')') |
Mario Bianucci | ebea79d | 2020-11-04 17:19:00 | [diff] [blame] | 176 | |
| 177 | # Some tests don't directly include gtest.h and instead include it via gmock.h |
| 178 | # or a test_utils.h file, so make sure these cases are captured. Also include |
| 179 | # files that use <...> for #includes instead of quotes. |
Tushar Agarwal | 9cd8e499 | 2022-05-20 15:03:11 | [diff] [blame] | 180 | GTEST_INCLUDE_REGEX= re.compile( |
| 181 | r'#include.*(gtest|gmock|_test_utils|browser_test)\.h("|>)') |
Dan Harrington | 27d104d | 2020-09-08 18:30:14 | [diff] [blame] | 182 | |
| 183 | |
| 184 | defExitWithMessage(*args): |
| 185 | print(*args, file=sys.stderr) |
| 186 | sys.exit(1) |
| 187 | |
Mario Bianucci | 6b54500 | 2020-12-02 01:33:39 | [diff] [blame] | 188 | |
| 189 | classTestValidity(Enum): |
| 190 | NOT_A_TEST=0# Does not match test file regex. |
| 191 | MAYBE_A_TEST=1# Matches test file regex, but doesn't include gtest files. |
| 192 | VALID_TEST=2# Matches test file regex and includes gtest files. |
| 193 | |
| 194 | |
Terrence Reilly | eab6dc2 | 2025-06-03 02:45:25 | [diff] [blame] | 195 | defFindRemoteCandidates(target): |
| 196 | """Find files using a remote code search utility, if installed.""" |
| 197 | ifnot shutil.which('cs'): |
| 198 | return[] |
| 199 | results=RunCommand([ |
| 200 | 'cs','-l', |
| 201 | # Give the local path to the file, if the file exists. |
| 202 | '--local', |
| 203 | f'file:{target}', |
| 204 | # Restrict our search to Chromium |
| 205 | 'git:chrome-internal/codesearch/chrome/src@main']).splitlines() |
| 206 | exact= set() |
| 207 | close= set() |
| 208 | for filenamein results: |
| 209 | file_validity=IsTestFile(filename) |
| 210 | if file_validityisTestValidity.VALID_TEST: |
| 211 | exact.add(filename) |
| 212 | elif file_validityisTestValidity.MAYBE_A_TEST: |
| 213 | close.add(filename) |
| 214 | return list(exact), list(close) |
| 215 | |
| 216 | |
Dan Harrington | 27d104d | 2020-09-08 18:30:14 | [diff] [blame] | 217 | defIsTestFile(file_path): |
| 218 | ifnot TEST_FILE_NAME_REGEX.match(file_path): |
Mario Bianucci | 6b54500 | 2020-12-02 01:33:39 | [diff] [blame] | 219 | returnTestValidity.NOT_A_TEST |
Dan Harrington | 27d104d | 2020-09-08 18:30:14 | [diff] [blame] | 220 | if file_path.endswith('.cc'): |
| 221 | # Try a bit harder to remove non-test files for c++. Without this, |
| 222 | # 'autotest.py base/' finds non-test files. |
| 223 | try: |
Mario Bianucci | ebea79d | 2020-11-04 17:19:00 | [diff] [blame] | 224 | with open(file_path,'r', encoding='utf-8')as f: |
Dan Harrington | 27d104d | 2020-09-08 18:30:14 | [diff] [blame] | 225 | if GTEST_INCLUDE_REGEX.search(f.read())isnotNone: |
Mario Bianucci | 6b54500 | 2020-12-02 01:33:39 | [diff] [blame] | 226 | returnTestValidity.VALID_TEST |
Dan Harrington | 27d104d | 2020-09-08 18:30:14 | [diff] [blame] | 227 | exceptIOError: |
| 228 | pass |
Mario Bianucci | 6b54500 | 2020-12-02 01:33:39 | [diff] [blame] | 229 | # It may still be a test file, even if it doesn't include a gtest file. |
| 230 | returnTestValidity.MAYBE_A_TEST |
| 231 | returnTestValidity.VALID_TEST |
Dan Harrington | 27d104d | 2020-09-08 18:30:14 | [diff] [blame] | 232 | |
Michael Thiessen | 09c0e1d0 | 2020-03-23 18:44:50 | [diff] [blame] | 233 | |
| 234 | classCommandError(Exception): |
Dan Harrington | 27d104d | 2020-09-08 18:30:14 | [diff] [blame] | 235 | """Exception thrown when a subcommand fails.""" |
Michael Thiessen | 09c0e1d0 | 2020-03-23 18:44:50 | [diff] [blame] | 236 | |
| 237 | def __init__(self, command, return_code, output=None): |
| 238 | Exception.__init__(self) |
| 239 | self.command= command |
| 240 | self.return_code= return_code |
| 241 | self.output= output |
| 242 | |
| 243 | def __str__(self): |
| 244 | message=(f'\n***\nERROR: Error while running command {self.command}' |
| 245 | f'.\nExit status: {self.return_code}\n') |
| 246 | if self.output: |
| 247 | message+= f'Output:\n{self.output}\n' |
| 248 | message+='***' |
| 249 | return message |
| 250 | |
| 251 | |
Dan Harrington | 27d104d | 2020-09-08 18:30:14 | [diff] [blame] | 252 | defStreamCommandOrExit(cmd,**kwargs): |
Michael Thiessen | 09c0e1d0 | 2020-03-23 18:44:50 | [diff] [blame] | 253 | try: |
| 254 | subprocess.check_call(cmd,**kwargs) |
| 255 | except subprocess.CalledProcessErroras e: |
Dan Harrington | 27d104d | 2020-09-08 18:30:14 | [diff] [blame] | 256 | sys.exit(1) |
Michael Thiessen | 09c0e1d0 | 2020-03-23 18:44:50 | [diff] [blame] | 257 | |
| 258 | |
| 259 | defRunCommand(cmd,**kwargs): |
Michael Thiessen | 09c0e1d0 | 2020-03-23 18:44:50 | [diff] [blame] | 260 | try: |
| 261 | # Set an encoding to convert the binary output to a string. |
| 262 | return subprocess.check_output( |
| 263 | cmd,**kwargs, encoding=locale.getpreferredencoding()) |
| 264 | except subprocess.CalledProcessErroras e: |
| 265 | raiseCommandError(e.cmd, e.returncode, e.output)fromNone |
| 266 | |
| 267 | |
Sam Maier | de993044 | 2025-06-20 15:10:15 | [diff] [blame] | 268 | defBuildTestTargets(out_dir, targets, dry_run, quiet): |
Dan Harrington | 27d104d | 2020-09-08 18:30:14 | [diff] [blame] | 269 | """Builds the specified targets with ninja""" |
Andrew Grieve | 911128a | 2023-07-10 19:06:42 | [diff] [blame] | 270 | cmd= gn_helpers.CreateBuildCommand(out_dir)+ targets |
| 271 | print('Building: '+ shlex.join(cmd)) |
Michael Thiessen | 09c0e1d0 | 2020-03-23 18:44:50 | [diff] [blame] | 272 | if(dry_run): |
Michael Thiessen | a8a82f5 | 2020-11-30 18:05:32 | [diff] [blame] | 273 | returnTrue |
Sam Maier | de993044 | 2025-06-20 15:10:15 | [diff] [blame] | 274 | completed_process= subprocess.run(cmd, |
| 275 | capture_output=quiet, |
| 276 | encoding='utf-8') |
| 277 | if completed_process.returncode!=0: |
| 278 | if quiet: |
| 279 | before, _, after= completed_process.stdout.partition('stderr:') |
| 280 | ifnot after: |
| 281 | before, _, after= completed_process.stdout.partition('stdout:') |
| 282 | if after: |
| 283 | print(after) |
| 284 | else: |
| 285 | print(before) |
Dan Harrington | aa2c7ba | 2020-09-16 15:34:24 | [diff] [blame] | 286 | returnFalse |
| 287 | returnTrue |
Michael Thiessen | 09c0e1d0 | 2020-03-23 18:44:50 | [diff] [blame] | 288 | |
| 289 | |
| 290 | defRecursiveMatchFilename(folder, filename): |
| 291 | current_dir= os.path.split(folder)[-1] |
| 292 | if current_dir.startswith('out')or current_dir.startswith('.'): |
Mario Bianucci | 6b54500 | 2020-12-02 01:33:39 | [diff] [blame] | 293 | return[[],[]] |
| 294 | exact=[] |
| 295 | close=[] |
Gary Tong | 21664c0 | 2025-03-06 16:17:17 | [diff] [blame] | 296 | try: |
| 297 | with os.scandir(folder)as it: |
| 298 | for entryin it: |
| 299 | if(entry.is_symlink()): |
| 300 | continue |
| 301 | if(entry.is_file()and filenamein entry.pathand |
| 302 | not os.path.basename(entry.path).startswith('.')): |
| 303 | file_validity=IsTestFile(entry.path) |
| 304 | if file_validityisTestValidity.VALID_TEST: |
| 305 | exact.append(entry.path) |
| 306 | elif file_validityisTestValidity.MAYBE_A_TEST: |
| 307 | close.append(entry.path) |
| 308 | if entry.is_dir(): |
| 309 | # On Windows, junctions are like a symlink that python interprets as a |
| 310 | # directory, leading to exceptions being thrown. We can just catch and |
| 311 | # ignore these exceptions like we would ignore symlinks. |
| 312 | try: |
| 313 | matches=RecursiveMatchFilename(entry.path, filename) |
| 314 | exact+= matches[0] |
| 315 | close+= matches[1] |
| 316 | exceptFileNotFoundErroras e: |
| 317 | if DEBUG: |
| 318 | print(f'Failed to scan directory "{entry}" - junction?') |
| 319 | pass |
| 320 | exceptPermissionError: |
| 321 | print(f'Permission error while scanning {folder}') |
| 322 | |
Mario Bianucci | 6b54500 | 2020-12-02 01:33:39 | [diff] [blame] | 323 | return[exact, close] |
Michael Thiessen | 09c0e1d0 | 2020-03-23 18:44:50 | [diff] [blame] | 324 | |
| 325 | |
Dan Harrington | 27d104d | 2020-09-08 18:30:14 | [diff] [blame] | 326 | defFindTestFilesInDirectory(directory): |
| 327 | test_files=[] |
Mario Bianucci | ebea79d | 2020-11-04 17:19:00 | [diff] [blame] | 328 | if DEBUG: |
| 329 | print('Test files:') |
Peter Wen | 1b84b4b | 2021-03-11 18:12:22 | [diff] [blame] | 330 | for root, _, filesin os.walk(directory): |
Dan Harrington | 27d104d | 2020-09-08 18:30:14 | [diff] [blame] | 331 | for fin files: |
| 332 | path= os.path.join(root, f) |
Mario Bianucci | 6b54500 | 2020-12-02 01:33:39 | [diff] [blame] | 333 | file_validity=IsTestFile(path) |
| 334 | if file_validityisTestValidity.VALID_TEST: |
Mario Bianucci | ebea79d | 2020-11-04 17:19:00 | [diff] [blame] | 335 | if DEBUG: |
| 336 | print(path) |
Dan Harrington | 27d104d | 2020-09-08 18:30:14 | [diff] [blame] | 337 | test_files.append(path) |
Mario Bianucci | 6b54500 | 2020-12-02 01:33:39 | [diff] [blame] | 338 | elif DEBUGand file_validityisTestValidity.MAYBE_A_TEST: |
| 339 | print(path+' matched but doesn\'t include gtest files, skipping.') |
Dan Harrington | 27d104d | 2020-09-08 18:30:14 | [diff] [blame] | 340 | return test_files |
| 341 | |
| 342 | |
Terrence Reilly | eab6dc2 | 2025-06-03 02:45:25 | [diff] [blame] | 343 | defFindMatchingTestFiles(target, remote_search=False): |
Dan Harrington | 27d104d | 2020-09-08 18:30:14 | [diff] [blame] | 344 | # Return early if there's an exact file match. |
| 345 | if os.path.isfile(target): |
Dan Harrington | aa2c7ba | 2020-09-16 15:34:24 | [diff] [blame] | 346 | # If the target is a C++ implementation file, try to guess the test file. |
| 347 | if target.endswith('.cc')or target.endswith('.h'): |
Mario Bianucci | 6b54500 | 2020-12-02 01:33:39 | [diff] [blame] | 348 | target_validity=IsTestFile(target) |
| 349 | if target_validityisTestValidity.VALID_TEST: |
Dan Harrington | aa2c7ba | 2020-09-16 15:34:24 | [diff] [blame] | 350 | return[target] |
| 351 | alternate= f"{target.rsplit('.', 1)[0]}_unittest.cc" |
Mario Bianucci | 6b54500 | 2020-12-02 01:33:39 | [diff] [blame] | 352 | alt_validity=TestValidity.NOT_A_TESTifnot os.path.isfile( |
| 353 | alternate)elseIsTestFile(alternate) |
| 354 | if alt_validityisTestValidity.VALID_TEST: |
| 355 | return[alternate] |
| 356 | |
| 357 | # If neither the target nor its alternative were valid, check if they just |
| 358 | # didn't include the gtest files before deciding to exit. |
| 359 | if target_validityisTestValidity.MAYBE_A_TEST: |
| 360 | return[target] |
| 361 | if alt_validityisTestValidity.MAYBE_A_TEST: |
Dan Harrington | aa2c7ba | 2020-09-16 15:34:24 | [diff] [blame] | 362 | return[alternate] |
| 363 | ExitWithMessage(f"{target} doesn't look like a test file") |
Dan Harrington | 27d104d | 2020-09-08 18:30:14 | [diff] [blame] | 364 | return[target] |
| 365 | # If this is a directory, return all the test files it contains. |
| 366 | if os.path.isdir(target): |
| 367 | files=FindTestFilesInDirectory(target) |
| 368 | ifnot files: |
| 369 | ExitWithMessage('No tests found in directory') |
| 370 | return files |
| 371 | |
Jesse McKenna | 83b6ac1b | 2020-05-07 18:25:38 | [diff] [blame] | 372 | if sys.platform.startswith('win32')and os.path.altsepin target: |
| 373 | # Use backslash as the path separator on Windows to match os.scandir(). |
| 374 | if DEBUG: |
| 375 | print('Replacing '+ os.path.altsep+' with '+ os.path.sep+' in: ' |
| 376 | + target) |
| 377 | target= target.replace(os.path.altsep, os.path.sep) |
Michael Thiessen | 09c0e1d0 | 2020-03-23 18:44:50 | [diff] [blame] | 378 | if DEBUG: |
| 379 | print('Finding files with full path containing: '+ target) |
Mario Bianucci | 6b54500 | 2020-12-02 01:33:39 | [diff] [blame] | 380 | |
Terrence Reilly | eab6dc2 | 2025-06-03 02:45:25 | [diff] [blame] | 381 | if remote_search: |
| 382 | exact, close=FindRemoteCandidates(target) |
| 383 | ifnot exactandnot close: |
| 384 | print('Failed to find remote candidates; searching recursively') |
| 385 | exact, close=RecursiveMatchFilename(SRC_DIR, target) |
| 386 | else: |
| 387 | exact, close=RecursiveMatchFilename(SRC_DIR, target) |
| 388 | |
Michael Thiessen | 09c0e1d0 | 2020-03-23 18:44:50 | [diff] [blame] | 389 | if DEBUG: |
Mario Bianucci | 6b54500 | 2020-12-02 01:33:39 | [diff] [blame] | 390 | if exact: |
| 391 | print('Found exact matching file(s):') |
| 392 | print('\n'.join(exact)) |
| 393 | if close: |
| 394 | print('Found possible matching file(s):') |
| 395 | print('\n'.join(close)) |
| 396 | |
Andrew Grieve | 774439a | 2023-09-06 14:36:10 | [diff] [blame] | 397 | if len(exact)>=1: |
| 398 | # Given "Foo", don't ask to disambiguate ModFoo.java vs Foo.java. |
| 399 | more_exact=[ |
| 400 | pfor pin exactif os.path.basename(p)in(target, f'{target}.java') |
| 401 | ] |
| 402 | if len(more_exact)==1: |
| 403 | test_files= more_exact |
| 404 | else: |
| 405 | test_files= exact |
| 406 | else: |
| 407 | test_files= close |
| 408 | |
Mario Bianucci | 6b54500 | 2020-12-02 01:33:39 | [diff] [blame] | 409 | if len(test_files)>1: |
Andrew Grieve | ecc9b87 | 2023-03-27 21:09:20 | [diff] [blame] | 410 | if len(test_files)<10: |
| 411 | test_files=[HaveUserPickFile(test_files)] |
| 412 | else: |
| 413 | # Arbitrarily capping at 10 results so we don't print the name of every |
| 414 | # file in the repo if the target is poorly specified. |
| 415 | test_files= test_files[:10] |
| 416 | ExitWithMessage(f'Target "{target}" is ambiguous. Matching files: ' |
| 417 | f'{test_files}') |
Mario Bianucci | 6b54500 | 2020-12-02 01:33:39 | [diff] [blame] | 418 | ifnot test_files: |
Dan Harrington | 27d104d | 2020-09-08 18:30:14 | [diff] [blame] | 419 | ExitWithMessage(f'Target "{target}" did not match any files.') |
Mario Bianucci | 6b54500 | 2020-12-02 01:33:39 | [diff] [blame] | 420 | return test_files |
Michael Thiessen | 09c0e1d0 | 2020-03-23 18:44:50 | [diff] [blame] | 421 | |
| 422 | |
Andrew Grieve | ecc9b87 | 2023-03-27 21:09:20 | [diff] [blame] | 423 | defHaveUserPickFile(paths): |
| 424 | paths= sorted(paths, key=lambda p:(len(p), p)) |
| 425 | path_list='\n'.join(f'{i}. {t}'for i, tin enumerate(paths)) |
| 426 | |
| 427 | whileTrue: |
| 428 | user_input= input(f'Please choose the path you mean.\n{path_list}\n') |
| 429 | try: |
| 430 | value= int(user_input) |
| 431 | return paths[value] |
| 432 | except(ValueError,IndexError): |
| 433 | print('Try again') |
| 434 | |
| 435 | |
Dan Harrington | 27d104d | 2020-09-08 18:30:14 | [diff] [blame] | 436 | defHaveUserPickTarget(paths, targets): |
Michael Thiessen | 09c0e1d0 | 2020-03-23 18:44:50 | [diff] [blame] | 437 | # Cap to 10 targets for convenience [0-9]. |
| 438 | targets= targets[:10] |
Dan Harrington | 27d104d | 2020-09-08 18:30:14 | [diff] [blame] | 439 | target_list='\n'.join(f'{i}. {t}'for i, tin enumerate(targets)) |
| 440 | |
| 441 | user_input= input(f'Target "{paths}" is used by multiple test targets.\n'+ |
Svend Larsen | 1c38e216 | 2024-12-20 15:35:08 | [diff] [blame] | 442 | target_list+'\nPlease pick a target by its numeric index' |
| 443 | 'listed below: ') |
Michael Thiessen | 09c0e1d0 | 2020-03-23 18:44:50 | [diff] [blame] | 444 | try: |
Dan Harrington | 27d104d | 2020-09-08 18:30:14 | [diff] [blame] | 445 | value= int(user_input) |
Michael Thiessen | 09c0e1d0 | 2020-03-23 18:44:50 | [diff] [blame] | 446 | return targets[value] |
Dan Harrington | 27d104d | 2020-09-08 18:30:14 | [diff] [blame] | 447 | except(ValueError,IndexError): |
Svend Larsen | 1c38e216 | 2024-12-20 15:35:08 | [diff] [blame] | 448 | print('Value entered was not a numeric index listed above. Trying again.') |
Dan Harrington | 27d104d | 2020-09-08 18:30:14 | [diff] [blame] | 449 | returnHaveUserPickTarget(paths, targets) |
Michael Thiessen | 09c0e1d0 | 2020-03-23 18:44:50 | [diff] [blame] | 450 | |
| 451 | |
Dan Harrington | 27d104d | 2020-09-08 18:30:14 | [diff] [blame] | 452 | # A persistent cache to avoid running gn on repeated runs of autotest. |
| 453 | classTargetCache: |
| 454 | def __init__(self, out_dir): |
Dan Harrington | aa2c7ba | 2020-09-16 15:34:24 | [diff] [blame] | 455 | self.out_dir= out_dir |
Dan Harrington | 27d104d | 2020-09-08 18:30:14 | [diff] [blame] | 456 | self.path= os.path.join(out_dir,'autotest_cache') |
Dan Harrington | aa2c7ba | 2020-09-16 15:34:24 | [diff] [blame] | 457 | self.gold_mtime= self.GetBuildNinjaMtime() |
Dan Harrington | 27d104d | 2020-09-08 18:30:14 | [diff] [blame] | 458 | self.cache={} |
| 459 | try: |
| 460 | mtime, cache= json.load(open(self.path,'r')) |
| 461 | if mtime== self.gold_mtime: |
| 462 | self.cache= cache |
| 463 | exceptException: |
| 464 | pass |
| 465 | |
| 466 | defSave(self): |
| 467 | with open(self.path,'w')as f: |
| 468 | json.dump([self.gold_mtime, self.cache], f) |
| 469 | |
| 470 | defFind(self, test_paths): |
| 471 | key=' '.join(test_paths) |
| 472 | return self.cache.get(key,None) |
| 473 | |
| 474 | defStore(self, test_paths, test_targets): |
| 475 | key=' '.join(test_paths) |
| 476 | self.cache[key]= test_targets |
| 477 | |
Dan Harrington | aa2c7ba | 2020-09-16 15:34:24 | [diff] [blame] | 478 | defGetBuildNinjaMtime(self): |
| 479 | return os.path.getmtime(os.path.join(self.out_dir,'build.ninja')) |
| 480 | |
| 481 | defIsStillValid(self): |
| 482 | return self.GetBuildNinjaMtime()== self.gold_mtime |
| 483 | |
Dan Harrington | 27d104d | 2020-09-08 18:30:14 | [diff] [blame] | 484 | |
Andrew Grieve | 5370b0a9 | 2023-07-06 21:43:20 | [diff] [blame] | 485 | def_TestTargetsFromGnRefs(targets): |
| 486 | # First apply allowlists: |
| 487 | ret=[tfor tin targetsif'__'notin t] |
| 488 | ret=[ |
| 489 | tfor tin ret |
| 490 | if _TEST_TARGET_REGEX.search(t)or tin _TEST_TARGET_ALLOWLIST |
| 491 | ] |
| 492 | if ret: |
| 493 | return ret |
| 494 | |
| 495 | _SUBTARGET_SUFFIXES=( |
| 496 | '__java_binary',# robolectric_binary() |
| 497 | '__test_runner_script',# test() targets |
| 498 | '__test_apk',# instrumentation_test_apk() targets |
| 499 | ) |
| 500 | ret=[] |
| 501 | for suffixin _SUBTARGET_SUFFIXES: |
| 502 | ret.extend(t[:-len(suffix)]for tin targetsif t.endswith(suffix)) |
| 503 | |
| 504 | return ret |
| 505 | |
| 506 | |
Dan Harrington | 27d104d | 2020-09-08 18:30:14 | [diff] [blame] | 507 | defFindTestTargets(target_cache, out_dir, paths, run_all): |
| 508 | # Normalize paths, so they can be cached. |
| 509 | paths=[os.path.realpath(p)for pin paths] |
| 510 | test_targets= target_cache.Find(paths) |
Dan Harrington | aa2c7ba | 2020-09-16 15:34:24 | [diff] [blame] | 511 | used_cache=True |
Dan Harrington | 27d104d | 2020-09-08 18:30:14 | [diff] [blame] | 512 | ifnot test_targets: |
Dan Harrington | aa2c7ba | 2020-09-16 15:34:24 | [diff] [blame] | 513 | used_cache=False |
Dan Harrington | 27d104d | 2020-09-08 18:30:14 | [diff] [blame] | 514 | |
| 515 | # Use gn refs to recursively find all targets that depend on |path|, filter |
| 516 | # internal gn targets, and match against well-known test suffixes, falling |
| 517 | # back to a list of known test targets if that fails. |
Henrique Nakashima | a227aa6d | 2025-05-01 19:26:34 | [diff] [blame] | 518 | gn_path= os.path.join(DEPOT_TOOLS_DIR,'gn.py') |
Dan Harrington | 27d104d | 2020-09-08 18:30:14 | [diff] [blame] | 519 | |
Henrique Nakashima | a227aa6d | 2025-05-01 19:26:34 | [diff] [blame] | 520 | cmd=[sys.executable, gn_path,'refs', out_dir,'--all']+ paths |
Dan Harrington | 27d104d | 2020-09-08 18:30:14 | [diff] [blame] | 521 | targets=RunCommand(cmd).splitlines() |
Andrew Grieve | 5370b0a9 | 2023-07-06 21:43:20 | [diff] [blame] | 522 | test_targets=_TestTargetsFromGnRefs(targets) |
| 523 | |
| 524 | # If not targets were identified as tests by looking at their names, ask GN |
| 525 | # if any are executables. |
| 526 | ifnot test_targetsand targets: |
| 527 | test_targets=RunCommand(cmd+['--type=executable']).splitlines() |
Michael Thiessen | 09c0e1d0 | 2020-03-23 18:44:50 | [diff] [blame] | 528 | |
| 529 | ifnot test_targets: |
Dan Harrington | 27d104d | 2020-09-08 18:30:14 | [diff] [blame] | 530 | ExitWithMessage( |
Andrew Grieve | 5370b0a9 | 2023-07-06 21:43:20 | [diff] [blame] | 531 | f'"{paths}" did not match any test targets. Consider adding' |
| 532 | f' one of the following targets to _TEST_TARGET_ALLOWLIST within ' |
| 533 | f'{__file__}: \n'+'\n'.join(targets)) |
Dan Harrington | 27d104d | 2020-09-08 18:30:14 | [diff] [blame] | 534 | |
Andrew Grieve | f6a1b1e | 2023-09-13 16:26:02 | [diff] [blame] | 535 | test_targets.sort() |
Dan Harrington | 27d104d | 2020-09-08 18:30:14 | [diff] [blame] | 536 | target_cache.Store(paths, test_targets) |
| 537 | target_cache.Save() |
| 538 | |
Michael Thiessen | 09c0e1d0 | 2020-03-23 18:44:50 | [diff] [blame] | 539 | if len(test_targets)>1: |
Dan Harrington | 27d104d | 2020-09-08 18:30:14 | [diff] [blame] | 540 | if run_all: |
| 541 | print(f'Warning, found {len(test_targets)} test targets.', |
| 542 | file=sys.stderr) |
| 543 | if len(test_targets)>10: |
| 544 | ExitWithMessage('Your query likely involves non-test sources.') |
| 545 | print('Trying to run all of them!', file=sys.stderr) |
| 546 | else: |
| 547 | test_targets=[HaveUserPickTarget(paths, test_targets)] |
Michael Thiessen | 09c0e1d0 | 2020-03-23 18:44:50 | [diff] [blame] | 548 | |
Andrew Grieve | f6a1b1e | 2023-09-13 16:26:02 | [diff] [blame] | 549 | # Remove the // prefix to turn GN label into ninja target. |
| 550 | test_targets=[t[2:]for tin test_targets] |
Dan Harrington | 27d104d | 2020-09-08 18:30:14 | [diff] [blame] | 551 | |
Dan Harrington | aa2c7ba | 2020-09-16 15:34:24 | [diff] [blame] | 552 | return(test_targets, used_cache) |
Michael Thiessen | 09c0e1d0 | 2020-03-23 18:44:50 | [diff] [blame] | 553 | |
| 554 | |
Edman Anjos | 5617af5e | 2024-02-01 18:08:18 | [diff] [blame] | 555 | defRunTestTargets(out_dir, targets, gtest_filter, pref_mapping_filter, |
| 556 | extra_args, dry_run, no_try_android_wrappers, |
| 557 | no_fast_local_dev): |
Olivier Li | 8ac87f41 | 2021-05-05 15:26:54 | [diff] [blame] | 558 | |
Dan Harrington | 27d104d | 2020-09-08 18:30:14 | [diff] [blame] | 559 | for targetin targets: |
Andrew Grieve | f6a1b1e | 2023-09-13 16:26:02 | [diff] [blame] | 560 | target_binary= target.split(':')[1] |
Olivier Li | 8ac87f41 | 2021-05-05 15:26:54 | [diff] [blame] | 561 | |
Dan Harrington | 27d104d | 2020-09-08 18:30:14 | [diff] [blame] | 562 | # Look for the Android wrapper script first. |
Andrew Grieve | f6a1b1e | 2023-09-13 16:26:02 | [diff] [blame] | 563 | path= os.path.join(out_dir,'bin', f'run_{target_binary}') |
Olivier Li | 8ac87f41 | 2021-05-05 15:26:54 | [diff] [blame] | 564 | if no_try_android_wrappersornot os.path.isfile(path): |
| 565 | # If the wrapper is not found or disabled use the Desktop target |
| 566 | # which is an executable. |
Andrew Grieve | f6a1b1e | 2023-09-13 16:26:02 | [diff] [blame] | 567 | path= os.path.join(out_dir, target_binary) |
Andrew Grieve | cf072762 | 2022-02-23 16:06:06 | [diff] [blame] | 568 | elifnot no_fast_local_dev: |
| 569 | # Usually want this flag when developing locally. |
| 570 | extra_args= extra_args+['--fast-local-dev'] |
Olivier Li | 8ac87f41 | 2021-05-05 15:26:54 | [diff] [blame] | 571 | |
Edman Anjos | 5617af5e | 2024-02-01 18:08:18 | [diff] [blame] | 572 | cmd=[path, f'--gtest_filter={gtest_filter}'] |
| 573 | if pref_mapping_filter: |
| 574 | cmd.append(f'--test_policy_to_pref_mappings_filter={pref_mapping_filter}') |
| 575 | cmd.extend(extra_args) |
| 576 | |
Andrew Grieve | 911128a | 2023-07-10 19:06:42 | [diff] [blame] | 577 | print('Running test: '+ shlex.join(cmd)) |
Dan Harrington | 27d104d | 2020-09-08 18:30:14 | [diff] [blame] | 578 | ifnot dry_run: |
| 579 | StreamCommandOrExit(cmd) |
| 580 | |
| 581 | |
| 582 | defBuildCppTestFilter(filenames, line): |
Mario Bianucci | ebea79d | 2020-11-04 17:19:00 | [diff] [blame] | 583 | make_filter_command=[ |
Roman Sorokin | 34f5e2a | 2022-02-02 16:31:27 | [diff] [blame] | 584 | sys.executable, SRC_DIR/'tools'/'make_gtest_filter.py' |
Mario Bianucci | ebea79d | 2020-11-04 17:19:00 | [diff] [blame] | 585 | ] |
Dan Harrington | 27d104d | 2020-09-08 18:30:14 | [diff] [blame] | 586 | if line: |
| 587 | make_filter_command+=['--line', str(line)] |
| 588 | else: |
| 589 | make_filter_command+=['--class-only'] |
| 590 | make_filter_command+= filenames |
| 591 | returnRunCommand(make_filter_command).strip() |
| 592 | |
| 593 | |
| 594 | defBuildJavaTestFilter(filenames): |
Michael Thiessen | 7bbda48 | 2020-09-19 02:07:34 | [diff] [blame] | 595 | return':'.join('*.{}*'.format(os.path.splitext(os.path.basename(f))[0]) |
Dan Harrington | 27d104d | 2020-09-08 18:30:14 | [diff] [blame] | 596 | for fin filenames) |
| 597 | |
| 598 | |
Edman Anjos | 5617af5e | 2024-02-01 18:08:18 | [diff] [blame] | 599 | _PREF_MAPPING_GTEST_FILTER='*PolicyPrefsTest.PolicyToPrefsMapping*' |
| 600 | |
| 601 | _PREF_MAPPING_FILE_REGEX= re.compile(_PREF_MAPPING_FILE_PATTERN) |
| 602 | |
| 603 | SPECIAL_TEST_FILTERS=[(_PREF_MAPPING_FILE_REGEX, _PREF_MAPPING_GTEST_FILTER)] |
| 604 | |
| 605 | |
Dan Harrington | 27d104d | 2020-09-08 18:30:14 | [diff] [blame] | 606 | defBuildTestFilter(filenames, line): |
| 607 | java_files=[ffor fin filenamesif f.endswith('.java')] |
| 608 | cc_files=[ffor fin filenamesif f.endswith('.cc')] |
| 609 | filters=[] |
| 610 | if java_files: |
| 611 | filters.append(BuildJavaTestFilter(java_files)) |
| 612 | if cc_files: |
| 613 | filters.append(BuildCppTestFilter(cc_files, line)) |
Edman Anjos | 5617af5e | 2024-02-01 18:08:18 | [diff] [blame] | 614 | for regex, gtest_filterin SPECIAL_TEST_FILTERS: |
| 615 | if any(Truefor fin filenamesif regex.match(f)): |
| 616 | filters.append(gtest_filter) |
| 617 | break |
Dan Harrington | 27d104d | 2020-09-08 18:30:14 | [diff] [blame] | 618 | return':'.join(filters) |
Michael Thiessen | 09c0e1d0 | 2020-03-23 18:44:50 | [diff] [blame] | 619 | |
| 620 | |
Edman Anjos | 5617af5e | 2024-02-01 18:08:18 | [diff] [blame] | 621 | defBuildPrefMappingTestFilter(filenames): |
| 622 | mapping_files=[ffor fin filenamesif _PREF_MAPPING_FILE_REGEX.match(f)] |
| 623 | ifnot mapping_files: |
| 624 | returnNone |
| 625 | names_without_extension=[Path(f).stemfor fin mapping_files] |
| 626 | return':'.join(names_without_extension) |
| 627 | |
| 628 | |
Michael Thiessen | 09c0e1d0 | 2020-03-23 18:44:50 | [diff] [blame] | 629 | def main(): |
| 630 | parser= argparse.ArgumentParser( |
| 631 | description=__doc__, formatter_class=argparse.RawTextHelpFormatter) |
Andrew Grieve | a5193d3a | 2020-09-21 14:58:34 | [diff] [blame] | 632 | parser.add_argument('--out-dir', |
Edman Anjos | 7d319a63e | 2024-01-29 10:58:31 | [diff] [blame] | 633 | '--out_dir', |
Peter Wen | 1b84b4b | 2021-03-11 18:12:22 | [diff] [blame] | 634 | '--output-directory', |
Edman Anjos | 7d319a63e | 2024-01-29 10:58:31 | [diff] [blame] | 635 | '--output_directory', |
Andrew Grieve | a5193d3a | 2020-09-21 14:58:34 | [diff] [blame] | 636 | '-C', |
| 637 | metavar='OUT_DIR', |
| 638 | help='output directory of the build') |
Terrence Reilly | eab6dc2 | 2025-06-03 02:45:25 | [diff] [blame] | 639 | parser.add_argument('--remote-search', |
| 640 | '--remote_search', |
| 641 | '-r', |
| 642 | action='store_true', |
| 643 | help='Search for tests using a remote service') |
Michael Thiessen | 09c0e1d0 | 2020-03-23 18:44:50 | [diff] [blame] | 644 | parser.add_argument( |
Dan Harrington | 27d104d | 2020-09-08 18:30:14 | [diff] [blame] | 645 | '--run-all', |
Edman Anjos | 7d319a63e | 2024-01-29 10:58:31 | [diff] [blame] | 646 | '--run_all', |
Dan Harrington | 27d104d | 2020-09-08 18:30:14 | [diff] [blame] | 647 | action='store_true', |
| 648 | help='Run all tests for the file or directory, instead of just one') |
| 649 | parser.add_argument('--line', |
| 650 | type=int, |
| 651 | help='run only the test on this line number. c++ only.') |
Edman Anjos | 7d319a63e | 2024-01-29 10:58:31 | [diff] [blame] | 652 | parser.add_argument('--gtest-filter', |
| 653 | '--gtest_filter', |
Michael Thiessen | fe328bc | 2022-11-30 02:37:52 | [diff] [blame] | 654 | '-f', |
| 655 | metavar='FILTER', |
| 656 | help='test filter') |
Edman Anjos | 5617af5e | 2024-02-01 18:08:18 | [diff] [blame] | 657 | parser.add_argument('--test-policy-to-pref-mappings-filter', |
| 658 | '--test_policy_to_pref_mappings_filter', |
| 659 | metavar='FILTER', |
| 660 | help='policy pref mappings test filter') |
Michael Thiessen | 09c0e1d0 | 2020-03-23 18:44:50 | [diff] [blame] | 661 | parser.add_argument( |
Dan Harrington | 27d104d | 2020-09-08 18:30:14 | [diff] [blame] | 662 | '--dry-run', |
Edman Anjos | 7d319a63e | 2024-01-29 10:58:31 | [diff] [blame] | 663 | '--dry_run', |
Michael Thiessen | 09c0e1d0 | 2020-03-23 18:44:50 | [diff] [blame] | 664 | '-n', |
| 665 | action='store_true', |
| 666 | help='Print ninja and test run commands without executing them.') |
Olivier Li | 8ac87f41 | 2021-05-05 15:26:54 | [diff] [blame] | 667 | parser.add_argument( |
Sam Maier | de993044 | 2025-06-20 15:10:15 | [diff] [blame] | 668 | '--quiet', |
| 669 | '-q', |
| 670 | action='store_true', |
| 671 | help='Do not print while building, only print if build fails.') |
| 672 | parser.add_argument( |
Olivier Li | 8ac87f41 | 2021-05-05 15:26:54 | [diff] [blame] | 673 | '--no-try-android-wrappers', |
Edman Anjos | 7d319a63e | 2024-01-29 10:58:31 | [diff] [blame] | 674 | '--no_try_android_wrappers', |
Olivier Li | 8ac87f41 | 2021-05-05 15:26:54 | [diff] [blame] | 675 | action='store_true', |
| 676 | help='Do not try to use Android test wrappers to run tests.') |
Andrew Grieve | cf072762 | 2022-02-23 16:06:06 | [diff] [blame] | 677 | parser.add_argument('--no-fast-local-dev', |
Edman Anjos | 7d319a63e | 2024-01-29 10:58:31 | [diff] [blame] | 678 | '--no_fast_local_dev', |
Andrew Grieve | cf072762 | 2022-02-23 16:06:06 | [diff] [blame] | 679 | action='store_true', |
| 680 | help='Do not add --fast-local-dev for Android tests.') |
Edman Anjos | ad4625e | 2023-06-06 21:16:49 | [diff] [blame] | 681 | parser.add_argument('files', |
Dan Harrington | aa2c7ba | 2020-09-16 15:34:24 | [diff] [blame] | 682 | metavar='FILE_NAME', |
Edman Anjos | 7d319a63e | 2024-01-29 10:58:31 | [diff] [blame] | 683 | nargs='+', |
Dan Harrington | aa2c7ba | 2020-09-16 15:34:24 | [diff] [blame] | 684 | help='test suite file (eg. FooTest.java)') |
Michael Thiessen | 09c0e1d0 | 2020-03-23 18:44:50 | [diff] [blame] | 685 | |
| 686 | args, _extras= parser.parse_known_args() |
| 687 | |
Peter Wen | 1b84b4b | 2021-03-11 18:12:22 | [diff] [blame] | 688 | if args.out_dir: |
| 689 | constants.SetOutputDirectory(args.out_dir) |
| 690 | constants.CheckOutputDirectory() |
| 691 | out_dir: str= constants.GetOutDirectory() |
| 692 | |
| 693 | ifnot os.path.isdir(out_dir): |
| 694 | parser.error(f'OUT_DIR "{out_dir}" does not exist.') |
| 695 | target_cache=TargetCache(out_dir) |
Edman Anjos | ad4625e | 2023-06-06 21:16:49 | [diff] [blame] | 696 | filenames=[] |
| 697 | for filein args.files: |
Terrence Reilly | eab6dc2 | 2025-06-03 02:45:25 | [diff] [blame] | 698 | filenames.extend(FindMatchingTestFiles(file, args.remote_search)) |
Dan Harrington | 27d104d | 2020-09-08 18:30:14 | [diff] [blame] | 699 | |
Peter Wen | 1b84b4b | 2021-03-11 18:12:22 | [diff] [blame] | 700 | targets, used_cache=FindTestTargets(target_cache, out_dir, filenames, |
Dan Harrington | aa2c7ba | 2020-09-16 15:34:24 | [diff] [blame] | 701 | args.run_all) |
Michael Thiessen | 09c0e1d0 | 2020-03-23 18:44:50 | [diff] [blame] | 702 | |
| 703 | gtest_filter= args.gtest_filter |
| 704 | ifnot gtest_filter: |
Dan Harrington | 27d104d | 2020-09-08 18:30:14 | [diff] [blame] | 705 | gtest_filter=BuildTestFilter(filenames, args.line) |
Michael Thiessen | 09c0e1d0 | 2020-03-23 18:44:50 | [diff] [blame] | 706 | |
Dan Harrington | 27d104d | 2020-09-08 18:30:14 | [diff] [blame] | 707 | ifnot gtest_filter: |
| 708 | ExitWithMessage('Failed to derive a gtest filter') |
| 709 | |
Edman Anjos | 5617af5e | 2024-02-01 18:08:18 | [diff] [blame] | 710 | pref_mapping_filter= args.test_policy_to_pref_mappings_filter |
| 711 | ifnot pref_mapping_filter: |
| 712 | pref_mapping_filter=BuildPrefMappingTestFilter(filenames) |
| 713 | |
Dan Harrington | 27d104d | 2020-09-08 18:30:14 | [diff] [blame] | 714 | assert targets |
Sam Maier | de993044 | 2025-06-20 15:10:15 | [diff] [blame] | 715 | build_ok=BuildTestTargets(out_dir, targets, args.dry_run, args.quiet) |
Dan Harrington | aa2c7ba | 2020-09-16 15:34:24 | [diff] [blame] | 716 | |
| 717 | # If we used the target cache, it's possible we chose the wrong target because |
| 718 | # a gn file was changed. The build step above will check for gn modifications |
| 719 | # and update build.ninja. Use this opportunity the verify the cache is still |
| 720 | # valid. |
| 721 | if used_cacheandnot target_cache.IsStillValid(): |
Peter Wen | 1b84b4b | 2021-03-11 18:12:22 | [diff] [blame] | 722 | target_cache=TargetCache(out_dir) |
| 723 | new_targets, _=FindTestTargets(target_cache, out_dir, filenames, |
Dan Harrington | aa2c7ba | 2020-09-16 15:34:24 | [diff] [blame] | 724 | args.run_all) |
| 725 | if targets!= new_targets: |
| 726 | # Note that this can happen, for example, if you rename a test target. |
| 727 | print('gn config was changed, trying to build again', file=sys.stderr) |
| 728 | targets= new_targets |
Sam Maier | de993044 | 2025-06-20 15:10:15 | [diff] [blame] | 729 | build_ok=BuildTestTargets(out_dir, targets, args.dry_run, args.quiet) |
Dan Harrington | 8e95b89 | 2021-05-14 21:02:10 | [diff] [blame] | 730 | |
| 731 | ifnot build_ok: sys.exit(1) |
Dan Harrington | aa2c7ba | 2020-09-16 15:34:24 | [diff] [blame] | 732 | |
Edman Anjos | 5617af5e | 2024-02-01 18:08:18 | [diff] [blame] | 733 | RunTestTargets(out_dir, targets, gtest_filter, pref_mapping_filter, _extras, |
| 734 | args.dry_run, args.no_try_android_wrappers, |
| 735 | args.no_fast_local_dev) |
Michael Thiessen | 09c0e1d0 | 2020-03-23 18:44:50 | [diff] [blame] | 736 | |
| 737 | |
| 738 | if __name__=='__main__': |
| 739 | sys.exit(main()) |