/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to brzlib/tests/blackbox/test_diff.py

  • Committer: Jelmer Vernooij
  • Date: 2017-05-21 12:41:27 UTC
  • mto: This revision was merged to the branch mainline in revision 6623.
  • Revision ID: jelmer@jelmer.uk-20170521124127-iv8etg0vwymyai6y
s/bzr/brz/ in apport config.

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
17
 
18
 
"""Black-box tests for brz diff."""
 
18
"""Black-box tests for bzr diff."""
19
19
 
20
20
import os
21
21
import re
22
22
 
23
 
from breezy import (
 
23
from brzlib import (
24
24
    tests,
25
25
    workingtree,
26
26
    )
27
 
from breezy.diff import (
 
27
from brzlib.diff import (
28
28
    DiffTree,
29
29
    format_registry as diff_format_registry,
30
30
    )
31
 
from breezy.tests import (
 
31
from brzlib.tests import (
32
32
    features,
33
33
    )
34
34
 
45
45
    def make_example_branch(self):
46
46
        tree = self.make_branch_and_tree('.')
47
47
        self.build_tree_contents([
48
 
            ('hello', b'foo\n'),
49
 
            ('goodbye', b'baz\n')])
 
48
            ('hello', 'foo\n'),
 
49
            ('goodbye', 'baz\n')])
50
50
        tree.add(['hello'])
51
51
        tree.commit('setup')
52
52
        tree.add(['goodbye'])
58
58
 
59
59
    def test_diff(self):
60
60
        tree = self.make_example_branch()
61
 
        self.build_tree_contents([('hello', b'hello world!')])
 
61
        self.build_tree_contents([('hello', 'hello world!')])
62
62
        tree.commit(message='fixing hello')
63
63
        output = self.run_bzr('diff -r 2..3', retcode=1)[0]
64
64
        self.assertTrue('\n+hello world!' in output)
76
76
    def test_diff_prefix(self):
77
77
        """diff --prefix appends to filenames in output"""
78
78
        self.make_example_branch()
79
 
        self.build_tree_contents([('hello', b'hello world!\n')])
 
79
        self.build_tree_contents([('hello', 'hello world!\n')])
80
80
        out, err = self.run_bzr('diff --prefix old/:new/', retcode=1)
81
81
        self.assertEqual(err, '')
82
82
        self.assertEqualDiff(subst_dates(out), '''\
93
93
        # There was an error in error reporting for this option
94
94
        out, err = self.run_bzr('diff --prefix old/', retcode=3)
95
95
        self.assertContainsRe(err,
96
 
                              '--prefix expects two values separated by a colon')
 
96
            '--prefix expects two values separated by a colon')
97
97
 
98
98
    def test_diff_p1(self):
99
99
        """diff -p1 produces lkml-style diffs"""
100
100
        self.make_example_branch()
101
 
        self.build_tree_contents([('hello', b'hello world!\n')])
 
101
        self.build_tree_contents([('hello', 'hello world!\n')])
102
102
        out, err = self.run_bzr('diff -p1', retcode=1)
103
103
        self.assertEqual(err, '')
104
104
        self.assertEqualDiff(subst_dates(out), '''\
114
114
    def test_diff_p0(self):
115
115
        """diff -p0 produces diffs with no prefix"""
116
116
        self.make_example_branch()
117
 
        self.build_tree_contents([('hello', b'hello world!\n')])
 
117
        self.build_tree_contents([('hello', 'hello world!\n')])
118
118
        out, err = self.run_bzr('diff -p0', retcode=1)
119
119
        self.assertEqual(err, '')
120
120
        self.assertEqualDiff(subst_dates(out), '''\
132
132
        # (Malone #3619)
133
133
        self.make_example_branch()
134
134
        out, err = self.run_bzr('diff does-not-exist', retcode=3,
135
 
                                error_regexes=('not versioned.*does-not-exist',))
 
135
            error_regexes=('not versioned.*does-not-exist',))
136
136
 
137
137
    def test_diff_illegal_revision_specifiers(self):
138
138
        out, err = self.run_bzr('diff -r 1..23..123', retcode=3,
139
 
                                error_regexes=('one or two revision specifiers',))
 
139
            error_regexes=('one or two revision specifiers',))
140
140
 
141
141
    def test_diff_using_and_format(self):
142
142
        out, err = self.run_bzr('diff --format=default --using=mydi', retcode=3,
143
 
                                error_regexes=('are mutually exclusive',))
 
143
            error_regexes=('are mutually exclusive',))
144
144
 
145
145
    def test_diff_nonexistent_revision(self):
146
146
        out, err = self.run_bzr('diff -r 123', retcode=3,
147
 
                                error_regexes=("Requested revision: '123' does not "
148
 
                                               "exist in branch:",))
 
147
            error_regexes=("Requested revision: '123' does not "
 
148
                "exist in branch:",))
149
149
 
150
150
    def test_diff_nonexistent_dotted_revision(self):
151
151
        out, err = self.run_bzr('diff -r 1.1', retcode=3)
152
152
        self.assertContainsRe(err,
153
 
                              "Requested revision: '1.1' does not exist in branch:")
 
153
            "Requested revision: '1.1' does not exist in branch:")
154
154
 
155
155
    def test_diff_nonexistent_dotted_revision_change(self):
156
156
        out, err = self.run_bzr('diff -c 1.1', retcode=3)
157
157
        self.assertContainsRe(err,
158
 
                              "Requested revision: '1.1' does not exist in branch:")
 
158
            "Requested revision: '1.1' does not exist in branch:")
159
159
 
160
160
    def test_diff_unversioned(self):
161
161
        # Get an error when diffing a non-versioned file.
174
174
        branch1_tree.add('file')
175
175
        branch1_tree.add('file2')
176
176
        branch1_tree.commit(message='add file and file2')
177
 
        branch2_tree = branch1_tree.controldir.sprout(
178
 
            'branch2').open_workingtree()
179
 
        self.build_tree_contents([('branch2/file', b'new content\n')])
 
177
        branch2_tree = branch1_tree.bzrdir.sprout('branch2').open_workingtree()
 
178
        self.build_tree_contents([('branch2/file', 'new content\n')])
180
179
        branch2_tree.commit(message='update file')
181
180
        return branch1_tree, branch2_tree
182
181
 
185
184
        out, err = self.run_bzr(cmd, retcode=1)
186
185
        self.assertEqual('', err)
187
186
        self.assertEqual("=== modified file 'file'\n"
188
 
                         "--- old/file\tYYYY-MM-DD HH:MM:SS +ZZZZ\n"
189
 
                         "+++ new/file\tYYYY-MM-DD HH:MM:SS +ZZZZ\n"
190
 
                         "@@ -1,1 +1,1 @@\n"
191
 
                         "-new content\n"
192
 
                         "+contents of branch1/file\n"
193
 
                         "\n", subst_dates(out))
 
187
                          "--- file\tYYYY-MM-DD HH:MM:SS +ZZZZ\n"
 
188
                          "+++ file\tYYYY-MM-DD HH:MM:SS +ZZZZ\n"
 
189
                          "@@ -1,1 +1,1 @@\n"
 
190
                          "-new content\n"
 
191
                          "+contents of branch1/file\n"
 
192
                          "\n", subst_dates(out))
194
193
 
195
194
    def check_b1_vs_b2(self, cmd):
196
195
        # Compare branch1 vs branch2 using cmd and check the result
197
196
        out, err = self.run_bzr(cmd, retcode=1)
198
197
        self.assertEqual('', err)
199
198
        self.assertEqualDiff("=== modified file 'file'\n"
200
 
                             "--- old/file\tYYYY-MM-DD HH:MM:SS +ZZZZ\n"
201
 
                             "+++ new/file\tYYYY-MM-DD HH:MM:SS +ZZZZ\n"
202
 
                             "@@ -1,1 +1,1 @@\n"
203
 
                             "-contents of branch1/file\n"
204
 
                             "+new content\n"
205
 
                             "\n", subst_dates(out))
 
199
                              "--- file\tYYYY-MM-DD HH:MM:SS +ZZZZ\n"
 
200
                              "+++ file\tYYYY-MM-DD HH:MM:SS +ZZZZ\n"
 
201
                              "@@ -1,1 +1,1 @@\n"
 
202
                              "-contents of branch1/file\n"
 
203
                              "+new content\n"
 
204
                              "\n", subst_dates(out))
206
205
 
207
206
    def check_no_diffs(self, cmd):
208
207
        # Check that running cmd returns an empty diff
230
229
    def test_diff_branches_no_working_trees(self):
231
230
        branch1_tree, branch2_tree = self.example_branches()
232
231
        # Compare a working tree to a branch without a WT
233
 
        dir1 = branch1_tree.controldir
 
232
        dir1 = branch1_tree.bzrdir
234
233
        dir1.destroy_workingtree()
235
234
        self.assertFalse(dir1.has_workingtree())
236
235
        self.check_b2_vs_b1('diff --old branch2 --new branch1')
241
240
        self.check_b1_vs_b2('diff --old branch1 branch2')
242
241
        self.check_b1_vs_b2('diff branch1 --new branch2')
243
242
        # Compare a branch with a WT against another without a WT
244
 
        dir2 = branch2_tree.controldir
 
243
        dir2 = branch2_tree.bzrdir
245
244
        dir2.destroy_workingtree()
246
245
        self.assertFalse(dir2.has_workingtree())
247
246
        self.check_b1_vs_b2('diff --old branch1 --new branch2')
251
250
    def test_diff_revno_branches(self):
252
251
        self.example_branches()
253
252
        branch2_tree = workingtree.WorkingTree.open_containing('branch2')[0]
254
 
        self.build_tree_contents([('branch2/file', b'even newer content')])
 
253
        self.build_tree_contents([('branch2/file', 'even newer content')])
255
254
        branch2_tree.commit(message='update file once more')
256
255
 
257
256
        out, err = self.run_bzr('diff -r revno:1:branch2..revno:1:branch1',
262
261
                                retcode=1)
263
262
        self.assertEqual('', err)
264
263
        self.assertEqualDiff("=== modified file 'file'\n"
265
 
                             "--- old/file\tYYYY-MM-DD HH:MM:SS +ZZZZ\n"
266
 
                             "+++ new/file\tYYYY-MM-DD HH:MM:SS +ZZZZ\n"
267
 
                             "@@ -1,1 +1,1 @@\n"
268
 
                             "-new content\n"
269
 
                             "+contents of branch1/file\n"
270
 
                             "\n", subst_dates(out))
271
 
 
272
 
    def test_diff_color_always(self):
273
 
        from ...terminal import colorstring
274
 
        from ... import colordiff
275
 
        self.overrideAttr(colordiff, 'GLOBAL_COLORDIFFRC', None)
276
 
        self.example_branches()
277
 
        branch2_tree = workingtree.WorkingTree.open_containing('branch2')[0]
278
 
        self.build_tree_contents([('branch2/file', b'even newer content')])
279
 
        branch2_tree.commit(message='update file once more')
280
 
 
281
 
        out, err = self.run_bzr('diff --color=always -r revno:2:branch2..revno:1:branch1',
282
 
                                retcode=1)
283
 
        self.assertEqual('', err)
284
 
        self.assertEqualDiff((
285
 
            colorstring(b"=== modified file 'file'\n", 'darkyellow') +
286
 
            colorstring(b"--- old/file\tYYYY-MM-DD HH:MM:SS +ZZZZ\n", 'darkred') +
287
 
            colorstring(b"+++ new/file\tYYYY-MM-DD HH:MM:SS +ZZZZ\n", 'darkblue') +
288
 
            colorstring(b"@@ -1 +1 @@\n", 'darkgreen') +
289
 
            colorstring(b"-new content\n", 'darkred') +
290
 
            colorstring(b"+contents of branch1/file\n", 'darkblue') +
291
 
            colorstring(b"\n", 'darkwhite')).decode(),
292
 
            subst_dates(out))
 
264
                              "--- file\tYYYY-MM-DD HH:MM:SS +ZZZZ\n"
 
265
                              "+++ file\tYYYY-MM-DD HH:MM:SS +ZZZZ\n"
 
266
                              "@@ -1,1 +1,1 @@\n"
 
267
                              "-new content\n"
 
268
                              "+contents of branch1/file\n"
 
269
                              "\n", subst_dates(out))
293
270
 
294
271
    def example_branch2(self):
295
272
        branch1_tree = self.make_branch_and_tree('branch1')
296
 
        self.build_tree_contents([('branch1/file1', b'original line\n')])
 
273
        self.build_tree_contents([('branch1/file1', 'original line\n')])
297
274
        branch1_tree.add('file1')
298
275
        branch1_tree.commit(message='first commit')
299
 
        self.build_tree_contents([('branch1/file1', b'repo line\n')])
 
276
        self.build_tree_contents([('branch1/file1', 'repo line\n')])
300
277
        branch1_tree.commit(message='second commit')
301
278
        return branch1_tree
302
279
 
303
280
    def test_diff_to_working_tree(self):
304
281
        self.example_branch2()
305
 
        self.build_tree_contents([('branch1/file1', b'new line')])
 
282
        self.build_tree_contents([('branch1/file1', 'new line')])
306
283
        output = self.run_bzr('diff -r 1.. branch1', retcode=1)
307
284
        self.assertContainsRe(output[0], '\n\\-original line\n\\+new line\n')
308
285
 
309
286
    def test_diff_to_working_tree_in_subdir(self):
310
287
        self.example_branch2()
311
 
        self.build_tree_contents([('branch1/file1', b'new line')])
 
288
        self.build_tree_contents([('branch1/file1', 'new line')])
312
289
        os.mkdir('branch1/dir1')
313
290
        output = self.run_bzr('diff -r 1..', retcode=1,
314
291
                              working_dir='branch1/dir1')
324
301
 
325
302
    def test_diff_to_branch_no_working_tree(self):
326
303
        branch1_tree = self.example_branch2()
327
 
        dir1 = branch1_tree.controldir
 
304
        dir1 = branch1_tree.bzrdir
328
305
        dir1.destroy_workingtree()
329
306
        self.assertFalse(dir1.has_workingtree())
330
307
        output = self.run_bzr('diff -r 1.. branch1', retcode=1)
336
313
            def show_diff(self, specific_files, extra_trees=None):
337
314
                self.to_file.write("BOO!\n")
338
315
                return super(BooDiffTree, self).show_diff(specific_files,
339
 
                                                          extra_trees)
 
316
                    extra_trees)
340
317
 
341
318
        diff_format_registry.register("boo", BooDiffTree, "Scary diff format")
342
319
        self.addCleanup(diff_format_registry.remove, "boo")
343
320
        self.make_example_branch()
344
 
        self.build_tree_contents([('hello', b'hello world!\n')])
 
321
        self.build_tree_contents([('hello', 'hello world!\n')])
345
322
        output = self.run_bzr('diff --format=boo', retcode=1)
346
323
        self.assertTrue("BOO!" in output[0])
347
324
        output = self.run_bzr('diff -Fboo', retcode=1)
348
325
        self.assertTrue("BOO!" in output[0])
349
326
 
350
 
    def test_binary_diff_remove(self):
351
 
        tree = self.make_branch_and_tree('.')
352
 
        self.build_tree_contents([('a', b'\x00' * 20)])
353
 
        tree.add(['a'])
354
 
        tree.commit('add binary file')
355
 
        os.unlink('a')
356
 
        output = self.run_bzr('diff', retcode=1)
357
 
        self.assertEqual(
358
 
            "=== removed file 'a'\nBinary files old/a and new/a differ\n",
359
 
            output[0])
360
 
 
361
327
 
362
328
class TestCheckoutDiff(TestDiff):
363
329
 
394
360
 
395
361
    def test_diff_label_added(self):
396
362
        tree = super(TestDiffLabels, self).make_example_branch()
397
 
        self.build_tree_contents([('barbar', b'barbar')])
 
363
        self.build_tree_contents([('barbar', 'barbar')])
398
364
        tree.add('barbar')
399
365
        diff = self.run_bzr('diff', retcode=1)
400
366
        self.assertTrue("=== added file 'barbar'" in diff[0])
401
367
 
402
368
    def test_diff_label_modified(self):
403
369
        super(TestDiffLabels, self).make_example_branch()
404
 
        self.build_tree_contents([('hello', b'barbar')])
 
370
        self.build_tree_contents([('hello', 'barbar')])
405
371
        diff = self.run_bzr('diff', retcode=1)
406
372
        self.assertTrue("=== modified file 'hello'" in diff[0])
407
373
 
426
392
            'diff -Oprogress_bar=none -r 1 --diff-options -ub',
427
393
            universal_newlines=True,
428
394
            retcode=None)
429
 
        if b'Diff is not installed on this machine' in err:
 
395
        if 'Diff is not installed on this machine' in err:
430
396
            raise tests.TestSkipped("No external 'diff' is available")
431
 
        self.assertEqual(b'', err)
 
397
        self.assertEqual('', err)
432
398
        # We have to skip the stuff in the middle, because it depends
433
399
        # on time.time()
434
 
        self.assertStartsWith(
435
 
            out,
436
 
            b"=== added file 'goodbye'\n"
437
 
            b"--- old/goodbye\t1970-01-01 00:00:00 +0000\n"
438
 
            b"+++ new/goodbye\t")
439
 
        self.assertEndsWith(out, b"\n@@ -0,0 +1 @@\n"
440
 
                                 b"+baz\n\n")
 
400
        self.assertStartsWith(out, "=== added file 'goodbye'\n"
 
401
                                   "--- goodbye\t1970-01-01 00:00:00 +0000\n"
 
402
                                   "+++ goodbye\t")
 
403
        self.assertEndsWith(out, "\n@@ -0,0 +1 @@\n"
 
404
                                 "+baz\n\n")
441
405
 
442
406
    def test_external_diff_options_and_using(self):
443
407
        """Test that the options are passed correctly to an external diff process"""
444
408
        self.requireFeature(features.diff_feature)
445
409
        self.make_example_branch()
446
 
        self.build_tree_contents([('hello', b'Foo\n')])
 
410
        self.build_tree_contents([('hello', 'Foo\n')])
447
411
        out, err = self.run_bzr('diff --diff-options -i --using diff',
448
 
                                retcode=1)
 
412
                                    retcode=1)
449
413
        self.assertEqual("=== modified file 'hello'\n", out)
450
414
        self.assertEqual('', err)
451
415
 
455
419
    def test_diff_output(self):
456
420
        # check that output doesn't mangle line-endings
457
421
        self.make_example_branch()
458
 
        self.build_tree_contents([('hello', b'hello world!\n')])
 
422
        self.build_tree_contents([('hello', 'hello world!\n')])
459
423
        output = self.run_bzr_subprocess('diff', retcode=1)[0]
460
 
        self.assertTrue(b'\n+hello world!\n' in output)
 
424
        self.assertTrue('\n+hello world!\n' in output)