~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_diff.py

  • Committer: Andrew Bennetts
  • Date: 2010-10-08 04:25:10 UTC
  • mto: This revision was merged to the branch mainline in revision 5472.
  • Revision ID: andrew.bennetts@canonical.com-20101008042510-sg9vdhmnggilzxsk
Fix stray TAB in source.

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
144
144
        self.check_patch(lines)
145
145
 
146
146
    def test_external_diff_binary_lang_c(self):
 
147
        old_env = {}
147
148
        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'])
 
149
            old_env[lang] = osutils.set_or_unset_env(lang, 'C')
 
150
        try:
 
151
            lines = external_udiff_lines(['\x00foobar\n'], ['foo\x00bar\n'])
 
152
            # Older versions of diffutils say "Binary files", newer
 
153
            # versions just say "Files".
 
154
            self.assertContainsRe(lines[0],
 
155
                                  '(Binary f|F)iles old and new differ\n')
 
156
            self.assertEquals(lines[1:], ['\n'])
 
157
        finally:
 
158
            for lang, old_val in old_env.iteritems():
 
159
                osutils.set_or_unset_env(lang, old_val)
154
160
 
155
161
    def test_no_external_diff(self):
156
162
        """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'])
 
163
        # Use os.environ['PATH'] to make sure no 'diff' command is available
 
164
        orig_path = os.environ['PATH']
 
165
        try:
 
166
            os.environ['PATH'] = ''
 
167
            self.assertRaises(errors.NoDiff, diff.external_diff,
 
168
                              'old', ['boo\n'], 'new', ['goo\n'],
 
169
                              StringIO(), diff_opts=['-u'])
 
170
        finally:
 
171
            os.environ['PATH'] = orig_path
163
172
 
164
173
    def test_internal_diff_default(self):
165
174
        # Default internal diff encoding is utf8
226
235
        output = StringIO.StringIO()
227
236
        diff.internal_diff(u'old_\xb5', ['old_text\n'],
228
237
                            u'new_\xe5', ['new_text\n'], output)
229
 
        self.assertIsInstance(output.getvalue(), str,
 
238
        self.failUnless(isinstance(output.getvalue(), str),
230
239
            'internal_diff should return bytestrings')
231
240
 
232
241
 
249
258
        self.assertEqual(out.splitlines(True) + ['\n'], lines)
250
259
 
251
260
 
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):
 
261
class TestShowDiffTreesHelper(tests.TestCaseWithTransport):
 
262
    """Has a helper for running show_diff_trees"""
 
263
 
 
264
    def get_diff(self, tree1, tree2, specific_files=None, working_tree=None):
 
265
        output = StringIO()
 
266
        if working_tree is not None:
 
267
            extra_trees = (working_tree,)
 
268
        else:
 
269
            extra_trees = ()
 
270
        diff.show_diff_trees(tree1, tree2, output,
 
271
                             specific_files=specific_files,
 
272
                             extra_trees=extra_trees, old_label='old/',
 
273
                             new_label='new/')
 
274
        return output.getvalue()
 
275
 
 
276
 
 
277
class TestDiffDates(TestShowDiffTreesHelper):
266
278
 
267
279
    def setUp(self):
268
280
        super(TestDiffDates, self).setUp()
303
315
        os.utime('file1', (1144195200, 1144195200)) # 2006-04-05 00:00:00 UTC
304
316
 
305
317
    def test_diff_rev_tree_working_tree(self):
306
 
        output = get_diff_as_string(self.wt.basis_tree(), self.wt)
 
318
        output = self.get_diff(self.wt.basis_tree(), self.wt)
307
319
        # note that the date for old/file1 is from rev 2 rather than from
308
320
        # the basis revision (rev 4)
309
321
        self.assertEqualDiff(output, '''\
319
331
    def test_diff_rev_tree_rev_tree(self):
320
332
        tree1 = self.b.repository.revision_tree('rev-2')
321
333
        tree2 = self.b.repository.revision_tree('rev-3')
322
 
        output = get_diff_as_string(tree1, tree2)
 
334
        output = self.get_diff(tree1, tree2)
323
335
        self.assertEqualDiff(output, '''\
324
336
=== modified file 'file2'
325
337
--- old/file2\t2006-04-01 00:00:00 +0000
333
345
    def test_diff_add_files(self):
334
346
        tree1 = self.b.repository.revision_tree(_mod_revision.NULL_REVISION)
335
347
        tree2 = self.b.repository.revision_tree('rev-1')
336
 
        output = get_diff_as_string(tree1, tree2)
 
348
        output = self.get_diff(tree1, tree2)
337
349
        # the files have the epoch time stamp for the tree in which
338
350
        # they don't exist.
339
351
        self.assertEqualDiff(output, '''\
354
366
    def test_diff_remove_files(self):
355
367
        tree1 = self.b.repository.revision_tree('rev-3')
356
368
        tree2 = self.b.repository.revision_tree('rev-4')
357
 
        output = get_diff_as_string(tree1, tree2)
 
369
        output = self.get_diff(tree1, tree2)
358
370
        # the file has the epoch time stamp for the tree in which
359
371
        # it doesn't exist.
360
372
        self.assertEqualDiff(output, '''\
371
383
        self.wt.rename_one('file1', 'file1b')
372
384
        old_tree = self.b.repository.revision_tree('rev-1')
373
385
        new_tree = self.b.repository.revision_tree('rev-4')
374
 
        out = get_diff_as_string(old_tree, new_tree, specific_files=['file1b'],
 
386
        out = self.get_diff(old_tree, new_tree, specific_files=['file1b'],
375
387
                            working_tree=self.wt)
376
388
        self.assertContainsRe(out, 'file1\t')
377
389
 
383
395
        self.wt.rename_one('file1', 'dir1/file1')
384
396
        old_tree = self.b.repository.revision_tree('rev-1')
385
397
        new_tree = self.b.repository.revision_tree('rev-4')
386
 
        out = get_diff_as_string(old_tree, new_tree, specific_files=['dir1'],
 
398
        out = self.get_diff(old_tree, new_tree, specific_files=['dir1'],
387
399
                            working_tree=self.wt)
388
400
        self.assertContainsRe(out, 'file1\t')
389
 
        out = get_diff_as_string(old_tree, new_tree, specific_files=['dir2'],
 
401
        out = self.get_diff(old_tree, new_tree, specific_files=['dir2'],
390
402
                            working_tree=self.wt)
391
403
        self.assertNotContainsRe(out, 'file1\t')
392
404
 
393
405
 
394
 
class TestShowDiffTrees(tests.TestCaseWithTransport):
 
406
 
 
407
class TestShowDiffTrees(TestShowDiffTreesHelper):
395
408
    """Direct tests for show_diff_trees"""
396
409
 
397
410
    def test_modified_file(self):
402
415
        tree.commit('one', rev_id='rev-1')
403
416
 
404
417
        self.build_tree_contents([('tree/file', 'new contents\n')])
405
 
        d = get_diff_as_string(tree.basis_tree(), tree)
 
418
        d = self.get_diff(tree.basis_tree(), tree)
406
419
        self.assertContainsRe(d, "=== modified file 'file'\n")
407
420
        self.assertContainsRe(d, '--- old/file\t')
408
421
        self.assertContainsRe(d, '\\+\\+\\+ new/file\t')
419
432
 
420
433
        tree.rename_one('dir', 'other')
421
434
        self.build_tree_contents([('tree/other/file', 'new contents\n')])
422
 
        d = get_diff_as_string(tree.basis_tree(), tree)
 
435
        d = self.get_diff(tree.basis_tree(), tree)
423
436
        self.assertContainsRe(d, "=== renamed directory 'dir' => 'other'\n")
424
437
        self.assertContainsRe(d, "=== modified file 'other/file'\n")
425
438
        # XXX: This is technically incorrect, because it used to be at another
438
451
        tree.commit('one', rev_id='rev-1')
439
452
 
440
453
        tree.rename_one('dir', 'newdir')
441
 
        d = get_diff_as_string(tree.basis_tree(), tree)
 
454
        d = self.get_diff(tree.basis_tree(), tree)
442
455
        # Renaming a directory should be a single "you renamed this dir" even
443
456
        # when there are files inside.
444
457
        self.assertEqual(d, "=== renamed directory 'dir' => 'newdir'\n")
451
464
        tree.commit('one', rev_id='rev-1')
452
465
 
453
466
        tree.rename_one('file', 'newname')
454
 
        d = get_diff_as_string(tree.basis_tree(), tree)
 
467
        d = self.get_diff(tree.basis_tree(), tree)
455
468
        self.assertContainsRe(d, "=== renamed file 'file' => 'newname'\n")
456
469
        # We shouldn't have a --- or +++ line, because there is no content
457
470
        # change
466
479
 
467
480
        tree.rename_one('file', 'newname')
468
481
        self.build_tree_contents([('tree/newname', 'new contents\n')])
469
 
        d = get_diff_as_string(tree.basis_tree(), tree)
 
482
        d = self.get_diff(tree.basis_tree(), tree)
470
483
        self.assertContainsRe(d, "=== renamed file 'file' => 'newname'\n")
471
484
        self.assertContainsRe(d, '--- old/file\t')
472
485
        self.assertContainsRe(d, '\\+\\+\\+ new/newname\t')
496
509
        tree.rename_one('c', 'new-c')
497
510
        tree.rename_one('d', 'new-d')
498
511
 
499
 
        d = get_diff_as_string(tree.basis_tree(), tree)
 
512
        d = self.get_diff(tree.basis_tree(), tree)
500
513
 
501
514
        self.assertContainsRe(d, r"file 'a'.*\(properties changed:"
502
515
                                  ".*\+x to -x.*\)")
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)
1480
1493
        return self.applyDeprecated(
1481
1494
            deprecated_in((2, 2, 0)), diff.get_trees_and_branches_to_diff,
1482
1495
            path_list, revision_specs, old_url, new_url)
 
1496