/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
2255.2.62 by John Arbash Meinel
add a workingtree_implementations test that makes sure smart_add_tree orks properly
1
# Copyright (C) 2007 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
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
2255.2.62 by John Arbash Meinel
add a workingtree_implementations test that makes sure smart_add_tree orks properly
16
17
"""Test that we can use smart_add on all Tree implementations."""
18
2568.2.4 by Robert Collins
* ``bzrlib.add.smart_add`` and ``bzrlib.add.smart_add_tree`` are now
19
from cStringIO import StringIO
4789.11.1 by John Arbash Meinel
Skip the assertFilenameSkipped tests on Windows.
20
import sys
2568.2.4 by Robert Collins
* ``bzrlib.add.smart_add`` and ``bzrlib.add.smart_add_tree`` are now
21
2255.2.62 by John Arbash Meinel
add a workingtree_implementations test that makes sure smart_add_tree orks properly
22
from bzrlib import (
23
    add,
24
    errors,
2568.2.4 by Robert Collins
* ``bzrlib.add.smart_add`` and ``bzrlib.add.smart_add_tree`` are now
25
    ignores,
26
    osutils,
2568.2.10 by Robert Collins
And a missing import.
27
    tests,
2255.2.62 by John Arbash Meinel
add a workingtree_implementations test that makes sure smart_add_tree orks properly
28
    workingtree,
29
    )
5013.2.2 by Vincent Ladeuil
Fix imports in per_workingtree/test_smart_add.py.
30
from bzrlib.tests import (
31
    test_smart_add,
32
    per_workingtree,
2255.7.92 by Martin Pool
Test for smart_add(save=false) should be run against all WorkingTrees; adjust the test to more precisely cover the contract.
33
    )
5013.2.2 by Vincent Ladeuil
Fix imports in per_workingtree/test_smart_add.py.
34
35
36
class TestSmartAddTree(per_workingtree.TestCaseWithWorkingTree):
2255.2.62 by John Arbash Meinel
add a workingtree_implementations test that makes sure smart_add_tree orks properly
37
38
    def test_single_file(self):
39
        tree = self.make_branch_and_tree('tree')
40
        self.build_tree(['tree/a'])
2568.2.4 by Robert Collins
* ``bzrlib.add.smart_add`` and ``bzrlib.add.smart_add_tree`` are now
41
        tree.smart_add(['tree'])
2255.2.62 by John Arbash Meinel
add a workingtree_implementations test that makes sure smart_add_tree orks properly
42
43
        tree.lock_read()
44
        try:
45
            files = [(path, status, kind)
46
                     for path, status, kind, file_id, parent_id
47
                      in tree.list_files(include_root=True)]
48
        finally:
49
            tree.unlock()
50
        self.assertEqual([('', 'V', 'directory'), ('a', 'V', 'file')],
51
                         files)
2255.7.92 by Martin Pool
Test for smart_add(save=false) should be run against all WorkingTrees; adjust the test to more precisely cover the contract.
52
4634.55.1 by Robert Collins
Do not add files whose name contains new lines or carriage returns
53
    def assertFilenameSkipped(self, filename):
54
        tree = self.make_branch_and_tree('tree')
4789.11.1 by John Arbash Meinel
Skip the assertFilenameSkipped tests on Windows.
55
        try:
56
            self.build_tree(['tree/'+filename])
57
        except errors.NoSuchFile:
58
            if sys.platform == 'win32':
59
                raise tests.TestNotApplicable('Cannot create files named %r on'
60
                    ' win32' % (filename,))
4634.55.1 by Robert Collins
Do not add files whose name contains new lines or carriage returns
61
        tree.smart_add(['tree'])
62
        self.assertEqual(None, tree.path2id(filename))
63
64
    def test_path_containing_newline_skips(self):
65
        self.assertFilenameSkipped('a\nb')
66
67
    def test_path_containing_carriagereturn_skips(self):
68
        self.assertFilenameSkipped('a\rb')
69
2255.7.92 by Martin Pool
Test for smart_add(save=false) should be run against all WorkingTrees; adjust the test to more precisely cover the contract.
70
    def test_save_false(self):
71
        """Dry-run add doesn't permanently affect the tree."""
72
        wt = self.make_branch_and_tree('.')
2585.1.1 by Aaron Bentley
Unify MutableTree.smart_add behavior by disabling quirky memory-only Inventory
73
        wt.lock_write()
74
        try:
75
            self.build_tree(['file'])
76
            wt.smart_add(['file'], save=False)
77
            # the file should not be added - no id.
78
            self.assertEqual(wt.path2id('file'), None)
79
        finally:
80
            wt.unlock()
2568.2.4 by Robert Collins
* ``bzrlib.add.smart_add`` and ``bzrlib.add.smart_add_tree`` are now
81
        # and the disk state should be the same - reopen to check.
2255.7.92 by Martin Pool
Test for smart_add(save=false) should be run against all WorkingTrees; adjust the test to more precisely cover the contract.
82
        wt = wt.bzrdir.open_workingtree()
83
        self.assertEqual(wt.path2id('file'), None)
2568.2.4 by Robert Collins
* ``bzrlib.add.smart_add`` and ``bzrlib.add.smart_add_tree`` are now
84
85
    def test_add_dot_from_root(self):
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
86
        """Test adding . from the root of the tree."""
2568.2.4 by Robert Collins
* ``bzrlib.add.smart_add`` and ``bzrlib.add.smart_add_tree`` are now
87
        paths = ("original/", "original/file1", "original/file2")
88
        self.build_tree(paths)
89
        wt = self.make_branch_and_tree('.')
90
        wt.smart_add((u".",))
91
        for path in paths:
92
            self.assertNotEqual(wt.path2id(path), None)
93
94
    def test_add_dot_from_subdir(self):
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
95
        """Test adding . from a subdir of the tree."""
2568.2.4 by Robert Collins
* ``bzrlib.add.smart_add`` and ``bzrlib.add.smart_add_tree`` are now
96
        paths = ("original/", "original/file1", "original/file2")
97
        self.build_tree(paths)
98
        wt = self.make_branch_and_tree('.')
99
        wt.smart_add((u".",))
100
        for path in paths:
101
            self.assertNotEqual(wt.path2id(path), None)
102
103
    def test_add_tree_from_above_tree(self):
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
104
        """Test adding a tree from above the tree."""
2568.2.4 by Robert Collins
* ``bzrlib.add.smart_add`` and ``bzrlib.add.smart_add_tree`` are now
105
        paths = ("original/", "original/file1", "original/file2")
106
        branch_paths = ("branch/", "branch/original/", "branch/original/file1",
107
                        "branch/original/file2")
108
        self.build_tree(branch_paths)
109
        wt = self.make_branch_and_tree('branch')
110
        wt.smart_add(("branch",))
111
        for path in paths:
112
            self.assertNotEqual(wt.path2id(path), None)
113
114
    def test_add_above_tree_preserves_tree(self):
115
        """Test nested trees are not affect by an add above them."""
116
        paths = ("original/", "original/file1", "original/file2")
117
        child_paths = ("path",)
118
        full_child_paths = ("original/child", "original/child/path")
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
119
        build_paths = ("original/", "original/file1", "original/file2",
2568.2.4 by Robert Collins
* ``bzrlib.add.smart_add`` and ``bzrlib.add.smart_add_tree`` are now
120
                       "original/child/", "original/child/path")
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
121
2568.2.4 by Robert Collins
* ``bzrlib.add.smart_add`` and ``bzrlib.add.smart_add_tree`` are now
122
        self.build_tree(build_paths)
123
        wt = self.make_branch_and_tree('.')
124
        child_tree = self.make_branch_and_tree('original/child')
125
        wt.smart_add((".",))
126
        for path in paths:
127
            self.assertNotEqual((path, wt.path2id(path)),
128
                                (path, None))
129
        for path in full_child_paths:
130
            self.assertEqual((path, wt.path2id(path)),
131
                             (path, None))
132
        for path in child_paths:
133
            self.assertEqual(child_tree.path2id(path), None)
134
135
    def test_add_paths(self):
136
        """Test smart-adding a list of paths."""
137
        paths = ("file1", "file2")
138
        self.build_tree(paths)
139
        wt = self.make_branch_and_tree('.')
140
        wt.smart_add(paths)
141
        for path in paths:
142
            self.assertNotEqual(wt.path2id(path), None)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
143
2568.2.4 by Robert Collins
* ``bzrlib.add.smart_add`` and ``bzrlib.add.smart_add_tree`` are now
144
    def test_add_ignored_nested_paths(self):
145
        """Test smart-adding a list of paths which includes ignored ones."""
146
        wt = self.make_branch_and_tree('.')
147
        tree_shape = ("adir/", "adir/CVS/", "adir/CVS/afile", "adir/CVS/afile2")
148
        add_paths = ("adir/CVS", "adir/CVS/afile", "adir")
149
        expected_paths = ("adir", "adir/CVS", "adir/CVS/afile", "adir/CVS/afile2")
150
        self.build_tree(tree_shape)
151
        wt.smart_add(add_paths)
152
        for path in expected_paths:
153
            self.assertNotEqual(wt.path2id(path), None, "No id added for %s" % path)
154
155
    def test_add_non_existant(self):
156
        """Test smart-adding a file that does not exist."""
157
        wt = self.make_branch_and_tree('.')
158
        self.assertRaises(errors.NoSuchFile, wt.smart_add, ['non-existant-file'])
159
160
    def test_returns_and_ignores(self):
161
        """Correctly returns added/ignored files"""
162
        wt = self.make_branch_and_tree('.')
163
        # The default ignore list includes '*.py[co]', but not CVS
164
        ignores._set_user_ignores(['*.py[co]'])
165
        self.build_tree(['inertiatic/', 'inertiatic/esp', 'inertiatic/CVS',
166
                        'inertiatic/foo.pyc'])
167
        added, ignored = wt.smart_add(u'.')
168
        self.assertSubset(('inertiatic', 'inertiatic/esp', 'inertiatic/CVS'),
169
                          added)
170
        self.assertSubset(('*.py[co]',), ignored)
171
        self.assertSubset(('inertiatic/foo.pyc',), ignored['*.py[co]'])
172
173
    def test_add_multiple_dirs(self):
174
        """Test smart adding multiple directories at once."""
175
        added_paths = ['file1', 'file2',
176
                       'dir1/', 'dir1/file3',
177
                       'dir1/subdir2/', 'dir1/subdir2/file4',
178
                       'dir2/', 'dir2/file5',
179
                      ]
180
        not_added = ['file6', 'dir3/', 'dir3/file7', 'dir3/file8']
181
        self.build_tree(added_paths)
182
        self.build_tree(not_added)
183
184
        wt = self.make_branch_and_tree('.')
185
        wt.smart_add(['file1', 'file2', 'dir1', 'dir2'])
186
187
        for path in added_paths:
188
            self.assertNotEqual(None, wt.path2id(path.rstrip('/')),
189
                    'Failed to add path: %s' % (path,))
190
        for path in not_added:
191
            self.assertEqual(None, wt.path2id(path.rstrip('/')),
192
                    'Accidentally added path: %s' % (path,))
193
4163.2.1 by Ian Clatworthy
Fix add in trees supports views
194
    def test_add_file_in_unknown_dir(self):
195
        # Test that parent directory addition is implicit
196
        tree = self.make_branch_and_tree('.')
197
        self.build_tree(['dir/', 'dir/subdir/', 'dir/subdir/foo'])
198
        tree.smart_add(['dir/subdir/foo'])
199
        tree.lock_read()
200
        self.addCleanup(tree.unlock)
201
        self.assertEqual(['', 'dir', 'dir/subdir', 'dir/subdir/foo'],
202
            [path for path, ie in tree.iter_entries_by_dir()])
203
2568.2.4 by Robert Collins
* ``bzrlib.add.smart_add`` and ``bzrlib.add.smart_add_tree`` are now
204
    def test_custom_ids(self):
205
        sio = StringIO()
5013.2.2 by Vincent Ladeuil
Fix imports in per_workingtree/test_smart_add.py.
206
        action = test_smart_add.AddCustomIDAction(to_file=sio,
207
                                                  should_print=True)
2568.2.4 by Robert Collins
* ``bzrlib.add.smart_add`` and ``bzrlib.add.smart_add_tree`` are now
208
        self.build_tree(['file1', 'dir1/', 'dir1/file2'])
209
210
        wt = self.make_branch_and_tree('.')
211
        wt.smart_add(['.'], action=action)
212
        # The order of adds is not strictly fixed:
213
        sio.seek(0)
214
        lines = sorted(sio.readlines())
3985.2.5 by Daniel Watkins
Reverted some irrelevant changes.
215
        self.assertEqualDiff(['added dir1 with id directory-dir1\n',
216
                              'added dir1/file2 with id file-dir1%file2\n',
217
                              'added file1 with id file-file1\n',
218
                             ], lines)
2568.2.4 by Robert Collins
* ``bzrlib.add.smart_add`` and ``bzrlib.add.smart_add_tree`` are now
219
        wt.lock_read()
220
        self.addCleanup(wt.unlock)
221
        self.assertEqual([('', wt.path2id('')),
222
                          ('dir1', 'directory-dir1'),
223
                          ('dir1/file2', 'file-dir1%file2'),
224
                          ('file1', 'file-file1'),
225
                         ], [(path, ie.file_id) for path, ie
226
                                in wt.inventory.iter_entries()])
227
228
    def make_unicode_containing_tree(self):
229
        try:
230
            self.build_tree([u'a\u030a'])
231
        except UnicodeError:
2568.2.10 by Robert Collins
And a missing import.
232
            raise tests.TestSkipped('Filesystem cannot create unicode filenames')
2568.2.4 by Robert Collins
* ``bzrlib.add.smart_add`` and ``bzrlib.add.smart_add_tree`` are now
233
        self.wt = self.make_branch_and_tree('.')
234
235
    def test_accessible_explicit(self):
236
        self.make_unicode_containing_tree()
237
        orig = osutils.normalized_filename
238
        osutils.normalized_filename = osutils._accessible_normalized_filename
239
        try:
240
            self.wt.smart_add([u'a\u030a'])
241
            self.wt.lock_read()
242
            self.addCleanup(self.wt.unlock)
243
            self.assertEqual([('', 'directory'), (u'\xe5', 'file')],
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
244
                    [(path, ie.kind) for path,ie in
2568.2.4 by Robert Collins
* ``bzrlib.add.smart_add`` and ``bzrlib.add.smart_add_tree`` are now
245
                        self.wt.inventory.iter_entries()])
246
        finally:
247
            osutils.normalized_filename = orig
248
249
    def test_accessible_implicit(self):
250
        self.make_unicode_containing_tree()
251
        orig = osutils.normalized_filename
252
        osutils.normalized_filename = osutils._accessible_normalized_filename
253
        try:
254
            self.wt.smart_add([])
255
            self.wt.lock_read()
256
            self.addCleanup(self.wt.unlock)
257
            self.assertEqual([('', 'directory'), (u'\xe5', 'file')],
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
258
                    [(path, ie.kind) for path,ie in
2568.2.4 by Robert Collins
* ``bzrlib.add.smart_add`` and ``bzrlib.add.smart_add_tree`` are now
259
                        self.wt.inventory.iter_entries()])
260
        finally:
261
            osutils.normalized_filename = orig
262
263
    def test_inaccessible_explicit(self):
264
        self.make_unicode_containing_tree()
265
        orig = osutils.normalized_filename
266
        osutils.normalized_filename = osutils._inaccessible_normalized_filename
267
        try:
268
            self.assertRaises(errors.InvalidNormalization,
269
                    self.wt.smart_add, [u'a\u030a'])
270
        finally:
271
            osutils.normalized_filename = orig
272
273
    def test_inaccessible_implicit(self):
274
        self.make_unicode_containing_tree()
275
        orig = osutils.normalized_filename
276
        osutils.normalized_filename = osutils._inaccessible_normalized_filename
277
        try:
278
            # TODO: jam 20060701 In the future, this should probably
279
            #       just ignore files that don't fit the normalization
280
            #       rules, rather than exploding
281
            self.assertRaises(errors.InvalidNormalization,
282
                    self.wt.smart_add, [])
283
        finally:
284
            osutils.normalized_filename = orig