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