~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_clean_tree.py

  • Committer: Jonathan Riddell
  • Date: 2011-05-16 10:05:25 UTC
  • mto: This revision was merged to the branch mainline in revision 5869.
  • Revision ID: jriddell@canonical.com-20110516100525-7q23m5opdnl4qg41
start adding licences

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2009 Canonical Ltd
 
1
# Copyright (C) 2009, 2010, 2011 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
17
 
 
18
import errno
18
19
import os
19
 
from StringIO import StringIO
 
20
import shutil
 
21
import sys
 
22
import types
20
23
 
 
24
from bzrlib import tests, ui
21
25
from bzrlib.bzrdir import (
22
26
    BzrDir,
23
27
    )
42
46
        BzrDir.create_standalone_workingtree('branch')
43
47
        os.symlink(os.path.realpath('no-die-please'), 'branch/die-please')
44
48
        os.mkdir('no-die-please')
45
 
        self.failUnlessExists('branch/die-please')
 
49
        self.assertPathExists('branch/die-please')
46
50
        os.mkdir('no-die-please/child')
47
51
 
48
52
        clean_tree('branch', unknown=True, no_prompt=True)
49
 
        self.failUnlessExists('no-die-please')
50
 
        self.failUnlessExists('no-die-please/child')
 
53
        self.assertPathExists('no-die-please')
 
54
        self.assertPathExists('no-die-please/child')
51
55
 
52
56
    def test_iter_deletable(self):
53
57
        """Files are selected for deletion appropriately"""
76
80
            self.assertEqual([], dels)
77
81
        finally:
78
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
            self.assertTrue(isinstance(onerror, types.FunctionType))
 
102
            # Indicate failure in removing 'path' if path is subdir0
 
103
            # We later check to ensure that this is indicated
 
104
            # to the user as a warning. We raise OSError to construct
 
105
            # proper excinfo that needs to be passed to onerror
 
106
            try:
 
107
                raise OSError
 
108
            except OSError, e:
 
109
                e.errno = errno.EACCES
 
110
                excinfo = sys.exc_info()
 
111
                function = os.remove
 
112
                if 'subdir0' not in path:
 
113
                    # onerror should show warning only for os.remove
 
114
                    # error. For any other failures the error should
 
115
                    # be shown to the user.
 
116
                    function = os.listdir
 
117
                onerror(function=function,
 
118
                    path=path, excinfo=excinfo)
 
119
 
 
120
        self.overrideAttr(os, 'unlink', _dummy_unlink)
 
121
        self.overrideAttr(shutil, 'rmtree', _dummy_rmtree)
 
122
        stdout = tests.StringIOWrapper()
 
123
        stderr = tests.StringIOWrapper()
 
124
        ui.ui_factory = tests.TestUIFactory(stdout=stdout, stderr=stderr)
 
125
 
 
126
        BzrDir.create_standalone_workingtree('.')
 
127
        self.build_tree(['0foo', '1bar', '2baz', 'subdir0/'])
 
128
        clean_tree('.', unknown=True, no_prompt=True)
 
129
        self.assertContainsRe(stderr.getvalue(),
 
130
            'bzr: warning: unable to remove.*0foo')
 
131
        self.assertContainsRe(stderr.getvalue(),
 
132
            'bzr: warning: unable to remove.*subdir0')
 
133
 
 
134
        # Ensure that error other than EACCES during os.remove are
 
135
        # not turned into warnings.
 
136
        self.build_tree(['subdir1/'])
 
137
        self.assertRaises(OSError, clean_tree, '.',
 
138
            unknown=True, no_prompt=True)
 
139