1
# Copyright (C) 2006 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
"""Test the executable bit under various working tree formats."""
24
from bzrlib.inventory import InventoryFile
25
from bzrlib.transform import TreeTransform
26
from bzrlib.tests.workingtree_implementations import TestCaseWithWorkingTree
29
class TestExecutable(TestCaseWithWorkingTree):
32
super(TestExecutable, self).setUp()
34
self.a_id = "a-20051208024829-849e76f7968d7a86"
35
self.b_id = "b-20051208024829-849e76f7968d7a86"
36
wt = self.make_branch_and_tree('b1')
38
tt = TreeTransform(wt)
39
tt.new_file('a', tt.root, 'a test\n', self.a_id, True)
40
tt.new_file('b', tt.root, 'b test\n', self.b_id, False)
45
def check_exist(self, tree):
46
"""Just check that both files have the right executable bits set"""
48
self.failUnless(tree.is_executable(self.a_id),
49
"'a' lost the execute bit")
50
self.failIf(tree.is_executable(self.b_id),
51
"'b' gained an execute bit")
54
def check_empty(self, tree, ignore_inv=False):
55
"""Check that the files are truly missing
56
:param ignore_inv: If you just delete files from a working tree
57
the inventory still shows them, so don't assert that
58
the inventory is empty, just that the tree doesn't have them
63
[('', tree.inventory.root)],
64
list(tree.inventory.iter_entries()))
65
self.failIf(tree.has_id(self.a_id))
66
self.failIf(tree.has_filename('a'))
67
self.failIf(tree.has_id(self.b_id))
68
self.failIf(tree.has_filename('b'))
71
def commit_and_branch(self):
72
"""Commit the current tree, and create a second tree"""
73
self.wt.commit('adding a,b', rev_id='r1')
74
# Now make sure that 'bzr branch' also preserves the
76
# TODO: Maybe this should be a blackbox test
77
dir2 = self.wt.branch.bzrdir.clone('b2', revision_id='r1')
78
wt2 = dir2.open_workingtree()
79
self.assertEqual(['r1'], wt2.get_parent_ids())
80
self.assertEqual('r1', wt2.branch.last_revision())
83
def test_01_is_executable(self):
84
"""Make sure that the tree was created and has the executable bit set"""
85
self.check_exist(self.wt)
87
def test_02_stays_executable(self):
88
"""reopen the tree and ensure it stuck."""
89
self.wt = self.wt.bzrdir.open_workingtree()
90
self.check_exist(self.wt)
92
def test_03_after_commit(self):
93
"""Commit the change, and check the history"""
94
self.wt.commit('adding a,b', rev_id='r1')
96
rev_tree = self.wt.branch.repository.revision_tree('r1')
97
self.check_exist(rev_tree)
99
def test_04_after_removed(self):
100
"""Make sure reverting removed files brings them back correctly"""
101
self.wt.commit('adding a,b', rev_id='r1')
103
# Make sure the entries are gone
106
self.check_empty(self.wt, ignore_inv=True)
108
# Make sure that revert is able to bring them back,
109
# and sets 'a' back to being executable
111
rev_tree = self.wt.branch.repository.revision_tree('r1')
113
self.wt.revert(['a', 'b'], rev_tree, backups=False)
114
self.check_exist(self.wt)
116
def test_05_removed_and_committed(self):
117
"""Check that reverting to an earlier commit restores them"""
118
self.wt.commit('adding a,b', rev_id='r1')
120
# Now remove them again, and make sure that after a
121
# commit, they are still marked correctly
124
self.wt.commit('removed', rev_id='r2')
126
self.check_empty(self.wt)
128
rev_tree = self.wt.branch.repository.revision_tree('r1')
129
# Now revert back to the previous commit
130
self.wt.revert(old_tree=rev_tree, backups=False)
132
self.check_exist(self.wt)
134
def test_06_branch(self):
135
"""branch b1=>b2 should preserve the executable bits"""
136
# TODO: Maybe this should be a blackbox test
137
wt2 = self.commit_and_branch()
139
self.check_exist(wt2)
141
def test_07_pull(self):
142
"""Test that pull will handle bits correctly"""
143
wt2 = self.commit_and_branch()
147
self.wt.commit('removed', rev_id='r2')
149
# now wt2 can pull and the files should be removed
151
# Make sure pull will delete the files
152
wt2.pull(self.wt.branch)
153
self.assertEquals(['r2'], wt2.get_parent_ids())
154
self.assertEquals('r2', wt2.branch.last_revision())
155
self.check_empty(wt2)
157
# Now restore the files on the first branch and commit
158
# so that the second branch can pull the changes
159
# and make sure that the executable bit has been copied
160
rev_tree = self.wt.branch.repository.revision_tree('r1')
161
self.wt.revert(old_tree=rev_tree, backups=False)
162
self.wt.commit('resurrected', rev_id='r3')
164
self.check_exist(self.wt)
166
wt2.pull(self.wt.branch)
167
self.assertEquals(['r3'], wt2.get_parent_ids())
168
self.assertEquals('r3', wt2.branch.last_revision())
169
self.check_exist(wt2)
171
def test_08_no_op_revert(self):
172
"""Just do a simple revert without anything changed
174
The bits shouldn't swap.
176
self.wt.commit('adding a,b', rev_id='r1')
177
rev_tree = self.wt.branch.repository.revision_tree('r1')
178
self.wt.revert(old_tree=rev_tree, backups=False)
179
self.check_exist(self.wt)
181
def test_commit_with_exec_from_basis(self):
182
self.wt._is_executable_from_path_and_stat = \
183
self.wt._is_executable_from_path_and_stat_from_basis
184
rev_id1 = self.wt.commit('one')
185
rev_tree1 = self.wt.branch.repository.revision_tree(rev_id1)
186
a_executable = rev_tree1.inventory[self.a_id].executable
187
b_executable = rev_tree1.inventory[self.b_id].executable
188
self.assertIsNot(None, a_executable)
189
self.assertTrue(a_executable)
190
self.assertIsNot(None, b_executable)
191
self.assertFalse(b_executable)
193
def test_use_exec_from_basis(self):
194
if osutils.supports_executable():
195
self.assertEqual(self.wt._is_executable_from_path_and_stat_from_stat,
196
self.wt._is_executable_from_path_and_stat)
198
self.assertEqual(self.wt._is_executable_from_path_and_stat_from_basis,
199
self.wt._is_executable_from_path_and_stat)