Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 1 | # Copyright (C) 2021 The Android Open Source Project |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
| 15 | """Unittests for the git_superproject.py module.""" |
| 16 | |
Raman Tenneti | 784e16f | 2021-06-11 17:29:45 -0700 | [diff] [blame] | 17 | import json |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 18 | import os |
Raman Tenneti | 080877e | 2021-03-09 15:19:06 -0800 | [diff] [blame] | 19 | import platform |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 20 | import tempfile |
| 21 | import unittest |
| 22 | from unittest import mock |
| 23 | |
Mike Frysinger | 6447733 | 2023-08-21 21:20:32 -0400 | [diff] [blame] | 24 | from test_manifest_xml import sort_attributes |
| 25 | |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 26 | import git_superproject |
Raman Tenneti | 784e16f | 2021-06-11 17:29:45 -0700 | [diff] [blame] | 27 | import git_trace2_event_log |
Raman Tenneti | 1fd7bc2 | 2021-02-04 14:39:38 -0800 | [diff] [blame] | 28 | import manifest_xml |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 29 | |
| 30 | |
| 31 | class SuperprojectTestCase(unittest.TestCase): |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 32 | """TestCase for the Superproject module.""" |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 33 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 34 | PARENT_SID_KEY = "GIT_TRACE2_PARENT_SID" |
| 35 | PARENT_SID_VALUE = "parent_sid" |
| 36 | SELF_SID_REGEX = r"repo-\d+T\d+Z-.*" |
Jason R. Coombs | b32ccbb | 2023-09-29 11:04:49 -0400 | [diff] [blame] | 37 | FULL_SID_REGEX = rf"^{PARENT_SID_VALUE}/{SELF_SID_REGEX}" |
Raman Tenneti | 784e16f | 2021-06-11 17:29:45 -0700 | [diff] [blame] | 38 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 39 | def setUp(self): |
| 40 | """Set up superproject every time.""" |
| 41 | self.tempdirobj = tempfile.TemporaryDirectory(prefix="repo_tests") |
| 42 | self.tempdir = self.tempdirobj.name |
| 43 | self.repodir = os.path.join(self.tempdir, ".repo") |
| 44 | self.manifest_file = os.path.join( |
| 45 | self.repodir, manifest_xml.MANIFEST_FILE_NAME |
| 46 | ) |
| 47 | os.mkdir(self.repodir) |
| 48 | self.platform = platform.system().lower() |
Raman Tenneti | 1fd7bc2 | 2021-02-04 14:39:38 -0800 | [diff] [blame] | 49 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 50 | # By default we initialize with the expected case where |
| 51 | # repo launches us (so GIT_TRACE2_PARENT_SID is set). |
| 52 | env = { |
| 53 | self.PARENT_SID_KEY: self.PARENT_SID_VALUE, |
| 54 | } |
| 55 | self.git_event_log = git_trace2_event_log.EventLog(env=env) |
Raman Tenneti | 784e16f | 2021-06-11 17:29:45 -0700 | [diff] [blame] | 56 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 57 | # The manifest parsing really wants a git repo currently. |
| 58 | gitdir = os.path.join(self.repodir, "manifests.git") |
| 59 | os.mkdir(gitdir) |
| 60 | with open(os.path.join(gitdir, "config"), "w") as fp: |
| 61 | fp.write( |
| 62 | """[remote "origin"] |
Raman Tenneti | 1fd7bc2 | 2021-02-04 14:39:38 -0800 | [diff] [blame] | 63 | url = https://localhost:0/manifest |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 64 | """ |
| 65 | ) |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 66 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 67 | manifest = self.getXmlManifest( |
| 68 | """ |
Raman Tenneti | 21dce3d | 2021-02-09 00:26:31 -0800 | [diff] [blame] | 69 | <manifest> |
| 70 | <remote name="default-remote" fetch="http://localhost" /> |
| 71 | <default remote="default-remote" revision="refs/heads/main" /> |
| 72 | <superproject name="superproject"/> |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 73 | <project path="art" name="platform/art" groups="notdefault,platform-""" |
| 74 | + self.platform |
| 75 | + """ |
Raman Tenneti | 080877e | 2021-03-09 15:19:06 -0800 | [diff] [blame] | 76 | " /></manifest> |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 77 | """ |
| 78 | ) |
| 79 | self._superproject = git_superproject.Superproject( |
| 80 | manifest, |
| 81 | name="superproject", |
| 82 | remote=manifest.remotes.get("default-remote").ToRemoteSpec( |
| 83 | "superproject" |
| 84 | ), |
| 85 | revision="refs/heads/main", |
| 86 | ) |
Raman Tenneti | 21dce3d | 2021-02-09 00:26:31 -0800 | [diff] [blame] | 87 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 88 | def tearDown(self): |
| 89 | """Tear down superproject every time.""" |
| 90 | self.tempdirobj.cleanup() |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 91 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 92 | def getXmlManifest(self, data): |
| 93 | """Helper to initialize a manifest for testing.""" |
| 94 | with open(self.manifest_file, "w") as fp: |
| 95 | fp.write(data) |
| 96 | return manifest_xml.XmlManifest(self.repodir, self.manifest_file) |
Raman Tenneti | 1fd7bc2 | 2021-02-04 14:39:38 -0800 | [diff] [blame] | 97 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 98 | def verifyCommonKeys(self, log_entry, expected_event_name, full_sid=True): |
| 99 | """Helper function to verify common event log keys.""" |
| 100 | self.assertIn("event", log_entry) |
| 101 | self.assertIn("sid", log_entry) |
| 102 | self.assertIn("thread", log_entry) |
| 103 | self.assertIn("time", log_entry) |
Raman Tenneti | 784e16f | 2021-06-11 17:29:45 -0700 | [diff] [blame] | 104 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 105 | # Do basic data format validation. |
| 106 | self.assertEqual(expected_event_name, log_entry["event"]) |
| 107 | if full_sid: |
| 108 | self.assertRegex(log_entry["sid"], self.FULL_SID_REGEX) |
| 109 | else: |
| 110 | self.assertRegex(log_entry["sid"], self.SELF_SID_REGEX) |
LuK1337 | aadd12c | 2023-09-16 09:36:49 +0200 | [diff] [blame] | 111 | self.assertRegex( |
| 112 | log_entry["time"], r"^\d+-\d+-\d+T\d+:\d+:\d+\.\d+\+00:00$" |
| 113 | ) |
Raman Tenneti | 784e16f | 2021-06-11 17:29:45 -0700 | [diff] [blame] | 114 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 115 | def readLog(self, log_path): |
| 116 | """Helper function to read log data into a list.""" |
| 117 | log_data = [] |
| 118 | with open(log_path, mode="rb") as f: |
| 119 | for line in f: |
| 120 | log_data.append(json.loads(line)) |
| 121 | return log_data |
Raman Tenneti | 784e16f | 2021-06-11 17:29:45 -0700 | [diff] [blame] | 122 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 123 | def verifyErrorEvent(self): |
| 124 | """Helper to verify that error event is written.""" |
Raman Tenneti | 784e16f | 2021-06-11 17:29:45 -0700 | [diff] [blame] | 125 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 126 | with tempfile.TemporaryDirectory(prefix="event_log_tests") as tempdir: |
| 127 | log_path = self.git_event_log.Write(path=tempdir) |
| 128 | self.log_data = self.readLog(log_path) |
Raman Tenneti | 784e16f | 2021-06-11 17:29:45 -0700 | [diff] [blame] | 129 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 130 | self.assertEqual(len(self.log_data), 2) |
| 131 | error_event = self.log_data[1] |
| 132 | self.verifyCommonKeys(self.log_data[0], expected_event_name="version") |
| 133 | self.verifyCommonKeys(error_event, expected_event_name="error") |
| 134 | # Check for 'error' event specific fields. |
| 135 | self.assertIn("msg", error_event) |
| 136 | self.assertIn("fmt", error_event) |
Raman Tenneti | 784e16f | 2021-06-11 17:29:45 -0700 | [diff] [blame] | 137 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 138 | def test_superproject_get_superproject_no_superproject(self): |
| 139 | """Test with no url.""" |
| 140 | manifest = self.getXmlManifest( |
| 141 | """ |
Raman Tenneti | 21dce3d | 2021-02-09 00:26:31 -0800 | [diff] [blame] | 142 | <manifest> |
| 143 | </manifest> |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 144 | """ |
| 145 | ) |
| 146 | self.assertIsNone(manifest.superproject) |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 147 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 148 | def test_superproject_get_superproject_invalid_url(self): |
| 149 | """Test with an invalid url.""" |
| 150 | manifest = self.getXmlManifest( |
| 151 | """ |
Raman Tenneti | 21dce3d | 2021-02-09 00:26:31 -0800 | [diff] [blame] | 152 | <manifest> |
| 153 | <remote name="test-remote" fetch="localhost" /> |
| 154 | <default remote="test-remote" revision="refs/heads/main" /> |
| 155 | <superproject name="superproject"/> |
| 156 | </manifest> |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 157 | """ |
| 158 | ) |
| 159 | superproject = git_superproject.Superproject( |
| 160 | manifest, |
| 161 | name="superproject", |
| 162 | remote=manifest.remotes.get("test-remote").ToRemoteSpec( |
| 163 | "superproject" |
| 164 | ), |
| 165 | revision="refs/heads/main", |
| 166 | ) |
| 167 | sync_result = superproject.Sync(self.git_event_log) |
Raman Tenneti | 784e16f | 2021-06-11 17:29:45 -0700 | [diff] [blame] | 168 | self.assertFalse(sync_result.success) |
| 169 | self.assertTrue(sync_result.fatal) |
Raman Tenneti | 7caa365 | 2021-02-01 11:15:16 -0800 | [diff] [blame] | 170 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 171 | def test_superproject_get_superproject_invalid_branch(self): |
| 172 | """Test with an invalid branch.""" |
| 173 | manifest = self.getXmlManifest( |
| 174 | """ |
| 175 | <manifest> |
| 176 | <remote name="test-remote" fetch="localhost" /> |
| 177 | <default remote="test-remote" revision="refs/heads/main" /> |
| 178 | <superproject name="superproject"/> |
| 179 | </manifest> |
| 180 | """ |
| 181 | ) |
| 182 | self._superproject = git_superproject.Superproject( |
| 183 | manifest, |
| 184 | name="superproject", |
| 185 | remote=manifest.remotes.get("test-remote").ToRemoteSpec( |
| 186 | "superproject" |
| 187 | ), |
| 188 | revision="refs/heads/main", |
| 189 | ) |
| 190 | with mock.patch.object(self._superproject, "_branch", "junk"): |
| 191 | sync_result = self._superproject.Sync(self.git_event_log) |
| 192 | self.assertFalse(sync_result.success) |
| 193 | self.assertTrue(sync_result.fatal) |
| 194 | self.verifyErrorEvent() |
Raman Tenneti | 6a872c9 | 2021-01-14 19:17:50 -0800 | [diff] [blame] | 195 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 196 | def test_superproject_get_superproject_mock_init(self): |
| 197 | """Test with _Init failing.""" |
| 198 | with mock.patch.object(self._superproject, "_Init", return_value=False): |
| 199 | sync_result = self._superproject.Sync(self.git_event_log) |
| 200 | self.assertFalse(sync_result.success) |
| 201 | self.assertTrue(sync_result.fatal) |
Raman Tenneti | 1fd7bc2 | 2021-02-04 14:39:38 -0800 | [diff] [blame] | 202 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 203 | def test_superproject_get_superproject_mock_fetch(self): |
| 204 | """Test with _Fetch failing.""" |
| 205 | with mock.patch.object(self._superproject, "_Init", return_value=True): |
| 206 | os.mkdir(self._superproject._superproject_path) |
| 207 | with mock.patch.object( |
| 208 | self._superproject, "_Fetch", return_value=False |
| 209 | ): |
| 210 | sync_result = self._superproject.Sync(self.git_event_log) |
| 211 | self.assertFalse(sync_result.success) |
| 212 | self.assertTrue(sync_result.fatal) |
| 213 | |
| 214 | def test_superproject_get_all_project_commit_ids_mock_ls_tree(self): |
| 215 | """Test with LsTree being a mock.""" |
| 216 | data = ( |
| 217 | "120000 blob 158258bdf146f159218e2b90f8b699c4d85b5804\tAndroid.bp\x00" |
| 218 | "160000 commit 2c2724cb36cd5a9cec6c852c681efc3b7c6b86ea\tart\x00" |
| 219 | "160000 commit e9d25da64d8d365dbba7c8ee00fe8c4473fe9a06\tbootable/recovery\x00" |
| 220 | "120000 blob acc2cbdf438f9d2141f0ae424cec1d8fc4b5d97f\tbootstrap.bash\x00" |
| 221 | "160000 commit ade9b7a0d874e25fff4bf2552488825c6f111928\tbuild/bazel\x00" |
| 222 | ) |
| 223 | with mock.patch.object(self._superproject, "_Init", return_value=True): |
| 224 | with mock.patch.object( |
| 225 | self._superproject, "_Fetch", return_value=True |
| 226 | ): |
| 227 | with mock.patch.object( |
| 228 | self._superproject, "_LsTree", return_value=data |
| 229 | ): |
| 230 | commit_ids_result = ( |
| 231 | self._superproject._GetAllProjectsCommitIds() |
| 232 | ) |
| 233 | self.assertEqual( |
| 234 | commit_ids_result.commit_ids, |
| 235 | { |
| 236 | "art": "2c2724cb36cd5a9cec6c852c681efc3b7c6b86ea", |
| 237 | "bootable/recovery": "e9d25da64d8d365dbba7c8ee00fe8c4473fe9a06", |
| 238 | "build/bazel": "ade9b7a0d874e25fff4bf2552488825c6f111928", |
| 239 | }, |
| 240 | ) |
| 241 | self.assertFalse(commit_ids_result.fatal) |
| 242 | |
| 243 | def test_superproject_write_manifest_file(self): |
| 244 | """Test with writing manifest to a file after setting revisionId.""" |
| 245 | self.assertEqual(len(self._superproject._manifest.projects), 1) |
| 246 | project = self._superproject._manifest.projects[0] |
| 247 | project.SetRevisionId("ABCDEF") |
| 248 | # Create temporary directory so that it can write the file. |
| 249 | os.mkdir(self._superproject._superproject_path) |
| 250 | manifest_path = self._superproject._WriteManifestFile() |
| 251 | self.assertIsNotNone(manifest_path) |
Jason R. Coombs | 034950b | 2023-10-20 23:32:02 +0545 | [diff] [blame] | 252 | with open(manifest_path) as fp: |
Raman Tenneti | 78f4dd3 | 2021-06-07 13:27:37 -0700 | [diff] [blame] | 253 | manifest_xml_data = fp.read() |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 254 | self.assertEqual( |
| 255 | sort_attributes(manifest_xml_data), |
| 256 | '<?xml version="1.0" ?><manifest>' |
| 257 | '<remote fetch="http://localhost" name="default-remote"/>' |
| 258 | '<default remote="default-remote" revision="refs/heads/main"/>' |
| 259 | '<project groups="notdefault,platform-' + self.platform + '" ' |
| 260 | 'name="platform/art" path="art" revision="ABCDEF" upstream="refs/heads/main"/>' |
| 261 | '<superproject name="superproject"/>' |
| 262 | "</manifest>", |
| 263 | ) |
Raman Tenneti | feb2891 | 2021-05-02 19:47:29 -0700 | [diff] [blame] | 264 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 265 | def test_superproject_update_project_revision_id(self): |
| 266 | """Test with LsTree being a mock.""" |
| 267 | self.assertEqual(len(self._superproject._manifest.projects), 1) |
| 268 | projects = self._superproject._manifest.projects |
| 269 | data = ( |
| 270 | "160000 commit 2c2724cb36cd5a9cec6c852c681efc3b7c6b86ea\tart\x00" |
| 271 | "160000 commit e9d25da64d8d365dbba7c8ee00fe8c4473fe9a06\tbootable/recovery\x00" |
| 272 | ) |
| 273 | with mock.patch.object(self._superproject, "_Init", return_value=True): |
| 274 | with mock.patch.object( |
| 275 | self._superproject, "_Fetch", return_value=True |
| 276 | ): |
| 277 | with mock.patch.object( |
| 278 | self._superproject, "_LsTree", return_value=data |
| 279 | ): |
| 280 | # Create temporary directory so that it can write the file. |
| 281 | os.mkdir(self._superproject._superproject_path) |
| 282 | update_result = self._superproject.UpdateProjectsRevisionId( |
| 283 | projects, self.git_event_log |
| 284 | ) |
| 285 | self.assertIsNotNone(update_result.manifest_path) |
| 286 | self.assertFalse(update_result.fatal) |
Jason R. Coombs | 034950b | 2023-10-20 23:32:02 +0545 | [diff] [blame] | 287 | with open(update_result.manifest_path) as fp: |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 288 | manifest_xml_data = fp.read() |
| 289 | self.assertEqual( |
| 290 | sort_attributes(manifest_xml_data), |
| 291 | '<?xml version="1.0" ?><manifest>' |
| 292 | '<remote fetch="http://localhost" name="default-remote"/>' |
| 293 | '<default remote="default-remote" revision="refs/heads/main"/>' |
| 294 | '<project groups="notdefault,platform-' |
| 295 | + self.platform |
| 296 | + '" ' |
| 297 | 'name="platform/art" path="art" ' |
| 298 | 'revision="2c2724cb36cd5a9cec6c852c681efc3b7c6b86ea" upstream="refs/heads/main"/>' |
| 299 | '<superproject name="superproject"/>' |
| 300 | "</manifest>", |
| 301 | ) |
| 302 | |
| 303 | def test_superproject_update_project_revision_id_no_superproject_tag(self): |
| 304 | """Test update of commit ids of a manifest without superproject tag.""" |
| 305 | manifest = self.getXmlManifest( |
| 306 | """ |
Raman Tenneti | 784e16f | 2021-06-11 17:29:45 -0700 | [diff] [blame] | 307 | <manifest> |
| 308 | <remote name="default-remote" fetch="http://localhost" /> |
| 309 | <default remote="default-remote" revision="refs/heads/main" /> |
| 310 | <project name="test-name"/> |
| 311 | </manifest> |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 312 | """ |
| 313 | ) |
| 314 | self.maxDiff = None |
| 315 | self.assertIsNone(manifest.superproject) |
| 316 | self.assertEqual( |
| 317 | sort_attributes(manifest.ToXml().toxml()), |
| 318 | '<?xml version="1.0" ?><manifest>' |
| 319 | '<remote fetch="http://localhost" name="default-remote"/>' |
| 320 | '<default remote="default-remote" revision="refs/heads/main"/>' |
| 321 | '<project name="test-name"/>' |
| 322 | "</manifest>", |
| 323 | ) |
Raman Tenneti | 784e16f | 2021-06-11 17:29:45 -0700 | [diff] [blame] | 324 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 325 | def test_superproject_update_project_revision_id_from_local_manifest_group( |
| 326 | self, |
| 327 | ): |
| 328 | """Test update of commit ids of a manifest that have local manifest no superproject group.""" |
| 329 | local_group = manifest_xml.LOCAL_MANIFEST_GROUP_PREFIX + ":local" |
| 330 | manifest = self.getXmlManifest( |
| 331 | """ |
Raman Tenneti | 78f4dd3 | 2021-06-07 13:27:37 -0700 | [diff] [blame] | 332 | <manifest> |
| 333 | <remote name="default-remote" fetch="http://localhost" /> |
| 334 | <remote name="goog" fetch="http://localhost2" /> |
| 335 | <default remote="default-remote" revision="refs/heads/main" /> |
| 336 | <superproject name="superproject"/> |
| 337 | <project path="vendor/x" name="platform/vendor/x" remote="goog" |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 338 | groups=\"""" |
| 339 | + local_group |
| 340 | + """ |
Raman Tenneti | 78f4dd3 | 2021-06-07 13:27:37 -0700 | [diff] [blame] | 341 | " revision="master-with-vendor" clone-depth="1" /> |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 342 | <project path="art" name="platform/art" groups="notdefault,platform-""" |
| 343 | + self.platform |
| 344 | + """ |
Raman Tenneti | 78f4dd3 | 2021-06-07 13:27:37 -0700 | [diff] [blame] | 345 | " /></manifest> |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 346 | """ |
| 347 | ) |
| 348 | self.maxDiff = None |
| 349 | self._superproject = git_superproject.Superproject( |
| 350 | manifest, |
| 351 | name="superproject", |
| 352 | remote=manifest.remotes.get("default-remote").ToRemoteSpec( |
| 353 | "superproject" |
| 354 | ), |
| 355 | revision="refs/heads/main", |
| 356 | ) |
| 357 | self.assertEqual(len(self._superproject._manifest.projects), 2) |
| 358 | projects = self._superproject._manifest.projects |
| 359 | data = "160000 commit 2c2724cb36cd5a9cec6c852c681efc3b7c6b86ea\tart\x00" |
| 360 | with mock.patch.object(self._superproject, "_Init", return_value=True): |
| 361 | with mock.patch.object( |
| 362 | self._superproject, "_Fetch", return_value=True |
| 363 | ): |
| 364 | with mock.patch.object( |
| 365 | self._superproject, "_LsTree", return_value=data |
| 366 | ): |
| 367 | # Create temporary directory so that it can write the file. |
| 368 | os.mkdir(self._superproject._superproject_path) |
| 369 | update_result = self._superproject.UpdateProjectsRevisionId( |
| 370 | projects, self.git_event_log |
| 371 | ) |
| 372 | self.assertIsNotNone(update_result.manifest_path) |
| 373 | self.assertFalse(update_result.fatal) |
Jason R. Coombs | 034950b | 2023-10-20 23:32:02 +0545 | [diff] [blame] | 374 | with open(update_result.manifest_path) as fp: |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 375 | manifest_xml_data = fp.read() |
| 376 | # Verify platform/vendor/x's project revision hasn't |
| 377 | # changed. |
| 378 | self.assertEqual( |
| 379 | sort_attributes(manifest_xml_data), |
| 380 | '<?xml version="1.0" ?><manifest>' |
| 381 | '<remote fetch="http://localhost" name="default-remote"/>' |
| 382 | '<remote fetch="http://localhost2" name="goog"/>' |
| 383 | '<default remote="default-remote" revision="refs/heads/main"/>' |
| 384 | '<project groups="notdefault,platform-' |
| 385 | + self.platform |
| 386 | + '" ' |
| 387 | 'name="platform/art" path="art" ' |
| 388 | 'revision="2c2724cb36cd5a9cec6c852c681efc3b7c6b86ea" upstream="refs/heads/main"/>' |
| 389 | '<superproject name="superproject"/>' |
| 390 | "</manifest>", |
| 391 | ) |
Raman Tenneti | 78f4dd3 | 2021-06-07 13:27:37 -0700 | [diff] [blame] | 392 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 393 | def test_superproject_update_project_revision_id_with_pinned_manifest(self): |
| 394 | """Test update of commit ids of a pinned manifest.""" |
| 395 | manifest = self.getXmlManifest( |
| 396 | """ |
Raman Tenneti | 1da6f30 | 2021-06-28 19:21:38 -0700 | [diff] [blame] | 397 | <manifest> |
| 398 | <remote name="default-remote" fetch="http://localhost" /> |
| 399 | <default remote="default-remote" revision="refs/heads/main" /> |
| 400 | <superproject name="superproject"/> |
| 401 | <project path="vendor/x" name="platform/vendor/x" revision="" /> |
| 402 | <project path="vendor/y" name="platform/vendor/y" |
| 403 | revision="52d3c9f7c107839ece2319d077de0cd922aa9d8f" /> |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 404 | <project path="art" name="platform/art" groups="notdefault,platform-""" |
| 405 | + self.platform |
| 406 | + """ |
Raman Tenneti | 1da6f30 | 2021-06-28 19:21:38 -0700 | [diff] [blame] | 407 | " /></manifest> |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 408 | """ |
| 409 | ) |
| 410 | self.maxDiff = None |
| 411 | self._superproject = git_superproject.Superproject( |
| 412 | manifest, |
| 413 | name="superproject", |
| 414 | remote=manifest.remotes.get("default-remote").ToRemoteSpec( |
| 415 | "superproject" |
| 416 | ), |
| 417 | revision="refs/heads/main", |
| 418 | ) |
| 419 | self.assertEqual(len(self._superproject._manifest.projects), 3) |
| 420 | projects = self._superproject._manifest.projects |
| 421 | data = ( |
| 422 | "160000 commit 2c2724cb36cd5a9cec6c852c681efc3b7c6b86ea\tart\x00" |
| 423 | "160000 commit e9d25da64d8d365dbba7c8ee00fe8c4473fe9a06\tvendor/x\x00" |
| 424 | ) |
| 425 | with mock.patch.object(self._superproject, "_Init", return_value=True): |
| 426 | with mock.patch.object( |
| 427 | self._superproject, "_Fetch", return_value=True |
| 428 | ): |
| 429 | with mock.patch.object( |
| 430 | self._superproject, "_LsTree", return_value=data |
| 431 | ): |
| 432 | # Create temporary directory so that it can write the file. |
| 433 | os.mkdir(self._superproject._superproject_path) |
| 434 | update_result = self._superproject.UpdateProjectsRevisionId( |
| 435 | projects, self.git_event_log |
| 436 | ) |
| 437 | self.assertIsNotNone(update_result.manifest_path) |
| 438 | self.assertFalse(update_result.fatal) |
Jason R. Coombs | 034950b | 2023-10-20 23:32:02 +0545 | [diff] [blame] | 439 | with open(update_result.manifest_path) as fp: |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 440 | manifest_xml_data = fp.read() |
| 441 | # Verify platform/vendor/x's project revision hasn't |
| 442 | # changed. |
| 443 | self.assertEqual( |
| 444 | sort_attributes(manifest_xml_data), |
| 445 | '<?xml version="1.0" ?><manifest>' |
| 446 | '<remote fetch="http://localhost" name="default-remote"/>' |
| 447 | '<default remote="default-remote" revision="refs/heads/main"/>' |
| 448 | '<project groups="notdefault,platform-' |
| 449 | + self.platform |
| 450 | + '" ' |
| 451 | 'name="platform/art" path="art" ' |
| 452 | 'revision="2c2724cb36cd5a9cec6c852c681efc3b7c6b86ea" upstream="refs/heads/main"/>' |
| 453 | '<project name="platform/vendor/x" path="vendor/x" ' |
| 454 | 'revision="e9d25da64d8d365dbba7c8ee00fe8c4473fe9a06" upstream="refs/heads/main"/>' |
| 455 | '<project name="platform/vendor/y" path="vendor/y" ' |
| 456 | 'revision="52d3c9f7c107839ece2319d077de0cd922aa9d8f"/>' |
| 457 | '<superproject name="superproject"/>' |
| 458 | "</manifest>", |
| 459 | ) |
Joanna Wang | 0e4f1e7 | 2022-12-08 17:46:28 -0500 | [diff] [blame] | 460 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 461 | def test_Fetch(self): |
| 462 | manifest = self.getXmlManifest( |
| 463 | """ |
Joanna Wang | 0e4f1e7 | 2022-12-08 17:46:28 -0500 | [diff] [blame] | 464 | <manifest> |
| 465 | <remote name="default-remote" fetch="http://localhost" /> |
| 466 | <default remote="default-remote" revision="refs/heads/main" /> |
| 467 | <superproject name="superproject"/> |
| 468 | " /></manifest> |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 469 | """ |
| 470 | ) |
| 471 | self.maxDiff = None |
| 472 | self._superproject = git_superproject.Superproject( |
| 473 | manifest, |
| 474 | name="superproject", |
| 475 | remote=manifest.remotes.get("default-remote").ToRemoteSpec( |
| 476 | "superproject" |
| 477 | ), |
| 478 | revision="refs/heads/main", |
| 479 | ) |
| 480 | os.mkdir(self._superproject._superproject_path) |
| 481 | os.mkdir(self._superproject._work_git) |
| 482 | with mock.patch.object(self._superproject, "_Init", return_value=True): |
| 483 | with mock.patch( |
| 484 | "git_superproject.GitCommand", autospec=True |
| 485 | ) as mock_git_command: |
| 486 | with mock.patch( |
| 487 | "git_superproject.GitRefs.get", autospec=True |
| 488 | ) as mock_git_refs: |
| 489 | instance = mock_git_command.return_value |
| 490 | instance.Wait.return_value = 0 |
| 491 | mock_git_refs.side_effect = ["", "1234"] |
Joanna Wang | 0e4f1e7 | 2022-12-08 17:46:28 -0500 | [diff] [blame] | 492 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 493 | self.assertTrue(self._superproject._Fetch()) |
| 494 | self.assertEqual( |
Daniel Kutik | da6ae1d | 2023-08-15 18:00:10 +0200 | [diff] [blame] | 495 | # TODO: Once we require Python 3.8+, |
| 496 | # use 'mock_git_command.call_args.args'. |
| 497 | mock_git_command.call_args[0], |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 498 | ( |
| 499 | None, |
| 500 | [ |
| 501 | "fetch", |
| 502 | "http://localhost/superproject", |
| 503 | "--depth", |
| 504 | "1", |
| 505 | "--force", |
| 506 | "--no-tags", |
| 507 | "--filter", |
| 508 | "blob:none", |
| 509 | "refs/heads/main:refs/heads/main", |
| 510 | ], |
| 511 | ), |
| 512 | ) |
Joanna Wang | 0e4f1e7 | 2022-12-08 17:46:28 -0500 | [diff] [blame] | 513 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 514 | # If branch for revision exists, set as --negotiation-tip. |
| 515 | self.assertTrue(self._superproject._Fetch()) |
| 516 | self.assertEqual( |
Daniel Kutik | da6ae1d | 2023-08-15 18:00:10 +0200 | [diff] [blame] | 517 | # TODO: Once we require Python 3.8+, |
| 518 | # use 'mock_git_command.call_args.args'. |
| 519 | mock_git_command.call_args[0], |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 520 | ( |
| 521 | None, |
| 522 | [ |
| 523 | "fetch", |
| 524 | "http://localhost/superproject", |
| 525 | "--depth", |
| 526 | "1", |
| 527 | "--force", |
| 528 | "--no-tags", |
| 529 | "--filter", |
| 530 | "blob:none", |
| 531 | "--negotiation-tip", |
| 532 | "1234", |
| 533 | "refs/heads/main:refs/heads/main", |
| 534 | ], |
| 535 | ), |
| 536 | ) |