def test_pre_commit_passes(self): empty_delta = delta.TreeDelta() root_delta = delta.TreeDelta() tree = self.make_branch_and_memory_tree('branch') tree.lock_write() tree.add('') root_delta.added = [('', tree.path2id(''), 'directory')] branch.Branch.hooks.install_named_hook("pre_commit", self.capture_pre_commit_hook, None) revid1 = tree.commit('first revision') revid2 = tree.commit('second revision') self.assertEqual( [('pre_commit', 0, revision.NULL_REVISION, 1, revid1, root_delta), ('pre_commit', 1, revid1, 2, revid2, empty_delta)], self.hook_calls) tree.unlock()
def test_pre_commit_fails(self): empty_delta = delta.TreeDelta() root_delta = delta.TreeDelta() tree = self.make_branch_and_memory_tree('branch') tree.lock_write() tree.add('') root_delta.added = [('', tree.path2id(''), 'directory')] class PreCommitException(Exception): pass def hook_func(local, master, old_revno, old_revid, new_revno, new_revid, tree_delta, future_tree): raise PreCommitException(new_revid) branch.Branch.hooks.install_named_hook("pre_commit", self.capture_pre_commit_hook, None) branch.Branch.hooks.install_named_hook("pre_commit", hook_func, None) revids = [None, None, None] # this commit will raise an exception # so the commit is rolled back and revno unchanged err = self.assertRaises(PreCommitException, tree.commit, 'message') # we have to record the revid to use in assertEqual later revids[0] = str(err) # unregister all pre_commit hooks branch.Branch.hooks["pre_commit"] = [] # and re-register the capture hook branch.Branch.hooks.install_named_hook("pre_commit", self.capture_pre_commit_hook, None) # now these commits should go through for i in range(1, 3): revids[i] = tree.commit('message') self.assertEqual( [('pre_commit', 0, revision.NULL_REVISION, 1, revids[0], root_delta), ('pre_commit', 0, revision.NULL_REVISION, 1, revids[1], root_delta), ('pre_commit', 1, revids[1], 2, revids[2], empty_delta)], self.hook_calls) tree.unlock()
def test_kind_change(self): """Doing a status when a file has changed kind should work""" tree = self.make_branch_and_tree('.') self.build_tree(['filename']) tree.add('filename', 'file-id') tree.commit('added filename') os.unlink('filename') self.build_tree(['filename/']) delta = tree.changes_from(tree.basis_tree()) self.assertEqual([('filename', 'file-id', 'file', 'directory')], delta.kind_changed) self.assertEqual([], delta.added) self.assertEqual([], delta.removed) self.assertEqual([], delta.renamed) self.assertEqual([], delta.modified) self.assertEqual([], delta.unchanged) self.assertTrue(delta.has_changed()) self.assertTrue(delta.touches_file_id('file-id')) self.assertEqual('kind changed:\n filename (file => directory)\n', self.show_string(delta)) other_delta = _mod_delta.TreeDelta() self.assertNotEqual(other_delta, delta) other_delta.kind_changed = [('filename', 'file-id', 'file', 'symlink')] self.assertNotEqual(other_delta, delta) other_delta.kind_changed = [('filename', 'file-id', 'file', 'directory')] self.assertEqual(other_delta, delta) self.assertEqualDiff( "TreeDelta(added=[], removed=[], renamed=[]," " kind_changed=[(u'filename', 'file-id', 'file', 'directory')]," " modified=[], unchanged=[], unversioned=[])", repr(delta)) self.assertEqual( 'K filename (file => directory) file-id\n', self.show_string(delta, show_ids=True, short_status=True)) tree.rename_one('filename', 'dirname') delta = tree.changes_from(tree.basis_tree()) self.assertEqual([], delta.kind_changed) # This loses the fact that kind changed, remembering it as a # modification self.assertEqual( [('filename', 'dirname', 'file-id', 'directory', True, False)], delta.renamed) self.assertTrue(delta.has_changed()) self.assertTrue(delta.touches_file_id('file-id'))
def test_pre_commit_delta(self): # This tests the TreeDelta object passed to pre_commit hook. # This does not try to validate data correctness in the delta. self.build_tree(['rootfile', 'dir/', 'dir/subfile']) tree = self.make_branch_and_tree('.') tree.lock_write() try: # setting up a playground tree.set_root_id('root_id') tree.add('rootfile', 'rootfile_id') tree.put_file_bytes_non_atomic('rootfile_id', 'abc') tree.add('dir', 'dir_id') tree.add('dir/subfile', 'dir_subfile_id') tree.mkdir('to_be_unversioned', 'to_be_unversioned_id') tree.put_file_bytes_non_atomic('dir_subfile_id', 'def') revid1 = tree.commit('first revision') finally: tree.unlock() tree.lock_write() try: # making changes tree.put_file_bytes_non_atomic('rootfile_id', 'jkl') tree.rename_one('dir/subfile', 'dir/subfile_renamed') tree.unversion(['to_be_unversioned_id']) tree.mkdir('added_dir', 'added_dir_id') # start to capture pre_commit delta branch.Branch.hooks.install_named_hook( "pre_commit", self.capture_pre_commit_hook, None) revid2 = tree.commit('second revision') finally: tree.unlock() expected_delta = delta.TreeDelta() expected_delta.added = [('added_dir', 'added_dir_id', 'directory')] expected_delta.removed = [('to_be_unversioned', 'to_be_unversioned_id', 'directory')] expected_delta.renamed = [('dir/subfile', 'dir/subfile_renamed', 'dir_subfile_id', 'file', False, False)] expected_delta.modified = [('rootfile', 'rootfile_id', 'file', True, False)] self.assertEqual( [('pre_commit', 1, revid1, 2, revid2, expected_delta)], self.hook_calls)