~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_diff.py

  • Committer: Martin Pool
  • Date: 2006-06-20 07:55:43 UTC
  • mfrom: (1798 +trunk)
  • mto: This revision was merged to the branch mainline in revision 1799.
  • Revision ID: mbp@sourcefrog.net-20060620075543-b10f6575d4a4fa32
[merge] bzr.dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
16
16
 
 
17
import os
17
18
from cStringIO import StringIO
 
19
import errno
 
20
from tempfile import TemporaryFile
18
21
 
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)
23
27
 
24
28
 
25
29
def udiff_lines(old, new, allow_binary=False):
29
33
    return output.readlines()
30
34
 
31
35
 
 
36
def external_udiff_lines(old, new, use_stringio=False):
 
37
    if use_stringio:
 
38
        # StringIO has no fileno, so it tests a different codepath
 
39
        output = StringIO()
 
40
    else:
 
41
        output = TemporaryFile()
 
42
    try:
 
43
        external_diff('old', old, 'new', new, output, diff_opts=['-u'])
 
44
    except NoDiff:
 
45
        raise TestSkipped('external "diff" not present to test')
 
46
    output.seek(0, 0)
 
47
    lines = output.readlines()
 
48
    output.close()
 
49
    return lines
 
50
 
 
51
 
32
52
class TestDiff(TestCase):
33
53
 
34
54
    def test_add_nl(self):
76
96
        udiff_lines([1023 * 'a' + '\x00'], [], allow_binary=True)
77
97
        udiff_lines([], [1023 * 'a' + '\x00'], allow_binary=True)
78
98
 
 
99
    def test_external_diff(self):
 
100
        lines = external_udiff_lines(['boo\n'], ['goo\n'])
 
101
        self.check_patch(lines)
 
102
 
 
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,
 
107
                                     ['goo\n']*10000,
 
108
                                     use_stringio=True)
 
109
        self.check_patch(lines)
 
110
        
79
111
    def test_internal_diff_default(self):
80
112
        # Default internal diff encoding is utf8
81
113
        output = StringIO()
83
115
                    u'new_\xe5', ['new_text\n'], output)
84
116
        lines = output.getvalue().splitlines(True)
85
117
        self.check_patch(lines)
86
 
        self.assertEquals(['--- old_\xc2\xb5\t\n',
87
 
                           '+++ new_\xc3\xa5\t\n',
 
118
        self.assertEquals(['--- old_\xc2\xb5\n',
 
119
                           '+++ new_\xc3\xa5\n',
88
120
                           '@@ -1,1 +1,1 @@\n',
89
121
                           '-old_text\n',
90
122
                           '+new_text\n',
99
131
                    path_encoding='utf8')
100
132
        lines = output.getvalue().splitlines(True)
101
133
        self.check_patch(lines)
102
 
        self.assertEquals(['--- old_\xc2\xb5\t\n',
103
 
                           '+++ new_\xc3\xa5\t\n',
 
134
        self.assertEquals(['--- old_\xc2\xb5\n',
 
135
                           '+++ new_\xc3\xa5\n',
104
136
                           '@@ -1,1 +1,1 @@\n',
105
137
                           '-old_text\n',
106
138
                           '+new_text\n',
115
147
                    path_encoding='iso-8859-1')
116
148
        lines = output.getvalue().splitlines(True)
117
149
        self.check_patch(lines)
118
 
        self.assertEquals(['--- old_\xb5\t\n',
119
 
                           '+++ new_\xe5\t\n',
 
150
        self.assertEquals(['--- old_\xb5\n',
 
151
                           '+++ new_\xe5\n',
120
152
                           '@@ -1,1 +1,1 @@\n',
121
153
                           '-old_text\n',
122
154
                           '+new_text\n',
133
165
            'internal_diff should return bytestrings')
134
166
 
135
167
 
 
168
class TestDiffDates(TestCaseWithTransport):
 
169
 
 
170
    def setUp(self):
 
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')
 
177
            ])
 
178
        self.wt.add(['file1', 'file2'])
 
179
        self.wt.commit(
 
180
            message='Revision 1',
 
181
            timestamp=1143849600, # 2006-04-01 00:00:00 UTC
 
182
            timezone=0,
 
183
            rev_id='rev-1')
 
184
        self.build_tree_contents([('file1', 'file1 contents at rev 2\n')])
 
185
        self.wt.commit(
 
186
            message='Revision 2',
 
187
            timestamp=1143936000, # 2006-04-02 00:00:00 UTC
 
188
            timezone=28800,
 
189
            rev_id='rev-2')
 
190
        self.build_tree_contents([('file2', 'file2 contents at rev 3\n')])
 
191
        self.wt.commit(
 
192
            message='Revision 3',
 
193
            timestamp=1144022400, # 2006-04-03 00:00:00 UTC
 
194
            timezone=-3600,
 
195
            rev_id='rev-3')
 
196
        self.wt.remove(['file2'])
 
197
        self.wt.commit(
 
198
            message='Revision 4',
 
199
            timestamp=1144108800, # 2006-04-04 00:00:00 UTC
 
200
            timezone=0,
 
201
            rev_id='rev-4')
 
202
        self.build_tree_contents([
 
203
            ('file1', 'file1 contents in working tree\n')
 
204
            ])
 
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
 
207
 
 
208
    def get_diff(self, tree1, tree2):
 
209
        output = StringIO()
 
210
        show_diff_trees(tree1, tree2, output,
 
211
                        old_label='old/', new_label='new/')
 
212
        return output.getvalue()
 
213
 
 
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
 
222
@@ -1,1 +1,1 @@
 
223
-file1 contents at rev 2
 
224
+file1 contents in working tree
 
225
 
 
226
''')
 
227
 
 
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
 
236
@@ -1,1 +1,1 @@
 
237
-file2 contents at rev 1
 
238
+file2 contents at rev 3
 
239
 
 
240
''')
 
241
        
 
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
 
247
        # they don't exist.
 
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
 
252
@@ -0,0 +1,1 @@
 
253
+file1 contents at rev 1
 
254
 
 
255
=== added file 'file2'
 
256
--- old/file2\t1970-01-01 00:00:00 +0000
 
257
+++ new/file2\t2006-04-01 00:00:00 +0000
 
258
@@ -0,0 +1,1 @@
 
259
+file2 contents at rev 1
 
260
 
 
261
''')
 
262
 
 
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
 
268
        # it doesn't exist.
 
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
 
273
@@ -1,1 +0,0 @@
 
274
-file2 contents at rev 3
 
275
 
 
276
''')
 
277
 
 
278
 
136
279
class TestPatienceDiffLib(TestCase):
137
280
 
138
281
    def test_unique_lcs(self):