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 that WorkingTrees don't fail if they are in a readonly dir."""
28
from bzrlib.tests.workingtree_implementations import TestCaseWithWorkingTree
31
class TestReadonly(TestCaseWithWorkingTree):
34
if not self.platform_supports_readonly_dirs():
35
raise tests.TestSkipped('platform does not support readonly'
37
super(TestReadonly, self).setUp()
39
def platform_supports_readonly_dirs(self):
40
if sys.platform in ('win32', 'cygwin'):
41
# Setting a directory to readonly in windows or cygwin doesn't seem
42
# to have any effect. You can still create files in subdirectories.
43
# TODO: jam 20061219 We could cheat and set just the hashcache file
44
# to readonly, which would make it fail when we try to delete
45
# or rewrite it. But that is a lot of cheating...
49
def _set_all_dirs(self, basedir, readonly=True):
50
"""Recursively set all directories beneath this one."""
56
for root, dirs, files in os.walk(basedir, topdown=False):
58
path = os.path.join(root, d)
61
def set_dirs_readonly(self, basedir):
62
"""Set all directories readonly, and have it cleanup on test exit."""
63
self._set_all_dirs(basedir, readonly=True)
66
self._set_all_dirs(basedir, readonly=False)
68
self.addCleanup(cleanup)
70
def create_basic_tree(self):
71
tree = self.make_branch_and_tree('tree')
72
self.build_tree(['tree/a', 'tree/b/', 'tree/b/c'])
73
tree.add(['a', 'b', 'b/c'])
74
tree.commit('creating an initial tree.')
77
def _custom_cutoff_time(self):
78
"""We need to fake the cutoff time."""
79
return time.time() + 10
81
def test_readonly_unclean(self):
82
"""Even if the tree is unclean, we should still handle readonly dirs."""
84
tree = self.create_basic_tree()
86
# XXX: *Ugly* *ugly* hack, we need the hashcache to think it is out of
87
# date, but we don't want to actually wait 3 seconds doing nothing.
89
the_hashcache = getattr(tree, '_hashcache')
90
if (the_hashcache is not None
91
and isinstance(the_hashcache, hashcache.HashCache)):
92
the_hashcache._cutoff_time = self._custom_cutoff_time
94
# Make it a little dirty
95
self.build_tree_contents([('tree/a', 'new contents of a\n')])
97
# Make it readonly, and do some operations and then unlock
98
self.set_dirs_readonly('tree')
102
# Make sure we check all the files
104
size = tree.get_file_size(file_id)
105
sha1 = tree.get_file_sha1(file_id)