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