~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: 2008-06-05 04:05:05 UTC
  • mfrom: (3473.1.1 ianc-integration)
  • Revision ID: pqm@pqm.ubuntu.com-20080605040505-i9kqxg2fps2qjdi0
Add the 'alias' command (Tim Penhey)

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
from cStringIO import StringIO
20
20
import errno
21
21
import subprocess
22
 
import sys
23
22
from tempfile import TemporaryFile
24
23
 
25
24
from bzrlib import tests
35
34
    )
36
35
from bzrlib.errors import BinaryFile, NoDiff, ExecutableMissing
37
36
import bzrlib.osutils as osutils
38
 
import bzrlib.revision as _mod_revision
39
37
import bzrlib.transform as transform
40
38
import bzrlib.patiencediff
41
39
import bzrlib._patiencediff_py
43
41
                          TestCaseInTempDir, TestSkipped)
44
42
 
45
43
 
46
 
class _AttribFeature(Feature):
47
 
 
48
 
    def _probe(self):
49
 
        if (sys.platform not in ('cygwin', 'win32')):
50
 
            return False
51
 
        try:
52
 
            proc = subprocess.Popen(['attrib', '.'], stdout=subprocess.PIPE)
53
 
        except OSError, e:
54
 
            return False
55
 
        return (0 == proc.wait())
56
 
 
57
 
    def feature_name(self):
58
 
        return 'attrib Windows command-line tool'
59
 
 
60
 
AttribFeature = _AttribFeature()
61
 
 
62
 
 
63
44
class _CompiledPatienceDiffFeature(Feature):
64
45
 
65
46
    def _probe(self):
75
56
CompiledPatienceDiffFeature = _CompiledPatienceDiffFeature()
76
57
 
77
58
 
 
59
class _UnicodeFilename(Feature):
 
60
    """Does the filesystem support Unicode filenames?"""
 
61
 
 
62
    def _probe(self):
 
63
        try:
 
64
            os.stat(u'\u03b1')
 
65
        except UnicodeEncodeError:
 
66
            return False
 
67
        except (IOError, OSError):
 
68
            # The filesystem allows the Unicode filename but the file doesn't
 
69
            # exist.
 
70
            return True
 
71
        else:
 
72
            # The filesystem allows the Unicode filename and the file exists,
 
73
            # for some reason.
 
74
            return True
 
75
 
 
76
UnicodeFilename = _UnicodeFilename()
 
77
 
 
78
 
 
79
class TestUnicodeFilename(TestCase):
 
80
 
 
81
    def test_probe_passes(self):
 
82
        """UnicodeFilename._probe passes."""
 
83
        # We can't test much more than that because the behaviour depends
 
84
        # on the platform.
 
85
        UnicodeFilename._probe()
 
86
        
 
87
 
78
88
def udiff_lines(old, new, allow_binary=False):
79
89
    output = StringIO()
80
90
    internal_diff('old', old, 'new', new, output, allow_binary)
357
367
''')
358
368
        
359
369
    def test_diff_add_files(self):
360
 
        tree1 = self.b.repository.revision_tree(_mod_revision.NULL_REVISION)
 
370
        tree1 = self.b.repository.revision_tree(None)
361
371
        tree2 = self.b.repository.revision_tree('rev-1')
362
372
        output = self.get_diff(tree1, tree2)
363
373
        # the files have the epoch time stamp for the tree in which
538
548
        is a binary file in the diff.
539
549
        """
540
550
        # See https://bugs.launchpad.net/bugs/110092.
541
 
        self.requireFeature(tests.UnicodeFilenameFeature)
 
551
        self.requireFeature(UnicodeFilename)
542
552
 
543
553
        # This bug isn't triggered with cStringIO.
544
554
        from StringIO import StringIO
563
573
 
564
574
    def test_unicode_filename(self):
565
575
        """Test when the filename are unicode."""
566
 
        self.requireFeature(tests.UnicodeFilenameFeature)
 
576
        self.requireFeature(UnicodeFilename)
567
577
 
568
578
        alpha, omega = u'\u03b1', u'\u03c9'
569
579
        autf8, outf8 = alpha.encode('utf8'), omega.encode('utf8')
770
780
        self._PatienceSequenceMatcher = \
771
781
            bzrlib._patiencediff_py.PatienceSequenceMatcher_py
772
782
 
773
 
    def test_diff_unicode_string(self):
774
 
        a = ''.join([unichr(i) for i in range(4000, 4500, 3)])
775
 
        b = ''.join([unichr(i) for i in range(4300, 4800, 2)])
776
 
        sm = self._PatienceSequenceMatcher(None, a, b)
777
 
        mb = sm.get_matching_blocks()
778
 
        self.assertEquals(35, len(mb))
779
 
 
780
783
    def test_unique_lcs(self):
781
784
        unique_lcs = self._unique_lcs
782
785
        self.assertEquals(unique_lcs('', ''), [])
1304
1307
        self.assertEqual('a-tool-which-is-unlikely-to-exist could not be found'
1305
1308
                         ' on this machine', str(e))
1306
1309
 
1307
 
    def test_prepare_files_creates_paths_readable_by_windows_tool(self):
1308
 
        self.requireFeature(AttribFeature)
1309
 
        output = StringIO()
1310
 
        tree = self.make_branch_and_tree('tree')
1311
 
        self.build_tree_contents([('tree/file', 'content')])
1312
 
        tree.add('file', 'file-id')
1313
 
        tree.commit('old tree')
1314
 
        tree.lock_read()
1315
 
        self.addCleanup(tree.unlock)
1316
 
        diff_obj = DiffFromTool(['python', '-c',
1317
 
                                 'print "%(old_path)s %(new_path)s"'],
1318
 
                                tree, tree, output)
1319
 
        diff_obj._prepare_files('file-id', 'file', 'file')
1320
 
        self.assertReadableByAttrib(diff_obj._root, 'old\\file', r'old\\file')
1321
 
        self.assertReadableByAttrib(diff_obj._root, 'new\\file', r'new\\file')
1322
 
 
1323
 
    def assertReadableByAttrib(self, cwd, relpath, regex):
1324
 
        proc = subprocess.Popen(['attrib', relpath],
1325
 
                                stdout=subprocess.PIPE,
1326
 
                                cwd=cwd)
1327
 
        proc.wait()
1328
 
        result = proc.stdout.read()
1329
 
        self.assertContainsRe(result, regex)
1330
 
 
1331
1310
    def test_prepare_files(self):
1332
1311
        output = StringIO()
1333
1312
        tree = self.make_branch_and_tree('tree')
1334
1313
        self.build_tree_contents([('tree/oldname', 'oldcontent')])
1335
 
        self.build_tree_contents([('tree/oldname2', 'oldcontent2')])
1336
1314
        tree.add('oldname', 'file-id')
1337
 
        tree.add('oldname2', 'file2-id')
1338
1315
        tree.commit('old tree', timestamp=0)
1339
1316
        tree.rename_one('oldname', 'newname')
1340
 
        tree.rename_one('oldname2', 'newname2')
1341
1317
        self.build_tree_contents([('tree/newname', 'newcontent')])
1342
 
        self.build_tree_contents([('tree/newname2', 'newcontent2')])
1343
1318
        old_tree = tree.basis_tree()
1344
1319
        old_tree.lock_read()
1345
1320
        self.addCleanup(old_tree.unlock)
1357
1332
        self.assertContainsRe(new_path, 'new/newname$')
1358
1333
        self.assertFileEqual('oldcontent', old_path)
1359
1334
        self.assertFileEqual('newcontent', new_path)
1360
 
        if osutils.host_os_dereferences_symlinks():
 
1335
        if osutils.has_symlinks():
1361
1336
            self.assertTrue(os.path.samefile('tree/newname', new_path))
1362
1337
        # make sure we can create files with the same parent directories
1363
 
        diff_obj._prepare_files('file2-id', 'oldname2', 'newname2')
 
1338
        diff_obj._prepare_files('file-id', 'oldname2', 'newname2')