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
18
17
from cStringIO import StringIO
20
from tempfile import TemporaryFile
22
from bzrlib.diff import internal_diff, external_diff, show_diff_trees
23
from bzrlib.errors import BinaryFile, NoDiff
19
from bzrlib.diff import internal_diff
20
from bzrlib.errors import BinaryFile
24
21
import bzrlib.patiencediff
25
from bzrlib.tests import (TestCase, TestCaseWithTransport,
26
TestCaseInTempDir, TestSkipped)
22
from bzrlib.tests import TestCase, TestCaseInTempDir
29
25
def udiff_lines(old, new, allow_binary=False):
96
76
udiff_lines([1023 * 'a' + '\x00'], [], allow_binary=True)
97
77
udiff_lines([], [1023 * 'a' + '\x00'], allow_binary=True)
99
def test_external_diff(self):
100
lines = external_udiff_lines(['boo\n'], ['goo\n'])
101
self.check_patch(lines)
102
self.assertEqual('\n', lines[-1])
104
def test_external_diff_no_fileno(self):
105
# Make sure that we can handle not having a fileno, even
106
# if the diff is large
107
lines = external_udiff_lines(['boo\n']*10000,
110
self.check_patch(lines)
112
def test_external_diff_binary(self):
113
lines = external_udiff_lines(['\x00foobar\n'], ['foo\x00bar\n'])
114
self.assertEqual(['Binary files old and new differ\n', '\n'], lines)
116
def test_no_external_diff(self):
117
"""Check that NoDiff is raised when diff is not available"""
118
# Use os.environ['PATH'] to make sure no 'diff' command is available
119
orig_path = os.environ['PATH']
121
os.environ['PATH'] = ''
122
self.assertRaises(NoDiff, external_diff,
123
'old', ['boo\n'], 'new', ['goo\n'],
124
StringIO(), diff_opts=['-u'])
126
os.environ['PATH'] = orig_path
128
79
def test_internal_diff_default(self):
129
80
# Default internal diff encoding is utf8
130
81
output = StringIO()
182
133
'internal_diff should return bytestrings')
185
class TestDiffDates(TestCaseWithTransport):
188
super(TestDiffDates, self).setUp()
189
self.wt = self.make_branch_and_tree('.')
190
self.b = self.wt.branch
191
self.build_tree_contents([
192
('file1', 'file1 contents at rev 1\n'),
193
('file2', 'file2 contents at rev 1\n')
195
self.wt.add(['file1', 'file2'])
197
message='Revision 1',
198
timestamp=1143849600, # 2006-04-01 00:00:00 UTC
201
self.build_tree_contents([('file1', 'file1 contents at rev 2\n')])
203
message='Revision 2',
204
timestamp=1143936000, # 2006-04-02 00:00:00 UTC
207
self.build_tree_contents([('file2', 'file2 contents at rev 3\n')])
209
message='Revision 3',
210
timestamp=1144022400, # 2006-04-03 00:00:00 UTC
213
self.wt.remove(['file2'])
215
message='Revision 4',
216
timestamp=1144108800, # 2006-04-04 00:00:00 UTC
219
self.build_tree_contents([
220
('file1', 'file1 contents in working tree\n')
222
# set the date stamps for files in the working tree to known values
223
os.utime('file1', (1144195200, 1144195200)) # 2006-04-05 00:00:00 UTC
225
def get_diff(self, tree1, tree2, specific_files=None, working_tree=None):
227
if working_tree is not None:
228
extra_trees = (working_tree,)
231
show_diff_trees(tree1, tree2, output, specific_files=specific_files,
232
extra_trees=extra_trees, old_label='old/',
234
return output.getvalue()
236
def test_diff_rev_tree_working_tree(self):
237
output = self.get_diff(self.wt.basis_tree(), self.wt)
238
# note that the date for old/file1 is from rev 2 rather than from
239
# the basis revision (rev 4)
240
self.assertEqualDiff(output, '''\
241
=== modified file 'file1'
242
--- old/file1\t2006-04-02 00:00:00 +0000
243
+++ new/file1\t2006-04-05 00:00:00 +0000
245
-file1 contents at rev 2
246
+file1 contents in working tree
250
def test_diff_rev_tree_rev_tree(self):
251
tree1 = self.b.repository.revision_tree('rev-2')
252
tree2 = self.b.repository.revision_tree('rev-3')
253
output = self.get_diff(tree1, tree2)
254
self.assertEqualDiff(output, '''\
255
=== modified file 'file2'
256
--- old/file2\t2006-04-01 00:00:00 +0000
257
+++ new/file2\t2006-04-03 00:00:00 +0000
259
-file2 contents at rev 1
260
+file2 contents at rev 3
264
def test_diff_add_files(self):
265
tree1 = self.b.repository.revision_tree(None)
266
tree2 = self.b.repository.revision_tree('rev-1')
267
output = self.get_diff(tree1, tree2)
268
# the files have the epoch time stamp for the tree in which
270
self.assertEqualDiff(output, '''\
271
=== added file 'file1'
272
--- old/file1\t1970-01-01 00:00:00 +0000
273
+++ new/file1\t2006-04-01 00:00:00 +0000
275
+file1 contents at rev 1
277
=== added file 'file2'
278
--- old/file2\t1970-01-01 00:00:00 +0000
279
+++ new/file2\t2006-04-01 00:00:00 +0000
281
+file2 contents at rev 1
285
def test_diff_remove_files(self):
286
tree1 = self.b.repository.revision_tree('rev-3')
287
tree2 = self.b.repository.revision_tree('rev-4')
288
output = self.get_diff(tree1, tree2)
289
# the file has the epoch time stamp for the tree in which
291
self.assertEqualDiff(output, '''\
292
=== removed file 'file2'
293
--- old/file2\t2006-04-03 00:00:00 +0000
294
+++ new/file2\t1970-01-01 00:00:00 +0000
296
-file2 contents at rev 3
300
def test_show_diff_specified(self):
301
"""A working tree filename can be used to identify a file"""
302
self.wt.rename_one('file1', 'file1b')
303
old_tree = self.b.repository.revision_tree('rev-1')
304
new_tree = self.b.repository.revision_tree('rev-4')
305
out = self.get_diff(old_tree, new_tree, specific_files=['file1b'],
306
working_tree=self.wt)
307
self.assertContainsRe(out, 'file1\t')
309
def test_recursive_diff(self):
310
"""Children of directories are matched"""
313
self.wt.add(['dir1', 'dir2'])
314
self.wt.rename_one('file1', 'dir1/file1')
315
old_tree = self.b.repository.revision_tree('rev-1')
316
new_tree = self.b.repository.revision_tree('rev-4')
317
out = self.get_diff(old_tree, new_tree, specific_files=['dir1'],
318
working_tree=self.wt)
319
self.assertContainsRe(out, 'file1\t')
320
out = self.get_diff(old_tree, new_tree, specific_files=['dir2'],
321
working_tree=self.wt)
322
self.assertNotContainsRe(out, 'file1\t')
325
136
class TestPatienceDiffLib(TestCase):
327
138
def test_unique_lcs(self):