~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_clean_tree.py

  • Committer: Martin Pool
  • Date: 2010-04-01 04:41:18 UTC
  • mto: This revision was merged to the branch mainline in revision 5128.
  • Revision ID: mbp@sourcefrog.net-20100401044118-shyctqc02ob08ngz
ignore .testrepository

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2009, 2010 Canonical Ltd
 
1
# Copyright (C) 2005, 2009 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
19
18
import os
20
 
import shutil
21
19
from StringIO import StringIO
22
 
import sys
23
 
import types
24
20
 
25
 
from bzrlib import tests, ui
26
21
from bzrlib.bzrdir import (
27
22
    BzrDir,
28
23
    )
81
76
            self.assertEqual([], dels)
82
77
        finally:
83
78
            tree.unlock()
84
 
 
85
 
    def test_delete_items_warnings(self):
86
 
        """Ensure delete_items issues warnings on EACCES. (bug #430785)
87
 
        """
88
 
        def _dummy_unlink(path):
89
 
            """unlink() files other than files named '0foo'.
90
 
            """
91
 
            if path.endswith('0foo'):
92
 
                # Simulate 'permission denied' error.
93
 
                # This should show up as a warning for the
94
 
                # user.
95
 
                e = OSError()
96
 
                e.errno = errno.EACCES
97
 
                raise e
98
 
 
99
 
        def _dummy_rmtree(path, ignore_errors=False, onerror=None):
100
 
            """Call user supplied error handler onerror.
101
 
            """
102
 
            self.assertTrue(isinstance(onerror, types.FunctionType))
103
 
            # Indicate failure in removing 'path' if path is subdir0
104
 
            # We later check to ensure that this is indicated
105
 
            # to the user as a warning. We raise OSError to construct
106
 
            # proper excinfo that needs to be passed to onerror
107
 
            try:
108
 
                raise OSError
109
 
            except OSError, e:
110
 
                e.errno = errno.EACCES
111
 
                excinfo = sys.exc_info()
112
 
                function = os.remove
113
 
                if 'subdir0' not in path:
114
 
                    # onerror should show warning only for os.remove
115
 
                    # error. For any other failures the error should
116
 
                    # be shown to the user.
117
 
                    function = os.listdir
118
 
                onerror(function=function,
119
 
                    path=path, excinfo=excinfo)
120
 
 
121
 
        self.overrideAttr(os, 'unlink', _dummy_unlink)
122
 
        self.overrideAttr(shutil, 'rmtree', _dummy_rmtree)
123
 
        stdout = tests.StringIOWrapper()
124
 
        stderr = tests.StringIOWrapper()
125
 
        ui.ui_factory = tests.TestUIFactory(stdout=stdout, stderr=stderr)
126
 
 
127
 
        BzrDir.create_standalone_workingtree('.')
128
 
        self.build_tree(['0foo', '1bar', '2baz', 'subdir0/'])
129
 
        clean_tree('.', unknown=True, no_prompt=True)
130
 
        self.assertContainsRe(stderr.getvalue(),
131
 
            'bzr: warning: unable to remove.*0foo')
132
 
        self.assertContainsRe(stderr.getvalue(),
133
 
            'bzr: warning: unable to remove.*subdir0')
134
 
 
135
 
        # Ensure that error other than EACCES during os.remove are
136
 
        # not turned into warnings.
137
 
        self.build_tree(['subdir1/'])
138
 
        self.assertRaises(OSError, clean_tree, '.',
139
 
            unknown=True, no_prompt=True)
140