blob: 4e66521b6f1fbdbefeaedbca0aa4e2e61c50f059 [file] [log] [blame]
Raman Tenneti6a872c92021-01-14 19:17:50 -08001# 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 Tenneti784e16f2021-06-11 17:29:45 -070017import json
Raman Tenneti6a872c92021-01-14 19:17:50 -080018import os
Raman Tenneti080877e2021-03-09 15:19:06 -080019import platform
Raman Tenneti6a872c92021-01-14 19:17:50 -080020import tempfile
21import unittest
22from unittest import mock
23
Mike Frysinger64477332023-08-21 21:20:32 -040024from test_manifest_xml import sort_attributes
25
Raman Tenneti6a872c92021-01-14 19:17:50 -080026import git_superproject
Raman Tenneti784e16f2021-06-11 17:29:45 -070027import git_trace2_event_log
Raman Tenneti1fd7bc22021-02-04 14:39:38 -080028import manifest_xml
Raman Tenneti6a872c92021-01-14 19:17:50 -080029
30
31class SuperprojectTestCase(unittest.TestCase):
Gavin Makea2e3302023-03-11 06:46:20 +000032 """TestCase for the Superproject module."""
Raman Tenneti6a872c92021-01-14 19:17:50 -080033
Gavin Makea2e3302023-03-11 06:46:20 +000034 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. Coombsb32ccbb2023-09-29 11:04:49 -040037 FULL_SID_REGEX = rf"^{PARENT_SID_VALUE}/{SELF_SID_REGEX}"
Raman Tenneti784e16f2021-06-11 17:29:45 -070038
Gavin Makea2e3302023-03-11 06:46:20 +000039 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 Tenneti1fd7bc22021-02-04 14:39:38 -080049
Gavin Makea2e3302023-03-11 06:46:20 +000050 # 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 Tenneti784e16f2021-06-11 17:29:45 -070056
Gavin Makea2e3302023-03-11 06:46:20 +000057 # 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 Tenneti1fd7bc22021-02-04 14:39:38 -080063 url = https://localhost:0/manifest
Gavin Makea2e3302023-03-11 06:46:20 +000064"""
65 )
Raman Tenneti6a872c92021-01-14 19:17:50 -080066
Gavin Makea2e3302023-03-11 06:46:20 +000067 manifest = self.getXmlManifest(
68 """
Raman Tenneti21dce3d2021-02-09 00:26:31 -080069<manifest>
70 <remote name="default-remote" fetch="http://localhost" />
71 <default remote="default-remote" revision="refs/heads/main" />
72 <superproject name="superproject"/>
Gavin Makea2e3302023-03-11 06:46:20 +000073 <project path="art" name="platform/art" groups="notdefault,platform-"""
74 + self.platform
75 + """
Raman Tenneti080877e2021-03-09 15:19:06 -080076 " /></manifest>
Gavin Makea2e3302023-03-11 06:46:20 +000077"""
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 Tenneti21dce3d2021-02-09 00:26:31 -080087
Gavin Makea2e3302023-03-11 06:46:20 +000088 def tearDown(self):
89 """Tear down superproject every time."""
90 self.tempdirobj.cleanup()
Raman Tenneti6a872c92021-01-14 19:17:50 -080091
Gavin Makea2e3302023-03-11 06:46:20 +000092 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 Tenneti1fd7bc22021-02-04 14:39:38 -080097
Gavin Makea2e3302023-03-11 06:46:20 +000098 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 Tenneti784e16f2021-06-11 17:29:45 -0700104
Gavin Makea2e3302023-03-11 06:46:20 +0000105 # 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)
LuK1337aadd12c2023-09-16 09:36:49 +0200111 self.assertRegex(
112 log_entry["time"], r"^\d+-\d+-\d+T\d+:\d+:\d+\.\d+\+00:00$"
113 )
Raman Tenneti784e16f2021-06-11 17:29:45 -0700114
Gavin Makea2e3302023-03-11 06:46:20 +0000115 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 Tenneti784e16f2021-06-11 17:29:45 -0700122
Gavin Makea2e3302023-03-11 06:46:20 +0000123 def verifyErrorEvent(self):
124 """Helper to verify that error event is written."""
Raman Tenneti784e16f2021-06-11 17:29:45 -0700125
Gavin Makea2e3302023-03-11 06:46:20 +0000126 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 Tenneti784e16f2021-06-11 17:29:45 -0700129
Gavin Makea2e3302023-03-11 06:46:20 +0000130 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 Tenneti784e16f2021-06-11 17:29:45 -0700137
Gavin Makea2e3302023-03-11 06:46:20 +0000138 def test_superproject_get_superproject_no_superproject(self):
139 """Test with no url."""
140 manifest = self.getXmlManifest(
141 """
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800142<manifest>
143</manifest>
Gavin Makea2e3302023-03-11 06:46:20 +0000144"""
145 )
146 self.assertIsNone(manifest.superproject)
Raman Tenneti6a872c92021-01-14 19:17:50 -0800147
Gavin Makea2e3302023-03-11 06:46:20 +0000148 def test_superproject_get_superproject_invalid_url(self):
149 """Test with an invalid url."""
150 manifest = self.getXmlManifest(
151 """
Raman Tenneti21dce3d2021-02-09 00:26:31 -0800152<manifest>
153 <remote name="test-remote" fetch="localhost" />
154 <default remote="test-remote" revision="refs/heads/main" />
155 <superproject name="superproject"/>
156</manifest>
Gavin Makea2e3302023-03-11 06:46:20 +0000157"""
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 Tenneti784e16f2021-06-11 17:29:45 -0700168 self.assertFalse(sync_result.success)
169 self.assertTrue(sync_result.fatal)
Raman Tenneti7caa3652021-02-01 11:15:16 -0800170
Gavin Makea2e3302023-03-11 06:46:20 +0000171 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 Tenneti6a872c92021-01-14 19:17:50 -0800195
Gavin Makea2e3302023-03-11 06:46:20 +0000196 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 Tenneti1fd7bc22021-02-04 14:39:38 -0800202
Gavin Makea2e3302023-03-11 06:46:20 +0000203 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. Coombs034950b2023-10-20 23:32:02 +0545252 with open(manifest_path) as fp:
Raman Tenneti78f4dd32021-06-07 13:27:37 -0700253 manifest_xml_data = fp.read()
Gavin Makea2e3302023-03-11 06:46:20 +0000254 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 Tennetifeb28912021-05-02 19:47:29 -0700264
Gavin Makea2e3302023-03-11 06:46:20 +0000265 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. Coombs034950b2023-10-20 23:32:02 +0545287 with open(update_result.manifest_path) as fp:
Gavin Makea2e3302023-03-11 06:46:20 +0000288 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 Tenneti784e16f2021-06-11 17:29:45 -0700307<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 Makea2e3302023-03-11 06:46:20 +0000312"""
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 Tenneti784e16f2021-06-11 17:29:45 -0700324
Gavin Makea2e3302023-03-11 06:46:20 +0000325 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 Tenneti78f4dd32021-06-07 13:27:37 -0700332<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 Makea2e3302023-03-11 06:46:20 +0000338 groups=\""""
339 + local_group
340 + """
Raman Tenneti78f4dd32021-06-07 13:27:37 -0700341 " revision="master-with-vendor" clone-depth="1" />
Gavin Makea2e3302023-03-11 06:46:20 +0000342 <project path="art" name="platform/art" groups="notdefault,platform-"""
343 + self.platform
344 + """
Raman Tenneti78f4dd32021-06-07 13:27:37 -0700345 " /></manifest>
Gavin Makea2e3302023-03-11 06:46:20 +0000346"""
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. Coombs034950b2023-10-20 23:32:02 +0545374 with open(update_result.manifest_path) as fp:
Gavin Makea2e3302023-03-11 06:46:20 +0000375 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 Tenneti78f4dd32021-06-07 13:27:37 -0700392
Gavin Makea2e3302023-03-11 06:46:20 +0000393 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 Tenneti1da6f302021-06-28 19:21:38 -0700397<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 Makea2e3302023-03-11 06:46:20 +0000404 <project path="art" name="platform/art" groups="notdefault,platform-"""
405 + self.platform
406 + """
Raman Tenneti1da6f302021-06-28 19:21:38 -0700407 " /></manifest>
Gavin Makea2e3302023-03-11 06:46:20 +0000408"""
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. Coombs034950b2023-10-20 23:32:02 +0545439 with open(update_result.manifest_path) as fp:
Gavin Makea2e3302023-03-11 06:46:20 +0000440 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 Wang0e4f1e72022-12-08 17:46:28 -0500460
Gavin Makea2e3302023-03-11 06:46:20 +0000461 def test_Fetch(self):
462 manifest = self.getXmlManifest(
463 """
Joanna Wang0e4f1e72022-12-08 17:46:28 -0500464<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 Makea2e3302023-03-11 06:46:20 +0000469"""
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 Wang0e4f1e72022-12-08 17:46:28 -0500492
Gavin Makea2e3302023-03-11 06:46:20 +0000493 self.assertTrue(self._superproject._Fetch())
494 self.assertEqual(
Daniel Kutikda6ae1d2023-08-15 18:00:10 +0200495 # TODO: Once we require Python 3.8+,
496 # use 'mock_git_command.call_args.args'.
497 mock_git_command.call_args[0],
Gavin Makea2e3302023-03-11 06:46:20 +0000498 (
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 Wang0e4f1e72022-12-08 17:46:28 -0500513
Gavin Makea2e3302023-03-11 06:46:20 +0000514 # If branch for revision exists, set as --negotiation-tip.
515 self.assertTrue(self._superproject._Fetch())
516 self.assertEqual(
Daniel Kutikda6ae1d2023-08-15 18:00:10 +0200517 # TODO: Once we require Python 3.8+,
518 # use 'mock_git_command.call_args.args'.
519 mock_git_command.call_args[0],
Gavin Makea2e3302023-03-11 06:46:20 +0000520 (
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 )