~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_diff.py

  • Committer: Jelmer Vernooij
  • Date: 2006-06-13 13:24:40 UTC
  • mfrom: (1767 +trunk)
  • mto: (1769.1.1 integration)
  • mto: This revision was merged to the branch mainline in revision 1770.
  • Revision ID: jelmer@samba.org-20060613132440-24e222a86f948f60
[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
18
19
 
19
 
from bzrlib.diff import internal_diff
 
20
from bzrlib.diff import internal_diff, show_diff_trees
20
21
from bzrlib.errors import BinaryFile
21
22
import bzrlib.patiencediff
 
23
from bzrlib.tests import TestCase, TestCaseWithTransport, TestCaseInTempDir
22
24
from bzrlib.tests import TestCase, TestCaseInTempDir
23
25
 
24
26
 
76
78
        udiff_lines([1023 * 'a' + '\x00'], [], allow_binary=True)
77
79
        udiff_lines([], [1023 * 'a' + '\x00'], allow_binary=True)
78
80
 
 
81
    def test_internal_diff_default(self):
 
82
        # Default internal diff encoding is utf8
 
83
        output = StringIO()
 
84
        internal_diff(u'old_\xb5', ['old_text\n'],
 
85
                    u'new_\xe5', ['new_text\n'], output)
 
86
        lines = output.getvalue().splitlines(True)
 
87
        self.check_patch(lines)
 
88
        self.assertEquals(['--- old_\xc2\xb5\n',
 
89
                           '+++ new_\xc3\xa5\n',
 
90
                           '@@ -1,1 +1,1 @@\n',
 
91
                           '-old_text\n',
 
92
                           '+new_text\n',
 
93
                           '\n',
 
94
                          ]
 
95
                          , lines)
 
96
 
 
97
    def test_internal_diff_utf8(self):
 
98
        output = StringIO()
 
99
        internal_diff(u'old_\xb5', ['old_text\n'],
 
100
                    u'new_\xe5', ['new_text\n'], output,
 
101
                    path_encoding='utf8')
 
102
        lines = output.getvalue().splitlines(True)
 
103
        self.check_patch(lines)
 
104
        self.assertEquals(['--- old_\xc2\xb5\n',
 
105
                           '+++ new_\xc3\xa5\n',
 
106
                           '@@ -1,1 +1,1 @@\n',
 
107
                           '-old_text\n',
 
108
                           '+new_text\n',
 
109
                           '\n',
 
110
                          ]
 
111
                          , lines)
 
112
 
 
113
    def test_internal_diff_iso_8859_1(self):
 
114
        output = StringIO()
 
115
        internal_diff(u'old_\xb5', ['old_text\n'],
 
116
                    u'new_\xe5', ['new_text\n'], output,
 
117
                    path_encoding='iso-8859-1')
 
118
        lines = output.getvalue().splitlines(True)
 
119
        self.check_patch(lines)
 
120
        self.assertEquals(['--- old_\xb5\n',
 
121
                           '+++ new_\xe5\n',
 
122
                           '@@ -1,1 +1,1 @@\n',
 
123
                           '-old_text\n',
 
124
                           '+new_text\n',
 
125
                           '\n',
 
126
                          ]
 
127
                          , lines)
 
128
 
 
129
    def test_internal_diff_returns_bytes(self):
 
130
        import StringIO
 
131
        output = StringIO.StringIO()
 
132
        internal_diff(u'old_\xb5', ['old_text\n'],
 
133
                    u'new_\xe5', ['new_text\n'], output)
 
134
        self.failUnless(isinstance(output.getvalue(), str),
 
135
            'internal_diff should return bytestrings')
 
136
 
 
137
 
 
138
class TestDiffDates(TestCaseWithTransport):
 
139
 
 
140
    def setUp(self):
 
141
        super(TestDiffDates, self).setUp()
 
142
        self.wt = self.make_branch_and_tree('.')
 
143
        self.b = self.wt.branch
 
144
        self.build_tree_contents([
 
145
            ('file1', 'file1 contents at rev 1\n'),
 
146
            ('file2', 'file2 contents at rev 1\n')
 
147
            ])
 
148
        self.wt.add(['file1', 'file2'])
 
149
        self.wt.commit(
 
150
            message='Revision 1',
 
151
            timestamp=1143849600, # 2006-04-01 00:00:00 UTC
 
152
            timezone=0,
 
153
            rev_id='rev-1')
 
154
        self.build_tree_contents([('file1', 'file1 contents at rev 2\n')])
 
155
        self.wt.commit(
 
156
            message='Revision 2',
 
157
            timestamp=1143936000, # 2006-04-02 00:00:00 UTC
 
158
            timezone=28800,
 
159
            rev_id='rev-2')
 
160
        self.build_tree_contents([('file2', 'file2 contents at rev 3\n')])
 
161
        self.wt.commit(
 
162
            message='Revision 3',
 
163
            timestamp=1144022400, # 2006-04-03 00:00:00 UTC
 
164
            timezone=-3600,
 
165
            rev_id='rev-3')
 
166
        self.wt.remove(['file2'])
 
167
        self.wt.commit(
 
168
            message='Revision 4',
 
169
            timestamp=1144108800, # 2006-04-04 00:00:00 UTC
 
170
            timezone=0,
 
171
            rev_id='rev-4')
 
172
        self.build_tree_contents([
 
173
            ('file1', 'file1 contents in working tree\n')
 
174
            ])
 
175
        # set the date stamps for files in the working tree to known values
 
176
        os.utime('file1', (1144195200, 1144195200)) # 2006-04-05 00:00:00 UTC
 
177
 
 
178
    def get_diff(self, tree1, tree2):
 
179
        output = StringIO()
 
180
        show_diff_trees(tree1, tree2, output,
 
181
                        old_label='old/', new_label='new/')
 
182
        return output.getvalue()
 
183
 
 
184
    def test_diff_rev_tree_working_tree(self):
 
185
        output = self.get_diff(self.wt.basis_tree(), self.wt)
 
186
        # note that the date for old/file1 is from rev 2 rather than from
 
187
        # the basis revision (rev 4)
 
188
        self.assertEqualDiff(output, '''\
 
189
=== modified file 'file1'
 
190
--- old/file1\t2006-04-02 00:00:00 +0000
 
191
+++ new/file1\t2006-04-05 00:00:00 +0000
 
192
@@ -1,1 +1,1 @@
 
193
-file1 contents at rev 2
 
194
+file1 contents in working tree
 
195
 
 
196
''')
 
197
 
 
198
    def test_diff_rev_tree_rev_tree(self):
 
199
        tree1 = self.b.repository.revision_tree('rev-2')
 
200
        tree2 = self.b.repository.revision_tree('rev-3')
 
201
        output = self.get_diff(tree1, tree2)
 
202
        self.assertEqualDiff(output, '''\
 
203
=== modified file 'file2'
 
204
--- old/file2\t2006-04-01 00:00:00 +0000
 
205
+++ new/file2\t2006-04-03 00:00:00 +0000
 
206
@@ -1,1 +1,1 @@
 
207
-file2 contents at rev 1
 
208
+file2 contents at rev 3
 
209
 
 
210
''')
 
211
        
 
212
    def test_diff_add_files(self):
 
213
        tree1 = self.b.repository.revision_tree(None)
 
214
        tree2 = self.b.repository.revision_tree('rev-1')
 
215
        output = self.get_diff(tree1, tree2)
 
216
        # the files have the epoch time stamp for the tree in which
 
217
        # they don't exist.
 
218
        self.assertEqualDiff(output, '''\
 
219
=== added file 'file1'
 
220
--- old/file1\t1970-01-01 00:00:00 +0000
 
221
+++ new/file1\t2006-04-01 00:00:00 +0000
 
222
@@ -0,0 +1,1 @@
 
223
+file1 contents at rev 1
 
224
 
 
225
=== added file 'file2'
 
226
--- old/file2\t1970-01-01 00:00:00 +0000
 
227
+++ new/file2\t2006-04-01 00:00:00 +0000
 
228
@@ -0,0 +1,1 @@
 
229
+file2 contents at rev 1
 
230
 
 
231
''')
 
232
 
 
233
    def test_diff_remove_files(self):
 
234
        tree1 = self.b.repository.revision_tree('rev-3')
 
235
        tree2 = self.b.repository.revision_tree('rev-4')
 
236
        output = self.get_diff(tree1, tree2)
 
237
        # the file has the epoch time stamp for the tree in which
 
238
        # it doesn't exist.
 
239
        self.assertEqualDiff(output, '''\
 
240
=== removed file 'file2'
 
241
--- old/file2\t2006-04-03 00:00:00 +0000
 
242
+++ new/file2\t1970-01-01 00:00:00 +0000
 
243
@@ -1,1 +0,0 @@
 
244
-file2 contents at rev 3
 
245
 
 
246
''')
 
247
 
79
248
 
80
249
class TestPatienceDiffLib(TestCase):
81
250