class DirectBranchCommitTestCase: """Base class for `DirectBranchCommit` tests.""" db_branch = None committer = None def setUp(self): super(DirectBranchCommitTestCase, self).setUp() self.useBzrBranches(direct_database=True) self.series = self.factory.makeProductSeries() self.db_branch, self.tree = self.create_branch_and_tree() self.series.translations_branch = self.db_branch self._setUpCommitter() self.addCleanup(self._tearDownCommitter) def _setUpCommitter(self, update_last_scanned_id=True): """Clean up any existing `DirectBranchCommit`, set up a new one.""" if self.committer: self.committer.unlock() self.committer = DirectBranchCommit(self.db_branch) if update_last_scanned_id: self.committer.last_scanned_id = ( self.committer.bzrbranch.last_revision()) def _tearDownCommitter(self): if self.committer: self.committer.unlock() def _getContents(self): """Return branch contents as dict mapping filenames to contents.""" return map_branch_contents(self.committer.db_branch.getBzrBranch())
def test_uses_committer_id(self): # getBzrCommitterID uses the committer string if provided. bzr_id = self.factory.getUniqueString() committer = DirectBranchCommit( self._makeBranch(), committer_id=bzr_id) self.addCleanup(committer.unlock) self.assertEqual(bzr_id, committer.getBzrCommitterID())
def test_uses_committer_email(self): # getBzrCommitterID returns the committing person's email address # if available (and if no committer string is given). branch = self._makeBranch() committer = DirectBranchCommit(branch) self.addCleanup(committer.unlock) self.assertIn( removeSecurityProxy(branch.owner).preferredemail.email, committer.getBzrCommitterID())
def test_falls_back_to_noreply(self): # If all else fails, getBzrCommitterID uses the noreply # address. team = self.factory.makeTeam() self.assertIs(None, team.preferredemail) branch = self._makeBranch(owner=team) committer = DirectBranchCommit(branch) self.addCleanup(committer.unlock) self.assertIn('noreply', committer.getBzrCommitterID())
def _setUpCommitter(self, update_last_scanned_id=True): """Clean up any existing `DirectBranchCommit`, set up a new one.""" if self.committer: self.committer.unlock() self.committer = DirectBranchCommit(self.db_branch) if update_last_scanned_id: self.committer.last_scanned_id = ( self.committer.bzrbranch.last_revision())
def test_raises_StaleLastMirrored(self): """Raise if the on-disk revision doesn't match last-mirrored.""" self.useBzrBranches(direct_database=True) bzr_id = self.factory.getUniqueString() db_branch, tree = self.create_branch_and_tree() tree.commit('unchanged', committer='*****@*****.**') with ExpectedException(StaleLastMirrored, '.*'): committer = DirectBranchCommit(db_branch, committer_id=bzr_id) self.addCleanup(committer.unlock) committer.commit('')
def test_raises_StaleLastMirrored(self): """Raise if the on-disk revision doesn't match last-mirrored.""" self.useBzrBranches(direct_database=True) bzr_id = self.factory.getUniqueString() db_branch, tree = self.create_branch_and_tree() tree.commit('unchanged', committer='*****@*****.**') with ExpectedException(StaleLastMirrored, '.*'): committer = DirectBranchCommit( db_branch, committer_id=bzr_id) self.addCleanup(committer.unlock) committer.commit('')
def test_scheduleTranslationTemplatesBuild_subscribed(self): # If the feature is enabled, a TipChanged event for a branch that # generates templates will schedule a templates build. branch = self._makeTranslationBranch() removeSecurityProxy(branch).last_scanned_id = 'null:' commit = DirectBranchCommit(branch) commit.writeFile('POTFILES.in', 'foo') commit.commit('message') notify(events.TipChanged(branch, None, False)) branchjobs = list(TranslationTemplatesBuildJob.iterReady()) self.assertEqual(1, len(branchjobs)) self.assertEqual(branch, branchjobs[0].branch)
def _makeDirectBranchCommit(self, db_branch): """Create a `DirectBranchCommit`. :param db_branch: A `Branch` object as defined in Launchpad. :return: A `DirectBranchCommit` for `db_branch`. """ committer_id = 'Launchpad Translations on behalf of %s' % ( db_branch.owner.name) return DirectBranchCommit(db_branch, committer_id=committer_id)
def _createBranch(self, content_map=None): """Create a working branch. :param content_map: optional dict mapping file names to file contents. Each of these files with their contents will be written to the branch. :return: a fresh lp.code.model.Branch backed by a real bzr branch. """ db_branch, tree = self.create_branch_and_tree() populist = DirectBranchCommit(db_branch) last_revision = populist.bzrbranch.last_revision() db_branch.last_scanned_id = populist.last_scanned_id = last_revision if content_map is not None: for name, contents in content_map.iteritems(): populist.writeFile(name, contents) populist.commit("Populating branch.") return db_branch
def commit_file(branch, path, contents, merge_parents=None): """Create a commit that updates a file to specified contents. This will create or modify the file, as needed. """ committer = DirectBranchCommit( removeSecurityProxy(branch), no_race_check=True, merge_parents=merge_parents) committer.writeFile(path, contents) try: return committer.commit('committing') finally: committer.unlock()
def test_scheduleTranslationTemplatesBuild_subscribed(self): # If the feature is enabled, a TipChanged event for a branch that # generates templates will schedule a templates build. branch = self._makeTranslationBranch() removeSecurityProxy(branch).last_scanned_id = 'null:' commit = DirectBranchCommit(branch) commit.writeFile('POTFILES.in', 'foo') commit.commit('message') notify(events.TipChanged(branch, commit.bzrbranch, False)) self.assertEqual( 1, TranslationTemplatesBuild.findByBranch(branch).count())
def test_commit_uses_merge_parents(self): # DirectBranchCommit.commit returns uses merge parents self._tearDownCommitter() # Merge parents cannot be specified for initial commit, so do an # empty commit. self.tree.commit('foo', committer='foo@bar', rev_id=b'foo') self.db_branch.last_mirrored_id = 'foo' committer = DirectBranchCommit( self.db_branch, merge_parents=[b'parent-1', b'parent-2']) committer.last_scanned_id = (committer.bzrbranch.last_revision()) committer.writeFile('file.txt', b'contents') committer.commit('') branch_revision_id = committer.bzrbranch.last_revision() branch_revision = committer.bzrbranch.repository.get_revision( branch_revision_id) self.assertEqual([b'parent-1', b'parent-2'], branch_revision.parent_ids[1:])
def test_commit_uses_merge_parents(self): # DirectBranchCommit.commit returns uses merge parents self._tearDownCommitter() # Merge parents cannot be specified for initial commit, so do an # empty commit. self.tree.commit('foo', committer='foo@bar', rev_id='foo') self.db_branch.last_mirrored_id = 'foo' committer = DirectBranchCommit( self.db_branch, merge_parents=['parent-1', 'parent-2']) committer.last_scanned_id = ( committer.bzrbranch.last_revision()) committer.writeFile('file.txt', 'contents') committer.commit('') branch_revision_id = committer.bzrbranch.last_revision() branch_revision = committer.bzrbranch.repository.get_revision( branch_revision_id) self.assertEqual( ['parent-1', 'parent-2'], branch_revision.parent_ids[1:])
def test_uses_committer_id(self): # getBzrCommitterID uses the committer string if provided. bzr_id = self.factory.getUniqueString() committer = DirectBranchCommit(self._makeBranch(), committer_id=bzr_id) self.addCleanup(committer.unlock) self.assertEqual(bzr_id, committer.getBzrCommitterID())