1
# Copyright (C) 2009, 2010, 2011 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23
from bzrlib import tests, ui
24
from bzrlib.controldir import (
27
from bzrlib.clean_tree import (
31
from bzrlib.osutils import (
34
from bzrlib.tests import (
39
class TestCleanTree(TestCaseInTempDir):
41
def test_symlinks(self):
42
if has_symlinks() is False:
45
ControlDir.create_standalone_workingtree('branch')
46
os.symlink(os.path.realpath('no-die-please'), 'branch/die-please')
47
os.mkdir('no-die-please')
48
self.assertPathExists('branch/die-please')
49
os.mkdir('no-die-please/child')
51
clean_tree('branch', unknown=True, no_prompt=True)
52
self.assertPathExists('no-die-please')
53
self.assertPathExists('no-die-please/child')
55
def test_iter_deletable(self):
56
"""Files are selected for deletion appropriately"""
58
tree = ControlDir.create_standalone_workingtree('branch')
59
transport = tree.bzrdir.root_transport
60
transport.put_bytes('.bzrignore', '*~\n*.pyc\n.bzrignore\n')
61
transport.put_bytes('file.BASE', 'contents')
64
self.assertEqual(len(list(iter_deletables(tree, unknown=True))), 1)
65
transport.put_bytes('file', 'contents')
66
transport.put_bytes('file~', 'contents')
67
transport.put_bytes('file.pyc', 'contents')
68
dels = sorted([r for a,r in iter_deletables(tree, unknown=True)])
69
self.assertEqual(['file', 'file.BASE'], dels)
71
dels = [r for a,r in iter_deletables(tree, detritus=True)]
72
self.assertEqual(sorted(['file~', 'file.BASE']), dels)
74
dels = [r for a,r in iter_deletables(tree, ignored=True)]
75
self.assertEqual(sorted(['file~', 'file.pyc', '.bzrignore']),
78
dels = [r for a,r in iter_deletables(tree, unknown=False)]
79
self.assertEqual([], dels)
83
def test_delete_items_warnings(self):
84
"""Ensure delete_items issues warnings on EACCES. (bug #430785)
86
def _dummy_unlink(path):
87
"""unlink() files other than files named '0foo'.
89
if path.endswith('0foo'):
90
# Simulate 'permission denied' error.
91
# This should show up as a warning for the
94
e.errno = errno.EACCES
97
def _dummy_rmtree(path, ignore_errors=False, onerror=None):
98
"""Call user supplied error handler onerror.
100
# Indicate failure in removing 'path' if path is subdir0
101
# We later check to ensure that this is indicated
102
# to the user as a warning. We raise OSError to construct
103
# proper excinfo that needs to be passed to onerror
107
e.errno = errno.EACCES
108
excinfo = sys.exc_info()
110
if 'subdir0' not in path:
111
# onerror should show warning only for os.remove
112
# error. For any other failures the error should
113
# be shown to the user.
114
function = os.listdir
115
onerror(function=function,
116
path=path, excinfo=excinfo)
118
self.overrideAttr(os, 'unlink', _dummy_unlink)
119
self.overrideAttr(shutil, 'rmtree', _dummy_rmtree)
120
stdout = tests.StringIOWrapper()
121
stderr = tests.StringIOWrapper()
122
ui.ui_factory = tests.TestUIFactory(stdout=stdout, stderr=stderr)
124
ControlDir.create_standalone_workingtree('.')
125
self.build_tree(['0foo', '1bar', '2baz', 'subdir0/'])
126
clean_tree('.', unknown=True, no_prompt=True)
127
self.assertContainsRe(stderr.getvalue(),
128
'bzr: warning: unable to remove.*0foo')
129
self.assertContainsRe(stderr.getvalue(),
130
'bzr: warning: unable to remove.*subdir0')
132
# Ensure that error other than EACCES during os.remove are
133
# not turned into warnings.
134
self.build_tree(['subdir1/'])
135
self.assertRaises(OSError, clean_tree, '.',
136
unknown=True, no_prompt=True)