~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_diff.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2010-05-28 00:25:32 UTC
  • mfrom: (5264.1.2 command-help-bug-177500)
  • Revision ID: pqm@pqm.ubuntu.com-20100528002532-9bzj1fajyxckd1rg
(lifeless) Stop raising at runtime when a command has no help,
 instead have a test in the test suite that checks all known command objects.
 (Robert Collins)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005-2011 Canonical Ltd
 
1
# Copyright (C) 2005-2010 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
33
33
    transform,
34
34
    )
35
35
from bzrlib.symbol_versioning import deprecated_in
36
 
from bzrlib.tests import features, EncodingAdapter
37
 
from bzrlib.tests.blackbox.test_diff import subst_dates
 
36
from bzrlib.tests import features
38
37
 
39
38
 
40
39
class _AttribFeature(tests.Feature):
144
143
        self.check_patch(lines)
145
144
 
146
145
    def test_external_diff_binary_lang_c(self):
 
146
        old_env = {}
147
147
        for lang in ('LANG', 'LC_ALL', 'LANGUAGE'):
148
 
            self.overrideEnv(lang, 'C')
149
 
        lines = external_udiff_lines(['\x00foobar\n'], ['foo\x00bar\n'])
150
 
        # Older versions of diffutils say "Binary files", newer
151
 
        # versions just say "Files".
152
 
        self.assertContainsRe(lines[0], '(Binary f|F)iles old and new differ\n')
153
 
        self.assertEquals(lines[1:], ['\n'])
 
148
            old_env[lang] = osutils.set_or_unset_env(lang, 'C')
 
149
        try:
 
150
            lines = external_udiff_lines(['\x00foobar\n'], ['foo\x00bar\n'])
 
151
            # Older versions of diffutils say "Binary files", newer
 
152
            # versions just say "Files".
 
153
            self.assertContainsRe(lines[0],
 
154
                                  '(Binary f|F)iles old and new differ\n')
 
155
            self.assertEquals(lines[1:], ['\n'])
 
156
        finally:
 
157
            for lang, old_val in old_env.iteritems():
 
158
                osutils.set_or_unset_env(lang, old_val)
154
159
 
155
160
    def test_no_external_diff(self):
156
161
        """Check that NoDiff is raised when diff is not available"""
157
 
        # Make sure no 'diff' command is available
158
 
        # XXX: Weird, using None instead of '' breaks the test -- vila 20101216
159
 
        self.overrideEnv('PATH', '')
160
 
        self.assertRaises(errors.NoDiff, diff.external_diff,
161
 
                          'old', ['boo\n'], 'new', ['goo\n'],
162
 
                          StringIO(), diff_opts=['-u'])
 
162
        # Use os.environ['PATH'] to make sure no 'diff' command is available
 
163
        orig_path = os.environ['PATH']
 
164
        try:
 
165
            os.environ['PATH'] = ''
 
166
            self.assertRaises(errors.NoDiff, diff.external_diff,
 
167
                              'old', ['boo\n'], 'new', ['goo\n'],
 
168
                              StringIO(), diff_opts=['-u'])
 
169
        finally:
 
170
            os.environ['PATH'] = orig_path
163
171
 
164
172
    def test_internal_diff_default(self):
165
173
        # Default internal diff encoding is utf8
226
234
        output = StringIO.StringIO()
227
235
        diff.internal_diff(u'old_\xb5', ['old_text\n'],
228
236
                            u'new_\xe5', ['new_text\n'], output)
229
 
        self.assertIsInstance(output.getvalue(), str,
 
237
        self.failUnless(isinstance(output.getvalue(), str),
230
238
            'internal_diff should return bytestrings')
231
239
 
232
240
 
249
257
        self.assertEqual(out.splitlines(True) + ['\n'], lines)
250
258
 
251
259
 
252
 
def get_diff_as_string(tree1, tree2, specific_files=None, working_tree=None):
253
 
    output = StringIO()
254
 
    if working_tree is not None:
255
 
        extra_trees = (working_tree,)
256
 
    else:
257
 
        extra_trees = ()
258
 
    diff.show_diff_trees(tree1, tree2, output,
259
 
        specific_files=specific_files,
260
 
        extra_trees=extra_trees, old_label='old/',
261
 
        new_label='new/')
262
 
    return output.getvalue()
263
 
 
264
 
 
265
 
class TestDiffDates(tests.TestCaseWithTransport):
 
260
class TestShowDiffTreesHelper(tests.TestCaseWithTransport):
 
261
    """Has a helper for running show_diff_trees"""
 
262
 
 
263
    def get_diff(self, tree1, tree2, specific_files=None, working_tree=None):
 
264
        output = StringIO()
 
265
        if working_tree is not None:
 
266
            extra_trees = (working_tree,)
 
267
        else:
 
268
            extra_trees = ()
 
269
        diff.show_diff_trees(tree1, tree2, output,
 
270
                             specific_files=specific_files,
 
271
                             extra_trees=extra_trees, old_label='old/',
 
272
                             new_label='new/')
 
273
        return output.getvalue()
 
274
 
 
275
 
 
276
class TestDiffDates(TestShowDiffTreesHelper):
266
277
 
267
278
    def setUp(self):
268
279
        super(TestDiffDates, self).setUp()
303
314
        os.utime('file1', (1144195200, 1144195200)) # 2006-04-05 00:00:00 UTC
304
315
 
305
316
    def test_diff_rev_tree_working_tree(self):
306
 
        output = get_diff_as_string(self.wt.basis_tree(), self.wt)
 
317
        output = self.get_diff(self.wt.basis_tree(), self.wt)
307
318
        # note that the date for old/file1 is from rev 2 rather than from
308
319
        # the basis revision (rev 4)
309
320
        self.assertEqualDiff(output, '''\
319
330
    def test_diff_rev_tree_rev_tree(self):
320
331
        tree1 = self.b.repository.revision_tree('rev-2')
321
332
        tree2 = self.b.repository.revision_tree('rev-3')
322
 
        output = get_diff_as_string(tree1, tree2)
 
333
        output = self.get_diff(tree1, tree2)
323
334
        self.assertEqualDiff(output, '''\
324
335
=== modified file 'file2'
325
336
--- old/file2\t2006-04-01 00:00:00 +0000
333
344
    def test_diff_add_files(self):
334
345
        tree1 = self.b.repository.revision_tree(_mod_revision.NULL_REVISION)
335
346
        tree2 = self.b.repository.revision_tree('rev-1')
336
 
        output = get_diff_as_string(tree1, tree2)
 
347
        output = self.get_diff(tree1, tree2)
337
348
        # the files have the epoch time stamp for the tree in which
338
349
        # they don't exist.
339
350
        self.assertEqualDiff(output, '''\
354
365
    def test_diff_remove_files(self):
355
366
        tree1 = self.b.repository.revision_tree('rev-3')
356
367
        tree2 = self.b.repository.revision_tree('rev-4')
357
 
        output = get_diff_as_string(tree1, tree2)
 
368
        output = self.get_diff(tree1, tree2)
358
369
        # the file has the epoch time stamp for the tree in which
359
370
        # it doesn't exist.
360
371
        self.assertEqualDiff(output, '''\
371
382
        self.wt.rename_one('file1', 'file1b')
372
383
        old_tree = self.b.repository.revision_tree('rev-1')
373
384
        new_tree = self.b.repository.revision_tree('rev-4')
374
 
        out = get_diff_as_string(old_tree, new_tree, specific_files=['file1b'],
 
385
        out = self.get_diff(old_tree, new_tree, specific_files=['file1b'],
375
386
                            working_tree=self.wt)
376
387
        self.assertContainsRe(out, 'file1\t')
377
388
 
383
394
        self.wt.rename_one('file1', 'dir1/file1')
384
395
        old_tree = self.b.repository.revision_tree('rev-1')
385
396
        new_tree = self.b.repository.revision_tree('rev-4')
386
 
        out = get_diff_as_string(old_tree, new_tree, specific_files=['dir1'],
 
397
        out = self.get_diff(old_tree, new_tree, specific_files=['dir1'],
387
398
                            working_tree=self.wt)
388
399
        self.assertContainsRe(out, 'file1\t')
389
 
        out = get_diff_as_string(old_tree, new_tree, specific_files=['dir2'],
 
400
        out = self.get_diff(old_tree, new_tree, specific_files=['dir2'],
390
401
                            working_tree=self.wt)
391
402
        self.assertNotContainsRe(out, 'file1\t')
392
403
 
393
404
 
394
 
class TestShowDiffTrees(tests.TestCaseWithTransport):
 
405
 
 
406
class TestShowDiffTrees(TestShowDiffTreesHelper):
395
407
    """Direct tests for show_diff_trees"""
396
408
 
397
409
    def test_modified_file(self):
402
414
        tree.commit('one', rev_id='rev-1')
403
415
 
404
416
        self.build_tree_contents([('tree/file', 'new contents\n')])
405
 
        d = get_diff_as_string(tree.basis_tree(), tree)
 
417
        d = self.get_diff(tree.basis_tree(), tree)
406
418
        self.assertContainsRe(d, "=== modified file 'file'\n")
407
419
        self.assertContainsRe(d, '--- old/file\t')
408
420
        self.assertContainsRe(d, '\\+\\+\\+ new/file\t')
419
431
 
420
432
        tree.rename_one('dir', 'other')
421
433
        self.build_tree_contents([('tree/other/file', 'new contents\n')])
422
 
        d = get_diff_as_string(tree.basis_tree(), tree)
 
434
        d = self.get_diff(tree.basis_tree(), tree)
423
435
        self.assertContainsRe(d, "=== renamed directory 'dir' => 'other'\n")
424
436
        self.assertContainsRe(d, "=== modified file 'other/file'\n")
425
437
        # XXX: This is technically incorrect, because it used to be at another
438
450
        tree.commit('one', rev_id='rev-1')
439
451
 
440
452
        tree.rename_one('dir', 'newdir')
441
 
        d = get_diff_as_string(tree.basis_tree(), tree)
 
453
        d = self.get_diff(tree.basis_tree(), tree)
442
454
        # Renaming a directory should be a single "you renamed this dir" even
443
455
        # when there are files inside.
444
456
        self.assertEqual(d, "=== renamed directory 'dir' => 'newdir'\n")
451
463
        tree.commit('one', rev_id='rev-1')
452
464
 
453
465
        tree.rename_one('file', 'newname')
454
 
        d = get_diff_as_string(tree.basis_tree(), tree)
 
466
        d = self.get_diff(tree.basis_tree(), tree)
455
467
        self.assertContainsRe(d, "=== renamed file 'file' => 'newname'\n")
456
468
        # We shouldn't have a --- or +++ line, because there is no content
457
469
        # change
466
478
 
467
479
        tree.rename_one('file', 'newname')
468
480
        self.build_tree_contents([('tree/newname', 'new contents\n')])
469
 
        d = get_diff_as_string(tree.basis_tree(), tree)
 
481
        d = self.get_diff(tree.basis_tree(), tree)
470
482
        self.assertContainsRe(d, "=== renamed file 'file' => 'newname'\n")
471
483
        self.assertContainsRe(d, '--- old/file\t')
472
484
        self.assertContainsRe(d, '\\+\\+\\+ new/newname\t')
496
508
        tree.rename_one('c', 'new-c')
497
509
        tree.rename_one('d', 'new-d')
498
510
 
499
 
        d = get_diff_as_string(tree.basis_tree(), tree)
 
511
        d = self.get_diff(tree.basis_tree(), tree)
500
512
 
501
513
        self.assertContainsRe(d, r"file 'a'.*\(properties changed:"
502
514
                                  ".*\+x to -x.*\)")
509
521
        self.assertNotContainsRe(d, r"file 'e'")
510
522
        self.assertNotContainsRe(d, r"file 'f'")
511
523
 
 
524
 
512
525
    def test_binary_unicode_filenames(self):
513
526
        """Test that contents of files are *not* encoded in UTF-8 when there
514
527
        is a binary file in the diff.
560
573
        tree.add(['add_'+alpha], ['file-id'])
561
574
        self.build_tree_contents([('tree/mod_'+alpha, 'contents_mod\n')])
562
575
 
563
 
        d = get_diff_as_string(tree.basis_tree(), tree)
 
576
        d = self.get_diff(tree.basis_tree(), tree)
564
577
        self.assertContainsRe(d,
565
578
                "=== renamed file 'ren_%s' => 'ren_%s'\n"%(autf8, outf8))
566
579
        self.assertContainsRe(d, "=== added file 'add_%s'"%autf8)
567
580
        self.assertContainsRe(d, "=== modified file 'mod_%s'"%autf8)
568
581
        self.assertContainsRe(d, "=== removed file 'del_%s'"%autf8)
569
582
 
570
 
    def test_unicode_filename_path_encoding(self):
571
 
        """Test for bug #382699: unicode filenames on Windows should be shown
572
 
        in user encoding.
573
 
        """
574
 
        self.requireFeature(tests.UnicodeFilenameFeature)
575
 
        # The word 'test' in Russian
576
 
        _russian_test = u'\u0422\u0435\u0441\u0442'
577
 
        directory = _russian_test + u'/'
578
 
        test_txt = _russian_test + u'.txt'
579
 
        u1234 = u'\u1234.txt'
580
 
 
581
 
        tree = self.make_branch_and_tree('.')
582
 
        self.build_tree_contents([
583
 
            (test_txt, 'foo\n'),
584
 
            (u1234, 'foo\n'),
585
 
            (directory, None),
586
 
            ])
587
 
        tree.add([test_txt, u1234, directory])
588
 
 
589
 
        sio = StringIO()
590
 
        diff.show_diff_trees(tree.basis_tree(), tree, sio,
591
 
            path_encoding='cp1251')
592
 
 
593
 
        output = subst_dates(sio.getvalue())
594
 
        shouldbe = ('''\
595
 
=== added directory '%(directory)s'
596
 
=== added file '%(test_txt)s'
597
 
--- a/%(test_txt)s\tYYYY-MM-DD HH:MM:SS +ZZZZ
598
 
+++ b/%(test_txt)s\tYYYY-MM-DD HH:MM:SS +ZZZZ
599
 
@@ -0,0 +1,1 @@
600
 
+foo
601
 
 
602
 
=== added file '?.txt'
603
 
--- a/?.txt\tYYYY-MM-DD HH:MM:SS +ZZZZ
604
 
+++ b/?.txt\tYYYY-MM-DD HH:MM:SS +ZZZZ
605
 
@@ -0,0 +1,1 @@
606
 
+foo
607
 
 
608
 
''' % {'directory': _russian_test.encode('cp1251'),
609
 
       'test_txt': test_txt.encode('cp1251'),
610
 
      })
611
 
        self.assertEqualDiff(output, shouldbe)
612
 
 
613
583
 
614
584
class DiffWasIs(diff.DiffPath):
615
585
 
1421
1391
        diff_obj._prepare_files('file2-id', 'oldname2', 'newname2')
1422
1392
 
1423
1393
 
1424
 
class TestDiffFromToolEncodedFilename(tests.TestCaseWithTransport):
1425
 
 
1426
 
    def test_encodable_filename(self):
1427
 
        # Just checks file path for external diff tool.
1428
 
        # We cannot change CPython's internal encoding used by os.exec*.
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
 
            self.overrideAttr(diffobj, '_fenc', lambda: encoding)
1438
 
            relpath = dirname + u'/' + filename
1439
 
            fullpath = diffobj._safe_filename('safe', relpath)
1440
 
            self.assertEqual(
1441
 
                    fullpath,
1442
 
                    fullpath.encode(encoding).decode(encoding)
1443
 
                    )
1444
 
            self.assert_(fullpath.startswith(diffobj._root + '/safe'))
1445
 
 
1446
 
    def test_unencodable_filename(self):
1447
 
        import sys
1448
 
        diffobj = diff.DiffFromTool(['dummy', '@old_path', '@new_path'],
1449
 
                                    None, None, None)
1450
 
        for _, scenario in EncodingAdapter.encoding_scenarios:
1451
 
            encoding = scenario['encoding']
1452
 
            dirname  = scenario['info']['directory']
1453
 
            filename = scenario['info']['filename']
1454
 
 
1455
 
            if encoding == 'iso-8859-1':
1456
 
                encoding = 'iso-8859-2'
1457
 
            else:
1458
 
                encoding = 'iso-8859-1'
1459
 
 
1460
 
            self.overrideAttr(diffobj, '_fenc', lambda: encoding)
1461
 
            relpath = dirname + u'/' + filename
1462
 
            fullpath = diffobj._safe_filename('safe', relpath)
1463
 
            self.assertEqual(
1464
 
                    fullpath,
1465
 
                    fullpath.encode(encoding).decode(encoding)
1466
 
                    )
1467
 
            self.assert_(fullpath.startswith(diffobj._root + '/safe'))
1468
 
 
1469
 
 
1470
1394
class TestGetTreesAndBranchesToDiffLocked(tests.TestCaseWithTransport):
1471
1395
 
1472
1396
    def call_gtabtd(self, path_list, revision_specs, old_url, new_url):
1526
1450
        return self.applyDeprecated(
1527
1451
            deprecated_in((2, 2, 0)), diff.get_trees_and_branches_to_diff,
1528
1452
            path_list, revision_specs, old_url, new_url)
 
1453