~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/per_workingtree/test_check_state.py

(vila) Fix bzrlib.tests.test_gpg.TestVerify.test_verify_revoked_signature
 with recent versions of gpg. (Vincent Ladeuil)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 (
 
20
    errors,
 
21
    tests,
 
22
    )
 
23
from bzrlib.tests.per_workingtree import TestCaseWithWorkingTree
 
24
 
 
25
 
 
26
 
 
27
class TestCaseWithState(TestCaseWithWorkingTree):
 
28
 
 
29
    def make_tree_with_broken_dirstate(self, path):
 
30
        tree = self.make_branch_and_tree(path)
 
31
        self.break_dirstate(tree)
 
32
        return tree
 
33
 
 
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')
 
39
        tree.lock_read()
 
40
        try:
 
41
            dirstate = tree.current_dirstate()
 
42
            dirstate_path = dirstate._filename
 
43
            self.assertPathExists(dirstate_path)
 
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.
 
48
        if completely:
 
49
            f = open(dirstate_path, 'wb')
 
50
        else:
 
51
            f = open(dirstate_path, 'ab')
 
52
        try:
 
53
            f.write('garbage-at-end-of-file\n')
 
54
        finally:
 
55
            f.close()
 
56
 
 
57
 
 
58
class TestCheckState(TestCaseWithState):
 
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.
 
64
        tree.check_state()
 
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
 
 
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')
 
78
        return tree
 
79
 
 
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'))
 
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'))
 
91
        self.assertPathDoesNotExist('tree/foo')
 
92
        self.assertPathExists('tree/baz')
 
93
 
 
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)
 
98
        tree.reset_state()
 
99
        tree.check_state()
 
100
        self.assertEqual(rev_id, tree.last_revision())
 
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()
 
110
        self.assertEqual(rev_id, tree.last_revision())