~bzr-pqm/bzr/bzr.dev

5630.2.2 by John Arbash Meinel
Start fleshing out the design. Something weird is causing my tests to all fail.
1
# Copyright (C) 2011 Canonical Ltd
2
#
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.
7
#
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.
12
#
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
16
17
"""Tests for WorkingTree.check_state."""
18
19
from bzrlib import (
5630.2.4 by John Arbash Meinel
Basically works in the case where the dirstate isn't corrupted.
20
    errors,
5630.2.2 by John Arbash Meinel
Start fleshing out the design. Something weird is causing my tests to all fail.
21
    tests,
22
    )
23
from bzrlib.tests.per_workingtree import TestCaseWithWorkingTree
24
25
5630.2.5 by John Arbash Meinel
(broken) Intermediate state to make reset_tree work even when things are broken.
26
5630.2.4 by John Arbash Meinel
Basically works in the case where the dirstate isn't corrupted.
27
class TestCaseWithState(TestCaseWithWorkingTree):
28
29
    def make_tree_with_broken_dirstate(self, path):
30
        tree = self.make_branch_and_tree(path)
5630.2.5 by John Arbash Meinel
(broken) Intermediate state to make reset_tree work even when things are broken.
31
        self.break_dirstate(tree)
32
        return tree
33
5630.2.6 by John Arbash Meinel
Implement a reset-to-known-state ability for DirState.
34
    def break_dirstate(self, tree, completely=False):
5630.2.5 by John Arbash Meinel
(broken) Intermediate state to make reset_tree work even when things are broken.
35
        """Write garbage into the dirstate file."""
5630.2.4 by John Arbash Meinel
Basically works in the case where the dirstate isn't corrupted.
36
        if getattr(tree, 'current_dirstate', None) is None:
37
            raise tests.TestNotApplicable(
38
                'Only applies to dirstate-based trees')
39
        tree.lock_read()
40
        try:
41
            dirstate = tree.current_dirstate()
42
            dirstate_path = dirstate._filename
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
43
            self.assertPathExists(dirstate_path)
5630.2.4 by John Arbash Meinel
Basically works in the case where the dirstate isn't corrupted.
44
        finally:
45
            tree.unlock()
46
        # We have to have the tree unlocked at this point, so we can safely
47
        # mutate the state file on all platforms.
5630.2.6 by John Arbash Meinel
Implement a reset-to-known-state ability for DirState.
48
        if completely:
49
            f = open(dirstate_path, 'wb')
50
        else:
51
            f = open(dirstate_path, 'ab')
5630.2.4 by John Arbash Meinel
Basically works in the case where the dirstate isn't corrupted.
52
        try:
53
            f.write('garbage-at-end-of-file\n')
54
        finally:
55
            f.close()
56
57
58
class TestCheckState(TestCaseWithState):
5630.2.2 by John Arbash Meinel
Start fleshing out the design. Something weird is causing my tests to all fail.
59
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
63
        # should be raised.
5630.2.3 by John Arbash Meinel
Looks like it was a stale experimental .pyd file causing trouble. Tests pass again now.
64
        tree.check_state()
5630.2.4 by John Arbash Meinel
Basically works in the case where the dirstate isn't corrupted.
65
66
    def test_check_broken_dirstate(self):
67
        tree = self.make_tree_with_broken_dirstate('tree')
68
        self.assertRaises(errors.BzrError, tree.check_state)
69
70
71
class TestResetState(TestCaseWithState):
72
5630.2.6 by John Arbash Meinel
Implement a reset-to-known-state ability for DirState.
73
    def make_initial_tree(self):
5630.2.4 by John Arbash Meinel
Basically works in the case where the dirstate isn't corrupted.
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')
5630.2.6 by John Arbash Meinel
Implement a reset-to-known-state ability for DirState.
78
        return tree
79
80
    def test_reset_state_forgets_changes(self):
81
        tree = self.make_initial_tree()
5630.2.4 by John Arbash Meinel
Basically works in the case where the dirstate isn't corrupted.
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'))
86
        tree.reset_state()
87
        # After reset, we should have forgotten about the rename, but we won't
88
        # have
89
        self.assertEqual(foo_id, tree.path2id('foo'))
90
        self.assertEqual(None, tree.path2id('baz'))
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
91
        self.assertPathDoesNotExist('tree/foo')
92
        self.assertPathExists('tree/baz')
5630.2.5 by John Arbash Meinel
(broken) Intermediate state to make reset_tree work even when things are broken.
93
94
    def test_reset_state_handles_corrupted_dirstate(self):
5630.2.6 by John Arbash Meinel
Implement a reset-to-known-state ability for DirState.
95
        tree = self.make_initial_tree()
5630.2.7 by John Arbash Meinel
Handle --force flags and propmting the user for what they might really want.
96
        rev_id = tree.last_revision()
5630.2.5 by John Arbash Meinel
(broken) Intermediate state to make reset_tree work even when things are broken.
97
        self.break_dirstate(tree)
98
        tree.reset_state()
99
        tree.check_state()
5630.2.7 by John Arbash Meinel
Handle --force flags and propmting the user for what they might really want.
100
        self.assertEqual(rev_id, tree.last_revision())
5630.2.6 by John Arbash Meinel
Implement a reset-to-known-state ability for DirState.
101
102
    def test_reset_state_handles_destroyed_dirstate(self):
103
        # If you pass the revision_id, we can handle a completely destroyed
104
        # dirstate file.
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])
109
        tree.check_state()
5630.2.7 by John Arbash Meinel
Handle --force flags and propmting the user for what they might really want.
110
        self.assertEqual(rev_id, tree.last_revision())