1
# Copyright (C) 2011 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
"""Tests for WorkingTree.check_state."""
23
from bzrlib.tests.per_workingtree import TestCaseWithWorkingTree
27
class TestCaseWithState(TestCaseWithWorkingTree):
29
def make_tree_with_broken_dirstate(self, path):
30
tree = self.make_branch_and_tree(path)
31
self.break_dirstate(tree)
34
def break_dirstate(self, tree, completely=False):
35
"""Write garbage into the dirstate file."""
36
if getattr(tree, 'current_dirstate', None) is None:
37
raise tests.TestNotApplicable(
38
'Only applies to dirstate-based trees')
41
dirstate = tree.current_dirstate()
42
dirstate_path = dirstate._filename
43
self.assertPathExists(dirstate_path)
46
# We have to have the tree unlocked at this point, so we can safely
47
# mutate the state file on all platforms.
49
f = open(dirstate_path, 'wb')
51
f = open(dirstate_path, 'ab')
53
f.write('garbage-at-end-of-file\n')
58
class TestCheckState(TestCaseWithState):
60
def test_check_state(self):
61
tree = self.make_branch_and_tree('tree')
62
# Everything should be fine with an unmodified tree, no exception
66
def test_check_broken_dirstate(self):
67
tree = self.make_tree_with_broken_dirstate('tree')
68
self.assertRaises(errors.BzrError, tree.check_state)
71
class TestResetState(TestCaseWithState):
73
def make_initial_tree(self):
74
tree = self.make_branch_and_tree('tree')
75
self.build_tree(['tree/foo', 'tree/dir/', 'tree/dir/bar'])
76
tree.add(['foo', 'dir', 'dir/bar'])
77
tree.commit('initial')
80
def test_reset_state_forgets_changes(self):
81
tree = self.make_initial_tree()
82
foo_id = tree.path2id('foo')
83
tree.rename_one('foo', 'baz')
84
self.assertEqual(None, tree.path2id('foo'))
85
self.assertEqual(foo_id, tree.path2id('baz'))
87
# After reset, we should have forgotten about the rename, but we won't
89
self.assertEqual(foo_id, tree.path2id('foo'))
90
self.assertEqual(None, tree.path2id('baz'))
91
self.assertPathDoesNotExist('tree/foo')
92
self.assertPathExists('tree/baz')
94
def test_reset_state_handles_corrupted_dirstate(self):
95
tree = self.make_initial_tree()
96
rev_id = tree.last_revision()
97
self.break_dirstate(tree)
100
self.assertEqual(rev_id, tree.last_revision())
102
def test_reset_state_handles_destroyed_dirstate(self):
103
# If you pass the revision_id, we can handle a completely destroyed
105
tree = self.make_initial_tree()
106
rev_id = tree.last_revision()
107
self.break_dirstate(tree, completely=True)
108
tree.reset_state(revision_ids=[rev_id])
110
self.assertEqual(rev_id, tree.last_revision())