~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: 2006-10-10 07:23:07 UTC
  • mfrom: (2067.1.1 urandom-56883)
  • Revision ID: pqm@pqm.ubuntu.com-20061010072307-037a6f63da8a1bdd
(John Arbash Meinel) Handle exceptions while opening /dev/urandom

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006 Canonical Ltd
 
1
# Copyright (C) 2005, 2006 Canonical Development 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
9
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
11
# GNU General Public License for more details.
12
 
#
 
12
 
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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
26
25
import bzrlib.patiencediff
27
26
from bzrlib.tests import (TestCase, TestCaseWithTransport,
28
27
                          TestCaseInTempDir, TestSkipped)
112
111
        self.check_patch(lines)
113
112
 
114
113
    def test_external_diff_binary_lang_c(self):
115
 
        old_env = {}
116
 
        for lang in ('LANG', 'LC_ALL', 'LANGUAGE'):
117
 
            old_env[lang] = osutils.set_or_unset_env(lang, 'C')
 
114
        orig_lang = os.environ.get('LANG')
118
115
        try:
 
116
            os.environ['LANG'] = 'C'
119
117
            lines = external_udiff_lines(['\x00foobar\n'], ['foo\x00bar\n'])
120
118
            # Older versions of diffutils say "Binary files", newer
121
119
            # versions just say "Files".
123
121
                                  '(Binary f|F)iles old and new differ\n')
124
122
            self.assertEquals(lines[1:], ['\n'])
125
123
        finally:
126
 
            for lang, old_val in old_env.iteritems():
127
 
                osutils.set_or_unset_env(lang, old_val)
 
124
            if orig_lang is None:
 
125
                del os.environ['LANG']
 
126
            else:
 
127
                os.environ['LANG'] = orig_lang
128
128
 
129
129
    def test_no_external_diff(self):
130
130
        """Check that NoDiff is raised when diff is not available"""
202
202
        # Make sure external_diff doesn't fail in the current LANG
203
203
        lines = external_udiff_lines(['\x00foobar\n'], ['foo\x00bar\n'])
204
204
 
205
 
        cmd = ['diff', '-u', '--binary', 'old', 'new']
 
205
        cmd = ['diff', '-u', 'old', 'new']
206
206
        open('old', 'wb').write('\x00foobar\n')
207
207
        open('new', 'wb').write('foo\x00bar\n')
208
208
        pipe = subprocess.Popen(cmd, stdout=subprocess.PIPE,
214
214
        self.assertEqual(out.splitlines(True) + ['\n'], lines)
215
215
 
216
216
 
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):
 
217
class TestDiffDates(TestCaseWithTransport):
233
218
 
234
219
    def setUp(self):
235
220
        super(TestDiffDates, self).setUp()
269
254
        # set the date stamps for files in the working tree to known values
270
255
        os.utime('file1', (1144195200, 1144195200)) # 2006-04-05 00:00:00 UTC
271
256
 
 
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
 
272
268
    def test_diff_rev_tree_working_tree(self):
273
269
        output = self.get_diff(self.wt.basis_tree(), self.wt)
274
270
        # note that the date for old/file1 is from rev 2 rather than from
358
354
        self.assertNotContainsRe(out, 'file1\t')
359
355
 
360
356
 
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
 
 
445
357
class TestPatienceDiffLib(TestCase):
446
358
 
447
359
    def test_unique_lcs(self):