61
97
udiff_lines([1023 * 'a' + '\x00'], [], allow_binary=True)
62
98
udiff_lines([], [1023 * 'a' + '\x00'], allow_binary=True)
65
class TestCDVDiffLib(TestCase):
100
def test_external_diff(self):
101
lines = external_udiff_lines(['boo\n'], ['goo\n'])
102
self.check_patch(lines)
103
self.assertEqual('\n', lines[-1])
105
def test_external_diff_no_fileno(self):
106
# Make sure that we can handle not having a fileno, even
107
# if the diff is large
108
lines = external_udiff_lines(['boo\n']*10000,
111
self.check_patch(lines)
113
def test_external_diff_binary_lang_c(self):
114
orig_lang = os.environ.get('LANG')
115
orig_lc_all = os.environ.get('LC_ALL')
117
os.environ['LANG'] = 'C'
118
os.environ['LC_ALL'] = 'C'
119
lines = external_udiff_lines(['\x00foobar\n'], ['foo\x00bar\n'])
120
# Older versions of diffutils say "Binary files", newer
121
# versions just say "Files".
122
self.assertContainsRe(lines[0],
123
'(Binary f|F)iles old and new differ\n')
124
self.assertEquals(lines[1:], ['\n'])
126
for name, value in [('LANG', orig_lang), ('LC_ALL', orig_lc_all)]:
130
os.environ[name] = value
132
def test_no_external_diff(self):
133
"""Check that NoDiff is raised when diff is not available"""
134
# Use os.environ['PATH'] to make sure no 'diff' command is available
135
orig_path = os.environ['PATH']
137
os.environ['PATH'] = ''
138
self.assertRaises(NoDiff, external_diff,
139
'old', ['boo\n'], 'new', ['goo\n'],
140
StringIO(), diff_opts=['-u'])
142
os.environ['PATH'] = orig_path
144
def test_internal_diff_default(self):
145
# Default internal diff encoding is utf8
147
internal_diff(u'old_\xb5', ['old_text\n'],
148
u'new_\xe5', ['new_text\n'], output)
149
lines = output.getvalue().splitlines(True)
150
self.check_patch(lines)
151
self.assertEquals(['--- old_\xc2\xb5\n',
152
'+++ new_\xc3\xa5\n',
160
def test_internal_diff_utf8(self):
162
internal_diff(u'old_\xb5', ['old_text\n'],
163
u'new_\xe5', ['new_text\n'], output,
164
path_encoding='utf8')
165
lines = output.getvalue().splitlines(True)
166
self.check_patch(lines)
167
self.assertEquals(['--- old_\xc2\xb5\n',
168
'+++ new_\xc3\xa5\n',
176
def test_internal_diff_iso_8859_1(self):
178
internal_diff(u'old_\xb5', ['old_text\n'],
179
u'new_\xe5', ['new_text\n'], output,
180
path_encoding='iso-8859-1')
181
lines = output.getvalue().splitlines(True)
182
self.check_patch(lines)
183
self.assertEquals(['--- old_\xb5\n',
192
def test_internal_diff_returns_bytes(self):
194
output = StringIO.StringIO()
195
internal_diff(u'old_\xb5', ['old_text\n'],
196
u'new_\xe5', ['new_text\n'], output)
197
self.failUnless(isinstance(output.getvalue(), str),
198
'internal_diff should return bytestrings')
201
class TestDiffFiles(TestCaseInTempDir):
203
def test_external_diff_binary(self):
204
"""The output when using external diff should use diff's i18n error"""
205
# Make sure external_diff doesn't fail in the current LANG
206
lines = external_udiff_lines(['\x00foobar\n'], ['foo\x00bar\n'])
208
cmd = ['diff', '-u', '--binary', 'old', 'new']
209
open('old', 'wb').write('\x00foobar\n')
210
open('new', 'wb').write('foo\x00bar\n')
211
pipe = subprocess.Popen(cmd, stdout=subprocess.PIPE,
212
stdin=subprocess.PIPE)
213
out, err = pipe.communicate()
214
# Diff returns '2' on Binary files.
215
self.assertEqual(2, pipe.returncode)
216
# We should output whatever diff tells us, plus a trailing newline
217
self.assertEqual(out.splitlines(True) + ['\n'], lines)
220
class TestDiffDates(TestCaseWithTransport):
223
super(TestDiffDates, self).setUp()
224
self.wt = self.make_branch_and_tree('.')
225
self.b = self.wt.branch
226
self.build_tree_contents([
227
('file1', 'file1 contents at rev 1\n'),
228
('file2', 'file2 contents at rev 1\n')
230
self.wt.add(['file1', 'file2'])
232
message='Revision 1',
233
timestamp=1143849600, # 2006-04-01 00:00:00 UTC
236
self.build_tree_contents([('file1', 'file1 contents at rev 2\n')])
238
message='Revision 2',
239
timestamp=1143936000, # 2006-04-02 00:00:00 UTC
242
self.build_tree_contents([('file2', 'file2 contents at rev 3\n')])
244
message='Revision 3',
245
timestamp=1144022400, # 2006-04-03 00:00:00 UTC
248
self.wt.remove(['file2'])
250
message='Revision 4',
251
timestamp=1144108800, # 2006-04-04 00:00:00 UTC
254
self.build_tree_contents([
255
('file1', 'file1 contents in working tree\n')
257
# set the date stamps for files in the working tree to known values
258
os.utime('file1', (1144195200, 1144195200)) # 2006-04-05 00:00:00 UTC
260
def get_diff(self, tree1, tree2, specific_files=None, working_tree=None):
262
if working_tree is not None:
263
extra_trees = (working_tree,)
266
show_diff_trees(tree1, tree2, output, specific_files=specific_files,
267
extra_trees=extra_trees, old_label='old/',
269
return output.getvalue()
271
def test_diff_rev_tree_working_tree(self):
272
output = self.get_diff(self.wt.basis_tree(), self.wt)
273
# note that the date for old/file1 is from rev 2 rather than from
274
# the basis revision (rev 4)
275
self.assertEqualDiff(output, '''\
276
=== modified file 'file1'
277
--- old/file1\t2006-04-02 00:00:00 +0000
278
+++ new/file1\t2006-04-05 00:00:00 +0000
280
-file1 contents at rev 2
281
+file1 contents in working tree
285
def test_diff_rev_tree_rev_tree(self):
286
tree1 = self.b.repository.revision_tree('rev-2')
287
tree2 = self.b.repository.revision_tree('rev-3')
288
output = self.get_diff(tree1, tree2)
289
self.assertEqualDiff(output, '''\
290
=== modified file 'file2'
291
--- old/file2\t2006-04-01 00:00:00 +0000
292
+++ new/file2\t2006-04-03 00:00:00 +0000
294
-file2 contents at rev 1
295
+file2 contents at rev 3
299
def test_diff_add_files(self):
300
tree1 = self.b.repository.revision_tree(None)
301
tree2 = self.b.repository.revision_tree('rev-1')
302
output = self.get_diff(tree1, tree2)
303
# the files have the epoch time stamp for the tree in which
305
self.assertEqualDiff(output, '''\
306
=== added file 'file1'
307
--- old/file1\t1970-01-01 00:00:00 +0000
308
+++ new/file1\t2006-04-01 00:00:00 +0000
310
+file1 contents at rev 1
312
=== added file 'file2'
313
--- old/file2\t1970-01-01 00:00:00 +0000
314
+++ new/file2\t2006-04-01 00:00:00 +0000
316
+file2 contents at rev 1
320
def test_diff_remove_files(self):
321
tree1 = self.b.repository.revision_tree('rev-3')
322
tree2 = self.b.repository.revision_tree('rev-4')
323
output = self.get_diff(tree1, tree2)
324
# the file has the epoch time stamp for the tree in which
326
self.assertEqualDiff(output, '''\
327
=== removed file 'file2'
328
--- old/file2\t2006-04-03 00:00:00 +0000
329
+++ new/file2\t1970-01-01 00:00:00 +0000
331
-file2 contents at rev 3
335
def test_show_diff_specified(self):
336
"""A working tree filename can be used to identify a file"""
337
self.wt.rename_one('file1', 'file1b')
338
old_tree = self.b.repository.revision_tree('rev-1')
339
new_tree = self.b.repository.revision_tree('rev-4')
340
out = self.get_diff(old_tree, new_tree, specific_files=['file1b'],
341
working_tree=self.wt)
342
self.assertContainsRe(out, 'file1\t')
344
def test_recursive_diff(self):
345
"""Children of directories are matched"""
348
self.wt.add(['dir1', 'dir2'])
349
self.wt.rename_one('file1', 'dir1/file1')
350
old_tree = self.b.repository.revision_tree('rev-1')
351
new_tree = self.b.repository.revision_tree('rev-4')
352
out = self.get_diff(old_tree, new_tree, specific_files=['dir1'],
353
working_tree=self.wt)
354
self.assertContainsRe(out, 'file1\t')
355
out = self.get_diff(old_tree, new_tree, specific_files=['dir2'],
356
working_tree=self.wt)
357
self.assertNotContainsRe(out, 'file1\t')
360
class TestPatienceDiffLib(TestCase):
67
362
def test_unique_lcs(self):
363
unique_lcs = bzrlib.patiencediff.unique_lcs
68
364
self.assertEquals(unique_lcs('', ''), [])
69
365
self.assertEquals(unique_lcs('a', 'a'), [(0,0)])
70
366
self.assertEquals(unique_lcs('a', 'b'), [])
133
429
'how are you today?\n'],
134
430
[(0, 0, 1), (2, 1, 1)])
136
chk_blocks('aBccDe', 'abccde', [(0,0,1), (2,2,2), (5,5,1)])
138
chk_blocks('aBcdEcdFg', 'abcdecdfg', [(0,0,1), (2,2,2),
141
chk_blocks('abbabbXd', 'cabbabxd', [(0,1,5), (7,7,1)])
142
chk_blocks('abbabbbb', 'cabbabbc', [(0,1,6)])
143
chk_blocks('bbbbbbbb', 'cbbbbbbc', [(0,1,6)])
432
# non unique lines surrounded by non-matching lines
434
chk_blocks('aBccDe', 'abccde', [(0,0,1), (5,5,1)])
436
# But they only need to be locally unique
437
chk_blocks('aBcDec', 'abcdec', [(0,0,1), (2,2,1), (4,4,2)])
439
# non unique blocks won't be matched
440
chk_blocks('aBcdEcdFg', 'abcdecdfg', [(0,0,1), (8,8,1)])
442
# but locally unique ones will
443
chk_blocks('aBcdEeXcdFg', 'abcdecdfg', [(0,0,1), (2,2,2),
444
(5,4,1), (7,5,2), (10,8,1)])
446
chk_blocks('abbabbXd', 'cabbabxd', [(7,7,1)])
447
chk_blocks('abbabbbb', 'cabbabbc', [])
448
chk_blocks('bbbbbbbb', 'cbbbbbbc', [])
145
450
def test_opcodes(self):
146
def chk_ops(a, b, codes):
147
s = SequenceMatcher(None, a, b)
148
self.assertEquals(codes, s.get_opcodes())
451
def chk_ops(a, b, expected_codes):
452
s = bzrlib.patiencediff.PatienceSequenceMatcher(None, a, b)
453
self.assertEquals(expected_codes, s.get_opcodes())
150
455
chk_ops('', '', [])
151
456
chk_ops([], [], [])
223
540
chk_blocks('ABCd efghIjk L'
224
541
, 'AxyzBCn mo pqrstuvwI1 2 L'
225
, [(0,0,1), (1, 4, 2), (4, 7, 1), (9, 19, 1), (12, 23, 3)])
542
, [(0,0,1), (1, 4, 2), (9, 19, 1), (12, 23, 3)])
544
# These are rot13 code snippets.
228
get added when you add a file in the directory.
230
takes_args = ['file*']
231
takes_options = ['no-recurse']
233
def run(self, file_list, no_recurse=False):
234
from bzrlib.add import smart_add, add_reporter_print, add_reporter_null
236
reporter = add_reporter_null
238
reporter = add_reporter_print
239
smart_add(file_list, not no_recurse, reporter)
242
class cmd_mkdir(Command):
245
get added when you add a file in the directory.
247
--dry-run will show which files would be added, but not actually
250
takes_args = ['file*']
251
takes_options = ['no-recurse', 'dry-run']
253
def run(self, file_list, no_recurse=False, dry_run=False):
258
# This is pointless, but I'd rather not raise an error
259
action = bzrlib.add.add_action_null
261
action = bzrlib.add.add_action_print
263
action = bzrlib.add.add_action_add
265
action = bzrlib.add.add_action_add_and_print
267
bzrlib.add.smart_add(file_list, not no_recurse, action)
270
class cmd_mkdir(Command):
546
trg nqqrq jura lbh nqq n svyr va gur qverpgbel.
548
gnxrf_netf = ['svyr*']
549
gnxrf_bcgvbaf = ['ab-erphefr']
551
qrs eha(frys, svyr_yvfg, ab_erphefr=Snyfr):
552
sebz omeyvo.nqq vzcbeg fzneg_nqq, nqq_ercbegre_cevag, nqq_ercbegre_ahyy
554
ercbegre = nqq_ercbegre_ahyy
556
ercbegre = nqq_ercbegre_cevag
557
fzneg_nqq(svyr_yvfg, abg ab_erphefr, ercbegre)
560
pynff pzq_zxqve(Pbzznaq):
561
'''.splitlines(True), '''\
562
trg nqqrq jura lbh nqq n svyr va gur qverpgbel.
564
--qel-eha jvyy fubj juvpu svyrf jbhyq or nqqrq, ohg abg npghnyyl
567
gnxrf_netf = ['svyr*']
568
gnxrf_bcgvbaf = ['ab-erphefr', 'qel-eha']
570
qrs eha(frys, svyr_yvfg, ab_erphefr=Snyfr, qel_eha=Snyfr):
575
# Guvf vf cbvagyrff, ohg V'q engure abg envfr na reebe
576
npgvba = omeyvo.nqq.nqq_npgvba_ahyy
578
npgvba = omeyvo.nqq.nqq_npgvba_cevag
580
npgvba = omeyvo.nqq.nqq_npgvba_nqq
582
npgvba = omeyvo.nqq.nqq_npgvba_nqq_naq_cevag
584
omeyvo.nqq.fzneg_nqq(svyr_yvfg, abg ab_erphefr, npgvba)
587
pynff pzq_zxqve(Pbzznaq):
271
588
'''.splitlines(True)
272
589
, [(0,0,1), (1, 4, 2), (9, 19, 1), (12, 23, 3)])
274
def test_cdv_unified_diff(self):
591
def test_patience_unified_diff(self):
275
592
txt_a = ['hello there\n',
277
594
'how are you today?\n']
278
595
txt_b = ['hello there\n',
279
596
'how are you today?\n']
597
unified_diff = bzrlib.patiencediff.unified_diff
598
psm = bzrlib.patiencediff.PatienceSequenceMatcher
280
599
self.assertEquals([ '--- \n',
282
601
'@@ -1,3 +1,2 @@\n',