~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_smart_add.py

merge dirstate and trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2005, 2006 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
16
 
 
17
from cStringIO import StringIO
 
18
import os
 
19
import unittest
 
20
 
 
21
from bzrlib import errors, ignores, osutils
 
22
from bzrlib.add import (
 
23
    AddAction,
 
24
    AddFromBaseAction,
 
25
    smart_add,
 
26
    smart_add_tree,
 
27
    )
 
28
from bzrlib.tests import TestCase, TestCaseWithTransport, TestSkipped
 
29
from bzrlib.errors import NoSuchFile
 
30
from bzrlib.inventory import InventoryFile, Inventory
 
31
from bzrlib.workingtree import WorkingTree
 
32
 
 
33
 
 
34
class TestSmartAdd(TestCaseWithTransport):
 
35
 
 
36
    def test_add_dot_from_root(self):
 
37
        """Test adding . from the root of the tree.""" 
 
38
        paths = ("original/", "original/file1", "original/file2")
 
39
        self.build_tree(paths)
 
40
        wt = self.make_branch_and_tree('.')
 
41
        smart_add_tree(wt, (u".",))
 
42
        for path in paths:
 
43
            self.assertNotEqual(wt.path2id(path), None)
 
44
 
 
45
    def test_add_dot_from_subdir(self):
 
46
        """Test adding . from a subdir of the tree.""" 
 
47
        from bzrlib.add import smart_add
 
48
        paths = ("original/", "original/file1", "original/file2")
 
49
        self.build_tree(paths)
 
50
        wt = self.make_branch_and_tree('.')
 
51
        os.chdir("original")
 
52
        smart_add_tree(wt, (u".",))
 
53
        for path in paths:
 
54
            self.assertNotEqual(wt.path2id(path), None)
 
55
 
 
56
    def test_add_tree_from_above_tree(self):
 
57
        """Test adding a tree from above the tree.""" 
 
58
        from bzrlib.add import smart_add
 
59
        paths = ("original/", "original/file1", "original/file2")
 
60
        branch_paths = ("branch/", "branch/original/", "branch/original/file1",
 
61
                        "branch/original/file2")
 
62
        self.build_tree(branch_paths)
 
63
        wt = self.make_branch_and_tree('branch')
 
64
        smart_add_tree(wt, ("branch",))
 
65
        for path in paths:
 
66
            self.assertNotEqual(wt.path2id(path), None)
 
67
 
 
68
    def test_add_above_tree_preserves_tree(self):
 
69
        """Test nested trees are not affect by an add above them."""
 
70
        from bzrlib.add import smart_add
 
71
        paths = ("original/", "original/file1", "original/file2")
 
72
        child_paths = ("path",)
 
73
        full_child_paths = ("original/child", "original/child/path")
 
74
        build_paths = ("original/", "original/file1", "original/file2", 
 
75
                       "original/child/", "original/child/path")
 
76
        
 
77
        self.build_tree(build_paths)
 
78
        wt = self.make_branch_and_tree('.')
 
79
        child_tree = self.make_branch_and_tree('original/child')
 
80
        smart_add_tree(wt, (".",))
 
81
        for path in paths:
 
82
            self.assertNotEqual((path, wt.path2id(path)),
 
83
                                (path, None))
 
84
        for path in full_child_paths:
 
85
            self.assertEqual((path, wt.path2id(path)),
 
86
                             (path, None))
 
87
        for path in child_paths:
 
88
            self.assertEqual(child_tree.path2id(path), None)
 
89
 
 
90
    def test_add_paths(self):
 
91
        """Test smart-adding a list of paths."""
 
92
        from bzrlib.add import smart_add
 
93
        paths = ("file1", "file2")
 
94
        self.build_tree(paths)
 
95
        wt = self.make_branch_and_tree('.')
 
96
        smart_add_tree(wt, paths)
 
97
        for path in paths:
 
98
            self.assertNotEqual(wt.path2id(path), None)
 
99
    
 
100
    def test_add_ignored_nested_paths(self):
 
101
        """Test smart-adding a list of paths which includes ignored ones."""
 
102
        wt = self.make_branch_and_tree('.')
 
103
        tree_shape = ("adir/", "adir/CVS/", "adir/CVS/afile", "adir/CVS/afile2")
 
104
        add_paths = ("adir/CVS", "adir/CVS/afile", "adir")
 
105
        expected_paths = ("adir", "adir/CVS", "adir/CVS/afile", "adir/CVS/afile2")
 
106
        self.build_tree(tree_shape)
 
107
        smart_add_tree(wt, add_paths)
 
108
        for path in expected_paths:
 
109
            self.assertNotEqual(wt.path2id(path), None, "No id added for %s" % path)
 
110
 
 
111
    def test_add_dry_run(self):
 
112
        """Test a dry run add, make sure nothing is added."""
 
113
        from bzrlib.commands import run_bzr
 
114
        eq = self.assertEqual
 
115
        wt = self.make_branch_and_tree('.')
 
116
        self.build_tree(['inertiatic/', 'inertiatic/esp'])
 
117
        eq(list(wt.unknowns()), ['inertiatic'])
 
118
        self.capture('add --dry-run .')
 
119
        eq(list(wt.unknowns()), ['inertiatic'])
 
120
 
 
121
    def test_add_non_existant(self):
 
122
        """Test smart-adding a file that does not exist."""
 
123
        from bzrlib.add import smart_add
 
124
        wt = self.make_branch_and_tree('.')
 
125
        self.assertRaises(NoSuchFile, smart_add_tree, wt, 'non-existant-file')
 
126
 
 
127
    def test_returns_and_ignores(self):
 
128
        """Correctly returns added/ignored files"""
 
129
        from bzrlib.commands import run_bzr
 
130
        wt = self.make_branch_and_tree('.')
 
131
        # The default ignore list includes '*.py[co]', but not CVS
 
132
        ignores._set_user_ignores(['*.py[co]'])
 
133
        self.build_tree(['inertiatic/', 'inertiatic/esp', 'inertiatic/CVS',
 
134
                        'inertiatic/foo.pyc'])
 
135
        added, ignored = smart_add_tree(wt, u'.')
 
136
        self.assertSubset(('inertiatic', 'inertiatic/esp', 'inertiatic/CVS'),
 
137
                          added)
 
138
        self.assertSubset(('*.py[co]',), ignored)
 
139
        self.assertSubset(('inertiatic/foo.pyc',), ignored['*.py[co]'])
 
140
 
 
141
 
 
142
class AddCustomIDAction(AddAction):
 
143
 
 
144
    def __call__(self, inv, parent_ie, path, kind):
 
145
        # The first part just logs if appropriate
 
146
        # Now generate a custom id
 
147
        file_id = kind + '-' + path.raw_path.replace('/', '%')
 
148
        if self.should_print:
 
149
            self._to_file.write('added %s with id %s\n' 
 
150
                                % (path.raw_path, file_id))
 
151
        return file_id
 
152
 
 
153
 
 
154
class TestSmartAddTree(TestCaseWithTransport):
 
155
    """Test smart adds with a specified branch."""
 
156
 
 
157
    def test_add_dot_from_root(self):
 
158
        """Test adding . from the root of the tree.""" 
 
159
        paths = ("original/", "original/file1", "original/file2")
 
160
        self.build_tree(paths)
 
161
        wt = self.make_branch_and_tree('.')
 
162
        smart_add_tree(wt, (u".",))
 
163
        for path in paths:
 
164
            self.assertNotEqual(wt.path2id(path), None)
 
165
 
 
166
    def test_add_dot_from_subdir(self):
 
167
        """Test adding . from a subdir of the tree.""" 
 
168
        paths = ("original/", "original/file1", "original/file2")
 
169
        self.build_tree(paths)
 
170
        wt = self.make_branch_and_tree('.')
 
171
        os.chdir("original")
 
172
        smart_add_tree(wt, (u".",))
 
173
        for path in paths:
 
174
            self.assertNotEqual(wt.path2id(path), None)
 
175
 
 
176
    def test_add_tree_from_above_tree(self):
 
177
        """Test adding a tree from above the tree.""" 
 
178
        paths = ("original/", "original/file1", "original/file2")
 
179
        branch_paths = ("branch/", "branch/original/", "branch/original/file1",
 
180
                        "branch/original/file2")
 
181
        self.build_tree(branch_paths)
 
182
        tree = self.make_branch_and_tree('branch')
 
183
        smart_add_tree(tree, ("branch",))
 
184
        for path in paths:
 
185
            self.assertNotEqual(tree.path2id(path), None)
 
186
 
 
187
    def test_add_above_tree_preserves_tree(self):
 
188
        """Test nested trees are not affect by an add above them."""
 
189
        paths = ("original/", "original/file1", "original/file2")
 
190
        child_paths = ("path")
 
191
        full_child_paths = ("original/child", "original/child/path")
 
192
        build_paths = ("original/", "original/file1", "original/file2", 
 
193
                       "original/child/", "original/child/path")
 
194
        self.build_tree(build_paths)
 
195
        tree = self.make_branch_and_tree('.')
 
196
        child_tree = self.make_branch_and_tree("original/child")
 
197
        smart_add_tree(tree, (u".",))
 
198
        for path in paths:
 
199
            self.assertNotEqual((path, tree.path2id(path)),
 
200
                                (path, None))
 
201
        for path in full_child_paths:
 
202
            self.assertEqual((path, tree.path2id(path)),
 
203
                             (path, None))
 
204
        for path in child_paths:
 
205
            self.assertEqual(child_tree.path2id(path), None)
 
206
 
 
207
    def test_add_paths(self):
 
208
        """Test smart-adding a list of paths."""
 
209
        paths = ("file1", "file2")
 
210
        self.build_tree(paths)
 
211
        wt = self.make_branch_and_tree('.')
 
212
        smart_add_tree(wt, paths)
 
213
        for path in paths:
 
214
            self.assertNotEqual(wt.path2id(path), None)
 
215
 
 
216
    def test_add_multiple_dirs(self):
 
217
        """Test smart adding multiple directories at once."""
 
218
        added_paths = ['file1', 'file2',
 
219
                       'dir1/', 'dir1/file3',
 
220
                       'dir1/subdir2/', 'dir1/subdir2/file4',
 
221
                       'dir2/', 'dir2/file5',
 
222
                      ]
 
223
        not_added = ['file6', 'dir3/', 'dir3/file7', 'dir3/file8']
 
224
        self.build_tree(added_paths)
 
225
        self.build_tree(not_added)
 
226
 
 
227
        wt = self.make_branch_and_tree('.')
 
228
        smart_add_tree(wt, ['file1', 'file2', 'dir1', 'dir2'])
 
229
 
 
230
        for path in added_paths:
 
231
            self.assertNotEqual(None, wt.path2id(path.rstrip('/')),
 
232
                    'Failed to add path: %s' % (path,))
 
233
        for path in not_added:
 
234
            self.assertEqual(None, wt.path2id(path.rstrip('/')),
 
235
                    'Accidentally added path: %s' % (path,))
 
236
 
 
237
    def test_custom_ids(self):
 
238
        sio = StringIO()
 
239
        action = AddCustomIDAction(to_file=sio, should_print=True)
 
240
        self.build_tree(['file1', 'dir1/', 'dir1/file2'])
 
241
 
 
242
        wt = self.make_branch_and_tree('.')
 
243
        smart_add_tree(wt, ['.'], action=action)
 
244
        # The order of adds is not strictly fixed:
 
245
        sio.seek(0)
 
246
        lines = sorted(sio.readlines())
 
247
        self.assertEqualDiff(['added dir1 with id directory-dir1\n',
 
248
                              'added dir1/file2 with id file-dir1%file2\n',
 
249
                              'added file1 with id file-file1\n',
 
250
                             ], lines)
 
251
        wt.lock_read()
 
252
        self.addCleanup(wt.unlock)
 
253
        self.assertEqual([('', wt.path2id('')),
 
254
                          ('dir1', 'directory-dir1'),
 
255
                          ('dir1/file2', 'file-dir1%file2'),
 
256
                          ('file1', 'file-file1'),
 
257
                         ], [(path, ie.file_id) for path, ie
 
258
                                in wt.inventory.iter_entries()])
 
259
 
 
260
 
 
261
class TestAddFrom(TestCaseWithTransport):
 
262
    """Tests for AddFromBaseAction"""
 
263
 
 
264
    def make_base_tree(self):
 
265
        self.base_tree = self.make_branch_and_tree('base')
 
266
        self.build_tree(['base/a', 'base/b',
 
267
                         'base/dir/', 'base/dir/a',
 
268
                         'base/dir/subdir/',
 
269
                         'base/dir/subdir/b',
 
270
                        ])
 
271
        self.base_tree.add(['a', 'b', 'dir', 'dir/a',
 
272
                            'dir/subdir', 'dir/subdir/b'])
 
273
        self.base_tree.commit('creating initial tree.')
 
274
 
 
275
    def add_helper(self, base_tree, base_path, new_tree, file_list,
 
276
                   should_print=False):
 
277
        to_file = StringIO()
 
278
        base_tree.lock_read()
 
279
        try:
 
280
            new_tree.lock_write()
 
281
            try:
 
282
                action = AddFromBaseAction(base_tree, base_path,
 
283
                                           to_file=to_file,
 
284
                                           should_print=should_print)
 
285
                smart_add_tree(new_tree, file_list, action=action)
 
286
            finally:
 
287
                new_tree.unlock()
 
288
        finally:
 
289
            base_tree.unlock()
 
290
        return to_file.getvalue()
 
291
 
 
292
    def test_copy_all(self):
 
293
        self.make_base_tree()
 
294
        new_tree = self.make_branch_and_tree('new')
 
295
        files = ['a', 'b',
 
296
                 'dir/', 'dir/a',
 
297
                 'dir/subdir/',
 
298
                 'dir/subdir/b',
 
299
                ]
 
300
        self.build_tree(['new/' + fn for fn in files])
 
301
        self.add_helper(self.base_tree, '', new_tree, ['new'])
 
302
 
 
303
        for fn in files:
 
304
            base_file_id = self.base_tree.path2id(fn)
 
305
            new_file_id = new_tree.path2id(fn)
 
306
            self.assertEqual(base_file_id, new_file_id)
 
307
 
 
308
    def test_copy_from_dir(self):
 
309
        self.make_base_tree()
 
310
        new_tree = self.make_branch_and_tree('new')
 
311
 
 
312
        self.build_tree(['new/a', 'new/b', 'new/c',
 
313
                         'new/subdir/', 'new/subdir/b', 'new/subdir/d'])
 
314
        new_tree.set_root_id(self.base_tree.get_root_id())
 
315
        self.add_helper(self.base_tree, 'dir', new_tree, ['new'])
 
316
 
 
317
        # We know 'a' and 'b' exist in the root, and they are being added
 
318
        # in a new 'root'. Since ROOT ids have been set as the same, we will
 
319
        # use those ids
 
320
        self.assertEqual(self.base_tree.path2id('a'),
 
321
                         new_tree.path2id('a'))
 
322
        self.assertEqual(self.base_tree.path2id('b'),
 
323
                         new_tree.path2id('b'))
 
324
 
 
325
        # Because we specified 'dir/' as the base path, and we have
 
326
        # nothing named 'subdir' in the base tree, we should grab the
 
327
        # ids from there
 
328
        self.assertEqual(self.base_tree.path2id('dir/subdir'),
 
329
                         new_tree.path2id('subdir'))
 
330
        self.assertEqual(self.base_tree.path2id('dir/subdir/b'),
 
331
                         new_tree.path2id('subdir/b'))
 
332
 
 
333
        # These should get newly generated ids
 
334
        c_id = new_tree.path2id('c')
 
335
        self.assertNotEqual(None, c_id)
 
336
        self.base_tree.lock_read()
 
337
        self.addCleanup(self.base_tree.unlock)
 
338
        self.failIf(c_id in self.base_tree)
 
339
 
 
340
        d_id = new_tree.path2id('subdir/d')
 
341
        self.assertNotEqual(None, d_id)
 
342
        self.failIf(d_id in self.base_tree)
 
343
 
 
344
    def test_copy_existing_dir(self):
 
345
        self.make_base_tree()
 
346
        new_tree = self.make_branch_and_tree('new')
 
347
        self.build_tree(['new/subby/', 'new/subby/a', 'new/subby/b'])
 
348
 
 
349
        subdir_file_id = self.base_tree.path2id('dir/subdir')
 
350
        new_tree.add(['subby'], [subdir_file_id])
 
351
        self.add_helper(self.base_tree, '', new_tree, ['new'])
 
352
        # Because 'subby' already points to subdir, we should add
 
353
        # 'b' with the same id
 
354
        self.assertEqual(self.base_tree.path2id('dir/subdir/b'),
 
355
                         new_tree.path2id('subby/b'))
 
356
 
 
357
        # 'subby/a' should be added with a new id because there is no
 
358
        # matching path or child of 'subby'.
 
359
        a_id = new_tree.path2id('subby/a')
 
360
        self.assertNotEqual(None, a_id)
 
361
        self.base_tree.lock_read()
 
362
        self.addCleanup(self.base_tree.unlock)
 
363
        self.failIf(a_id in self.base_tree)
 
364
 
 
365
 
 
366
class TestAddNonNormalized(TestCaseWithTransport):
 
367
 
 
368
    def make(self):
 
369
        try:
 
370
            self.build_tree([u'a\u030a'])
 
371
        except UnicodeError:
 
372
            raise TestSkipped('Filesystem cannot create unicode filenames')
 
373
 
 
374
        self.wt = self.make_branch_and_tree('.')
 
375
 
 
376
    def test_accessible_explicit(self):
 
377
        self.make()
 
378
        orig = osutils.normalized_filename
 
379
        osutils.normalized_filename = osutils._accessible_normalized_filename
 
380
        try:
 
381
            smart_add_tree(self.wt, [u'a\u030a'])
 
382
            self.wt.lock_read()
 
383
            self.addCleanup(self.wt.unlock)
 
384
            self.assertEqual([('', 'directory'), (u'\xe5', 'file')],
 
385
                    [(path, ie.kind) for path,ie in 
 
386
                        self.wt.inventory.iter_entries()])
 
387
        finally:
 
388
            osutils.normalized_filename = orig
 
389
 
 
390
    def test_accessible_implicit(self):
 
391
        self.make()
 
392
        orig = osutils.normalized_filename
 
393
        osutils.normalized_filename = osutils._accessible_normalized_filename
 
394
        try:
 
395
            smart_add_tree(self.wt, [])
 
396
            self.wt.lock_read()
 
397
            self.addCleanup(self.wt.unlock)
 
398
            self.assertEqual([('', 'directory'), (u'\xe5', 'file')],
 
399
                    [(path, ie.kind) for path,ie in 
 
400
                        self.wt.inventory.iter_entries()])
 
401
        finally:
 
402
            osutils.normalized_filename = orig
 
403
 
 
404
    def test_inaccessible_explicit(self):
 
405
        self.make()
 
406
        orig = osutils.normalized_filename
 
407
        osutils.normalized_filename = osutils._inaccessible_normalized_filename
 
408
        try:
 
409
            self.assertRaises(errors.InvalidNormalization,
 
410
                    smart_add_tree, self.wt, [u'a\u030a'])
 
411
        finally:
 
412
            osutils.normalized_filename = orig
 
413
 
 
414
    def test_inaccessible_implicit(self):
 
415
        self.make()
 
416
        orig = osutils.normalized_filename
 
417
        osutils.normalized_filename = osutils._inaccessible_normalized_filename
 
418
        try:
 
419
            # TODO: jam 20060701 In the future, this should probably
 
420
            #       just ignore files that don't fit the normalization
 
421
            #       rules, rather than exploding
 
422
            self.assertRaises(errors.InvalidNormalization,
 
423
                    smart_add_tree, self.wt, [])
 
424
        finally:
 
425
            osutils.normalized_filename = orig
 
426
 
 
427
 
 
428
class TestAddActions(TestCase):
 
429
 
 
430
    def test_quiet(self):
 
431
        self.run_action("")
 
432
 
 
433
    def test__print(self):
 
434
        self.run_action("added path\n")
 
435
 
 
436
    def run_action(self, output):
 
437
        from bzrlib.add import AddAction, FastPath
 
438
        inv = Inventory()
 
439
        stdout = StringIO()
 
440
        action = AddAction(to_file=stdout, should_print=bool(output))
 
441
 
 
442
        self.apply_redirected(None, stdout, None, action, inv, None, FastPath('path'), 'file')
 
443
        self.assertEqual(stdout.getvalue(), output)