Jeff Vander Stoep | cec8bac | 2021-01-15 14:15:37 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | # |
| 3 | # Copyright (C) 2020 The Android Open Source Project |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
Thiébaud Weksteen | 3604b75 | 2021-06-10 14:22:00 +0200 | [diff] [blame] | 16 | """Add or update tests to TEST_MAPPING. |
| 17 | |
| 18 | This script uses Bazel to find reverse dependencies on a crate and generates a |
| 19 | TEST_MAPPING file. It accepts the absolute path to a crate as argument. If no |
| 20 | argument is provided, it assumes the crate is the current directory. |
| 21 | |
| 22 | Usage: |
| 23 | $ . build/envsetup.sh |
| 24 | $ lunch aosp_arm64-eng |
| 25 | $ update_crate_tests.py $ANDROID_BUILD_TOP/external/rust/crates/libc |
| 26 | |
| 27 | This script is automatically called by external_updater. |
| 28 | """ |
| 29 | |
Joel Galenson | 0835244 | 2021-08-20 11:39:48 -0700 | [diff] [blame] | 30 | import argparse |
| 31 | import glob |
Jeff Vander Stoep | cec8bac | 2021-01-15 14:15:37 +0100 | [diff] [blame] | 32 | import json |
| 33 | import os |
| 34 | import platform |
Joel Galenson | 4a08c64 | 2021-10-14 13:25:57 -0700 | [diff] [blame] | 35 | import re |
Jeff Vander Stoep | cec8bac | 2021-01-15 14:15:37 +0100 | [diff] [blame] | 36 | import subprocess |
| 37 | import sys |
Joel Galenson | 4a2a3a8 | 2021-08-20 12:26:37 -0700 | [diff] [blame] | 38 | from datetime import datetime |
Joel Galenson | 0835244 | 2021-08-20 11:39:48 -0700 | [diff] [blame] | 39 | from pathlib import Path |
Jeff Vander Stoep | cec8bac | 2021-01-15 14:15:37 +0100 | [diff] [blame] | 40 | |
Thiébaud Weksteen | 3604b75 | 2021-06-10 14:22:00 +0200 | [diff] [blame] | 41 | # Some tests requires specific options. Consider fixing the upstream crate |
| 42 | # before updating this dictionary. |
| 43 | TEST_OPTIONS = { |
Joel Galenson | 54d6553 | 2021-08-31 14:08:05 -0700 | [diff] [blame] | 44 | "ring_test_tests_digest_tests": [{"test-timeout": "600000"}], |
| 45 | "ring_test_src_lib": [{"test-timeout": "100000"}], |
Joel Galenson | a0d4c5e | 2021-04-06 09:36:47 -0700 | [diff] [blame] | 46 | } |
Thiébaud Weksteen | 3604b75 | 2021-06-10 14:22:00 +0200 | [diff] [blame] | 47 | |
Matthew Maurer | 24758f8 | 2021-12-10 18:51:22 +0000 | [diff] [blame] | 48 | # Groups to add tests to. "presubmit" runs x86_64 device tests+host tests, and |
| 49 | # "presubmit-rust" runs arm64 device tests on physical devices. |
| 50 | TEST_GROUPS = [ |
| 51 | "presubmit", |
| 52 | "presubmit-rust" |
| 53 | ] |
| 54 | |
Thiébaud Weksteen | 3604b75 | 2021-06-10 14:22:00 +0200 | [diff] [blame] | 55 | # Excluded tests. These tests will be ignored by this script. |
| 56 | TEST_EXCLUDE = [ |
Joel Galenson | 54d6553 | 2021-08-31 14:08:05 -0700 | [diff] [blame] | 57 | "ash_test_src_lib", |
| 58 | "ash_test_tests_constant_size_arrays", |
| 59 | "ash_test_tests_display", |
| 60 | "shared_library_test_src_lib", |
Ivan Lozano | 69030b3 | 2022-03-04 14:03:47 -0500 | [diff] [blame] | 61 | "vulkano_test_src_lib", |
| 62 | |
| 63 | # These are helper binaries for aidl_integration_test |
| 64 | # and aren't actually meant to run as individual tests. |
| 65 | "aidl_test_rust_client", |
| 66 | "aidl_test_rust_service", |
| 67 | "aidl_test_rust_service_async", |
| 68 | |
David LeGare | d3bbf8c | 2022-03-15 16:01:49 +0000 | [diff] [blame] | 69 | # This is a helper binary for AuthFsHostTest and shouldn't |
| 70 | # be run directly. |
| 71 | "open_then_run", |
| 72 | |
Ivan Lozano | 69030b3 | 2022-03-04 14:03:47 -0500 | [diff] [blame] | 73 | # TODO: Remove when b/198197213 is closed. |
| 74 | "diced_client_test", |
Thiébaud Weksteen | 3604b75 | 2021-06-10 14:22:00 +0200 | [diff] [blame] | 75 | ] |
| 76 | |
| 77 | # Excluded modules. |
| 78 | EXCLUDE_PATHS = [ |
Jeff Vander Stoep | 0b0e24f | 2021-01-24 20:50:26 +0100 | [diff] [blame] | 79 | "//external/adhd", |
| 80 | "//external/crosvm", |
| 81 | "//external/libchromeos-rs", |
| 82 | "//external/vm_tools" |
Thiébaud Weksteen | 3604b75 | 2021-06-10 14:22:00 +0200 | [diff] [blame] | 83 | ] |
Jeff Vander Stoep | 0b0e24f | 2021-01-24 20:50:26 +0100 | [diff] [blame] | 84 | |
Joel Galenson | 4a08c64 | 2021-10-14 13:25:57 -0700 | [diff] [blame] | 85 | LABEL_PAT = re.compile('^//(.*):.*$') |
| 86 | EXTERNAL_PAT = re.compile('^//external/rust/') |
| 87 | |
Thiébaud Weksteen | 5212f8a | 2021-06-10 08:18:32 +0200 | [diff] [blame] | 88 | |
| 89 | class UpdaterException(Exception): |
Thiébaud Weksteen | 3604b75 | 2021-06-10 14:22:00 +0200 | [diff] [blame] | 90 | """Exception generated by this script.""" |
Thiébaud Weksteen | 5212f8a | 2021-06-10 08:18:32 +0200 | [diff] [blame] | 91 | |
| 92 | |
Jeff Vander Stoep | cec8bac | 2021-01-15 14:15:37 +0100 | [diff] [blame] | 93 | class Env(object): |
Thiébaud Weksteen | 3604b75 | 2021-06-10 14:22:00 +0200 | [diff] [blame] | 94 | """Env captures the execution environment. |
| 95 | |
| 96 | It ensures this script is executed within an AOSP repository. |
| 97 | |
| 98 | Attributes: |
| 99 | ANDROID_BUILD_TOP: A string representing the absolute path to the top |
| 100 | of the repository. |
| 101 | """ |
Thiébaud Weksteen | fc485b2 | 2021-06-10 13:30:20 +0200 | [diff] [blame] | 102 | def __init__(self): |
Jeff Vander Stoep | cec8bac | 2021-01-15 14:15:37 +0100 | [diff] [blame] | 103 | try: |
| 104 | self.ANDROID_BUILD_TOP = os.environ['ANDROID_BUILD_TOP'] |
Thiébaud Weksteen | 5212f8a | 2021-06-10 08:18:32 +0200 | [diff] [blame] | 105 | except KeyError: |
| 106 | raise UpdaterException('$ANDROID_BUILD_TOP is not defined; you ' |
| 107 | 'must first source build/envsetup.sh and ' |
| 108 | 'select a target.') |
Thiébaud Weksteen | 5212f8a | 2021-06-10 08:18:32 +0200 | [diff] [blame] | 109 | |
Jeff Vander Stoep | cec8bac | 2021-01-15 14:15:37 +0100 | [diff] [blame] | 110 | |
| 111 | class Bazel(object): |
Thiébaud Weksteen | 3604b75 | 2021-06-10 14:22:00 +0200 | [diff] [blame] | 112 | """Bazel wrapper. |
| 113 | |
| 114 | The wrapper is used to call bazel queryview and generate the list of |
| 115 | reverse dependencies. |
| 116 | |
| 117 | Attributes: |
| 118 | path: The path to the bazel executable. |
| 119 | """ |
Jeff Vander Stoep | cec8bac | 2021-01-15 14:15:37 +0100 | [diff] [blame] | 120 | def __init__(self, env): |
Thiébaud Weksteen | 3604b75 | 2021-06-10 14:22:00 +0200 | [diff] [blame] | 121 | """Constructor. |
| 122 | |
| 123 | Note that the current directory is changed to ANDROID_BUILD_TOP. |
| 124 | |
| 125 | Args: |
| 126 | env: An instance of Env. |
| 127 | |
| 128 | Raises: |
| 129 | UpdaterException: an error occurred while calling soong_ui. |
| 130 | """ |
Thiébaud Weksteen | 3e32afc | 2021-06-10 07:56:06 +0200 | [diff] [blame] | 131 | if platform.system() != 'Linux': |
Thiébaud Weksteen | 5212f8a | 2021-06-10 08:18:32 +0200 | [diff] [blame] | 132 | raise UpdaterException('This script has only been tested on Linux.') |
Thiébaud Weksteen | 3e32afc | 2021-06-10 07:56:06 +0200 | [diff] [blame] | 133 | self.path = os.path.join(env.ANDROID_BUILD_TOP, "tools", "bazel") |
Thiébaud Weksteen | df132d6 | 2021-06-10 08:45:37 +0200 | [diff] [blame] | 134 | soong_ui = os.path.join(env.ANDROID_BUILD_TOP, "build", "soong", "soong_ui.bash") |
Thiébaud Weksteen | fc485b2 | 2021-06-10 13:30:20 +0200 | [diff] [blame] | 135 | |
| 136 | # soong_ui requires to be at the root of the repository. |
Jeff Vander Stoep | cec8bac | 2021-01-15 14:15:37 +0100 | [diff] [blame] | 137 | os.chdir(env.ANDROID_BUILD_TOP) |
Thiébaud Weksteen | df132d6 | 2021-06-10 08:45:37 +0200 | [diff] [blame] | 138 | print("Generating Bazel files...") |
Lukacs T. Berki | 9206f1c | 2021-09-02 17:51:13 +0200 | [diff] [blame] | 139 | cmd = [soong_ui, "--make-mode", "bp2build"] |
Jeff Vander Stoep | 1b24dc3 | 2021-02-03 18:52:42 +0100 | [diff] [blame] | 140 | try: |
Thiébaud Weksteen | df132d6 | 2021-06-10 08:45:37 +0200 | [diff] [blame] | 141 | subprocess.check_output(cmd, stderr=subprocess.STDOUT, text=True) |
| 142 | except subprocess.CalledProcessError as e: |
| 143 | raise UpdaterException('Unable to generate bazel workspace: ' + e.output) |
| 144 | |
| 145 | print("Building Bazel Queryview. This can take a couple of minutes...") |
| 146 | cmd = [soong_ui, "--build-mode", "--all-modules", "--dir=.", "queryview"] |
| 147 | try: |
| 148 | subprocess.check_output(cmd, stderr=subprocess.STDOUT, text=True) |
Jeff Vander Stoep | 1b24dc3 | 2021-02-03 18:52:42 +0100 | [diff] [blame] | 149 | except subprocess.CalledProcessError as e: |
Thiébaud Weksteen | 5212f8a | 2021-06-10 08:18:32 +0200 | [diff] [blame] | 150 | raise UpdaterException('Unable to update TEST_MAPPING: ' + e.output) |
Jeff Vander Stoep | cec8bac | 2021-01-15 14:15:37 +0100 | [diff] [blame] | 151 | |
Jeff Vander Stoep | cec8bac | 2021-01-15 14:15:37 +0100 | [diff] [blame] | 152 | def query_modules(self, path): |
Thiébaud Weksteen | 3604b75 | 2021-06-10 14:22:00 +0200 | [diff] [blame] | 153 | """Returns all modules for a given path.""" |
Thiébaud Weksteen | 3e32afc | 2021-06-10 07:56:06 +0200 | [diff] [blame] | 154 | cmd = self.path + " query --config=queryview /" + path + ":all" |
Thiébaud Weksteen | 76c4e23 | 2021-06-10 07:35:19 +0200 | [diff] [blame] | 155 | out = subprocess.check_output(cmd, shell=True, stderr=subprocess.DEVNULL, text=True).strip().split("\n") |
| 156 | modules = set() |
| 157 | for line in out: |
| 158 | # speed up by excluding unused modules. |
| 159 | if "windows_x86" in line: |
| 160 | continue |
| 161 | modules.add(line) |
| 162 | return modules |
Jeff Vander Stoep | cec8bac | 2021-01-15 14:15:37 +0100 | [diff] [blame] | 163 | |
Jeff Vander Stoep | cec8bac | 2021-01-15 14:15:37 +0100 | [diff] [blame] | 164 | def query_rdeps(self, module): |
Thiébaud Weksteen | 3604b75 | 2021-06-10 14:22:00 +0200 | [diff] [blame] | 165 | """Returns all reverse dependencies for a single module.""" |
Thiébaud Weksteen | 3e32afc | 2021-06-10 07:56:06 +0200 | [diff] [blame] | 166 | cmd = (self.path + " query --config=queryview \'rdeps(//..., " + |
Thiébaud Weksteen | 76c4e23 | 2021-06-10 07:35:19 +0200 | [diff] [blame] | 167 | module + ")\' --output=label_kind") |
| 168 | out = (subprocess.check_output(cmd, shell=True, stderr=subprocess.DEVNULL, text=True) |
| 169 | .strip().split("\n")) |
| 170 | if '' in out: |
| 171 | out.remove('') |
| 172 | return out |
Jeff Vander Stoep | cec8bac | 2021-01-15 14:15:37 +0100 | [diff] [blame] | 173 | |
Jeff Vander Stoep | 0b0e24f | 2021-01-24 20:50:26 +0100 | [diff] [blame] | 174 | def exclude_module(self, module): |
Thiébaud Weksteen | 3604b75 | 2021-06-10 14:22:00 +0200 | [diff] [blame] | 175 | for path in EXCLUDE_PATHS: |
Jeff Vander Stoep | 0b0e24f | 2021-01-24 20:50:26 +0100 | [diff] [blame] | 176 | if module.startswith(path): |
| 177 | return True |
| 178 | return False |
| 179 | |
Joel Galenson | 4a08c64 | 2021-10-14 13:25:57 -0700 | [diff] [blame] | 180 | def query_rdep_tests_dirs(self, modules, path): |
Thiébaud Weksteen | 3604b75 | 2021-06-10 14:22:00 +0200 | [diff] [blame] | 181 | """Returns all reverse dependency tests for modules in this package.""" |
Jeff Vander Stoep | cec8bac | 2021-01-15 14:15:37 +0100 | [diff] [blame] | 182 | rdep_tests = set() |
Joel Galenson | 4a08c64 | 2021-10-14 13:25:57 -0700 | [diff] [blame] | 183 | rdep_dirs = set() |
| 184 | path_pat = re.compile("^/%s:.*$" % path) |
Jeff Vander Stoep | cec8bac | 2021-01-15 14:15:37 +0100 | [diff] [blame] | 185 | for module in modules: |
| 186 | for rdep in self.query_rdeps(module): |
Thiébaud Weksteen | 2e532bb | 2021-06-10 09:01:34 +0200 | [diff] [blame] | 187 | rule_type, _, mod = rdep.split(" ") |
Jeff Vander Stoep | cec8bac | 2021-01-15 14:15:37 +0100 | [diff] [blame] | 188 | if rule_type == "rust_test_" or rule_type == "rust_test": |
Joel Galenson | 4a08c64 | 2021-10-14 13:25:57 -0700 | [diff] [blame] | 189 | if self.exclude_module(mod): |
| 190 | continue |
| 191 | path_match = path_pat.match(mod) |
| 192 | if path_match or not EXTERNAL_PAT.match(mod): |
Jeff Vander Stoep | 0b0e24f | 2021-01-24 20:50:26 +0100 | [diff] [blame] | 193 | rdep_tests.add(mod.split(":")[1].split("--")[0]) |
Joel Galenson | 4a08c64 | 2021-10-14 13:25:57 -0700 | [diff] [blame] | 194 | else: |
| 195 | label_match = LABEL_PAT.match(mod) |
| 196 | if label_match: |
| 197 | rdep_dirs.add(label_match.group(1)) |
| 198 | return (rdep_tests, rdep_dirs) |
Jeff Vander Stoep | cec8bac | 2021-01-15 14:15:37 +0100 | [diff] [blame] | 199 | |
| 200 | |
Thiébaud Weksteen | 2e532bb | 2021-06-10 09:01:34 +0200 | [diff] [blame] | 201 | class Package(object): |
Thiébaud Weksteen | 3604b75 | 2021-06-10 14:22:00 +0200 | [diff] [blame] | 202 | """A Bazel package. |
| 203 | |
| 204 | Attributes: |
| 205 | dir: The absolute path to this package. |
| 206 | dir_rel: The relative path to this package. |
| 207 | rdep_tests: The list of computed reverse dependencies. |
Joel Galenson | 4a08c64 | 2021-10-14 13:25:57 -0700 | [diff] [blame] | 208 | rdep_dirs: The list of computed reverse dependency directories. |
Thiébaud Weksteen | 3604b75 | 2021-06-10 14:22:00 +0200 | [diff] [blame] | 209 | """ |
Thiébaud Weksteen | fc485b2 | 2021-06-10 13:30:20 +0200 | [diff] [blame] | 210 | def __init__(self, path, env, bazel): |
Thiébaud Weksteen | 3604b75 | 2021-06-10 14:22:00 +0200 | [diff] [blame] | 211 | """Constructor. |
| 212 | |
| 213 | Note that the current directory is changed to the package location when |
| 214 | called. |
| 215 | |
| 216 | Args: |
| 217 | path: Path to the package. |
| 218 | env: An instance of Env. |
| 219 | bazel: An instance of Bazel. |
| 220 | |
| 221 | Raises: |
| 222 | UpdaterException: the package does not appear to belong to the |
| 223 | current repository. |
| 224 | """ |
Joel Galenson | 1779104 | 2021-06-17 14:59:15 -0700 | [diff] [blame] | 225 | self.dir = path |
Thiébaud Weksteen | fc485b2 | 2021-06-10 13:30:20 +0200 | [diff] [blame] | 226 | try: |
| 227 | self.dir_rel = self.dir.split(env.ANDROID_BUILD_TOP)[1] |
| 228 | except IndexError: |
| 229 | raise UpdaterException('The path ' + self.dir + ' is not under ' + |
| 230 | env.ANDROID_BUILD_TOP + '; You must be in the ' |
| 231 | 'directory of a crate or pass its absolute path ' |
Joel Galenson | 1779104 | 2021-06-17 14:59:15 -0700 | [diff] [blame] | 232 | 'as the argument.') |
Thiébaud Weksteen | fc485b2 | 2021-06-10 13:30:20 +0200 | [diff] [blame] | 233 | |
| 234 | # Move to the package_directory. |
| 235 | os.chdir(self.dir) |
| 236 | modules = bazel.query_modules(self.dir_rel) |
Joel Galenson | 4a08c64 | 2021-10-14 13:25:57 -0700 | [diff] [blame] | 237 | (self.rdep_tests, self.rdep_dirs) = bazel.query_rdep_tests_dirs(modules, self.dir_rel) |
Jeff Vander Stoep | cec8bac | 2021-01-15 14:15:37 +0100 | [diff] [blame] | 238 | |
Joel Galenson | 4a08c64 | 2021-10-14 13:25:57 -0700 | [diff] [blame] | 239 | def get_rdep_tests_dirs(self): |
| 240 | return (self.rdep_tests, self.rdep_dirs) |
Jeff Vander Stoep | cec8bac | 2021-01-15 14:15:37 +0100 | [diff] [blame] | 241 | |
| 242 | |
| 243 | class TestMapping(object): |
Thiébaud Weksteen | 3604b75 | 2021-06-10 14:22:00 +0200 | [diff] [blame] | 244 | """A TEST_MAPPING file. |
| 245 | |
| 246 | Attributes: |
| 247 | package: The package associated with this TEST_MAPPING file. |
| 248 | """ |
Joel Galenson | 1779104 | 2021-06-17 14:59:15 -0700 | [diff] [blame] | 249 | def __init__(self, env, bazel, path): |
Thiébaud Weksteen | 3604b75 | 2021-06-10 14:22:00 +0200 | [diff] [blame] | 250 | """Constructor. |
| 251 | |
| 252 | Args: |
Joel Galenson | 1779104 | 2021-06-17 14:59:15 -0700 | [diff] [blame] | 253 | env: An instance of Env. |
| 254 | bazel: An instance of Bazel. |
Thiébaud Weksteen | 3604b75 | 2021-06-10 14:22:00 +0200 | [diff] [blame] | 255 | path: The absolute path to the package. |
| 256 | """ |
Thiébaud Weksteen | fc485b2 | 2021-06-10 13:30:20 +0200 | [diff] [blame] | 257 | self.package = Package(path, env, bazel) |
Jeff Vander Stoep | cec8bac | 2021-01-15 14:15:37 +0100 | [diff] [blame] | 258 | |
Thiébaud Weksteen | 2e532bb | 2021-06-10 09:01:34 +0200 | [diff] [blame] | 259 | def create(self): |
Thiébaud Weksteen | 3604b75 | 2021-06-10 14:22:00 +0200 | [diff] [blame] | 260 | """Generates the TEST_MAPPING file.""" |
Joel Galenson | 4a08c64 | 2021-10-14 13:25:57 -0700 | [diff] [blame] | 261 | (tests, dirs) = self.package.get_rdep_tests_dirs() |
| 262 | if not bool(tests) and not bool(dirs): |
| 263 | if os.path.isfile('TEST_MAPPING'): |
| 264 | os.remove('TEST_MAPPING') |
Jeff Vander Stoep | cec8bac | 2021-01-15 14:15:37 +0100 | [diff] [blame] | 265 | return |
Joel Galenson | 4a08c64 | 2021-10-14 13:25:57 -0700 | [diff] [blame] | 266 | test_mapping = self.tests_dirs_to_mapping(tests, dirs) |
Jeff Vander Stoep | cec8bac | 2021-01-15 14:15:37 +0100 | [diff] [blame] | 267 | self.write_test_mapping(test_mapping) |
| 268 | |
Joel Galenson | 4a08c64 | 2021-10-14 13:25:57 -0700 | [diff] [blame] | 269 | def tests_dirs_to_mapping(self, tests, dirs): |
Thiébaud Weksteen | 3604b75 | 2021-06-10 14:22:00 +0200 | [diff] [blame] | 270 | """Translate the test list into a dictionary.""" |
Matthew Maurer | 24758f8 | 2021-12-10 18:51:22 +0000 | [diff] [blame] | 271 | test_mapping = {"imports": []} |
| 272 | for test_group in TEST_GROUPS: |
| 273 | test_mapping[test_group] = [] |
| 274 | for test in tests: |
| 275 | if test in TEST_EXCLUDE: |
| 276 | continue |
| 277 | if test in TEST_OPTIONS: |
| 278 | test_mapping[test_group].append({"name": test, "options": TEST_OPTIONS[test]}) |
| 279 | else: |
| 280 | test_mapping[test_group].append({"name": test}) |
| 281 | test_mapping[test_group] = sorted(test_mapping[test_group], key=lambda t: t["name"]) |
Joel Galenson | 4a08c64 | 2021-10-14 13:25:57 -0700 | [diff] [blame] | 282 | for dir in dirs: |
| 283 | test_mapping["imports"].append({"path": dir}) |
Joel Galenson | 4a08c64 | 2021-10-14 13:25:57 -0700 | [diff] [blame] | 284 | test_mapping["imports"] = sorted(test_mapping["imports"], key=lambda t: t["path"]) |
Matthew Maurer | 24758f8 | 2021-12-10 18:51:22 +0000 | [diff] [blame] | 285 | test_mapping = {section: entry for (section, entry) in test_mapping.items() if entry} |
Jeff Vander Stoep | cec8bac | 2021-01-15 14:15:37 +0100 | [diff] [blame] | 286 | return test_mapping |
| 287 | |
| 288 | def write_test_mapping(self, test_mapping): |
Thiébaud Weksteen | 3604b75 | 2021-06-10 14:22:00 +0200 | [diff] [blame] | 289 | """Writes the TEST_MAPPING file.""" |
Jeff Vander Stoep | cec8bac | 2021-01-15 14:15:37 +0100 | [diff] [blame] | 290 | with open("TEST_MAPPING", "w") as json_file: |
Jeff Vander Stoep | 1b24dc3 | 2021-02-03 18:52:42 +0100 | [diff] [blame] | 291 | json_file.write("// Generated by update_crate_tests.py for tests that depend on this crate.\n") |
Jeff Vander Stoep | cec8bac | 2021-01-15 14:15:37 +0100 | [diff] [blame] | 292 | json.dump(test_mapping, json_file, indent=2, separators=(',', ': '), sort_keys=True) |
| 293 | json_file.write("\n") |
Joel Galenson | 1779104 | 2021-06-17 14:59:15 -0700 | [diff] [blame] | 294 | print("TEST_MAPPING successfully updated for %s!" % self.package.dir_rel) |
Jeff Vander Stoep | cec8bac | 2021-01-15 14:15:37 +0100 | [diff] [blame] | 295 | |
Thiébaud Weksteen | fc485b2 | 2021-06-10 13:30:20 +0200 | [diff] [blame] | 296 | |
Joel Galenson | 0835244 | 2021-08-20 11:39:48 -0700 | [diff] [blame] | 297 | def parse_args(): |
| 298 | parser = argparse.ArgumentParser('update_crate_tests') |
Joel Galenson | 4a2a3a8 | 2021-08-20 12:26:37 -0700 | [diff] [blame] | 299 | parser.add_argument('paths', |
| 300 | nargs='*', |
| 301 | help='Absolute or relative paths of the projects as globs.') |
| 302 | parser.add_argument('--branch_and_commit', |
| 303 | action='store_true', |
| 304 | help='Starts a new branch and commit changes.') |
| 305 | parser.add_argument('--push_change', |
| 306 | action='store_true', |
| 307 | help='Pushes change to Gerrit.') |
Joel Galenson | 0835244 | 2021-08-20 11:39:48 -0700 | [diff] [blame] | 308 | return parser.parse_args() |
| 309 | |
| 310 | |
Jeff Vander Stoep | cec8bac | 2021-01-15 14:15:37 +0100 | [diff] [blame] | 311 | def main(): |
Joel Galenson | 0835244 | 2021-08-20 11:39:48 -0700 | [diff] [blame] | 312 | args = parse_args() |
| 313 | paths = args.paths if len(args.paths) > 0 else [os.getcwd()] |
| 314 | # We want to use glob to get all the paths, so we first convert to absolute. |
| 315 | paths = [Path(path).resolve() for path in paths] |
| 316 | paths = sorted([path for abs_path in paths |
| 317 | for path in glob.glob(str(abs_path))]) |
| 318 | |
Joel Galenson | 1779104 | 2021-06-17 14:59:15 -0700 | [diff] [blame] | 319 | env = Env() |
| 320 | bazel = Bazel(env) |
| 321 | for path in paths: |
| 322 | try: |
| 323 | test_mapping = TestMapping(env, bazel, path) |
Joel Galenson | 4a2a3a8 | 2021-08-20 12:26:37 -0700 | [diff] [blame] | 324 | test_mapping.create() |
| 325 | changed = (subprocess.call(['git', 'diff', '--quiet']) == 1) |
Joel Galenson | bdf3ab4 | 2021-08-30 08:57:18 -0700 | [diff] [blame] | 326 | untracked = (os.path.isfile('TEST_MAPPING') and |
| 327 | (subprocess.run(['git', 'ls-files', '--error-unmatch', 'TEST_MAPPING'], |
| 328 | stderr=subprocess.DEVNULL, |
| 329 | stdout=subprocess.DEVNULL).returncode == 1)) |
| 330 | if args.branch_and_commit and (changed or untracked): |
Joel Galenson | 4a2a3a8 | 2021-08-20 12:26:37 -0700 | [diff] [blame] | 331 | subprocess.check_output(['repo', 'start', |
| 332 | 'tmp_auto_test_mapping', '.']) |
| 333 | subprocess.check_output(['git', 'add', 'TEST_MAPPING']) |
| 334 | subprocess.check_output(['git', 'commit', '-m', |
| 335 | 'Update TEST_MAPPING\n\nTest: None']) |
Joel Galenson | bdf3ab4 | 2021-08-30 08:57:18 -0700 | [diff] [blame] | 336 | if args.push_change and (changed or untracked): |
Joel Galenson | 4a2a3a8 | 2021-08-20 12:26:37 -0700 | [diff] [blame] | 337 | date = datetime.today().strftime('%m-%d') |
| 338 | subprocess.check_output(['git', 'push', 'aosp', 'HEAD:refs/for/master', |
| 339 | '-o', 'topic=test-mapping-%s' % date]) |
| 340 | except (UpdaterException, subprocess.CalledProcessError) as err: |
Joel Galenson | 1779104 | 2021-06-17 14:59:15 -0700 | [diff] [blame] | 341 | sys.exit("Error: " + str(err)) |
Jeff Vander Stoep | cec8bac | 2021-01-15 14:15:37 +0100 | [diff] [blame] | 342 | |
| 343 | if __name__ == '__main__': |
| 344 | main() |