/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
5557.1.15 by John Arbash Meinel
Merge bzr.dev 5597 to resolve NEWS, aka bzr-2.3.txt
1
# Copyright (C) 2009, 2010, 2011 Canonical Ltd
4020.1.1 by Jelmer Vernooij
Import clean-tree from bzrtools.
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
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
4020.1.1 by Jelmer Vernooij
Import clean-tree from bzrtools.
16
17
5376.1.3 by Parth Malwankar
clean-tree now specifically handes EACCES
18
import errno
4020.1.1 by Jelmer Vernooij
Import clean-tree from bzrtools.
19
import os
5376.1.1 by Parth Malwankar
clean-tree issues warning if it cant delete a file
20
import shutil
5376.1.8 by Parth Malwankar
onerror and test updated to handle EACCES specifically
21
import sys
4020.1.2 by Jelmer Vernooij
Import blackbox test for clean-tree, run regular tests.
22
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
23
from .. import tests, ui
24
from ..controldir import (
6472.2.2 by Jelmer Vernooij
Use controldir rather than bzrdir in a couple more places.
25
    ControlDir,
4020.1.2 by Jelmer Vernooij
Import blackbox test for clean-tree, run regular tests.
26
    )
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
27
from ..clean_tree import (
4020.1.2 by Jelmer Vernooij
Import blackbox test for clean-tree, run regular tests.
28
    clean_tree,
29
    iter_deletables,
30
    )
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
31
from ..osutils import (
4020.1.2 by Jelmer Vernooij
Import blackbox test for clean-tree, run regular tests.
32
    has_symlinks,
33
    )
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
34
from . import (
4020.1.2 by Jelmer Vernooij
Import blackbox test for clean-tree, run regular tests.
35
    TestCaseInTempDir,
36
    )
4020.1.1 by Jelmer Vernooij
Import clean-tree from bzrtools.
37
4020.1.5 by Aaron Bentley
Fix some formatting issues.
38
4020.1.1 by Jelmer Vernooij
Import clean-tree from bzrtools.
39
class TestCleanTree(TestCaseInTempDir):
4020.1.2 by Jelmer Vernooij
Import blackbox test for clean-tree, run regular tests.
40
4020.1.1 by Jelmer Vernooij
Import clean-tree from bzrtools.
41
    def test_symlinks(self):
42
        if has_symlinks() is False:
43
            return
44
        os.mkdir('branch')
6472.2.2 by Jelmer Vernooij
Use controldir rather than bzrdir in a couple more places.
45
        ControlDir.create_standalone_workingtree('branch')
4020.1.1 by Jelmer Vernooij
Import clean-tree from bzrtools.
46
        os.symlink(os.path.realpath('no-die-please'), 'branch/die-please')
47
        os.mkdir('no-die-please')
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
48
        self.assertPathExists('branch/die-please')
4020.1.1 by Jelmer Vernooij
Import clean-tree from bzrtools.
49
        os.mkdir('no-die-please/child')
50
51
        clean_tree('branch', unknown=True, no_prompt=True)
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
52
        self.assertPathExists('no-die-please')
53
        self.assertPathExists('no-die-please/child')
4020.1.1 by Jelmer Vernooij
Import clean-tree from bzrtools.
54
55
    def test_iter_deletable(self):
56
        """Files are selected for deletion appropriately"""
57
        os.mkdir('branch')
6472.2.2 by Jelmer Vernooij
Use controldir rather than bzrdir in a couple more places.
58
        tree = ControlDir.create_standalone_workingtree('branch')
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
59
        transport = tree.controldir.root_transport
6973.9.1 by Jelmer Vernooij
More test fixes.
60
        transport.put_bytes('.bzrignore', b'*~\n*.pyc\n.bzrignore\n')
61
        transport.put_bytes('file.BASE', b'contents')
62
        with tree.lock_write():
4020.1.1 by Jelmer Vernooij
Import clean-tree from bzrtools.
63
            self.assertEqual(len(list(iter_deletables(tree, unknown=True))), 1)
6973.9.1 by Jelmer Vernooij
More test fixes.
64
            transport.put_bytes('file', b'contents')
65
            transport.put_bytes('file~', b'contents')
66
            transport.put_bytes('file.pyc', b'contents')
6809.1.1 by Martin
Apply 2to3 ws_comma fixer
67
            dels = sorted([r for a, r in iter_deletables(tree, unknown=True)])
4020.1.7 by Aaron Bentley
Convert asserts to TestCase helper methods
68
            self.assertEqual(['file', 'file.BASE'], dels)
4020.1.1 by Jelmer Vernooij
Import clean-tree from bzrtools.
69
6809.1.1 by Martin
Apply 2to3 ws_comma fixer
70
            dels = [r for a, r in iter_deletables(tree, detritus=True)]
4020.1.7 by Aaron Bentley
Convert asserts to TestCase helper methods
71
            self.assertEqual(sorted(['file~', 'file.BASE']), dels)
4020.1.1 by Jelmer Vernooij
Import clean-tree from bzrtools.
72
6809.1.1 by Martin
Apply 2to3 ws_comma fixer
73
            dels = [r for a, r in iter_deletables(tree, ignored=True)]
4020.1.7 by Aaron Bentley
Convert asserts to TestCase helper methods
74
            self.assertEqual(sorted(['file~', 'file.pyc', '.bzrignore']),
75
                             dels)
4020.1.1 by Jelmer Vernooij
Import clean-tree from bzrtools.
76
6809.1.1 by Martin
Apply 2to3 ws_comma fixer
77
            dels = [r for a, r in iter_deletables(tree, unknown=False)]
4020.1.7 by Aaron Bentley
Convert asserts to TestCase helper methods
78
            self.assertEqual([], dels)
5376.1.1 by Parth Malwankar
clean-tree issues warning if it cant delete a file
79
80
    def test_delete_items_warnings(self):
5376.1.8 by Parth Malwankar
onerror and test updated to handle EACCES specifically
81
        """Ensure delete_items issues warnings on EACCES. (bug #430785)
5376.1.1 by Parth Malwankar
clean-tree issues warning if it cant delete a file
82
        """
83
        def _dummy_unlink(path):
5376.1.6 by Parth Malwankar
closed review comments
84
            """unlink() files other than files named '0foo'.
85
            """
5376.1.1 by Parth Malwankar
clean-tree issues warning if it cant delete a file
86
            if path.endswith('0foo'):
5376.1.6 by Parth Malwankar
closed review comments
87
                # Simulate 'permission denied' error.
88
                # This should show up as a warning for the
89
                # user.
5376.1.3 by Parth Malwankar
clean-tree now specifically handes EACCES
90
                e = OSError()
91
                e.errno = errno.EACCES
92
                raise e
5376.1.1 by Parth Malwankar
clean-tree issues warning if it cant delete a file
93
94
        def _dummy_rmtree(path, ignore_errors=False, onerror=None):
5376.1.8 by Parth Malwankar
onerror and test updated to handle EACCES specifically
95
            """Call user supplied error handler onerror.
5376.1.6 by Parth Malwankar
closed review comments
96
            """
5376.1.8 by Parth Malwankar
onerror and test updated to handle EACCES specifically
97
            # Indicate failure in removing 'path' if path is subdir0
5376.1.6 by Parth Malwankar
closed review comments
98
            # We later check to ensure that this is indicated
5376.1.9 by Parth Malwankar
improved comments
99
            # to the user as a warning. We raise OSError to construct
100
            # proper excinfo that needs to be passed to onerror
5376.1.8 by Parth Malwankar
onerror and test updated to handle EACCES specifically
101
            try:
102
                raise OSError
6619.3.2 by Jelmer Vernooij
Apply 2to3 except fix.
103
            except OSError as e:
5376.1.8 by Parth Malwankar
onerror and test updated to handle EACCES specifically
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,
7143.15.2 by Jelmer Vernooij
Run autopep8.
113
                        path=path, excinfo=excinfo)
5376.1.1 by Parth Malwankar
clean-tree issues warning if it cant delete a file
114
115
        self.overrideAttr(os, 'unlink', _dummy_unlink)
116
        self.overrideAttr(shutil, 'rmtree', _dummy_rmtree)
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
117
        ui.ui_factory = tests.TestUIFactory()
118
        stderr = ui.ui_factory.stderr
5376.1.9 by Parth Malwankar
improved comments
119
6472.2.2 by Jelmer Vernooij
Use controldir rather than bzrdir in a couple more places.
120
        ControlDir.create_standalone_workingtree('.')
5376.1.8 by Parth Malwankar
onerror and test updated to handle EACCES specifically
121
        self.build_tree(['0foo', '1bar', '2baz', 'subdir0/'])
5376.1.1 by Parth Malwankar
clean-tree issues warning if it cant delete a file
122
        clean_tree('.', unknown=True, no_prompt=True)
123
        self.assertContainsRe(stderr.getvalue(),
7143.15.2 by Jelmer Vernooij
Run autopep8.
124
                              'bzr: warning: unable to remove.*0foo')
5376.1.1 by Parth Malwankar
clean-tree issues warning if it cant delete a file
125
        self.assertContainsRe(stderr.getvalue(),
7143.15.2 by Jelmer Vernooij
Run autopep8.
126
                              'bzr: warning: unable to remove.*subdir0')
5376.1.8 by Parth Malwankar
onerror and test updated to handle EACCES specifically
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, '.',
7143.15.2 by Jelmer Vernooij
Run autopep8.
132
                          unknown=True, no_prompt=True)