/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_clean_tree.py

  • Committer: Vincent Ladeuil
  • Date: 2012-01-18 14:09:19 UTC
  • mto: This revision was merged to the branch mainline in revision 6468.
  • Revision ID: v.ladeuil+lp@free.fr-20120118140919-rlvdrhpc0nq1lbwi
Change set/remove to require a lock for the branch config files.

This means that tests (or any plugin for that matter) do not requires an
explicit lock on the branch anymore to change a single option. This also
means the optimisation becomes "opt-in" and as such won't be as
spectacular as it may be and/or harder to get right (nothing fails
anymore).

This reduces the diff by ~300 lines.

Code/tests that were updating more than one config option is still taking
a lock to at least avoid some IOs and demonstrate the benefits through
the decreased number of hpss calls.

The duplication between BranchStack and BranchOnlyStack will be removed
once the same sharing is in place for local config files, at which point
the Stack class itself may be able to host the changes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2009, 2010, 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
 
 
18
import errno
 
19
import os
 
20
import shutil
 
21
import sys
 
22
import types
 
23
 
 
24
from bzrlib import tests, ui
 
25
from bzrlib.bzrdir import (
 
26
    BzrDir,
 
27
    )
 
28
from bzrlib.clean_tree import (
 
29
    clean_tree,
 
30
    iter_deletables,
 
31
    )
 
32
from bzrlib.osutils import (
 
33
    has_symlinks,
 
34
    )
 
35
from bzrlib.tests import (
 
36
    TestCaseInTempDir,
 
37
    )
 
38
 
 
39
 
 
40
class TestCleanTree(TestCaseInTempDir):
 
41
 
 
42
    def test_symlinks(self):
 
43
        if has_symlinks() is False:
 
44
            return
 
45
        os.mkdir('branch')
 
46
        BzrDir.create_standalone_workingtree('branch')
 
47
        os.symlink(os.path.realpath('no-die-please'), 'branch/die-please')
 
48
        os.mkdir('no-die-please')
 
49
        self.assertPathExists('branch/die-please')
 
50
        os.mkdir('no-die-please/child')
 
51
 
 
52
        clean_tree('branch', unknown=True, no_prompt=True)
 
53
        self.assertPathExists('no-die-please')
 
54
        self.assertPathExists('no-die-please/child')
 
55
 
 
56
    def test_iter_deletable(self):
 
57
        """Files are selected for deletion appropriately"""
 
58
        os.mkdir('branch')
 
59
        tree = BzrDir.create_standalone_workingtree('branch')
 
60
        transport = tree.bzrdir.root_transport
 
61
        transport.put_bytes('.bzrignore', '*~\n*.pyc\n.bzrignore\n')
 
62
        transport.put_bytes('file.BASE', 'contents')
 
63
        tree.lock_write()
 
64
        try:
 
65
            self.assertEqual(len(list(iter_deletables(tree, unknown=True))), 1)
 
66
            transport.put_bytes('file', 'contents')
 
67
            transport.put_bytes('file~', 'contents')
 
68
            transport.put_bytes('file.pyc', 'contents')
 
69
            dels = sorted([r for a,r in iter_deletables(tree, unknown=True)])
 
70
            self.assertEqual(['file', 'file.BASE'], dels)
 
71
 
 
72
            dels = [r for a,r in iter_deletables(tree, detritus=True)]
 
73
            self.assertEqual(sorted(['file~', 'file.BASE']), dels)
 
74
 
 
75
            dels = [r for a,r in iter_deletables(tree, ignored=True)]
 
76
            self.assertEqual(sorted(['file~', 'file.pyc', '.bzrignore']),
 
77
                             dels)
 
78
 
 
79
            dels = [r for a,r in iter_deletables(tree, unknown=False)]
 
80
            self.assertEqual([], dels)
 
81
        finally:
 
82
            tree.unlock()
 
83
 
 
84
    def test_delete_items_warnings(self):
 
85
        """Ensure delete_items issues warnings on EACCES. (bug #430785)
 
86
        """
 
87
        def _dummy_unlink(path):
 
88
            """unlink() files other than files named '0foo'.
 
89
            """
 
90
            if path.endswith('0foo'):
 
91
                # Simulate 'permission denied' error.
 
92
                # This should show up as a warning for the
 
93
                # user.
 
94
                e = OSError()
 
95
                e.errno = errno.EACCES
 
96
                raise e
 
97
 
 
98
        def _dummy_rmtree(path, ignore_errors=False, onerror=None):
 
99
            """Call user supplied error handler onerror.
 
100
            """
 
101
            # Indicate failure in removing 'path' if path is subdir0
 
102
            # We later check to ensure that this is indicated
 
103
            # to the user as a warning. We raise OSError to construct
 
104
            # proper excinfo that needs to be passed to onerror
 
105
            try:
 
106
                raise OSError
 
107
            except OSError, e:
 
108
                e.errno = errno.EACCES
 
109
                excinfo = sys.exc_info()
 
110
                function = os.remove
 
111
                if 'subdir0' not in path:
 
112
                    # onerror should show warning only for os.remove
 
113
                    # error. For any other failures the error should
 
114
                    # be shown to the user.
 
115
                    function = os.listdir
 
116
                onerror(function=function,
 
117
                    path=path, excinfo=excinfo)
 
118
 
 
119
        self.overrideAttr(os, 'unlink', _dummy_unlink)
 
120
        self.overrideAttr(shutil, 'rmtree', _dummy_rmtree)
 
121
        stdout = tests.StringIOWrapper()
 
122
        stderr = tests.StringIOWrapper()
 
123
        ui.ui_factory = tests.TestUIFactory(stdout=stdout, stderr=stderr)
 
124
 
 
125
        BzrDir.create_standalone_workingtree('.')
 
126
        self.build_tree(['0foo', '1bar', '2baz', 'subdir0/'])
 
127
        clean_tree('.', unknown=True, no_prompt=True)
 
128
        self.assertContainsRe(stderr.getvalue(),
 
129
            'bzr: warning: unable to remove.*0foo')
 
130
        self.assertContainsRe(stderr.getvalue(),
 
131
            'bzr: warning: unable to remove.*subdir0')
 
132
 
 
133
        # Ensure that error other than EACCES during os.remove are
 
134
        # not turned into warnings.
 
135
        self.build_tree(['subdir1/'])
 
136
        self.assertRaises(OSError, clean_tree, '.',
 
137
            unknown=True, no_prompt=True)
 
138