xref: /aosp_15_r20/tools/external_updater/tests/endtoend/treebuilder/fakeproject.py (revision 3c875a214f382db1236d28570d1304ce57138f32)
1*3c875a21SAndroid Build Coastguard Worker#
2*3c875a21SAndroid Build Coastguard Worker# Copyright (C) 2023 The Android Open Source Project
3*3c875a21SAndroid Build Coastguard Worker#
4*3c875a21SAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License");
5*3c875a21SAndroid Build Coastguard Worker# you may not use this file except in compliance with the License.
6*3c875a21SAndroid Build Coastguard Worker# You may obtain a copy of the License at
7*3c875a21SAndroid Build Coastguard Worker#
8*3c875a21SAndroid Build Coastguard Worker#      http://www.apache.org/licenses/LICENSE-2.0
9*3c875a21SAndroid Build Coastguard Worker#
10*3c875a21SAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software
11*3c875a21SAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS,
12*3c875a21SAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*3c875a21SAndroid Build Coastguard Worker# See the License for the specific language governing permissions and
14*3c875a21SAndroid Build Coastguard Worker# limitations under the License.
15*3c875a21SAndroid Build Coastguard Worker#
16*3c875a21SAndroid Build Coastguard Worker"""Git repository fakes for use in tests."""
17*3c875a21SAndroid Build Coastguard Workerimport textwrap
18*3c875a21SAndroid Build Coastguard Workerfrom pathlib import Path
19*3c875a21SAndroid Build Coastguard Worker
20*3c875a21SAndroid Build Coastguard Workerfrom tests.gitrepo import GitRepo
21*3c875a21SAndroid Build Coastguard Worker
22*3c875a21SAndroid Build Coastguard Worker
23*3c875a21SAndroid Build Coastguard Workerclass FakeProject:  # pylint: disable=too-few-public-methods
24*3c875a21SAndroid Build Coastguard Worker    """A collection of git repositories for use in tests.
25*3c875a21SAndroid Build Coastguard Worker
26*3c875a21SAndroid Build Coastguard Worker    This shouldn't be used directly. Use the tree_builder fixture.
27*3c875a21SAndroid Build Coastguard Worker
28*3c875a21SAndroid Build Coastguard Worker    A project contains the three git repos that are used in the Android upstream
29*3c875a21SAndroid Build Coastguard Worker    mirroring process:
30*3c875a21SAndroid Build Coastguard Worker
31*3c875a21SAndroid Build Coastguard Worker    1. The true upstream repository. Where the third-party commits their work.
32*3c875a21SAndroid Build Coastguard Worker    2. The Android repository. Where Gerrit submits work.
33*3c875a21SAndroid Build Coastguard Worker    3. The local repository. This is where work happens before being uploaded to Gerrit.
34*3c875a21SAndroid Build Coastguard Worker    """
35*3c875a21SAndroid Build Coastguard Worker
36*3c875a21SAndroid Build Coastguard Worker    def __init__(
37*3c875a21SAndroid Build Coastguard Worker        self, tree_path: Path, upstream_path: Path, android_mirror_path: Path
38*3c875a21SAndroid Build Coastguard Worker    ) -> None:
39*3c875a21SAndroid Build Coastguard Worker        self.local = GitRepo(tree_path)
40*3c875a21SAndroid Build Coastguard Worker        self.upstream = GitRepo(upstream_path)
41*3c875a21SAndroid Build Coastguard Worker        self.android_mirror = GitRepo(android_mirror_path)
42*3c875a21SAndroid Build Coastguard Worker        self._initialize_repo(self.upstream)
43*3c875a21SAndroid Build Coastguard Worker
44*3c875a21SAndroid Build Coastguard Worker    def _initialize_repo(self, repo: GitRepo) -> None:
45*3c875a21SAndroid Build Coastguard Worker        """Create a git repo and initial commit in the upstream repository."""
46*3c875a21SAndroid Build Coastguard Worker        repo.init(branch_name="main")
47*3c875a21SAndroid Build Coastguard Worker        repo.commit("Initial commit.", allow_empty=True)
48*3c875a21SAndroid Build Coastguard Worker
49*3c875a21SAndroid Build Coastguard Worker    def initial_import(self, upstream_is_tag: bool = False) -> None:
50*3c875a21SAndroid Build Coastguard Worker        """Perform the initial import of the upstream repo into the mirror repo.
51*3c875a21SAndroid Build Coastguard Worker
52*3c875a21SAndroid Build Coastguard Worker        These are an approximation of the steps that would be taken for the initial
53*3c875a21SAndroid Build Coastguard Worker        import as part of go/android3p:
54*3c875a21SAndroid Build Coastguard Worker
55*3c875a21SAndroid Build Coastguard Worker        * A new git repo is created with a single empty commit. That commit is **not**
56*3c875a21SAndroid Build Coastguard Worker          present in the upstream repo. Android imports begin with unrelated histories.
57*3c875a21SAndroid Build Coastguard Worker        * The upstream repository is merged into the local tree.
58*3c875a21SAndroid Build Coastguard Worker        * METADATA, NOTICE, MODULE_LICENSE_*, and OWNERS files are added to the local
59*3c875a21SAndroid Build Coastguard Worker          tree. We only bother with METADATA for now since that's all the tests need.
60*3c875a21SAndroid Build Coastguard Worker        """
61*3c875a21SAndroid Build Coastguard Worker        self.android_mirror.init()
62*3c875a21SAndroid Build Coastguard Worker        self.android_mirror.commit("Initial commit.", allow_empty=True)
63*3c875a21SAndroid Build Coastguard Worker
64*3c875a21SAndroid Build Coastguard Worker        upstream_version = self.upstream.head()
65*3c875a21SAndroid Build Coastguard Worker        if upstream_is_tag:
66*3c875a21SAndroid Build Coastguard Worker            upstream_version = self.upstream.describe(upstream_version)
67*3c875a21SAndroid Build Coastguard Worker        self.android_mirror.fetch(self.upstream)
68*3c875a21SAndroid Build Coastguard Worker        self.android_mirror.merge(
69*3c875a21SAndroid Build Coastguard Worker            "FETCH_HEAD", allow_fast_forward=False, allow_unrelated_histories=True
70*3c875a21SAndroid Build Coastguard Worker        )
71*3c875a21SAndroid Build Coastguard Worker
72*3c875a21SAndroid Build Coastguard Worker        self.android_mirror.commit(
73*3c875a21SAndroid Build Coastguard Worker            "Add metadata files.",
74*3c875a21SAndroid Build Coastguard Worker            update_files={
75*3c875a21SAndroid Build Coastguard Worker                "OWNERS": "nobody",
76*3c875a21SAndroid Build Coastguard Worker                "METADATA": textwrap.dedent(
77*3c875a21SAndroid Build Coastguard Worker                    f"""\
78*3c875a21SAndroid Build Coastguard Worker                    name: "test"
79*3c875a21SAndroid Build Coastguard Worker                    description: "It's a test."
80*3c875a21SAndroid Build Coastguard Worker                    third_party {{
81*3c875a21SAndroid Build Coastguard Worker                      license_type: UNENCUMBERED
82*3c875a21SAndroid Build Coastguard Worker                      last_upgrade_date {{
83*3c875a21SAndroid Build Coastguard Worker                        year: 2023
84*3c875a21SAndroid Build Coastguard Worker                        month: 12
85*3c875a21SAndroid Build Coastguard Worker                        day: 1
86*3c875a21SAndroid Build Coastguard Worker                      }}
87*3c875a21SAndroid Build Coastguard Worker                      identifier {{
88*3c875a21SAndroid Build Coastguard Worker                        type: "Git"
89*3c875a21SAndroid Build Coastguard Worker                        value: "{self.upstream.path.as_uri()}"
90*3c875a21SAndroid Build Coastguard Worker                        version: "{upstream_version}"
91*3c875a21SAndroid Build Coastguard Worker                      }}
92*3c875a21SAndroid Build Coastguard Worker                    }}
93*3c875a21SAndroid Build Coastguard Worker                    """
94*3c875a21SAndroid Build Coastguard Worker                ),
95*3c875a21SAndroid Build Coastguard Worker            },
96*3c875a21SAndroid Build Coastguard Worker        )
97