~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_diff.py

  • Committer: Martin Pool
  • Date: 2008-10-20 23:58:12 UTC
  • mto: This revision was merged to the branch mainline in revision 3787.
  • Revision ID: mbp@sourcefrog.net-20081020235812-itg90mk0u4dez92z
lp-upload-release now handles names like bzr-1.8.tar.gz

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
17
17
import os
18
18
import os.path
32
32
    external_diff,
33
33
    internal_diff,
34
34
    show_diff_trees,
35
 
    get_trees_and_branches_to_diff,
36
35
    )
37
36
from bzrlib.errors import BinaryFile, NoDiff, ExecutableMissing
38
37
import bzrlib.osutils as osutils
42
41
import bzrlib._patiencediff_py
43
42
from bzrlib.tests import (Feature, TestCase, TestCaseWithTransport,
44
43
                          TestCaseInTempDir, TestSkipped)
45
 
from bzrlib.revisiontree import RevisionTree
46
 
from bzrlib.revisionspec import RevisionSpec
47
44
 
48
45
 
49
46
class _AttribFeature(Feature):
63
60
AttribFeature = _AttribFeature()
64
61
 
65
62
 
66
 
compiled_patiencediff_feature = tests.ModuleAvailableFeature(
67
 
                                    'bzrlib._patiencediff_c')
 
63
class _CompiledPatienceDiffFeature(Feature):
 
64
 
 
65
    def _probe(self):
 
66
        try:
 
67
            import bzrlib._patiencediff_c
 
68
        except ImportError:
 
69
            return False
 
70
        return True
 
71
 
 
72
    def feature_name(self):
 
73
        return 'bzrlib._patiencediff_c'
 
74
 
 
75
CompiledPatienceDiffFeature = _CompiledPatienceDiffFeature()
68
76
 
69
77
 
70
78
def udiff_lines(old, new, allow_binary=False):
176
184
                              StringIO(), diff_opts=['-u'])
177
185
        finally:
178
186
            os.environ['PATH'] = orig_path
179
 
 
 
187
        
180
188
    def test_internal_diff_default(self):
181
189
        # Default internal diff encoding is utf8
182
190
        output = StringIO()
347
355
+file2 contents at rev 3
348
356
 
349
357
''')
350
 
 
 
358
        
351
359
    def test_diff_add_files(self):
352
360
        tree1 = self.b.repository.revision_tree(_mod_revision.NULL_REVISION)
353
361
        tree2 = self.b.repository.revision_tree('rev-1')
389
397
        self.wt.rename_one('file1', 'file1b')
390
398
        old_tree = self.b.repository.revision_tree('rev-1')
391
399
        new_tree = self.b.repository.revision_tree('rev-4')
392
 
        out = self.get_diff(old_tree, new_tree, specific_files=['file1b'],
 
400
        out = self.get_diff(old_tree, new_tree, specific_files=['file1b'], 
393
401
                            working_tree=self.wt)
394
402
        self.assertContainsRe(out, 'file1\t')
395
403
 
401
409
        self.wt.rename_one('file1', 'dir1/file1')
402
410
        old_tree = self.b.repository.revision_tree('rev-1')
403
411
        new_tree = self.b.repository.revision_tree('rev-4')
404
 
        out = self.get_diff(old_tree, new_tree, specific_files=['dir1'],
 
412
        out = self.get_diff(old_tree, new_tree, specific_files=['dir1'], 
405
413
                            working_tree=self.wt)
406
414
        self.assertContainsRe(out, 'file1\t')
407
 
        out = self.get_diff(old_tree, new_tree, specific_files=['dir2'],
 
415
        out = self.get_diff(old_tree, new_tree, specific_files=['dir2'], 
408
416
                            working_tree=self.wt)
409
417
        self.assertNotContainsRe(out, 'file1\t')
410
418
 
696
704
            r'--- olddir/oldfile.*\n\+\+\+ newdir/newfile.*\n\@\@ -1,1 \+0,0'
697
705
             ' \@\@\n-old\n\n')
698
706
        self.assertContainsRe(self.differ.to_file.getvalue(),
699
 
                              "=== target is u'new'\n")
 
707
                              "=== target is 'new'\n")
700
708
 
701
709
    def test_diff_directory(self):
702
710
        self.build_tree(['new-tree/new-dir/'])
779
787
        self.assertEquals(unique_lcs('ab', 'ab'), [(0,0), (1,1)])
780
788
        self.assertEquals(unique_lcs('abcde', 'cdeab'), [(2,0), (3,1), (4,2)])
781
789
        self.assertEquals(unique_lcs('cdeab', 'abcde'), [(0,2), (1,3), (2,4)])
782
 
        self.assertEquals(unique_lcs('abXde', 'abYde'), [(0,0), (1,1),
 
790
        self.assertEquals(unique_lcs('abXde', 'abYde'), [(0,0), (1,1), 
783
791
                                                         (3,3), (4,4)])
784
792
        self.assertEquals(unique_lcs('acbac', 'abc'), [(2,1)])
785
793
 
800
808
        test_one('abcdbce', 'afbcgdbce', [(0,0), (1, 2), (2, 3), (3, 5),
801
809
                                          (4, 6), (5, 7), (6, 8)])
802
810
 
803
 
        # recurse_matches doesn't match non-unique
 
811
        # recurse_matches doesn't match non-unique 
804
812
        # lines surrounded by bogus text.
805
813
        # The update has been done in patiencediff.SequenceMatcher instead
806
814
 
943
951
                 ('delete', 1,2, 1,1),
944
952
                 ('equal',  2,3, 1,2),
945
953
                ])
946
 
        chk_ops('aBccDe', 'abccde',
 
954
        chk_ops('aBccDe', 'abccde', 
947
955
                [('equal',   0,1, 0,1),
948
956
                 ('replace', 1,5, 1,5),
949
957
                 ('equal',   5,6, 5,6),
950
958
                ])
951
 
        chk_ops('aBcDec', 'abcdec',
 
959
        chk_ops('aBcDec', 'abcdec', 
952
960
                [('equal',   0,1, 0,1),
953
961
                 ('replace', 1,2, 1,2),
954
962
                 ('equal',   2,3, 2,3),
955
963
                 ('replace', 3,4, 3,4),
956
964
                 ('equal',   4,6, 4,6),
957
965
                ])
958
 
        chk_ops('aBcdEcdFg', 'abcdecdfg',
 
966
        chk_ops('aBcdEcdFg', 'abcdecdfg', 
959
967
                [('equal',   0,1, 0,1),
960
968
                 ('replace', 1,8, 1,8),
961
969
                 ('equal',   8,9, 8,9)
962
970
                ])
963
 
        chk_ops('aBcdEeXcdFg', 'abcdecdfg',
 
971
        chk_ops('aBcdEeXcdFg', 'abcdecdfg', 
964
972
                [('equal',   0,1, 0,1),
965
973
                 ('replace', 1,2, 1,2),
966
974
                 ('equal',   2,4, 2,4),
1026
1034
    """
1027
1035
    gnxrf_netf = ['svyr*']
1028
1036
    gnxrf_bcgvbaf = ['ab-erphefr']
1029
 
 
 
1037
  
1030
1038
    qrs eha(frys, svyr_yvfg, ab_erphefr=Snyfr):
1031
1039
        sebz omeyvo.nqq vzcbeg fzneg_nqq, nqq_ercbegre_cevag, nqq_ercbegre_ahyy
1032
1040
        vs vf_dhvrg():
1040
1048
'''.splitlines(True), '''\
1041
1049
    trg nqqrq jura lbh nqq n svyr va gur qverpgbel.
1042
1050
 
1043
 
    --qel-eha jvyy fubj juvpu svyrf jbhyq or nqqrq, ohg abg npghnyyl
 
1051
    --qel-eha jvyy fubj juvpu svyrf jbhyq or nqqrq, ohg abg npghnyyl 
1044
1052
    nqq gurz.
1045
1053
    """
1046
1054
    gnxrf_netf = ['svyr*']
1075
1083
                 'how are you today?\n']
1076
1084
        unified_diff = bzrlib.patiencediff.unified_diff
1077
1085
        psm = self._PatienceSequenceMatcher
1078
 
        self.assertEquals(['--- \n',
1079
 
                           '+++ \n',
 
1086
        self.assertEquals([ '---  \n',
 
1087
                           '+++  \n',
1080
1088
                           '@@ -1,3 +1,2 @@\n',
1081
1089
                           ' hello there\n',
1082
1090
                           '-world\n',
1087
1095
        txt_a = map(lambda x: x+'\n', 'abcdefghijklmnop')
1088
1096
        txt_b = map(lambda x: x+'\n', 'abcdefxydefghijklmnop')
1089
1097
        # This is the result with LongestCommonSubstring matching
1090
 
        self.assertEquals(['--- \n',
1091
 
                           '+++ \n',
 
1098
        self.assertEquals(['---  \n',
 
1099
                           '+++  \n',
1092
1100
                           '@@ -1,6 +1,11 @@\n',
1093
1101
                           ' a\n',
1094
1102
                           ' b\n',
1103
1111
                           ' f\n']
1104
1112
                          , list(unified_diff(txt_a, txt_b)))
1105
1113
        # And the patience diff
1106
 
        self.assertEquals(['--- \n',
1107
 
                           '+++ \n',
 
1114
        self.assertEquals(['---  \n',
 
1115
                           '+++  \n',
1108
1116
                           '@@ -4,6 +4,11 @@\n',
1109
1117
                           ' d\n',
1110
1118
                           ' e\n',
1121
1129
                          , list(unified_diff(txt_a, txt_b,
1122
1130
                                 sequencematcher=psm)))
1123
1131
 
1124
 
    def test_patience_unified_diff_with_dates(self):
1125
 
        txt_a = ['hello there\n',
1126
 
                 'world\n',
1127
 
                 'how are you today?\n']
1128
 
        txt_b = ['hello there\n',
1129
 
                 'how are you today?\n']
1130
 
        unified_diff = bzrlib.patiencediff.unified_diff
1131
 
        psm = self._PatienceSequenceMatcher
1132
 
        self.assertEquals(['--- a\t2008-08-08\n',
1133
 
                           '+++ b\t2008-09-09\n',
1134
 
                           '@@ -1,3 +1,2 @@\n',
1135
 
                           ' hello there\n',
1136
 
                           '-world\n',
1137
 
                           ' how are you today?\n'
1138
 
                          ]
1139
 
                          , list(unified_diff(txt_a, txt_b,
1140
 
                                 fromfile='a', tofile='b',
1141
 
                                 fromfiledate='2008-08-08',
1142
 
                                 tofiledate='2008-09-09',
1143
 
                                 sequencematcher=psm)))
1144
 
 
1145
1132
 
1146
1133
class TestPatienceDiffLib_c(TestPatienceDiffLib):
1147
1134
 
1148
 
    _test_needs_features = [compiled_patiencediff_feature]
 
1135
    _test_needs_features = [CompiledPatienceDiffFeature]
1149
1136
 
1150
1137
    def setUp(self):
1151
1138
        super(TestPatienceDiffLib_c, self).setUp()
1187
1174
 
1188
1175
        unified_diff_files = bzrlib.patiencediff.unified_diff_files
1189
1176
        psm = self._PatienceSequenceMatcher
1190
 
        self.assertEquals(['--- a1\n',
1191
 
                           '+++ b1\n',
 
1177
        self.assertEquals(['--- a1 \n',
 
1178
                           '+++ b1 \n',
1192
1179
                           '@@ -1,3 +1,2 @@\n',
1193
1180
                           ' hello there\n',
1194
1181
                           '-world\n',
1203
1190
        open('b2', 'wb').writelines(txt_b)
1204
1191
 
1205
1192
        # This is the result with LongestCommonSubstring matching
1206
 
        self.assertEquals(['--- a2\n',
1207
 
                           '+++ b2\n',
 
1193
        self.assertEquals(['--- a2 \n',
 
1194
                           '+++ b2 \n',
1208
1195
                           '@@ -1,6 +1,11 @@\n',
1209
1196
                           ' a\n',
1210
1197
                           ' b\n',
1220
1207
                          , list(unified_diff_files('a2', 'b2')))
1221
1208
 
1222
1209
        # And the patience diff
1223
 
        self.assertEquals(['--- a2\n',
1224
 
                           '+++ b2\n',
 
1210
        self.assertEquals(['--- a2 \n',
 
1211
                           '+++ b2 \n',
1225
1212
                           '@@ -4,6 +4,11 @@\n',
1226
1213
                           ' d\n',
1227
1214
                           ' e\n',
1241
1228
 
1242
1229
class TestPatienceDiffLibFiles_c(TestPatienceDiffLibFiles):
1243
1230
 
1244
 
    _test_needs_features = [compiled_patiencediff_feature]
 
1231
    _test_needs_features = [CompiledPatienceDiffFeature]
1245
1232
 
1246
1233
    def setUp(self):
1247
1234
        super(TestPatienceDiffLibFiles_c, self).setUp()
1253
1240
class TestUsingCompiledIfAvailable(TestCase):
1254
1241
 
1255
1242
    def test_PatienceSequenceMatcher(self):
1256
 
        if compiled_patiencediff_feature.available():
 
1243
        if CompiledPatienceDiffFeature.available():
1257
1244
            from bzrlib._patiencediff_c import PatienceSequenceMatcher_c
1258
1245
            self.assertIs(PatienceSequenceMatcher_c,
1259
1246
                          bzrlib.patiencediff.PatienceSequenceMatcher)
1263
1250
                          bzrlib.patiencediff.PatienceSequenceMatcher)
1264
1251
 
1265
1252
    def test_unique_lcs(self):
1266
 
        if compiled_patiencediff_feature.available():
 
1253
        if CompiledPatienceDiffFeature.available():
1267
1254
            from bzrlib._patiencediff_c import unique_lcs_c
1268
1255
            self.assertIs(unique_lcs_c,
1269
1256
                          bzrlib.patiencediff.unique_lcs)
1273
1260
                          bzrlib.patiencediff.unique_lcs)
1274
1261
 
1275
1262
    def test_recurse_matches(self):
1276
 
        if compiled_patiencediff_feature.available():
 
1263
        if CompiledPatienceDiffFeature.available():
1277
1264
            from bzrlib._patiencediff_c import recurse_matches_c
1278
1265
            self.assertIs(recurse_matches_c,
1279
1266
                          bzrlib.patiencediff.recurse_matches)
1288
1275
    def test_from_string(self):
1289
1276
        diff_obj = DiffFromTool.from_string('diff', None, None, None)
1290
1277
        self.addCleanup(diff_obj.finish)
1291
 
        self.assertEqual(['diff', '@old_path', '@new_path'],
 
1278
        self.assertEqual(['diff', '%(old_path)s', '%(new_path)s'],
1292
1279
            diff_obj.command_template)
1293
1280
 
1294
1281
    def test_from_string_u5(self):
1295
1282
        diff_obj = DiffFromTool.from_string('diff -u\\ 5', None, None, None)
1296
1283
        self.addCleanup(diff_obj.finish)
1297
 
        self.assertEqual(['diff', '-u 5', '@old_path', '@new_path'],
 
1284
        self.assertEqual(['diff', '-u 5', '%(old_path)s', '%(new_path)s'],
1298
1285
                         diff_obj.command_template)
1299
1286
        self.assertEqual(['diff', '-u 5', 'old-path', 'new-path'],
1300
1287
                         diff_obj._get_command('old-path', 'new-path'))
1302
1289
    def test_execute(self):
1303
1290
        output = StringIO()
1304
1291
        diff_obj = DiffFromTool(['python', '-c',
1305
 
                                 'print "@old_path @new_path"'],
 
1292
                                 'print "%(old_path)s %(new_path)s"'],
1306
1293
                                None, None, output)
1307
1294
        self.addCleanup(diff_obj.finish)
1308
1295
        diff_obj._execute('old', 'new')
1326
1313
        tree.commit('old tree')
1327
1314
        tree.lock_read()
1328
1315
        self.addCleanup(tree.unlock)
1329
 
        basis_tree = tree.basis_tree()
1330
 
        basis_tree.lock_read()
1331
 
        self.addCleanup(basis_tree.unlock)
1332
1316
        diff_obj = DiffFromTool(['python', '-c',
1333
 
                                 'print "@old_path @new_path"'],
1334
 
                                basis_tree, tree, output)
 
1317
                                 'print "%(old_path)s %(new_path)s"'],
 
1318
                                tree, tree, output)
1335
1319
        diff_obj._prepare_files('file-id', 'file', 'file')
1336
 
        # The old content should be readonly
1337
 
        self.assertReadableByAttrib(diff_obj._root, 'old\\file',
1338
 
                                    r'R.*old\\file$')
1339
 
        # The new content should use the tree object, not a 'new' file anymore
1340
 
        self.assertEndsWith(tree.basedir, 'work/tree')
1341
 
        self.assertReadableByAttrib(tree.basedir, 'file', r'work\\tree\\file$')
 
1320
        self.assertReadableByAttrib(diff_obj._root, 'old\\file', r'old\\file')
 
1321
        self.assertReadableByAttrib(diff_obj._root, 'new\\file', r'new\\file')
1342
1322
 
1343
1323
    def assertReadableByAttrib(self, cwd, relpath, regex):
1344
1324
        proc = subprocess.Popen(['attrib', relpath],
1345
1325
                                stdout=subprocess.PIPE,
1346
1326
                                cwd=cwd)
1347
 
        (result, err) = proc.communicate()
1348
 
        self.assertContainsRe(result.replace('\r\n', '\n'), regex)
 
1327
        proc.wait()
 
1328
        result = proc.stdout.read()
 
1329
        self.assertContainsRe(result, regex)
1349
1330
 
1350
1331
    def test_prepare_files(self):
1351
1332
        output = StringIO()
1365
1346
        tree.lock_read()
1366
1347
        self.addCleanup(tree.unlock)
1367
1348
        diff_obj = DiffFromTool(['python', '-c',
1368
 
                                 'print "@old_path @new_path"'],
 
1349
                                 'print "%(old_path)s %(new_path)s"'],
1369
1350
                                old_tree, tree, output)
1370
1351
        self.addCleanup(diff_obj.finish)
1371
1352
        self.assertContainsRe(diff_obj._root, 'bzr-diff-[^/]*')
1373
1354
                                                     'newname')
1374
1355
        self.assertContainsRe(old_path, 'old/oldname$')
1375
1356
        self.assertEqual(0, os.stat(old_path).st_mtime)
1376
 
        self.assertContainsRe(new_path, 'tree/newname$')
 
1357
        self.assertContainsRe(new_path, 'new/newname$')
1377
1358
        self.assertFileEqual('oldcontent', old_path)
1378
1359
        self.assertFileEqual('newcontent', new_path)
1379
1360
        if osutils.host_os_dereferences_symlinks():
1380
1361
            self.assertTrue(os.path.samefile('tree/newname', new_path))
1381
1362
        # make sure we can create files with the same parent directories
1382
1363
        diff_obj._prepare_files('file2-id', 'oldname2', 'newname2')
1383
 
 
1384
 
 
1385
 
class TestGetTreesAndBranchesToDiff(TestCaseWithTransport):
1386
 
 
1387
 
    def test_basic(self):
1388
 
        tree = self.make_branch_and_tree('tree')
1389
 
        (old_tree, new_tree,
1390
 
         old_branch, new_branch,
1391
 
         specific_files, extra_trees) = \
1392
 
            get_trees_and_branches_to_diff(['tree'], None, None, None)
1393
 
 
1394
 
        self.assertIsInstance(old_tree, RevisionTree)
1395
 
        #print dir (old_tree)
1396
 
        self.assertEqual(_mod_revision.NULL_REVISION, old_tree.get_revision_id())
1397
 
        self.assertEqual(tree.basedir, new_tree.basedir)
1398
 
        self.assertEqual(tree.branch.base, old_branch.base)
1399
 
        self.assertEqual(tree.branch.base, new_branch.base)
1400
 
        self.assertIs(None, specific_files)
1401
 
        self.assertIs(None, extra_trees)
1402
 
 
1403
 
    def test_with_rev_specs(self):
1404
 
        tree = self.make_branch_and_tree('tree')
1405
 
        self.build_tree_contents([('tree/file', 'oldcontent')])
1406
 
        tree.add('file', 'file-id')
1407
 
        tree.commit('old tree', timestamp=0, rev_id="old-id")
1408
 
        self.build_tree_contents([('tree/file', 'newcontent')])
1409
 
        tree.commit('new tree', timestamp=0, rev_id="new-id")
1410
 
 
1411
 
        revisions = [RevisionSpec.from_string('1'),
1412
 
                     RevisionSpec.from_string('2')]
1413
 
        (old_tree, new_tree,
1414
 
         old_branch, new_branch,
1415
 
         specific_files, extra_trees) = \
1416
 
            get_trees_and_branches_to_diff(['tree'], revisions, None, None)
1417
 
 
1418
 
        self.assertIsInstance(old_tree, RevisionTree)
1419
 
        self.assertEqual("old-id", old_tree.get_revision_id())
1420
 
        self.assertIsInstance(new_tree, RevisionTree)
1421
 
        self.assertEqual("new-id", new_tree.get_revision_id())
1422
 
        self.assertEqual(tree.branch.base, old_branch.base)
1423
 
        self.assertEqual(tree.branch.base, new_branch.base)
1424
 
        self.assertIs(None, specific_files)
1425
 
        self.assertEqual(tree.basedir, extra_trees[0].basedir)