xref: /aosp_15_r20/tools/metalava/scripts/refresh-baselines.py (revision 115816f9299ab6ddd6b9673b81f34e707f6bacab)
1*115816f9SAndroid Build Coastguard Worker#!/usr/bin/env -S python3
2*115816f9SAndroid Build Coastguard Worker#  Copyright (C) 2024 The Android Open Source Project
3*115816f9SAndroid Build Coastguard Worker#
4*115816f9SAndroid Build Coastguard Worker#  Licensed under the Apache License, Version 2.0 (the "License");
5*115816f9SAndroid Build Coastguard Worker#  you may not use this file except in compliance with the License.
6*115816f9SAndroid Build Coastguard Worker#  You may obtain a copy of the License at
7*115816f9SAndroid Build Coastguard Worker#
8*115816f9SAndroid Build Coastguard Worker#       http://www.apache.org/licenses/LICENSE-2.0
9*115816f9SAndroid Build Coastguard Worker#
10*115816f9SAndroid Build Coastguard Worker#  Unless required by applicable law or agreed to in writing, software
11*115816f9SAndroid Build Coastguard Worker#  distributed under the License is distributed on an "AS IS" BASIS,
12*115816f9SAndroid Build Coastguard Worker#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*115816f9SAndroid Build Coastguard Worker#  See the License for the specific language governing permissions and
14*115816f9SAndroid Build Coastguard Worker#  limitations under the License.
15*115816f9SAndroid Build Coastguard Workerimport argparse
16*115816f9SAndroid Build Coastguard Workerimport dataclasses
17*115816f9SAndroid Build Coastguard Workerimport subprocess
18*115816f9SAndroid Build Coastguard Workerimport sys
19*115816f9SAndroid Build Coastguard Workerfrom pathlib import Path
20*115816f9SAndroid Build Coastguard Worker
21*115816f9SAndroid Build Coastguard Worker
22*115816f9SAndroid Build Coastguard Worker@dataclasses.dataclass(frozen=True)
23*115816f9SAndroid Build Coastguard Workerclass BaselineProject:
24*115816f9SAndroid Build Coastguard Worker    """A project that has a baseline file to update."""
25*115816f9SAndroid Build Coastguard Worker    # The name of the project
26*115816f9SAndroid Build Coastguard Worker    name: str
27*115816f9SAndroid Build Coastguard Worker
28*115816f9SAndroid Build Coastguard Worker    # The baseline file path.
29*115816f9SAndroid Build Coastguard Worker    baseline_file: Path
30*115816f9SAndroid Build Coastguard Worker
31*115816f9SAndroid Build Coastguard Worker
32*115816f9SAndroid Build Coastguard Workerdef resource_path(project_dir, resource_path):
33*115816f9SAndroid Build Coastguard Worker    return project_dir / "src" / "test" / "resources" / resource_path
34*115816f9SAndroid Build Coastguard Worker
35*115816f9SAndroid Build Coastguard Worker
36*115816f9SAndroid Build Coastguard Workerdef find_baseline_projects(metalava_dir):
37*115816f9SAndroid Build Coastguard Worker    projects = []
38*115816f9SAndroid Build Coastguard Worker    for buildFile in metalava_dir.glob("*/build.gradle.kts"):
39*115816f9SAndroid Build Coastguard Worker        for line in open(buildFile, 'r'):
40*115816f9SAndroid Build Coastguard Worker            if """id("metalava-model-provider-plugin")""" in line:
41*115816f9SAndroid Build Coastguard Worker                project_dir = buildFile.parent
42*115816f9SAndroid Build Coastguard Worker                baseline = BaselineProject(
43*115816f9SAndroid Build Coastguard Worker                    name=project_dir.name,
44*115816f9SAndroid Build Coastguard Worker                    baseline_file=resource_path(project_dir, "model-test-suite-baseline.txt"),
45*115816f9SAndroid Build Coastguard Worker                )
46*115816f9SAndroid Build Coastguard Worker                projects.append(baseline)
47*115816f9SAndroid Build Coastguard Worker    projects.append(BaselineProject(
48*115816f9SAndroid Build Coastguard Worker        name="metalava",
49*115816f9SAndroid Build Coastguard Worker        baseline_file=resource_path(metalava_dir / "metalava", "source-model-provider-baseline.txt")
50*115816f9SAndroid Build Coastguard Worker    ))
51*115816f9SAndroid Build Coastguard Worker    return projects
52*115816f9SAndroid Build Coastguard Worker
53*115816f9SAndroid Build Coastguard Worker
54*115816f9SAndroid Build Coastguard Workerdef main(args):
55*115816f9SAndroid Build Coastguard Worker    args_parser = argparse.ArgumentParser(description="Refresh the baseline files.")
56*115816f9SAndroid Build Coastguard Worker    args_parser.add_argument("projects", nargs='*')
57*115816f9SAndroid Build Coastguard Worker    args = args_parser.parse_args(args)
58*115816f9SAndroid Build Coastguard Worker
59*115816f9SAndroid Build Coastguard Worker    # Get various directories.
60*115816f9SAndroid Build Coastguard Worker    this = Path(__file__)
61*115816f9SAndroid Build Coastguard Worker    script_dir = this.parent
62*115816f9SAndroid Build Coastguard Worker    metalava_dir = script_dir.parent
63*115816f9SAndroid Build Coastguard Worker    out_dir = metalava_dir.parent.parent / "out"
64*115816f9SAndroid Build Coastguard Worker    metalava_out_dir = out_dir / "metalava"
65*115816f9SAndroid Build Coastguard Worker
66*115816f9SAndroid Build Coastguard Worker    # Get the projects which have a baseline file to update.
67*115816f9SAndroid Build Coastguard Worker    baseline_projects = find_baseline_projects(metalava_dir)
68*115816f9SAndroid Build Coastguard Worker
69*115816f9SAndroid Build Coastguard Worker    # Filter the baseline projects by the names specified on the command line.
70*115816f9SAndroid Build Coastguard Worker    if args.projects:
71*115816f9SAndroid Build Coastguard Worker        baseline_projects = [p for p in baseline_projects if p.name in args.projects]
72*115816f9SAndroid Build Coastguard Worker
73*115816f9SAndroid Build Coastguard Worker    for baseline_project in baseline_projects:
74*115816f9SAndroid Build Coastguard Worker        project_name = baseline_project.name
75*115816f9SAndroid Build Coastguard Worker
76*115816f9SAndroid Build Coastguard Worker        # Delete all the test report files.
77*115816f9SAndroid Build Coastguard Worker        print(f"Deleting test report files for {project_name}")
78*115816f9SAndroid Build Coastguard Worker        test_reports_dir = metalava_out_dir / project_name / "build" / "test-results" / "test"
79*115816f9SAndroid Build Coastguard Worker        for f in test_reports_dir.glob("**/TEST-*.xml"):
80*115816f9SAndroid Build Coastguard Worker            f.unlink()
81*115816f9SAndroid Build Coastguard Worker
82*115816f9SAndroid Build Coastguard Worker        # Delete the baseline file.
83*115816f9SAndroid Build Coastguard Worker        baseline_file = baseline_project.baseline_file
84*115816f9SAndroid Build Coastguard Worker        print(f"Deleting baseline file - {baseline_file}")
85*115816f9SAndroid Build Coastguard Worker        baseline_file.unlink(missing_ok=True)
86*115816f9SAndroid Build Coastguard Worker
87*115816f9SAndroid Build Coastguard Worker        # Run the tests.
88*115816f9SAndroid Build Coastguard Worker        print(f"Running all tests in {project_name}")
89*115816f9SAndroid Build Coastguard Worker        subprocess.run(["./gradlew", f":{project_name}:test", "--continue"], cwd=metalava_dir)
90*115816f9SAndroid Build Coastguard Worker
91*115816f9SAndroid Build Coastguard Worker        print(f"Updating baseline file - {baseline_file}")
92*115816f9SAndroid Build Coastguard Worker        test_report_files = " ".join([f"'{str(f)}'" for f in test_reports_dir.glob("**/TEST-*.xml")])
93*115816f9SAndroid Build Coastguard Worker        project_dir = metalava_dir / project_name
94*115816f9SAndroid Build Coastguard Worker        subprocess.run(["./gradlew", f":metalava-model-testsuite-cli:run",
95*115816f9SAndroid Build Coastguard Worker                        f"""--args={test_report_files} --baseline-file '{baseline_file}'"""], cwd=metalava_dir)
96*115816f9SAndroid Build Coastguard Worker
97*115816f9SAndroid Build Coastguard Worker
98*115816f9SAndroid Build Coastguard Workerif __name__ == "__main__":
99*115816f9SAndroid Build Coastguard Worker    main(sys.argv[1:])
100