/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 breezy/tests/blackbox/test_diff.py

  • Committer: Breezy landing bot
  • Author(s): Colin Watson
  • Date: 2020-11-16 21:47:08 UTC
  • mfrom: (7521.1.1 remove-lp-workaround)
  • Revision ID: breezy.the.bot@gmail.com-20201116214708-jos209mgxi41oy15
Remove breezy.git workaround for bazaar.launchpad.net.

Merged from https://code.launchpad.net/~cjwatson/brz/remove-lp-workaround/+merge/393710

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006-2012 Canonical Ltd
 
1
# Copyright (C) 2006-2012, 2016 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
17
 
18
 
"""Black-box tests for bzr diff."""
 
18
"""Black-box tests for brz diff."""
19
19
 
20
20
import os
21
21
import re
22
22
 
23
 
from bzrlib import (
 
23
from breezy import (
24
24
    tests,
25
25
    workingtree,
26
26
    )
27
 
from bzrlib.diff import (
 
27
from breezy.diff import (
28
28
    DiffTree,
29
29
    format_registry as diff_format_registry,
30
30
    )
31
 
from bzrlib.tests import (
 
31
from breezy.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', 'foo\n'),
49
 
            ('goodbye', 'baz\n')])
 
48
            ('hello', b'foo\n'),
 
49
            ('goodbye', b'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', 'hello world!')])
 
61
        self.build_tree_contents([('hello', b'hello world!')])
62
62
        tree.commit(message='fixing hello')
63
63
        output = self.run_bzr('diff -r 2..3', retcode=1)[0]
64
 
        self.assert_('\n+hello world!' in output)
 
64
        self.assertTrue('\n+hello world!' in output)
65
65
        output = self.run_bzr('diff -c 3', retcode=1)[0]
66
 
        self.assert_('\n+hello world!' in output)
 
66
        self.assertTrue('\n+hello world!' in output)
67
67
        output = self.run_bzr('diff -r last:3..last:1', retcode=1)[0]
68
 
        self.assert_('\n+baz' in output)
 
68
        self.assertTrue('\n+baz' in output)
69
69
        output = self.run_bzr('diff -c last:2', retcode=1)[0]
70
 
        self.assert_('\n+baz' in output)
 
70
        self.assertTrue('\n+baz' in output)
71
71
        self.build_tree(['moo'])
72
72
        tree.add('moo')
73
73
        os.unlink('moo')
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', 'hello world!\n')])
 
79
        self.build_tree_contents([('hello', b'hello world!\n')])
80
80
        out, err = self.run_bzr('diff --prefix old/:new/', retcode=1)
81
 
        self.assertEquals(err, '')
 
81
        self.assertEqual(err, '')
82
82
        self.assertEqualDiff(subst_dates(out), '''\
83
83
=== modified file 'hello'
84
84
--- old/hello\tYYYY-MM-DD HH:MM:SS +ZZZZ
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', 'hello world!\n')])
 
101
        self.build_tree_contents([('hello', b'hello world!\n')])
102
102
        out, err = self.run_bzr('diff -p1', retcode=1)
103
 
        self.assertEquals(err, '')
 
103
        self.assertEqual(err, '')
104
104
        self.assertEqualDiff(subst_dates(out), '''\
105
105
=== modified file 'hello'
106
106
--- old/hello\tYYYY-MM-DD HH:MM:SS +ZZZZ
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', 'hello world!\n')])
 
117
        self.build_tree_contents([('hello', b'hello world!\n')])
118
118
        out, err = self.run_bzr('diff -p0', retcode=1)
119
 
        self.assertEquals(err, '')
 
119
        self.assertEqual(err, '')
120
120
        self.assertEqualDiff(subst_dates(out), '''\
121
121
=== modified file 'hello'
122
122
--- hello\tYYYY-MM-DD HH:MM:SS +ZZZZ
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.bzrdir.sprout('branch2').open_workingtree()
178
 
        self.build_tree_contents([('branch2/file', 'new content\n')])
 
177
        branch2_tree = branch1_tree.controldir.sprout(
 
178
            'branch2').open_workingtree()
 
179
        self.build_tree_contents([('branch2/file', b'new content\n')])
179
180
        branch2_tree.commit(message='update file')
180
181
        return branch1_tree, branch2_tree
181
182
 
182
183
    def check_b2_vs_b1(self, cmd):
183
184
        # Compare branch2 vs branch1 using cmd and check the result
184
185
        out, err = self.run_bzr(cmd, retcode=1)
185
 
        self.assertEquals('', err)
186
 
        self.assertEquals("=== modified file 'file'\n"
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))
 
186
        self.assertEqual('', err)
 
187
        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))
193
194
 
194
195
    def check_b1_vs_b2(self, cmd):
195
196
        # Compare branch1 vs branch2 using cmd and check the result
196
197
        out, err = self.run_bzr(cmd, retcode=1)
197
 
        self.assertEquals('', err)
 
198
        self.assertEqual('', err)
198
199
        self.assertEqualDiff("=== modified file 'file'\n"
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))
 
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))
205
206
 
206
207
    def check_no_diffs(self, cmd):
207
208
        # Check that running cmd returns an empty diff
208
209
        out, err = self.run_bzr(cmd, retcode=0)
209
 
        self.assertEquals('', err)
210
 
        self.assertEquals('', out)
 
210
        self.assertEqual('', err)
 
211
        self.assertEqual('', out)
211
212
 
212
213
    def test_diff_branches(self):
213
214
        self.example_branches()
229
230
    def test_diff_branches_no_working_trees(self):
230
231
        branch1_tree, branch2_tree = self.example_branches()
231
232
        # Compare a working tree to a branch without a WT
232
 
        dir1 = branch1_tree.bzrdir
 
233
        dir1 = branch1_tree.controldir
233
234
        dir1.destroy_workingtree()
234
235
        self.assertFalse(dir1.has_workingtree())
235
236
        self.check_b2_vs_b1('diff --old branch2 --new branch1')
240
241
        self.check_b1_vs_b2('diff --old branch1 branch2')
241
242
        self.check_b1_vs_b2('diff branch1 --new branch2')
242
243
        # Compare a branch with a WT against another without a WT
243
 
        dir2 = branch2_tree.bzrdir
 
244
        dir2 = branch2_tree.controldir
244
245
        dir2.destroy_workingtree()
245
246
        self.assertFalse(dir2.has_workingtree())
246
247
        self.check_b1_vs_b2('diff --old branch1 --new branch2')
250
251
    def test_diff_revno_branches(self):
251
252
        self.example_branches()
252
253
        branch2_tree = workingtree.WorkingTree.open_containing('branch2')[0]
253
 
        self.build_tree_contents([('branch2/file', 'even newer content')])
 
254
        self.build_tree_contents([('branch2/file', b'even newer content')])
254
255
        branch2_tree.commit(message='update file once more')
255
256
 
256
257
        out, err = self.run_bzr('diff -r revno:1:branch2..revno:1:branch1',
257
258
                                )
258
 
        self.assertEquals('', err)
259
 
        self.assertEquals('', out)
 
259
        self.assertEqual('', err)
 
260
        self.assertEqual('', out)
260
261
        out, err = self.run_bzr('diff -r revno:2:branch2..revno:1:branch1',
261
262
                                retcode=1)
262
 
        self.assertEquals('', err)
 
263
        self.assertEqual('', err)
263
264
        self.assertEqualDiff("=== modified file 'file'\n"
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))
 
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))
270
293
 
271
294
    def example_branch2(self):
272
295
        branch1_tree = self.make_branch_and_tree('branch1')
273
 
        self.build_tree_contents([('branch1/file1', 'original line\n')])
 
296
        self.build_tree_contents([('branch1/file1', b'original line\n')])
274
297
        branch1_tree.add('file1')
275
298
        branch1_tree.commit(message='first commit')
276
 
        self.build_tree_contents([('branch1/file1', 'repo line\n')])
 
299
        self.build_tree_contents([('branch1/file1', b'repo line\n')])
277
300
        branch1_tree.commit(message='second commit')
278
301
        return branch1_tree
279
302
 
280
303
    def test_diff_to_working_tree(self):
281
304
        self.example_branch2()
282
 
        self.build_tree_contents([('branch1/file1', 'new line')])
 
305
        self.build_tree_contents([('branch1/file1', b'new line')])
283
306
        output = self.run_bzr('diff -r 1.. branch1', retcode=1)
284
307
        self.assertContainsRe(output[0], '\n\\-original line\n\\+new line\n')
285
308
 
286
309
    def test_diff_to_working_tree_in_subdir(self):
287
310
        self.example_branch2()
288
 
        self.build_tree_contents([('branch1/file1', 'new line')])
 
311
        self.build_tree_contents([('branch1/file1', b'new line')])
289
312
        os.mkdir('branch1/dir1')
290
313
        output = self.run_bzr('diff -r 1..', retcode=1,
291
314
                              working_dir='branch1/dir1')
301
324
 
302
325
    def test_diff_to_branch_no_working_tree(self):
303
326
        branch1_tree = self.example_branch2()
304
 
        dir1 = branch1_tree.bzrdir
 
327
        dir1 = branch1_tree.controldir
305
328
        dir1.destroy_workingtree()
306
329
        self.assertFalse(dir1.has_workingtree())
307
330
        output = self.run_bzr('diff -r 1.. branch1', retcode=1)
313
336
            def show_diff(self, specific_files, extra_trees=None):
314
337
                self.to_file.write("BOO!\n")
315
338
                return super(BooDiffTree, self).show_diff(specific_files,
316
 
                    extra_trees)
 
339
                                                          extra_trees)
317
340
 
318
341
        diff_format_registry.register("boo", BooDiffTree, "Scary diff format")
319
342
        self.addCleanup(diff_format_registry.remove, "boo")
320
343
        self.make_example_branch()
321
 
        self.build_tree_contents([('hello', 'hello world!\n')])
 
344
        self.build_tree_contents([('hello', b'hello world!\n')])
322
345
        output = self.run_bzr('diff --format=boo', retcode=1)
323
346
        self.assertTrue("BOO!" in output[0])
324
347
        output = self.run_bzr('diff -Fboo', retcode=1)
325
348
        self.assertTrue("BOO!" in output[0])
326
349
 
 
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
    def test_moved_away(self):
 
362
        # pad.lv/1880354
 
363
        tree = self.make_branch_and_tree('.')
 
364
        self.build_tree_contents([('a', 'asdf\n')])
 
365
        tree.add(['a'])
 
366
        tree.commit('add a')
 
367
        tree.rename_one('a', 'b')
 
368
        self.build_tree_contents([('a', 'qwer\n')])
 
369
        tree.add('a')
 
370
        output, error = self.run_bzr('diff -p0', retcode=1)
 
371
        self.assertEqualDiff("""\
 
372
=== added file 'a'
 
373
--- a\tYYYY-MM-DD HH:MM:SS +ZZZZ
 
374
+++ a\tYYYY-MM-DD HH:MM:SS +ZZZZ
 
375
@@ -0,0 +1,1 @@
 
376
+qwer
 
377
 
 
378
=== renamed file 'a' => 'b'
 
379
""", subst_dates(output))
 
380
 
327
381
 
328
382
class TestCheckoutDiff(TestDiff):
329
383
 
360
414
 
361
415
    def test_diff_label_added(self):
362
416
        tree = super(TestDiffLabels, self).make_example_branch()
363
 
        self.build_tree_contents([('barbar', 'barbar')])
 
417
        self.build_tree_contents([('barbar', b'barbar')])
364
418
        tree.add('barbar')
365
419
        diff = self.run_bzr('diff', retcode=1)
366
420
        self.assertTrue("=== added file 'barbar'" in diff[0])
367
421
 
368
422
    def test_diff_label_modified(self):
369
423
        super(TestDiffLabels, self).make_example_branch()
370
 
        self.build_tree_contents([('hello', 'barbar')])
 
424
        self.build_tree_contents([('hello', b'barbar')])
371
425
        diff = self.run_bzr('diff', retcode=1)
372
426
        self.assertTrue("=== modified file 'hello'" in diff[0])
373
427
 
392
446
            'diff -Oprogress_bar=none -r 1 --diff-options -ub',
393
447
            universal_newlines=True,
394
448
            retcode=None)
395
 
        if 'Diff is not installed on this machine' in err:
 
449
        if b'Diff is not installed on this machine' in err:
396
450
            raise tests.TestSkipped("No external 'diff' is available")
397
 
        self.assertEqual('', err)
 
451
        self.assertEqual(b'', err)
398
452
        # We have to skip the stuff in the middle, because it depends
399
453
        # on time.time()
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")
 
454
        self.assertStartsWith(
 
455
            out,
 
456
            b"=== added file 'goodbye'\n"
 
457
            b"--- old/goodbye\t1970-01-01 00:00:00 +0000\n"
 
458
            b"+++ new/goodbye\t")
 
459
        self.assertEndsWith(out, b"\n@@ -0,0 +1 @@\n"
 
460
                                 b"+baz\n\n")
405
461
 
406
462
    def test_external_diff_options_and_using(self):
407
463
        """Test that the options are passed correctly to an external diff process"""
408
464
        self.requireFeature(features.diff_feature)
409
465
        self.make_example_branch()
410
 
        self.build_tree_contents([('hello', 'Foo\n')])
 
466
        self.build_tree_contents([('hello', b'Foo\n')])
411
467
        out, err = self.run_bzr('diff --diff-options -i --using diff',
412
 
                                    retcode=1)
413
 
        self.assertEquals("=== modified file 'hello'\n", out)
414
 
        self.assertEquals('', err)
 
468
                                retcode=1)
 
469
        self.assertEqual("=== modified file 'hello'\n", out)
 
470
        self.assertEqual('', err)
415
471
 
416
472
 
417
473
class TestDiffOutput(DiffBase):
419
475
    def test_diff_output(self):
420
476
        # check that output doesn't mangle line-endings
421
477
        self.make_example_branch()
422
 
        self.build_tree_contents([('hello', 'hello world!\n')])
 
478
        self.build_tree_contents([('hello', b'hello world!\n')])
423
479
        output = self.run_bzr_subprocess('diff', retcode=1)[0]
424
 
        self.assert_('\n+hello world!\n' in output)
 
480
        self.assertTrue(b'\n+hello world!\n' in output)