~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

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

  • Committer: John Arbash Meinel
  • Date: 2006-12-19 17:06:42 UTC
  • mto: This revision was merged to the branch mainline in revision 2202.
  • Revision ID: john@arbash-meinel.com-20061219170642-wkqlrka2qlkr75ao
Fix bug #76299 by ignoring write errors during readonly hashcache write.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2006 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
16
 
 
17
"""Test that WorkingTrees don't fail if they are in a readonly dir."""
 
18
 
 
19
import os
 
20
import sys
 
21
import time
 
22
 
 
23
from bzrlib import (
 
24
    errors,
 
25
    hashcache,
 
26
    tests,
 
27
    )
 
28
from bzrlib.tests.workingtree_implementations import TestCaseWithWorkingTree
 
29
 
 
30
 
 
31
class TestReadonly(TestCaseWithWorkingTree):
 
32
 
 
33
    def setUp(self):
 
34
        if not self.platform_supports_readonly_dirs():
 
35
            raise tests.TestSkipped('platform does not support readonly'
 
36
                                    ' directories.')
 
37
        super(TestReadonly, self).setUp()
 
38
 
 
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...
 
46
            return False
 
47
        return True
 
48
 
 
49
    def _set_all_dirs(self, basedir, readonly=True):
 
50
        """Recursively set all directories beneath this one."""
 
51
        if readonly:
 
52
            mode = 0555
 
53
        else:
 
54
            mode = 0755
 
55
 
 
56
        for root, dirs, files in os.walk(basedir, topdown=False):
 
57
            for d in dirs:
 
58
                path = os.path.join(root, d)
 
59
                os.chmod(path, mode)
 
60
 
 
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)
 
64
 
 
65
        def cleanup():
 
66
            self._set_all_dirs(basedir, readonly=False)
 
67
 
 
68
        self.addCleanup(cleanup)
 
69
 
 
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.')
 
75
        return tree
 
76
 
 
77
    def _custom_cutoff_time(self):
 
78
        """We need to fake the cutoff time."""
 
79
        return time.time() + 10
 
80
 
 
81
    def test_readonly_unclean(self):
 
82
        """Even if the tree is unclean, we should still handle readonly dirs."""
 
83
        # First create a tree
 
84
        tree = self.create_basic_tree()
 
85
 
 
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.
 
88
 
 
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
 
93
 
 
94
        # Make it a little dirty
 
95
        self.build_tree_contents([('tree/a', 'new contents of a\n')])
 
96
 
 
97
        # Make it readonly, and do some operations and then unlock
 
98
        self.set_dirs_readonly('tree')
 
99
 
 
100
        tree.lock_read()
 
101
        try:
 
102
            # Make sure we check all the files
 
103
            for file_id in tree:
 
104
                size = tree.get_file_size(file_id)
 
105
                sha1 = tree.get_file_sha1(file_id)
 
106
        finally:
 
107
            tree.unlock()