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"""
18Tool switch the deprecated jetpack test runner to the correct one.
19
20Typical 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
26import collections
27import os
28import re
29import subprocess
30import sys
31
32re_result = re.compile("I/ModuleListener.+?null-device-0 (.+?)#(.+?) ([A-Z_]+)(.*)$")
33
34OLD_RUNNER = "androidx.test.runner.AndroidJUnit4"
35NEW_RUNNER = "androidx.test.ext.junit.runners.AndroidJUnit4"
36SED_ARG = r"s/%s/%s/g" % (OLD_RUNNER, NEW_RUNNER)
37
38target = collections.defaultdict()
39
40with 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
48if len(target) == 0:
49    print("No tests need updating.")
50    sys.exit(0)
51
52num_fixed = 0
53for 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
68print("Tests fixed", num_fixed)
69