/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 breezy/tests/test_clean_tree.py

  • Committer: John Arbash Meinel
  • Date: 2006-04-25 15:05:42 UTC
  • mfrom: (1185.85.85 bzr-encoding)
  • mto: This revision was merged to the branch mainline in revision 1752.
  • Revision ID: john@arbash-meinel.com-20060425150542-c7b518dca9928691
[merge] the old bzr-encoding changes, reparenting them on bzr.dev

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