~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/workingtree_implementations/test_executable.py

refactor test_executable into smaller tests.

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
 
25
25
class TestExecutable(TestCaseWithWorkingTree):
26
26
 
27
 
    def test_stays_executable(self):
28
 
        a_id = "a-20051208024829-849e76f7968d7a86"
29
 
        b_id = "b-20051208024829-849e76f7968d7a86"
 
27
    def setUp(self):
 
28
        super(TestExecutable, self).setUp()
 
29
 
 
30
        self.a_id = "a-20051208024829-849e76f7968d7a86"
 
31
        self.b_id = "b-20051208024829-849e76f7968d7a86"
30
32
        wt = self.make_branch_and_tree('b1')
31
33
        b = wt.branch
32
34
        tt = TreeTransform(wt)
33
 
        tt.new_file('a', tt.root, 'a test\n', a_id, True)
34
 
        tt.new_file('b', tt.root, 'b test\n', b_id, False)
 
35
        tt.new_file('a', tt.root, 'a test\n', self.a_id, True)
 
36
        tt.new_file('b', tt.root, 'b test\n', self.b_id, False)
35
37
        tt.apply()
36
38
 
37
 
        self.failUnless(wt.is_executable(a_id), "'a' lost the execute bit")
38
 
 
39
 
        # reopen the tree and ensure it stuck.
40
 
        wt = wt.bzrdir.open_workingtree()
41
 
        self.assertEqual(['a', 'b'], [cn for cn,ie in wt.inventory.iter_entries()])
42
 
 
43
 
        self.failUnless(wt.is_executable(a_id), "'a' lost the execute bit")
44
 
        self.failIf(wt.is_executable(b_id), "'b' gained an execute bit")
45
 
 
46
 
        wt.commit('adding a,b', rev_id='r1')
47
 
 
48
 
        rev_tree = b.repository.revision_tree('r1')
49
 
        self.failUnless(rev_tree.is_executable(a_id), "'a' lost the execute bit")
50
 
        self.failIf(rev_tree.is_executable(b_id), "'b' gained an execute bit")
51
 
 
52
 
        self.failUnless(rev_tree.inventory[a_id].executable)
53
 
        self.failIf(rev_tree.inventory[b_id].executable)
 
39
        self.wt = wt
 
40
 
 
41
    def check_exist(self, tree):
 
42
        """Just check that both files have the right executable bits set"""
 
43
        measured = [(cn,ie.executable) 
 
44
                    for cn,ie in tree.inventory.iter_entries()]
 
45
        self.assertEqual([('a', True), ('b', False)], measured)
 
46
        self.failUnless(tree.is_executable(self.a_id),
 
47
                        "'a' lost the execute bit")
 
48
        self.failIf(tree.is_executable(self.b_id),
 
49
                    "'b' gained an execute bit")
 
50
 
 
51
    def check_empty(self, tree, ignore_inv=False):
 
52
        """Check that the files are truly missing
 
53
        :param ignore_inv: If you just delete files from a working tree
 
54
                the inventory still shows them, so don't assert that
 
55
                the inventory is empty, just that the tree doesn't have them
 
56
        """
 
57
        if not ignore_inv:
 
58
            self.assertEqual([], list(tree.inventory.iter_entries()))
 
59
        self.failIf(tree.has_id(self.a_id))
 
60
        self.failIf(tree.has_filename('a'))
 
61
        self.failIf(tree.has_id(self.b_id))
 
62
        self.failIf(tree.has_filename('b'))
 
63
 
 
64
    def commit_and_branch(self):
 
65
        """Commit the current tree, and create a second tree"""
 
66
        self.wt.commit('adding a,b', rev_id='r1')
 
67
 
 
68
        # Now make sure that 'bzr branch' also preserves the
 
69
        # executable bit
 
70
        # TODO: Maybe this should be a blackbox test
 
71
        dir2 = self.wt.branch.bzrdir.clone('b2', revision_id='r1')
 
72
        wt2 = dir2.open_workingtree()
 
73
        self.assertEqual('r1', wt2.last_revision())
 
74
        self.assertEqual('r1', wt2.branch.last_revision())
 
75
        return wt2
 
76
 
 
77
    def test_01_is_executable(self):
 
78
        """Make sure that the tree was created and has the executable bit set"""
 
79
        self.check_exist(self.wt)
 
80
 
 
81
    def test_02_stays_executable(self):
 
82
        """reopen the tree and ensure it stuck."""
 
83
        self.wt = self.wt.bzrdir.open_workingtree()
 
84
        self.check_exist(self.wt)
 
85
 
 
86
    def test_03_after_commit(self):
 
87
        """Commit the change, and check the history"""
 
88
        self.wt.commit('adding a,b', rev_id='r1')
 
89
 
 
90
        rev_tree = self.wt.branch.repository.revision_tree('r1')
 
91
        self.check_exist(rev_tree)
 
92
 
 
93
    def test_04_after_removed(self):
 
94
        """Make sure reverting removed files brings them back correctly"""
 
95
        self.wt.commit('adding a,b', rev_id='r1')
54
96
 
55
97
        # Make sure the entries are gone
56
98
        os.remove('b1/a')
57
99
        os.remove('b1/b')
58
 
        self.failIf(wt.has_id(a_id))
59
 
        self.failIf(wt.has_filename('a'))
60
 
        self.failIf(wt.has_id(b_id))
61
 
        self.failIf(wt.has_filename('b'))
 
100
        self.check_empty(self.wt, ignore_inv=True)
62
101
 
63
102
        # Make sure that revert is able to bring them back,
64
103
        # and sets 'a' back to being executable
65
104
 
66
 
        wt.revert(['a', 'b'], rev_tree, backups=False)
67
 
        self.assertEqual(['a', 'b'], [cn for cn,ie in wt.inventory.iter_entries()])
68
 
 
69
 
        self.failUnless(wt.is_executable(a_id), "'a' lost the execute bit")
70
 
        self.failIf(wt.is_executable(b_id), "'b' gained an execute bit")
 
105
        rev_tree = self.wt.branch.repository.revision_tree('r1')
 
106
 
 
107
        self.wt.revert(['a', 'b'], rev_tree, backups=False)
 
108
        self.check_exist(self.wt)
 
109
 
 
110
    def test_05_removed_and_committed(self):
 
111
        """Check that reverting to an earlier commit restores them"""
 
112
        self.wt.commit('adding a,b', rev_id='r1')
71
113
 
72
114
        # Now remove them again, and make sure that after a
73
115
        # commit, they are still marked correctly
74
116
        os.remove('b1/a')
75
117
        os.remove('b1/b')
76
 
        wt.commit('removed', rev_id='r2')
77
 
 
78
 
        self.assertEqual([], [cn for cn,ie in wt.inventory.iter_entries()])
79
 
        self.failIf(wt.has_id(a_id))
80
 
        self.failIf(wt.has_filename('a'))
81
 
        self.failIf(wt.has_id(b_id))
82
 
        self.failIf(wt.has_filename('b'))
83
 
 
 
118
        self.wt.commit('removed', rev_id='r2')
 
119
 
 
120
        self.check_empty(self.wt)
 
121
 
 
122
        rev_tree = self.wt.branch.repository.revision_tree('r1')
84
123
        # Now revert back to the previous commit
85
 
        wt.revert([], rev_tree, backups=False)
86
 
        self.assertEqual(['a', 'b'], [cn for cn,ie in wt.inventory.iter_entries()])
87
 
 
88
 
        self.failUnless(wt.is_executable(a_id), "'a' lost the execute bit")
89
 
        self.failIf(wt.is_executable(b_id), "'b' gained an execute bit")
90
 
 
91
 
        # Now make sure that 'bzr branch' also preserves the
92
 
        # executable bit
 
124
        self.wt.revert([], rev_tree, backups=False)
 
125
 
 
126
        self.check_exist(self.wt)
 
127
 
 
128
    def test_06_branch(self):
 
129
        """branch b1=>b2 should preserve the executable bits"""
93
130
        # TODO: Maybe this should be a blackbox test
94
 
        d2 = b.bzrdir.clone('b2', revision_id='r1')
95
 
        t2 = d2.open_workingtree()
96
 
        b2 = t2.branch
97
 
        self.assertEquals('r1', b2.last_revision())
98
 
 
99
 
        self.assertEqual(['a', 'b'], [cn for cn,ie in t2.inventory.iter_entries()])
100
 
        self.failUnless(t2.is_executable(a_id), "'a' lost the execute bit")
101
 
        self.failIf(t2.is_executable(b_id), "'b' gained an execute bit")
 
131
        wt2 = self.commit_and_branch()
 
132
 
 
133
        self.check_exist(wt2)
 
134
 
 
135
    def test_07_pull(self):
 
136
        """Test that pull will handle bits correctly"""
 
137
        wt2 = self.commit_and_branch()
 
138
 
 
139
        os.remove('b1/a')
 
140
        os.remove('b1/b')
 
141
        self.wt.commit('removed', rev_id='r2')
 
142
 
 
143
        # now wt2 can pull and the files should be removed
102
144
 
103
145
        # Make sure pull will delete the files
104
 
        t2.pull(b)
105
 
        self.assertEquals('r2', b2.last_revision())
106
 
        self.assertEqual([], [cn for cn,ie in t2.inventory.iter_entries()])
 
146
        wt2.pull(self.wt.branch)
 
147
        self.assertEquals('r2', wt2.last_revision())
 
148
        self.assertEquals('r2', wt2.branch.last_revision())
 
149
        self.check_empty(wt2)
107
150
 
108
 
        # Now commit the changes on the first branch
 
151
        # Now restore the files on the first branch and commit
109
152
        # so that the second branch can pull the changes
110
153
        # and make sure that the executable bit has been copied
111
 
        wt.commit('resurrected', rev_id='r3')
112
 
 
113
 
        t2.pull(b)
114
 
        self.assertEquals('r3', b2.last_revision())
115
 
        self.assertEqual(['a', 'b'], [cn for cn,ie in t2.inventory.iter_entries()])
116
 
 
117
 
        self.failUnless(t2.is_executable(a_id), "'a' lost the execute bit")
118
 
        self.failIf(t2.is_executable(b_id), "'b' gained an execute bit")
119
 
 
120
 
        # Just do a simple revert without anything changed, and 
121
 
        # make sure the bits don't swap.
122
 
        t2.revert([], t2.branch.repository.revision_tree('r3'), backups=False)
123
 
        self.assertEqual(['a', 'b'], [cn for cn,ie in t2.inventory.iter_entries()])
124
 
 
125
 
        self.failUnless(t2.is_executable(a_id), "'a' lost the execute bit")
126
 
        self.failIf(t2.is_executable(b_id), "'b' gained an execute bit")
127
 
 
128
 
    def test_executable(self):
129
 
        """Format 3 trees should keep executable=yes in the working inventory."""
130
 
        wt = self.make_branch_and_tree('.')
131
 
        tt = TreeTransform(wt)
132
 
        tt.new_file('a', tt.root, 'contents of a\n', 'a-xxyy', True)
133
 
        tt.new_file('b', tt.root, 'contents of b\n', 'b-xxyy', False)
134
 
        tt.apply()
135
 
 
136
 
        tree_values = [(cn, ie.executable) 
137
 
                       for cn,ie in wt.inventory.iter_entries()]
138
 
        self.assertEqual([('a', True), ('b', False)], tree_values)
139
 
 
140
 
        # Committing shouldn't remove it
141
 
        wt.commit('first rev')
142
 
        tree_values = [(cn, ie.executable) 
143
 
                       for cn,ie in wt.inventory.iter_entries()]
144
 
        self.assertEqual([('a', True), ('b', False)], tree_values)
145
 
 
146
 
        # And neither should reverting
147
 
        last_tree = wt.branch.repository.revision_tree(wt.last_revision())
148
 
        wt.revert([], last_tree, backups=False)
149
 
        tree_values = [(cn, ie.executable) 
150
 
                       for cn,ie in wt.inventory.iter_entries()]
151
 
        self.assertEqual([('a', True), ('b', False)], tree_values)
 
154
        rev_tree = self.wt.branch.repository.revision_tree('r1')
 
155
        self.wt.revert([], rev_tree, backups=False)
 
156
        self.wt.commit('resurrected', rev_id='r3')
 
157
 
 
158
        self.check_exist(self.wt)
 
159
 
 
160
        wt2.pull(self.wt.branch)
 
161
        self.assertEquals('r3', wt2.last_revision())
 
162
        self.assertEquals('r3', wt2.branch.last_revision())
 
163
        self.check_exist(wt2)
 
164
 
 
165
    def test_08_no_op_revert(self):
 
166
        """Just do a simple revert without anything changed
 
167
        
 
168
        The bits shouldn't swap.
 
169
        """
 
170
        self.wt.commit('adding a,b', rev_id='r1')
 
171
        rev_tree = self.wt.branch.repository.revision_tree('r1')
 
172
        self.wt.revert([], rev_tree, backups=False)
 
173
        self.check_exist(self.wt)
 
174