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