~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_diff.py

  • Committer: Jonathan Lange
  • Date: 2012-10-23 10:22:25 UTC
  • mto: This revision was merged to the branch mainline in revision 6571.
  • Revision ID: jml@canonical.com-20121023102225-2rg3vuygc9npii7w
NEWS update

Show diffs side-by-side

added added

removed removed

Lines of Context:
35
35
from bzrlib.symbol_versioning import deprecated_in
36
36
from bzrlib.tests import features, EncodingAdapter
37
37
from bzrlib.tests.blackbox.test_diff import subst_dates
38
 
 
39
 
 
40
 
class _AttribFeature(tests.Feature):
41
 
 
42
 
    def _probe(self):
43
 
        if (sys.platform not in ('cygwin', 'win32')):
44
 
            return False
45
 
        try:
46
 
            proc = subprocess.Popen(['attrib', '.'], stdout=subprocess.PIPE)
47
 
        except OSError, e:
48
 
            return False
49
 
        return (0 == proc.wait())
50
 
 
51
 
    def feature_name(self):
52
 
        return 'attrib Windows command-line tool'
53
 
 
54
 
AttribFeature = _AttribFeature()
55
 
 
56
 
 
57
 
compiled_patiencediff_feature = tests.ModuleAvailableFeature(
58
 
                                    'bzrlib._patiencediff_c')
 
38
from bzrlib.tests import (
 
39
    features,
 
40
    )
59
41
 
60
42
 
61
43
def udiff_lines(old, new, allow_binary=False):
229
211
        self.assertIsInstance(output.getvalue(), str,
230
212
            'internal_diff should return bytestrings')
231
213
 
 
214
    def test_internal_diff_default_context(self):
 
215
        output = StringIO()
 
216
        diff.internal_diff('old', ['same_text\n','same_text\n','same_text\n',
 
217
                           'same_text\n','same_text\n','old_text\n'],
 
218
                           'new', ['same_text\n','same_text\n','same_text\n',
 
219
                           'same_text\n','same_text\n','new_text\n'], output)
 
220
        lines = output.getvalue().splitlines(True)
 
221
        self.check_patch(lines)
 
222
        self.assertEquals(['--- old\n',
 
223
                           '+++ new\n',
 
224
                           '@@ -3,4 +3,4 @@\n',
 
225
                           ' same_text\n',
 
226
                           ' same_text\n',
 
227
                           ' same_text\n',
 
228
                           '-old_text\n',
 
229
                           '+new_text\n',
 
230
                           '\n',
 
231
                          ]
 
232
                          , lines)
 
233
 
 
234
    def test_internal_diff_no_context(self):
 
235
        output = StringIO()
 
236
        diff.internal_diff('old', ['same_text\n','same_text\n','same_text\n',
 
237
                           'same_text\n','same_text\n','old_text\n'],
 
238
                           'new', ['same_text\n','same_text\n','same_text\n',
 
239
                           'same_text\n','same_text\n','new_text\n'], output,
 
240
                           context_lines=0)
 
241
        lines = output.getvalue().splitlines(True)
 
242
        self.check_patch(lines)
 
243
        self.assertEquals(['--- old\n',
 
244
                           '+++ new\n',
 
245
                           '@@ -6,1 +6,1 @@\n',
 
246
                           '-old_text\n',
 
247
                           '+new_text\n',
 
248
                           '\n',
 
249
                          ]
 
250
                          , lines)
 
251
 
 
252
    def test_internal_diff_more_context(self):
 
253
        output = StringIO()
 
254
        diff.internal_diff('old', ['same_text\n','same_text\n','same_text\n',
 
255
                           'same_text\n','same_text\n','old_text\n'],
 
256
                           'new', ['same_text\n','same_text\n','same_text\n',
 
257
                           'same_text\n','same_text\n','new_text\n'], output,
 
258
                           context_lines=4)
 
259
        lines = output.getvalue().splitlines(True)
 
260
        self.check_patch(lines)
 
261
        self.assertEquals(['--- old\n',
 
262
                           '+++ new\n',
 
263
                           '@@ -2,5 +2,5 @@\n',
 
264
                           ' same_text\n',
 
265
                           ' same_text\n',
 
266
                           ' same_text\n',
 
267
                           ' same_text\n',
 
268
                           '-old_text\n',
 
269
                           '+new_text\n',
 
270
                           '\n',
 
271
                          ]
 
272
                          , lines)
 
273
 
 
274
 
 
275
 
 
276
 
232
277
 
233
278
class TestDiffFiles(tests.TestCaseInTempDir):
234
279
 
238
283
        lines = external_udiff_lines(['\x00foobar\n'], ['foo\x00bar\n'])
239
284
 
240
285
        cmd = ['diff', '-u', '--binary', 'old', 'new']
241
 
        open('old', 'wb').write('\x00foobar\n')
242
 
        open('new', 'wb').write('foo\x00bar\n')
 
286
        with open('old', 'wb') as f: f.write('\x00foobar\n')
 
287
        with open('new', 'wb') as f: f.write('foo\x00bar\n')
243
288
        pipe = subprocess.Popen(cmd, stdout=subprocess.PIPE,
244
289
                                     stdin=subprocess.PIPE)
245
290
        out, err = pipe.communicate()
514
559
        is a binary file in the diff.
515
560
        """
516
561
        # See https://bugs.launchpad.net/bugs/110092.
517
 
        self.requireFeature(tests.UnicodeFilenameFeature)
 
562
        self.requireFeature(features.UnicodeFilenameFeature)
518
563
 
519
564
        # This bug isn't triggered with cStringIO.
520
565
        from StringIO import StringIO
539
584
 
540
585
    def test_unicode_filename(self):
541
586
        """Test when the filename are unicode."""
542
 
        self.requireFeature(tests.UnicodeFilenameFeature)
 
587
        self.requireFeature(features.UnicodeFilenameFeature)
543
588
 
544
589
        alpha, omega = u'\u03b1', u'\u03c9'
545
590
        autf8, outf8 = alpha.encode('utf8'), omega.encode('utf8')
571
616
        """Test for bug #382699: unicode filenames on Windows should be shown
572
617
        in user encoding.
573
618
        """
574
 
        self.requireFeature(tests.UnicodeFilenameFeature)
 
619
        self.requireFeature(features.UnicodeFilenameFeature)
575
620
        # The word 'test' in Russian
576
621
        _russian_test = u'\u0422\u0435\u0441\u0442'
577
622
        directory = _russian_test + u'/'
708
753
             ' \@\@\n-old\n\+new\n\n')
709
754
 
710
755
    def test_diff_kind_change(self):
711
 
        self.requireFeature(tests.SymlinkFeature)
 
756
        self.requireFeature(features.SymlinkFeature)
712
757
        self.build_tree_contents([('old-tree/olddir/',),
713
758
                                  ('old-tree/olddir/oldfile', 'old\n')])
714
759
        self.old_tree.add('olddir')
1172
1217
 
1173
1218
class TestPatienceDiffLib_c(TestPatienceDiffLib):
1174
1219
 
1175
 
    _test_needs_features = [compiled_patiencediff_feature]
 
1220
    _test_needs_features = [features.compiled_patiencediff_feature]
1176
1221
 
1177
1222
    def setUp(self):
1178
1223
        super(TestPatienceDiffLib_c, self).setUp()
1209
1254
                 'how are you today?\n']
1210
1255
        txt_b = ['hello there\n',
1211
1256
                 'how are you today?\n']
1212
 
        open('a1', 'wb').writelines(txt_a)
1213
 
        open('b1', 'wb').writelines(txt_b)
 
1257
        with open('a1', 'wb') as f: f.writelines(txt_a)
 
1258
        with open('b1', 'wb') as f: f.writelines(txt_b)
1214
1259
 
1215
1260
        unified_diff_files = patiencediff.unified_diff_files
1216
1261
        psm = self._PatienceSequenceMatcher
1226
1271
 
1227
1272
        txt_a = map(lambda x: x+'\n', 'abcdefghijklmnop')
1228
1273
        txt_b = map(lambda x: x+'\n', 'abcdefxydefghijklmnop')
1229
 
        open('a2', 'wb').writelines(txt_a)
1230
 
        open('b2', 'wb').writelines(txt_b)
 
1274
        with open('a2', 'wb') as f: f.writelines(txt_a)
 
1275
        with open('b2', 'wb') as f: f.writelines(txt_b)
1231
1276
 
1232
1277
        # This is the result with LongestCommonSubstring matching
1233
1278
        self.assertEquals(['--- a2\n',
1268
1313
 
1269
1314
class TestPatienceDiffLibFiles_c(TestPatienceDiffLibFiles):
1270
1315
 
1271
 
    _test_needs_features = [compiled_patiencediff_feature]
 
1316
    _test_needs_features = [features.compiled_patiencediff_feature]
1272
1317
 
1273
1318
    def setUp(self):
1274
1319
        super(TestPatienceDiffLibFiles_c, self).setUp()
1280
1325
class TestUsingCompiledIfAvailable(tests.TestCase):
1281
1326
 
1282
1327
    def test_PatienceSequenceMatcher(self):
1283
 
        if compiled_patiencediff_feature.available():
 
1328
        if features.compiled_patiencediff_feature.available():
1284
1329
            from bzrlib._patiencediff_c import PatienceSequenceMatcher_c
1285
1330
            self.assertIs(PatienceSequenceMatcher_c,
1286
1331
                          patiencediff.PatienceSequenceMatcher)
1290
1335
                          patiencediff.PatienceSequenceMatcher)
1291
1336
 
1292
1337
    def test_unique_lcs(self):
1293
 
        if compiled_patiencediff_feature.available():
 
1338
        if features.compiled_patiencediff_feature.available():
1294
1339
            from bzrlib._patiencediff_c import unique_lcs_c
1295
1340
            self.assertIs(unique_lcs_c,
1296
1341
                          patiencediff.unique_lcs)
1300
1345
                          patiencediff.unique_lcs)
1301
1346
 
1302
1347
    def test_recurse_matches(self):
1303
 
        if compiled_patiencediff_feature.available():
 
1348
        if features.compiled_patiencediff_feature.available():
1304
1349
            from bzrlib._patiencediff_c import recurse_matches_c
1305
1350
            self.assertIs(recurse_matches_c,
1306
1351
                          patiencediff.recurse_matches)
1356
1401
                         ' on this machine', str(e))
1357
1402
 
1358
1403
    def test_prepare_files_creates_paths_readable_by_windows_tool(self):
1359
 
        self.requireFeature(AttribFeature)
 
1404
        self.requireFeature(features.AttribFeature)
1360
1405
        output = StringIO()
1361
1406
        tree = self.make_branch_and_tree('tree')
1362
1407
        self.build_tree_contents([('tree/file', 'content')])
1470
1515
class TestGetTreesAndBranchesToDiffLocked(tests.TestCaseWithTransport):
1471
1516
 
1472
1517
    def call_gtabtd(self, path_list, revision_specs, old_url, new_url):
1473
 
        """Call get_trees_and_branches_to_diff_locked.  Overridden by
1474
 
        TestGetTreesAndBranchesToDiff.
1475
 
        """
 
1518
        """Call get_trees_and_branches_to_diff_locked."""
1476
1519
        return diff.get_trees_and_branches_to_diff_locked(
1477
1520
            path_list, revision_specs, old_url, new_url, self.addCleanup)
1478
1521
 
1515
1558
        self.assertEqual(tree.branch.base, new_branch.base)
1516
1559
        self.assertIs(None, specific_files)
1517
1560
        self.assertEqual(tree.basedir, extra_trees[0].basedir)
1518
 
 
1519
 
 
1520
 
class TestGetTreesAndBranchesToDiff(TestGetTreesAndBranchesToDiffLocked):
1521
 
    """Apply the tests for get_trees_and_branches_to_diff_locked to the
1522
 
    deprecated get_trees_and_branches_to_diff function.
1523
 
    """
1524
 
 
1525
 
    def call_gtabtd(self, path_list, revision_specs, old_url, new_url):
1526
 
        return self.applyDeprecated(
1527
 
            deprecated_in((2, 2, 0)), diff.get_trees_and_branches_to_diff,
1528
 
            path_list, revision_specs, old_url, new_url)