~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_diff.py

  • Committer: John Arbash Meinel
  • Date: 2007-04-12 20:36:40 UTC
  • mfrom: (2413 +trunk)
  • mto: This revision was merged to the branch mainline in revision 2566.
  • Revision ID: john@arbash-meinel.com-20070412203640-z1jld315288moxvy
[merge] bzr.dev 2413

Show diffs side-by-side

added added

removed removed

Lines of Context:
214
214
        self.assertEqual(out.splitlines(True) + ['\n'], lines)
215
215
 
216
216
 
217
 
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):
218
233
 
219
234
    def setUp(self):
220
235
        super(TestDiffDates, self).setUp()
254
269
        # set the date stamps for files in the working tree to known values
255
270
        os.utime('file1', (1144195200, 1144195200)) # 2006-04-05 00:00:00 UTC
256
271
 
257
 
    def get_diff(self, tree1, tree2, specific_files=None, working_tree=None):
258
 
        output = StringIO()
259
 
        if working_tree is not None:
260
 
            extra_trees = (working_tree,)
261
 
        else:
262
 
            extra_trees = ()
263
 
        show_diff_trees(tree1, tree2, output, specific_files=specific_files,
264
 
                        extra_trees=extra_trees, old_label='old/', 
265
 
                        new_label='new/')
266
 
        return output.getvalue()
267
 
 
268
272
    def test_diff_rev_tree_working_tree(self):
269
273
        output = self.get_diff(self.wt.basis_tree(), self.wt)
270
274
        # note that the date for old/file1 is from rev 2 rather than from
354
358
        self.assertNotContainsRe(out, 'file1\t')
355
359
 
356
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
 
357
445
class TestPatienceDiffLib(TestCase):
358
446
 
359
447
    def test_unique_lcs(self):