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
17
18
from cStringIO import StringIO
20
from tempfile import TemporaryFile
19
from bzrlib.diff import internal_diff
20
from bzrlib.errors import BinaryFile
22
from bzrlib.diff import internal_diff, external_diff, show_diff_trees
23
from bzrlib.errors import BinaryFile, NoDiff
21
24
import bzrlib.patiencediff
22
from bzrlib.tests import TestCase, TestCaseInTempDir
25
from bzrlib.tests import (TestCase, TestCaseWithTransport,
26
TestCaseInTempDir, TestSkipped)
25
29
def udiff_lines(old, new, allow_binary=False):
76
96
udiff_lines([1023 * 'a' + '\x00'], [], allow_binary=True)
77
97
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)
103
def test_external_diff_no_fileno(self):
104
# Make sure that we can handle not having a fileno, even
105
# if the diff is large
106
lines = external_udiff_lines(['boo\n']*10000,
109
self.check_patch(lines)
79
111
def test_internal_diff_default(self):
80
112
# Default internal diff encoding is utf8
81
113
output = StringIO()
133
165
'internal_diff should return bytestrings')
168
class TestDiffDates(TestCaseWithTransport):
171
super(TestDiffDates, self).setUp()
172
self.wt = self.make_branch_and_tree('.')
173
self.b = self.wt.branch
174
self.build_tree_contents([
175
('file1', 'file1 contents at rev 1\n'),
176
('file2', 'file2 contents at rev 1\n')
178
self.wt.add(['file1', 'file2'])
180
message='Revision 1',
181
timestamp=1143849600, # 2006-04-01 00:00:00 UTC
184
self.build_tree_contents([('file1', 'file1 contents at rev 2\n')])
186
message='Revision 2',
187
timestamp=1143936000, # 2006-04-02 00:00:00 UTC
190
self.build_tree_contents([('file2', 'file2 contents at rev 3\n')])
192
message='Revision 3',
193
timestamp=1144022400, # 2006-04-03 00:00:00 UTC
196
self.wt.remove(['file2'])
198
message='Revision 4',
199
timestamp=1144108800, # 2006-04-04 00:00:00 UTC
202
self.build_tree_contents([
203
('file1', 'file1 contents in working tree\n')
205
# set the date stamps for files in the working tree to known values
206
os.utime('file1', (1144195200, 1144195200)) # 2006-04-05 00:00:00 UTC
208
def get_diff(self, tree1, tree2):
210
show_diff_trees(tree1, tree2, output,
211
old_label='old/', new_label='new/')
212
return output.getvalue()
214
def test_diff_rev_tree_working_tree(self):
215
output = self.get_diff(self.wt.basis_tree(), self.wt)
216
# note that the date for old/file1 is from rev 2 rather than from
217
# the basis revision (rev 4)
218
self.assertEqualDiff(output, '''\
219
=== modified file 'file1'
220
--- old/file1\t2006-04-02 00:00:00 +0000
221
+++ new/file1\t2006-04-05 00:00:00 +0000
223
-file1 contents at rev 2
224
+file1 contents in working tree
228
def test_diff_rev_tree_rev_tree(self):
229
tree1 = self.b.repository.revision_tree('rev-2')
230
tree2 = self.b.repository.revision_tree('rev-3')
231
output = self.get_diff(tree1, tree2)
232
self.assertEqualDiff(output, '''\
233
=== modified file 'file2'
234
--- old/file2\t2006-04-01 00:00:00 +0000
235
+++ new/file2\t2006-04-03 00:00:00 +0000
237
-file2 contents at rev 1
238
+file2 contents at rev 3
242
def test_diff_add_files(self):
243
tree1 = self.b.repository.revision_tree(None)
244
tree2 = self.b.repository.revision_tree('rev-1')
245
output = self.get_diff(tree1, tree2)
246
# the files have the epoch time stamp for the tree in which
248
self.assertEqualDiff(output, '''\
249
=== added file 'file1'
250
--- old/file1\t1970-01-01 00:00:00 +0000
251
+++ new/file1\t2006-04-01 00:00:00 +0000
253
+file1 contents at rev 1
255
=== added file 'file2'
256
--- old/file2\t1970-01-01 00:00:00 +0000
257
+++ new/file2\t2006-04-01 00:00:00 +0000
259
+file2 contents at rev 1
263
def test_diff_remove_files(self):
264
tree1 = self.b.repository.revision_tree('rev-3')
265
tree2 = self.b.repository.revision_tree('rev-4')
266
output = self.get_diff(tree1, tree2)
267
# the file has the epoch time stamp for the tree in which
269
self.assertEqualDiff(output, '''\
270
=== removed file 'file2'
271
--- old/file2\t2006-04-03 00:00:00 +0000
272
+++ new/file2\t1970-01-01 00:00:00 +0000
274
-file2 contents at rev 3
136
279
class TestPatienceDiffLib(TestCase):
138
281
def test_unique_lcs(self):