def test_superproject_update_project_revision_id_no_superproject_tag(self): """Test update of commit ids of a manifest without superproject tag.""" manifest = self.getXmlManifest(""" <manifest> <remote name="default-remote" fetch="http://localhost" /> <default remote="default-remote" revision="refs/heads/main" /> <project name="test-name"/> </manifest> """) self.maxDiff = None self._superproject = git_superproject.Superproject( manifest, self.repodir, self.git_event_log) self.assertEqual(len(self._superproject._manifest.projects), 1) projects = self._superproject._manifest.projects project = projects[0] project.SetRevisionId('ABCDEF') update_result = self._superproject.UpdateProjectsRevisionId(projects) self.assertIsNone(update_result.manifest_path) self.assertFalse(update_result.fatal) self.verifyErrorEvent() self.assertEqual( sort_attributes(manifest.ToXml().toxml()), '<?xml version="1.0" ?><manifest>' '<remote fetch="http://localhost" name="default-remote"/>' '<default remote="default-remote" revision="refs/heads/main"/>' '<project name="test-name" revision="ABCDEF" upstream="refs/heads/main"/>' '</manifest>')
def setUp(self): """Set up superproject every time.""" self.tempdir = tempfile.mkdtemp(prefix='repo_tests') self.repodir = os.path.join(self.tempdir, '.repo') self.manifest_file = os.path.join(self.repodir, manifest_xml.MANIFEST_FILE_NAME) os.mkdir(self.repodir) self.platform = platform.system().lower() # By default we initialize with the expected case where # repo launches us (so GIT_TRACE2_PARENT_SID is set). env = { self.PARENT_SID_KEY: self.PARENT_SID_VALUE, } self.git_event_log = git_trace2_event_log.EventLog(env=env) # The manifest parsing really wants a git repo currently. gitdir = os.path.join(self.repodir, 'manifests.git') os.mkdir(gitdir) with open(os.path.join(gitdir, 'config'), 'w') as fp: fp.write("""[remote "origin"] url = https://localhost:0/manifest """) manifest = self.getXmlManifest(""" <manifest> <remote name="default-remote" fetch="http://localhost" /> <default remote="default-remote" revision="refs/heads/main" /> <superproject name="superproject"/> <project path="art" name="platform/art" groups="notdefault,platform-""" + self.platform + """ " /></manifest> """) self._superproject = git_superproject.Superproject( manifest, self.repodir, self.git_event_log)
def setUp(self): """Set up superproject every time.""" self.tempdir = tempfile.mkdtemp(prefix='repo_tests') self.repodir = os.path.join(self.tempdir, '.repo') self.manifest_file = os.path.join( self.repodir, manifest_xml.MANIFEST_FILE_NAME) os.mkdir(self.repodir) self.platform = platform.system().lower() # The manifest parsing really wants a git repo currently. gitdir = os.path.join(self.repodir, 'manifests.git') os.mkdir(gitdir) with open(os.path.join(gitdir, 'config'), 'w') as fp: fp.write("""[remote "origin"] url = https://localhost:0/manifest """) manifest = self.getXmlManifest(""" <manifest> <remote name="default-remote" fetch="http://localhost" /> <default remote="default-remote" revision="refs/heads/main" /> <superproject name="superproject"/> <project path="art" name="platform/art" groups="notdefault,platform-""" + self.platform + """ " /></manifest> """) self._superproject = git_superproject.Superproject(manifest, self.repodir)
def _UpdateProjectsRevisionId(self, opt, args): """Update revisionId of every project with the SHA from superproject. This function updates each project's revisionId with SHA from superproject. It writes the updated manifest into a file and reloads the manifest from it. Args: opt: Program options returned from optparse. See _Options(). args: Arguments to pass to GetProjects. See the GetProjects docstring for details. Returns: Returns path to the overriding manifest file. """ superproject = git_superproject.Superproject(self.manifest, self.repodir, quiet=opt.quiet) all_projects = self.GetProjects(args, missing_ok=True, submodules_ok=opt.fetch_submodules) manifest_path = superproject.UpdateProjectsRevisionId(all_projects) if not manifest_path: print('error: Update of revsionId from superproject has failed', file=sys.stderr) sys.exit(1) self._ReloadManifest(manifest_path) return manifest_path
def test_superproject_get_superproject_no_superproject(self): """Test with no url.""" manifest = self.getXmlManifest(""" <manifest> </manifest> """) superproject = git_superproject.Superproject(manifest, self.repodir) self.assertFalse(superproject.Sync())
def test_superproject_update_project_revision_id_from_local_manifest_group( self): """Test update of commit ids of a manifest that have local manifest no superproject group.""" local_group = manifest_xml.LOCAL_MANIFEST_GROUP_PREFIX + ':local' manifest = self.getXmlManifest(""" <manifest> <remote name="default-remote" fetch="http://localhost" /> <remote name="goog" fetch="http://localhost2" /> <default remote="default-remote" revision="refs/heads/main" /> <superproject name="superproject"/> <project path="vendor/x" name="platform/vendor/x" remote="goog" groups=\"""" + local_group + """ " revision="master-with-vendor" clone-depth="1" /> <project path="art" name="platform/art" groups="notdefault,platform-""" + self.platform + """ " /></manifest> """) self.maxDiff = None self._superproject = git_superproject.Superproject( manifest, self.repodir, self.git_event_log) self.assertEqual(len(self._superproject._manifest.projects), 2) projects = self._superproject._manifest.projects data = ( '160000 commit 2c2724cb36cd5a9cec6c852c681efc3b7c6b86ea\tart\x00') with mock.patch.object(self._superproject, '_Init', return_value=True): with mock.patch.object(self._superproject, '_Fetch', return_value=True): with mock.patch.object(self._superproject, '_LsTree', return_value=data): # Create temporary directory so that it can write the file. os.mkdir(self._superproject._superproject_path) update_result = self._superproject.UpdateProjectsRevisionId( projects) self.assertIsNotNone(update_result.manifest_path) self.assertFalse(update_result.fatal) with open(update_result.manifest_path, 'r') as fp: manifest_xml_data = fp.read() # Verify platform/vendor/x's project revision hasn't changed. self.assertEqual( sort_attributes(manifest_xml_data), '<?xml version="1.0" ?><manifest>' '<remote fetch="http://localhost" name="default-remote"/>' '<remote fetch="http://localhost2" name="goog"/>' '<default remote="default-remote" revision="refs/heads/main"/>' '<project groups="notdefault,platform-' + self.platform + '" ' 'name="platform/art" path="art" ' 'revision="2c2724cb36cd5a9cec6c852c681efc3b7c6b86ea" upstream="refs/heads/main"/>' '<project clone-depth="1" groups="' + local_group + '" ' 'name="platform/vendor/x" path="vendor/x" remote="goog" ' 'revision="master-with-vendor"/>' '<superproject name="superproject"/>' '</manifest>')
def test_superproject_update_project_revision_id_with_pinned_manifest( self): """Test update of commit ids of a pinned manifest.""" manifest = self.getXmlManifest(""" <manifest> <remote name="default-remote" fetch="http://localhost" /> <default remote="default-remote" revision="refs/heads/main" /> <superproject name="superproject"/> <project path="vendor/x" name="platform/vendor/x" revision="" /> <project path="vendor/y" name="platform/vendor/y" revision="52d3c9f7c107839ece2319d077de0cd922aa9d8f" /> <project path="art" name="platform/art" groups="notdefault,platform-""" + self.platform + """ " /></manifest> """) self.maxDiff = None self._superproject = git_superproject.Superproject( manifest, self.repodir, self.git_event_log) self.assertEqual(len(self._superproject._manifest.projects), 3) projects = self._superproject._manifest.projects data = ( '160000 commit 2c2724cb36cd5a9cec6c852c681efc3b7c6b86ea\tart\x00' '160000 commit e9d25da64d8d365dbba7c8ee00fe8c4473fe9a06\tvendor/x\x00' ) with mock.patch.object(self._superproject, '_Init', return_value=True): with mock.patch.object(self._superproject, '_Fetch', return_value=True): with mock.patch.object(self._superproject, '_LsTree', return_value=data): # Create temporary directory so that it can write the file. os.mkdir(self._superproject._superproject_path) update_result = self._superproject.UpdateProjectsRevisionId( projects) self.assertIsNotNone(update_result.manifest_path) self.assertFalse(update_result.fatal) with open(update_result.manifest_path, 'r') as fp: manifest_xml_data = fp.read() # Verify platform/vendor/x's project revision hasn't changed. self.assertEqual( sort_attributes(manifest_xml_data), '<?xml version="1.0" ?><manifest>' '<remote fetch="http://localhost" name="default-remote"/>' '<default remote="default-remote" revision="refs/heads/main"/>' '<project groups="notdefault,platform-' + self.platform + '" ' 'name="platform/art" path="art" ' 'revision="2c2724cb36cd5a9cec6c852c681efc3b7c6b86ea" upstream="refs/heads/main"/>' '<project name="platform/vendor/x" path="vendor/x" ' 'revision="e9d25da64d8d365dbba7c8ee00fe8c4473fe9a06" upstream="refs/heads/main"/>' '<project name="platform/vendor/y" path="vendor/y" ' 'revision="52d3c9f7c107839ece2319d077de0cd922aa9d8f"/>' '<superproject name="superproject"/>' '</manifest>')
def test_superproject_get_superproject_invalid_url(self): """Test with an invalid url.""" manifest = self.getXmlManifest(""" <manifest> <remote name="test-remote" fetch="localhost" /> <default remote="test-remote" revision="refs/heads/main" /> <superproject name="superproject"/> </manifest> """) superproject = git_superproject.Superproject(manifest, self.repodir) self.assertFalse(superproject.Sync())
def test_superproject_get_superproject_invalid_branch(self): """Test with an invalid branch.""" manifest = self.getXmlManifest(""" <manifest> <remote name="test-remote" fetch="localhost" /> <default remote="test-remote" revision="refs/heads/main" /> <superproject name="superproject"/> </manifest> """) superproject = git_superproject.Superproject(manifest, self.repodir) with mock.patch.object(self._superproject, '_GetBranch', return_value='junk'): self.assertFalse(superproject.Sync())
def _CloneSuperproject(self, opt): """Clone the superproject based on the superproject's url and branch. Args: opt: Program options returned from optparse. See _Options(). """ superproject = git_superproject.Superproject(self.manifest, self.repodir, quiet=opt.quiet) if not superproject.Sync(): print('error: git update of superproject failed', file=sys.stderr) sys.exit(1)
def test_superproject_get_superproject_no_superproject(self): """Test with no url.""" manifest = self.getXmlManifest(""" <manifest> </manifest> """) superproject = git_superproject.Superproject(manifest, self.repodir, self.git_event_log) # Test that exit condition is false when there is no superproject tag. sync_result = superproject.Sync() self.assertFalse(sync_result.success) self.assertFalse(sync_result.fatal) self.verifyErrorEvent()
def test_superproject_get_superproject_invalid_branch(self): """Test with an invalid branch.""" manifest = self.getXmlManifest(""" <manifest> <remote name="test-remote" fetch="localhost" /> <default remote="test-remote" revision="refs/heads/main" /> <superproject name="superproject"/> </manifest> """) self._superproject = git_superproject.Superproject( manifest, self.repodir, self.git_event_log) with mock.patch.object(self._superproject, '_branch', 'junk'): sync_result = self._superproject.Sync() self.assertFalse(sync_result.success) self.assertTrue(sync_result.fatal)
def _UpdateProjectsRevisionId(self, opt, args, load_local_manifests, superproject_logging_data): """Update revisionId of every project with the SHA from superproject. This function updates each project's revisionId with SHA from superproject. It writes the updated manifest into a file and reloads the manifest from it. Args: opt: Program options returned from optparse. See _Options(). args: Arguments to pass to GetProjects. See the GetProjects docstring for details. load_local_manifests: Whether to load local manifests. superproject_logging_data: A dictionary of superproject data that is to be logged. Returns: Returns path to the overriding manifest file instead of None. """ print_messages = git_superproject.PrintMessages(opt, self.manifest) superproject = git_superproject.Superproject(self.manifest, self.repodir, self.git_event_log, quiet=opt.quiet, print_messages=print_messages) if opt.local_only: manifest_path = superproject.manifest_path if manifest_path: self._ReloadManifest(manifest_path, load_local_manifests) return manifest_path all_projects = self.GetProjects(args, missing_ok=True, submodules_ok=opt.fetch_submodules) update_result = superproject.UpdateProjectsRevisionId(all_projects) manifest_path = update_result.manifest_path superproject_logging_data['updatedrevisionid'] = bool(manifest_path) if manifest_path: self._ReloadManifest(manifest_path, load_local_manifests) else: if print_messages: print('warning: Update of revisionId from superproject has failed, ' 'repo sync will not use superproject to fetch the source. ', 'Please resync with the --no-use-superproject option to avoid this repo warning.', file=sys.stderr) if update_result.fatal and opt.use_superproject is not None: sys.exit(1) return manifest_path
def test_superproject_update_project_revision_id_with_different_remotes(self): """Test update of commit ids of a manifest with mutiple remotes.""" manifest = self.getXmlManifest(""" <manifest> <remote name="default-remote" fetch="http://localhost" /> <remote name="goog" fetch="http://localhost2" /> <default remote="default-remote" revision="refs/heads/main" /> <superproject name="superproject"/> <project path="vendor/x" name="platform/vendor/x" remote="goog" groups="vendor" revision="master-with-vendor" clone-depth="1" /> <project path="art" name="platform/art" groups="notdefault,platform-""" + self.platform + """ " /></manifest> """) self.maxDiff = None self._superproject = git_superproject.Superproject(manifest, self.repodir) self.assertEqual(len(self._superproject._manifest.projects), 2) projects = self._superproject._manifest.projects data = ('160000 commit 2c2724cb36cd5a9cec6c852c681efc3b7c6b86ea\tart\x00' '160000 commit e9d25da64d8d365dbba7c8ee00fe8c4473fe9a06\tbootable/recovery\x00') with mock.patch.object(self._superproject, '_Init', return_value=True): with mock.patch.object(self._superproject, '_Fetch', return_value=True): with mock.patch.object(self._superproject, '_LsTree', return_value=data): # Create temporary directory so that it can write the file. os.mkdir(self._superproject._superproject_path) manifest_path = self._superproject.UpdateProjectsRevisionId(projects) self.assertIsNotNone(manifest_path) with open(manifest_path, 'r') as fp: manifest_xml = fp.read() self.assertEqual( sort_attributes(manifest_xml), '<?xml version="1.0" ?><manifest>' '<remote fetch="http://localhost" name="default-remote"/>' '<remote fetch="http://localhost2" name="goog"/>' '<default remote="default-remote" revision="refs/heads/main"/>' '<project groups="notdefault,platform-' + self.platform + '" ' 'name="platform/art" path="art" ' 'revision="2c2724cb36cd5a9cec6c852c681efc3b7c6b86ea"/>' '<project clone-depth="1" groups="vendor" ' 'name="platform/vendor/x" path="vendor/x" remote="goog" ' 'revision="master-with-vendor"/>' '<superproject name="superproject"/>' '</manifest>')
def _CloneSuperproject(self, opt): """Clone the superproject based on the superproject's url and branch. Args: opt: Program options returned from optparse. See _Options(). """ superproject = git_superproject.Superproject(self.manifest, self.repodir, self.git_event_log, quiet=opt.quiet) sync_result = superproject.Sync() if not sync_result.success: print( 'warning: git update of superproject failed, repo sync will not ' 'use superproject to fetch source; while this error is not fatal, ' 'and you can continue to run repo sync, please run repo init with ' 'the --no-use-superproject option to stop seeing this warning', file=sys.stderr) if sync_result.fatal and opt.use_superproject is not None: sys.exit(1)