| Makoto Onuki | 03e9c7c | 2024-02-01 09:42:37 -0800 | [diff] [blame^] | 1 | #!/usr/bin/env python3 |
| 2 | # |
| 3 | # Copyright (C) 2024 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. |
| 16 | |
| 17 | """ |
| 18 | Tool switch the deprecated jetpack test runner to the correct one. |
| 19 | |
| 20 | Typical usage: |
| 21 | $ RAVENWOOD_OPTIONAL_VALIDATION=1 atest MyTestsRavenwood # Prepend RAVENWOOD_RUN_DISABLED_TESTS=1 as needed |
| 22 | $ cd /path/to/tests/root |
| 23 | $ python bulk_enable.py /path/to/atest/output/host_log.txt |
| 24 | """ |
| 25 | |
| 26 | import collections |
| 27 | import os |
| 28 | import re |
| 29 | import subprocess |
| 30 | import sys |
| 31 | |
| 32 | re_result = re.compile("I/ModuleListener.+?null-device-0 (.+?)#(.+?) ([A-Z_]+)(.*)$") |
| 33 | |
| 34 | OLD_RUNNER = "androidx.test.runner.AndroidJUnit4" |
| 35 | NEW_RUNNER = "androidx.test.ext.junit.runners.AndroidJUnit4" |
| 36 | SED_ARG = r"s/%s/%s/g" % (OLD_RUNNER, NEW_RUNNER) |
| 37 | |
| 38 | target = collections.defaultdict() |
| 39 | |
| 40 | with open(sys.argv[1]) as f: |
| 41 | for line in f.readlines(): |
| 42 | result = re_result.search(line) |
| 43 | if result: |
| 44 | clazz, method, state, msg = result.groups() |
| 45 | if NEW_RUNNER in msg: |
| 46 | target[clazz] = 1 |
| 47 | |
| 48 | if len(target) == 0: |
| 49 | print("No tests need updating.") |
| 50 | sys.exit(0) |
| 51 | |
| 52 | num_fixed = 0 |
| 53 | for clazz in target.keys(): |
| 54 | print("Fixing test runner", clazz) |
| 55 | clazz_match = re.compile("%s\.(kt|java)" % (clazz.split(".")[-1])) |
| 56 | found = False |
| 57 | for root, dirs, files in os.walk("."): |
| 58 | for f in files: |
| 59 | if clazz_match.match(f): |
| 60 | found = True |
| 61 | num_fixed += 1 |
| 62 | path = os.path.join(root, f) |
| 63 | subprocess.run(["sed", "-i", "-E", SED_ARG, path]) |
| 64 | if not found: |
| 65 | print(f" Warining: tests {clazz} not found") |
| 66 | |
| 67 | |
| 68 | print("Tests fixed", num_fixed) |