~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_diff.py

Merge bzr.dev.

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
 
23
23
from bzrlib.diff import internal_diff, external_diff, show_diff_trees
24
24
from bzrlib.errors import BinaryFile, NoDiff
 
25
import bzrlib.osutils as osutils
25
26
import bzrlib.patiencediff
26
27
from bzrlib.tests import (TestCase, TestCaseWithTransport,
27
28
                          TestCaseInTempDir, TestSkipped)
111
112
        self.check_patch(lines)
112
113
 
113
114
    def test_external_diff_binary_lang_c(self):
114
 
        orig_lang = os.environ.get('LANG')
115
 
        orig_lc_all = os.environ.get('LC_ALL')
 
115
        old_env = {}
 
116
        for lang in ('LANG', 'LC_ALL', 'LANGUAGE'):
 
117
            old_env[lang] = osutils.set_or_unset_env(lang, 'C')
116
118
        try:
117
 
            os.environ['LANG'] = 'C'
118
 
            os.environ['LC_ALL'] = 'C'
119
119
            lines = external_udiff_lines(['\x00foobar\n'], ['foo\x00bar\n'])
120
120
            # Older versions of diffutils say "Binary files", newer
121
121
            # versions just say "Files".
123
123
                                  '(Binary f|F)iles old and new differ\n')
124
124
            self.assertEquals(lines[1:], ['\n'])
125
125
        finally:
126
 
            for name, value in [('LANG', orig_lang), ('LC_ALL', orig_lc_all)]:
127
 
                if value is None:
128
 
                    del os.environ[name]
129
 
                else:
130
 
                    os.environ[name] = value
 
126
            for lang, old_val in old_env.iteritems():
 
127
                osutils.set_or_unset_env(lang, old_val)
131
128
 
132
129
    def test_no_external_diff(self):
133
130
        """Check that NoDiff is raised when diff is not available"""
205
202
        # Make sure external_diff doesn't fail in the current LANG
206
203
        lines = external_udiff_lines(['\x00foobar\n'], ['foo\x00bar\n'])
207
204
 
208
 
        cmd = ['diff', '-u', 'old', 'new']
 
205
        cmd = ['diff', '-u', '--binary', 'old', 'new']
209
206
        open('old', 'wb').write('\x00foobar\n')
210
207
        open('new', 'wb').write('foo\x00bar\n')
211
208
        pipe = subprocess.Popen(cmd, stdout=subprocess.PIPE,
217
214
        self.assertEqual(out.splitlines(True) + ['\n'], lines)
218
215
 
219
216
 
220
 
class TestDiffDates(TestCaseWithTransport):
 
217
class TestShowDiffTreesHelper(TestCaseWithTransport):
 
218
    """Has a helper for running show_diff_trees"""
 
219
 
 
220
    def get_diff(self, tree1, tree2, specific_files=None, working_tree=None):
 
221
        output = StringIO()
 
222
        if working_tree is not None:
 
223
            extra_trees = (working_tree,)
 
224
        else:
 
225
            extra_trees = ()
 
226
        show_diff_trees(tree1, tree2, output, specific_files=specific_files,
 
227
                        extra_trees=extra_trees, old_label='old/',
 
228
                        new_label='new/')
 
229
        return output.getvalue()
 
230
 
 
231
 
 
232
class TestDiffDates(TestShowDiffTreesHelper):
221
233
 
222
234
    def setUp(self):
223
235
        super(TestDiffDates, self).setUp()
257
269
        # set the date stamps for files in the working tree to known values
258
270
        os.utime('file1', (1144195200, 1144195200)) # 2006-04-05 00:00:00 UTC
259
271
 
260
 
    def get_diff(self, tree1, tree2, specific_files=None, working_tree=None):
261
 
        output = StringIO()
262
 
        if working_tree is not None:
263
 
            extra_trees = (working_tree,)
264
 
        else:
265
 
            extra_trees = ()
266
 
        show_diff_trees(tree1, tree2, output, specific_files=specific_files,
267
 
                        extra_trees=extra_trees, old_label='old/', 
268
 
                        new_label='new/')
269
 
        return output.getvalue()
270
 
 
271
272
    def test_diff_rev_tree_working_tree(self):
272
273
        output = self.get_diff(self.wt.basis_tree(), self.wt)
273
274
        # note that the date for old/file1 is from rev 2 rather than from
357
358
        self.assertNotContainsRe(out, 'file1\t')
358
359
 
359
360
 
 
361
 
 
362
class TestShowDiffTrees(TestShowDiffTreesHelper):
 
363
    """Direct tests for show_diff_trees"""
 
364
 
 
365
    def test_modified_file(self):
 
366
        """Test when a file is modified."""
 
367
        tree = self.make_branch_and_tree('tree')
 
368
        self.build_tree_contents([('tree/file', 'contents\n')])
 
369
        tree.add(['file'], ['file-id'])
 
370
        tree.commit('one', rev_id='rev-1')
 
371
 
 
372
        self.build_tree_contents([('tree/file', 'new contents\n')])
 
373
        diff = self.get_diff(tree.basis_tree(), tree)
 
374
        self.assertContainsRe(diff, "=== modified file 'file'\n")
 
375
        self.assertContainsRe(diff, '--- old/file\t')
 
376
        self.assertContainsRe(diff, '\\+\\+\\+ new/file\t')
 
377
        self.assertContainsRe(diff, '-contents\n'
 
378
                                    '\\+new contents\n')
 
379
 
 
380
    def test_modified_file_in_renamed_dir(self):
 
381
        """Test when a file is modified in a renamed directory."""
 
382
        tree = self.make_branch_and_tree('tree')
 
383
        self.build_tree(['tree/dir/'])
 
384
        self.build_tree_contents([('tree/dir/file', 'contents\n')])
 
385
        tree.add(['dir', 'dir/file'], ['dir-id', 'file-id'])
 
386
        tree.commit('one', rev_id='rev-1')
 
387
 
 
388
        tree.rename_one('dir', 'other')
 
389
        self.build_tree_contents([('tree/other/file', 'new contents\n')])
 
390
        diff = self.get_diff(tree.basis_tree(), tree)
 
391
        self.assertContainsRe(diff, "=== renamed directory 'dir' => 'other'\n")
 
392
        self.assertContainsRe(diff, "=== modified file 'other/file'\n")
 
393
        # XXX: This is technically incorrect, because it used to be at another
 
394
        # location. What to do?
 
395
        self.assertContainsRe(diff, '--- old/dir/file\t')
 
396
        self.assertContainsRe(diff, '\\+\\+\\+ new/other/file\t')
 
397
        self.assertContainsRe(diff, '-contents\n'
 
398
                                    '\\+new contents\n')
 
399
 
 
400
    def test_renamed_directory(self):
 
401
        """Test when only a directory is only renamed."""
 
402
        tree = self.make_branch_and_tree('tree')
 
403
        self.build_tree(['tree/dir/'])
 
404
        self.build_tree_contents([('tree/dir/file', 'contents\n')])
 
405
        tree.add(['dir', 'dir/file'], ['dir-id', 'file-id'])
 
406
        tree.commit('one', rev_id='rev-1')
 
407
 
 
408
        tree.rename_one('dir', 'newdir')
 
409
        diff = self.get_diff(tree.basis_tree(), tree)
 
410
        # Renaming a directory should be a single "you renamed this dir" even
 
411
        # when there are files inside.
 
412
        self.assertEqual("=== renamed directory 'dir' => 'newdir'\n", diff)
 
413
 
 
414
    def test_renamed_file(self):
 
415
        """Test when a file is only renamed."""
 
416
        tree = self.make_branch_and_tree('tree')
 
417
        self.build_tree_contents([('tree/file', 'contents\n')])
 
418
        tree.add(['file'], ['file-id'])
 
419
        tree.commit('one', rev_id='rev-1')
 
420
 
 
421
        tree.rename_one('file', 'newname')
 
422
        diff = self.get_diff(tree.basis_tree(), tree)
 
423
        self.assertContainsRe(diff, "=== renamed file 'file' => 'newname'\n")
 
424
        # We shouldn't have a --- or +++ line, because there is no content
 
425
        # change
 
426
        self.assertNotContainsRe(diff, '---')
 
427
 
 
428
    def test_renamed_and_modified_file(self):
 
429
        """Test when a file is only renamed."""
 
430
        tree = self.make_branch_and_tree('tree')
 
431
        self.build_tree_contents([('tree/file', 'contents\n')])
 
432
        tree.add(['file'], ['file-id'])
 
433
        tree.commit('one', rev_id='rev-1')
 
434
 
 
435
        tree.rename_one('file', 'newname')
 
436
        self.build_tree_contents([('tree/newname', 'new contents\n')])
 
437
        diff = self.get_diff(tree.basis_tree(), tree)
 
438
        self.assertContainsRe(diff, "=== renamed file 'file' => 'newname'\n")
 
439
        self.assertContainsRe(diff, '--- old/file\t')
 
440
        self.assertContainsRe(diff, '\\+\\+\\+ new/newname\t')
 
441
        self.assertContainsRe(diff, '-contents\n'
 
442
                                    '\\+new contents\n')
 
443
 
 
444
 
360
445
class TestPatienceDiffLib(TestCase):
361
446
 
362
447
    def test_unique_lcs(self):