~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_diff.py

  • Committer: Jelmer Vernooij
  • Date: 2011-04-09 19:25:42 UTC
  • mto: (5777.5.1 inventoryworkingtree)
  • mto: This revision was merged to the branch mainline in revision 5781.
  • Revision ID: jelmer@samba.org-20110409192542-8bbedp36s7nj928e
Split InventoryTree out of Tree.

Show diffs side-by-side

added added

removed removed

Lines of Context:
33
33
    transform,
34
34
    )
35
35
from bzrlib.symbol_versioning import deprecated_in
36
 
from bzrlib.tests import features, EncodingAdapter
 
36
from bzrlib.tests import features
37
37
from bzrlib.tests.blackbox.test_diff import subst_dates
38
 
from bzrlib.tests import (
39
 
    features,
40
 
    )
 
38
 
 
39
 
 
40
class _AttribFeature(tests.Feature):
 
41
 
 
42
    def _probe(self):
 
43
        if (sys.platform not in ('cygwin', 'win32')):
 
44
            return False
 
45
        try:
 
46
            proc = subprocess.Popen(['attrib', '.'], stdout=subprocess.PIPE)
 
47
        except OSError, e:
 
48
            return False
 
49
        return (0 == proc.wait())
 
50
 
 
51
    def feature_name(self):
 
52
        return 'attrib Windows command-line tool'
 
53
 
 
54
AttribFeature = _AttribFeature()
 
55
 
 
56
 
 
57
compiled_patiencediff_feature = tests.ModuleAvailableFeature(
 
58
                                    'bzrlib._patiencediff_c')
41
59
 
42
60
 
43
61
def udiff_lines(old, new, allow_binary=False):
208
226
        output = StringIO.StringIO()
209
227
        diff.internal_diff(u'old_\xb5', ['old_text\n'],
210
228
                            u'new_\xe5', ['new_text\n'], output)
211
 
        self.assertIsInstance(output.getvalue(), str,
 
229
        self.failUnless(isinstance(output.getvalue(), str),
212
230
            'internal_diff should return bytestrings')
213
231
 
214
232
 
231
249
        self.assertEqual(out.splitlines(True) + ['\n'], lines)
232
250
 
233
251
 
234
 
def get_diff_as_string(tree1, tree2, specific_files=None, working_tree=None):
235
 
    output = StringIO()
236
 
    if working_tree is not None:
237
 
        extra_trees = (working_tree,)
238
 
    else:
239
 
        extra_trees = ()
240
 
    diff.show_diff_trees(tree1, tree2, output,
241
 
        specific_files=specific_files,
242
 
        extra_trees=extra_trees, old_label='old/',
243
 
        new_label='new/')
244
 
    return output.getvalue()
245
 
 
246
 
 
247
 
class TestDiffDates(tests.TestCaseWithTransport):
 
252
class TestShowDiffTreesHelper(tests.TestCaseWithTransport):
 
253
    """Has a helper for running show_diff_trees"""
 
254
 
 
255
    def get_diff(self, tree1, tree2, specific_files=None, working_tree=None):
 
256
        output = StringIO()
 
257
        if working_tree is not None:
 
258
            extra_trees = (working_tree,)
 
259
        else:
 
260
            extra_trees = ()
 
261
        diff.show_diff_trees(tree1, tree2, output,
 
262
                             specific_files=specific_files,
 
263
                             extra_trees=extra_trees, old_label='old/',
 
264
                             new_label='new/')
 
265
        return output.getvalue()
 
266
 
 
267
 
 
268
class TestDiffDates(TestShowDiffTreesHelper):
248
269
 
249
270
    def setUp(self):
250
271
        super(TestDiffDates, self).setUp()
285
306
        os.utime('file1', (1144195200, 1144195200)) # 2006-04-05 00:00:00 UTC
286
307
 
287
308
    def test_diff_rev_tree_working_tree(self):
288
 
        output = get_diff_as_string(self.wt.basis_tree(), self.wt)
 
309
        output = self.get_diff(self.wt.basis_tree(), self.wt)
289
310
        # note that the date for old/file1 is from rev 2 rather than from
290
311
        # the basis revision (rev 4)
291
312
        self.assertEqualDiff(output, '''\
301
322
    def test_diff_rev_tree_rev_tree(self):
302
323
        tree1 = self.b.repository.revision_tree('rev-2')
303
324
        tree2 = self.b.repository.revision_tree('rev-3')
304
 
        output = get_diff_as_string(tree1, tree2)
 
325
        output = self.get_diff(tree1, tree2)
305
326
        self.assertEqualDiff(output, '''\
306
327
=== modified file 'file2'
307
328
--- old/file2\t2006-04-01 00:00:00 +0000
315
336
    def test_diff_add_files(self):
316
337
        tree1 = self.b.repository.revision_tree(_mod_revision.NULL_REVISION)
317
338
        tree2 = self.b.repository.revision_tree('rev-1')
318
 
        output = get_diff_as_string(tree1, tree2)
 
339
        output = self.get_diff(tree1, tree2)
319
340
        # the files have the epoch time stamp for the tree in which
320
341
        # they don't exist.
321
342
        self.assertEqualDiff(output, '''\
336
357
    def test_diff_remove_files(self):
337
358
        tree1 = self.b.repository.revision_tree('rev-3')
338
359
        tree2 = self.b.repository.revision_tree('rev-4')
339
 
        output = get_diff_as_string(tree1, tree2)
 
360
        output = self.get_diff(tree1, tree2)
340
361
        # the file has the epoch time stamp for the tree in which
341
362
        # it doesn't exist.
342
363
        self.assertEqualDiff(output, '''\
353
374
        self.wt.rename_one('file1', 'file1b')
354
375
        old_tree = self.b.repository.revision_tree('rev-1')
355
376
        new_tree = self.b.repository.revision_tree('rev-4')
356
 
        out = get_diff_as_string(old_tree, new_tree, specific_files=['file1b'],
 
377
        out = self.get_diff(old_tree, new_tree, specific_files=['file1b'],
357
378
                            working_tree=self.wt)
358
379
        self.assertContainsRe(out, 'file1\t')
359
380
 
365
386
        self.wt.rename_one('file1', 'dir1/file1')
366
387
        old_tree = self.b.repository.revision_tree('rev-1')
367
388
        new_tree = self.b.repository.revision_tree('rev-4')
368
 
        out = get_diff_as_string(old_tree, new_tree, specific_files=['dir1'],
 
389
        out = self.get_diff(old_tree, new_tree, specific_files=['dir1'],
369
390
                            working_tree=self.wt)
370
391
        self.assertContainsRe(out, 'file1\t')
371
 
        out = get_diff_as_string(old_tree, new_tree, specific_files=['dir2'],
 
392
        out = self.get_diff(old_tree, new_tree, specific_files=['dir2'],
372
393
                            working_tree=self.wt)
373
394
        self.assertNotContainsRe(out, 'file1\t')
374
395
 
375
396
 
376
 
class TestShowDiffTrees(tests.TestCaseWithTransport):
 
397
 
 
398
class TestShowDiffTrees(TestShowDiffTreesHelper):
377
399
    """Direct tests for show_diff_trees"""
378
400
 
379
401
    def test_modified_file(self):
384
406
        tree.commit('one', rev_id='rev-1')
385
407
 
386
408
        self.build_tree_contents([('tree/file', 'new contents\n')])
387
 
        d = get_diff_as_string(tree.basis_tree(), tree)
 
409
        d = self.get_diff(tree.basis_tree(), tree)
388
410
        self.assertContainsRe(d, "=== modified file 'file'\n")
389
411
        self.assertContainsRe(d, '--- old/file\t')
390
412
        self.assertContainsRe(d, '\\+\\+\\+ new/file\t')
401
423
 
402
424
        tree.rename_one('dir', 'other')
403
425
        self.build_tree_contents([('tree/other/file', 'new contents\n')])
404
 
        d = get_diff_as_string(tree.basis_tree(), tree)
 
426
        d = self.get_diff(tree.basis_tree(), tree)
405
427
        self.assertContainsRe(d, "=== renamed directory 'dir' => 'other'\n")
406
428
        self.assertContainsRe(d, "=== modified file 'other/file'\n")
407
429
        # XXX: This is technically incorrect, because it used to be at another
420
442
        tree.commit('one', rev_id='rev-1')
421
443
 
422
444
        tree.rename_one('dir', 'newdir')
423
 
        d = get_diff_as_string(tree.basis_tree(), tree)
 
445
        d = self.get_diff(tree.basis_tree(), tree)
424
446
        # Renaming a directory should be a single "you renamed this dir" even
425
447
        # when there are files inside.
426
448
        self.assertEqual(d, "=== renamed directory 'dir' => 'newdir'\n")
433
455
        tree.commit('one', rev_id='rev-1')
434
456
 
435
457
        tree.rename_one('file', 'newname')
436
 
        d = get_diff_as_string(tree.basis_tree(), tree)
 
458
        d = self.get_diff(tree.basis_tree(), tree)
437
459
        self.assertContainsRe(d, "=== renamed file 'file' => 'newname'\n")
438
460
        # We shouldn't have a --- or +++ line, because there is no content
439
461
        # change
448
470
 
449
471
        tree.rename_one('file', 'newname')
450
472
        self.build_tree_contents([('tree/newname', 'new contents\n')])
451
 
        d = get_diff_as_string(tree.basis_tree(), tree)
 
473
        d = self.get_diff(tree.basis_tree(), tree)
452
474
        self.assertContainsRe(d, "=== renamed file 'file' => 'newname'\n")
453
475
        self.assertContainsRe(d, '--- old/file\t')
454
476
        self.assertContainsRe(d, '\\+\\+\\+ new/newname\t')
478
500
        tree.rename_one('c', 'new-c')
479
501
        tree.rename_one('d', 'new-d')
480
502
 
481
 
        d = get_diff_as_string(tree.basis_tree(), tree)
 
503
        d = self.get_diff(tree.basis_tree(), tree)
482
504
 
483
505
        self.assertContainsRe(d, r"file 'a'.*\(properties changed:"
484
506
                                  ".*\+x to -x.*\)")
496
518
        is a binary file in the diff.
497
519
        """
498
520
        # See https://bugs.launchpad.net/bugs/110092.
499
 
        self.requireFeature(features.UnicodeFilenameFeature)
 
521
        self.requireFeature(tests.UnicodeFilenameFeature)
500
522
 
501
523
        # This bug isn't triggered with cStringIO.
502
524
        from StringIO import StringIO
521
543
 
522
544
    def test_unicode_filename(self):
523
545
        """Test when the filename are unicode."""
524
 
        self.requireFeature(features.UnicodeFilenameFeature)
 
546
        self.requireFeature(tests.UnicodeFilenameFeature)
525
547
 
526
548
        alpha, omega = u'\u03b1', u'\u03c9'
527
549
        autf8, outf8 = alpha.encode('utf8'), omega.encode('utf8')
542
564
        tree.add(['add_'+alpha], ['file-id'])
543
565
        self.build_tree_contents([('tree/mod_'+alpha, 'contents_mod\n')])
544
566
 
545
 
        d = get_diff_as_string(tree.basis_tree(), tree)
 
567
        d = self.get_diff(tree.basis_tree(), tree)
546
568
        self.assertContainsRe(d,
547
569
                "=== renamed file 'ren_%s' => 'ren_%s'\n"%(autf8, outf8))
548
570
        self.assertContainsRe(d, "=== added file 'add_%s'"%autf8)
553
575
        """Test for bug #382699: unicode filenames on Windows should be shown
554
576
        in user encoding.
555
577
        """
556
 
        self.requireFeature(features.UnicodeFilenameFeature)
 
578
        self.requireFeature(tests.UnicodeFilenameFeature)
557
579
        # The word 'test' in Russian
558
580
        _russian_test = u'\u0422\u0435\u0441\u0442'
559
581
        directory = _russian_test + u'/'
690
712
             ' \@\@\n-old\n\+new\n\n')
691
713
 
692
714
    def test_diff_kind_change(self):
693
 
        self.requireFeature(features.SymlinkFeature)
 
715
        self.requireFeature(tests.SymlinkFeature)
694
716
        self.build_tree_contents([('old-tree/olddir/',),
695
717
                                  ('old-tree/olddir/oldfile', 'old\n')])
696
718
        self.old_tree.add('olddir')
1154
1176
 
1155
1177
class TestPatienceDiffLib_c(TestPatienceDiffLib):
1156
1178
 
1157
 
    _test_needs_features = [features.compiled_patiencediff_feature]
 
1179
    _test_needs_features = [compiled_patiencediff_feature]
1158
1180
 
1159
1181
    def setUp(self):
1160
1182
        super(TestPatienceDiffLib_c, self).setUp()
1250
1272
 
1251
1273
class TestPatienceDiffLibFiles_c(TestPatienceDiffLibFiles):
1252
1274
 
1253
 
    _test_needs_features = [features.compiled_patiencediff_feature]
 
1275
    _test_needs_features = [compiled_patiencediff_feature]
1254
1276
 
1255
1277
    def setUp(self):
1256
1278
        super(TestPatienceDiffLibFiles_c, self).setUp()
1262
1284
class TestUsingCompiledIfAvailable(tests.TestCase):
1263
1285
 
1264
1286
    def test_PatienceSequenceMatcher(self):
1265
 
        if features.compiled_patiencediff_feature.available():
 
1287
        if compiled_patiencediff_feature.available():
1266
1288
            from bzrlib._patiencediff_c import PatienceSequenceMatcher_c
1267
1289
            self.assertIs(PatienceSequenceMatcher_c,
1268
1290
                          patiencediff.PatienceSequenceMatcher)
1272
1294
                          patiencediff.PatienceSequenceMatcher)
1273
1295
 
1274
1296
    def test_unique_lcs(self):
1275
 
        if features.compiled_patiencediff_feature.available():
 
1297
        if compiled_patiencediff_feature.available():
1276
1298
            from bzrlib._patiencediff_c import unique_lcs_c
1277
1299
            self.assertIs(unique_lcs_c,
1278
1300
                          patiencediff.unique_lcs)
1282
1304
                          patiencediff.unique_lcs)
1283
1305
 
1284
1306
    def test_recurse_matches(self):
1285
 
        if features.compiled_patiencediff_feature.available():
 
1307
        if compiled_patiencediff_feature.available():
1286
1308
            from bzrlib._patiencediff_c import recurse_matches_c
1287
1309
            self.assertIs(recurse_matches_c,
1288
1310
                          patiencediff.recurse_matches)
1338
1360
                         ' on this machine', str(e))
1339
1361
 
1340
1362
    def test_prepare_files_creates_paths_readable_by_windows_tool(self):
1341
 
        self.requireFeature(features.AttribFeature)
 
1363
        self.requireFeature(AttribFeature)
1342
1364
        output = StringIO()
1343
1365
        tree = self.make_branch_and_tree('tree')
1344
1366
        self.build_tree_contents([('tree/file', 'content')])
1403
1425
        diff_obj._prepare_files('file2-id', 'oldname2', 'newname2')
1404
1426
 
1405
1427
 
1406
 
class TestDiffFromToolEncodedFilename(tests.TestCaseWithTransport):
1407
 
 
1408
 
    def test_encodable_filename(self):
1409
 
        # Just checks file path for external diff tool.
1410
 
        # We cannot change CPython's internal encoding used by os.exec*.
1411
 
        import sys
1412
 
        diffobj = diff.DiffFromTool(['dummy', '@old_path', '@new_path'],
1413
 
                                    None, None, None)
1414
 
        for _, scenario in EncodingAdapter.encoding_scenarios:
1415
 
            encoding = scenario['encoding']
1416
 
            dirname  = scenario['info']['directory']
1417
 
            filename = scenario['info']['filename']
1418
 
 
1419
 
            self.overrideAttr(diffobj, '_fenc', lambda: encoding)
1420
 
            relpath = dirname + u'/' + filename
1421
 
            fullpath = diffobj._safe_filename('safe', relpath)
1422
 
            self.assertEqual(
1423
 
                    fullpath,
1424
 
                    fullpath.encode(encoding).decode(encoding)
1425
 
                    )
1426
 
            self.assert_(fullpath.startswith(diffobj._root + '/safe'))
1427
 
 
1428
 
    def test_unencodable_filename(self):
1429
 
        import sys
1430
 
        diffobj = diff.DiffFromTool(['dummy', '@old_path', '@new_path'],
1431
 
                                    None, None, None)
1432
 
        for _, scenario in EncodingAdapter.encoding_scenarios:
1433
 
            encoding = scenario['encoding']
1434
 
            dirname  = scenario['info']['directory']
1435
 
            filename = scenario['info']['filename']
1436
 
 
1437
 
            if encoding == 'iso-8859-1':
1438
 
                encoding = 'iso-8859-2'
1439
 
            else:
1440
 
                encoding = 'iso-8859-1'
1441
 
 
1442
 
            self.overrideAttr(diffobj, '_fenc', lambda: encoding)
1443
 
            relpath = dirname + u'/' + filename
1444
 
            fullpath = diffobj._safe_filename('safe', relpath)
1445
 
            self.assertEqual(
1446
 
                    fullpath,
1447
 
                    fullpath.encode(encoding).decode(encoding)
1448
 
                    )
1449
 
            self.assert_(fullpath.startswith(diffobj._root + '/safe'))
1450
 
 
1451
 
 
1452
1428
class TestGetTreesAndBranchesToDiffLocked(tests.TestCaseWithTransport):
1453
1429
 
1454
1430
    def call_gtabtd(self, path_list, revision_specs, old_url, new_url):
1455
 
        """Call get_trees_and_branches_to_diff_locked."""
 
1431
        """Call get_trees_and_branches_to_diff_locked.  Overridden by
 
1432
        TestGetTreesAndBranchesToDiff.
 
1433
        """
1456
1434
        return diff.get_trees_and_branches_to_diff_locked(
1457
1435
            path_list, revision_specs, old_url, new_url, self.addCleanup)
1458
1436
 
1495
1473
        self.assertEqual(tree.branch.base, new_branch.base)
1496
1474
        self.assertIs(None, specific_files)
1497
1475
        self.assertEqual(tree.basedir, extra_trees[0].basedir)
 
1476
 
 
1477
 
 
1478
class TestGetTreesAndBranchesToDiff(TestGetTreesAndBranchesToDiffLocked):
 
1479
    """Apply the tests for get_trees_and_branches_to_diff_locked to the
 
1480
    deprecated get_trees_and_branches_to_diff function.
 
1481
    """
 
1482
 
 
1483
    def call_gtabtd(self, path_list, revision_specs, old_url, new_url):
 
1484
        return self.applyDeprecated(
 
1485
            deprecated_in((2, 2, 0)), diff.get_trees_and_branches_to_diff,
 
1486
            path_list, revision_specs, old_url, new_url)
 
1487