/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
6614.1.1 by Vincent Ladeuil
Fix assert_ being deprecated by using assertTrue.
1
# Copyright (C) 2006-2012, 2016 Canonical Ltd
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
2
#
1185.50.44 by John Arbash Meinel
[patch] Robey Pointer: diff -r 1.. should diff against working tree.
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
7
#
1185.50.44 by John Arbash Meinel
[patch] Robey Pointer: diff -r 1.. should diff against working tree.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
12
#
1185.50.44 by John Arbash Meinel
[patch] Robey Pointer: diff -r 1.. should diff against working tree.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
1185.50.44 by John Arbash Meinel
[patch] Robey Pointer: diff -r 1.. should diff against working tree.
16
17
6622.1.29 by Jelmer Vernooij
Fix some more tests.
18
"""Black-box tests for brz diff."""
1185.50.44 by John Arbash Meinel
[patch] Robey Pointer: diff -r 1.. should diff against working tree.
19
20
import os
1740.2.5 by Aaron Bentley
Merge from bzr.dev
21
import re
1185.50.44 by John Arbash Meinel
[patch] Robey Pointer: diff -r 1.. should diff against working tree.
22
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
23
from breezy import (
4695.3.1 by Vincent Ladeuil
Fix test failures with no C extensions loaded.
24
    tests,
25
    workingtree,
26
    )
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
27
from breezy.diff import (
5131.1.4 by Jelmer Vernooij
Add test for custom diff format.
28
    DiffTree,
29
    format_registry as diff_format_registry,
30
    )
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
31
from breezy.tests import (
5349.1.5 by Matthäus G. Chajdas
Fix issues raised by Vincent Ladeuil.
32
    features,
33
    )
1185.50.44 by John Arbash Meinel
[patch] Robey Pointer: diff -r 1.. should diff against working tree.
34
35
1740.2.5 by Aaron Bentley
Merge from bzr.dev
36
def subst_dates(string):
37
    """Replace date strings with constant values."""
38
    return re.sub(r'\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2} [-\+]\d{4}',
39
                  'YYYY-MM-DD HH:MM:SS +ZZZZ', string)
40
41
4695.3.1 by Vincent Ladeuil
Fix test failures with no C extensions loaded.
42
class DiffBase(tests.TestCaseWithTransport):
2172.2.1 by Alexander Belchenko
Remove unnecessary duplication of tests in blackbox/test_diff.py
43
    """Base class with common setup method"""
1658.1.9 by Martin Pool
Give an error for bzr diff on an nonexistent file (Malone #3619)
44
1899.1.2 by John Arbash Meinel
Add a test that we can use an external diff program.
45
    def make_example_branch(self):
46
        tree = self.make_branch_and_tree('.')
2664.10.1 by Daniel Watkins
tests.blackbox.test_diff now uses internals where appropriate.
47
        self.build_tree_contents([
6855.4.1 by Jelmer Vernooij
Yet more bees.
48
            ('hello', b'foo\n'),
49
            ('goodbye', b'baz\n')])
1899.1.2 by John Arbash Meinel
Add a test that we can use an external diff program.
50
        tree.add(['hello'])
51
        tree.commit('setup')
52
        tree.add(['goodbye'])
53
        tree.commit('setup')
2664.10.1 by Daniel Watkins
tests.blackbox.test_diff now uses internals where appropriate.
54
        return tree
1185.50.44 by John Arbash Meinel
[patch] Robey Pointer: diff -r 1.. should diff against working tree.
55
2172.2.1 by Alexander Belchenko
Remove unnecessary duplication of tests in blackbox/test_diff.py
56
57
class TestDiff(DiffBase):
58
1185.50.44 by John Arbash Meinel
[patch] Robey Pointer: diff -r 1.. should diff against working tree.
59
    def test_diff(self):
2738.1.2 by Daniel Watkins
Tests now run on both branches and checkouts.
60
        tree = self.make_example_branch()
6855.4.1 by Jelmer Vernooij
Yet more bees.
61
        self.build_tree_contents([('hello', b'hello world!')])
2664.10.1 by Daniel Watkins
tests.blackbox.test_diff now uses internals where appropriate.
62
        tree.commit(message='fixing hello')
2530.3.4 by Martin Pool
Deprecate run_bzr_captured in favour of just run_bzr
63
        output = self.run_bzr('diff -r 2..3', retcode=1)[0]
6614.1.1 by Vincent Ladeuil
Fix assert_ being deprecated by using assertTrue.
64
        self.assertTrue('\n+hello world!' in output)
2745.4.3 by Lukáš Lalinsky
Change -C to -c.
65
        output = self.run_bzr('diff -c 3', retcode=1)[0]
6614.1.1 by Vincent Ladeuil
Fix assert_ being deprecated by using assertTrue.
66
        self.assertTrue('\n+hello world!' in output)
2745.4.1 by Lukáš Lalinsky
New option -C/--change for diff and status to show changes in one revision. (#56299)
67
        output = self.run_bzr('diff -r last:3..last:1', retcode=1)[0]
6614.1.1 by Vincent Ladeuil
Fix assert_ being deprecated by using assertTrue.
68
        self.assertTrue('\n+baz' in output)
2745.4.3 by Lukáš Lalinsky
Change -C to -c.
69
        output = self.run_bzr('diff -c last:2', retcode=1)[0]
6614.1.1 by Vincent Ladeuil
Fix assert_ being deprecated by using assertTrue.
70
        self.assertTrue('\n+baz' in output)
2530.3.1 by Martin Pool
Cleanup old variations on run_bzr in the test suite
71
        self.build_tree(['moo'])
2664.10.1 by Daniel Watkins
tests.blackbox.test_diff now uses internals where appropriate.
72
        tree.add('moo')
1185.50.44 by John Arbash Meinel
[patch] Robey Pointer: diff -r 1.. should diff against working tree.
73
        os.unlink('moo')
2530.3.1 by Martin Pool
Cleanup old variations on run_bzr in the test suite
74
        self.run_bzr('diff')
1185.50.44 by John Arbash Meinel
[patch] Robey Pointer: diff -r 1.. should diff against working tree.
75
1694.2.2 by Martin Pool
Add test for diff --diff-prefix, which was previously untested.
76
    def test_diff_prefix(self):
1694.2.3 by Martin Pool
Add -p0, -p1 options for diff.
77
        """diff --prefix appends to filenames in output"""
78
        self.make_example_branch()
6855.4.1 by Jelmer Vernooij
Yet more bees.
79
        self.build_tree_contents([('hello', b'hello world!\n')])
2530.3.1 by Martin Pool
Cleanup old variations on run_bzr in the test suite
80
        out, err = self.run_bzr('diff --prefix old/:new/', retcode=1)
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
81
        self.assertEqual(err, '')
1740.2.5 by Aaron Bentley
Merge from bzr.dev
82
        self.assertEqualDiff(subst_dates(out), '''\
1694.2.4 by Martin Pool
When a diff prefix is given, don't show it in === summary lines, only on the diffs themselves.
83
=== modified file 'hello'
1740.2.5 by Aaron Bentley
Merge from bzr.dev
84
--- old/hello\tYYYY-MM-DD HH:MM:SS +ZZZZ
85
+++ new/hello\tYYYY-MM-DD HH:MM:SS +ZZZZ
1694.2.3 by Martin Pool
Add -p0, -p1 options for diff.
86
@@ -1,1 +1,1 @@
87
-foo
88
+hello world!
89
90
''')
91
2324.1.2 by Dmitry Vasiliev
Added test for bzr diff --prefix illegal_value
92
    def test_diff_illegal_prefix_value(self):
93
        # There was an error in error reporting for this option
2530.3.1 by Martin Pool
Cleanup old variations on run_bzr in the test suite
94
        out, err = self.run_bzr('diff --prefix old/', retcode=3)
2324.1.2 by Dmitry Vasiliev
Added test for bzr diff --prefix illegal_value
95
        self.assertContainsRe(err,
7143.15.2 by Jelmer Vernooij
Run autopep8.
96
                              '--prefix expects two values separated by a colon')
2324.1.2 by Dmitry Vasiliev
Added test for bzr diff --prefix illegal_value
97
1694.2.3 by Martin Pool
Add -p0, -p1 options for diff.
98
    def test_diff_p1(self):
99
        """diff -p1 produces lkml-style diffs"""
100
        self.make_example_branch()
6855.4.1 by Jelmer Vernooij
Yet more bees.
101
        self.build_tree_contents([('hello', b'hello world!\n')])
2530.3.1 by Martin Pool
Cleanup old variations on run_bzr in the test suite
102
        out, err = self.run_bzr('diff -p1', retcode=1)
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
103
        self.assertEqual(err, '')
1740.2.5 by Aaron Bentley
Merge from bzr.dev
104
        self.assertEqualDiff(subst_dates(out), '''\
1694.2.4 by Martin Pool
When a diff prefix is given, don't show it in === summary lines, only on the diffs themselves.
105
=== modified file 'hello'
1740.2.5 by Aaron Bentley
Merge from bzr.dev
106
--- old/hello\tYYYY-MM-DD HH:MM:SS +ZZZZ
107
+++ new/hello\tYYYY-MM-DD HH:MM:SS +ZZZZ
1694.2.3 by Martin Pool
Add -p0, -p1 options for diff.
108
@@ -1,1 +1,1 @@
109
-foo
110
+hello world!
111
112
''')
113
114
    def test_diff_p0(self):
115
        """diff -p0 produces diffs with no prefix"""
116
        self.make_example_branch()
6855.4.1 by Jelmer Vernooij
Yet more bees.
117
        self.build_tree_contents([('hello', b'hello world!\n')])
2530.3.1 by Martin Pool
Cleanup old variations on run_bzr in the test suite
118
        out, err = self.run_bzr('diff -p0', retcode=1)
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
119
        self.assertEqual(err, '')
1740.2.5 by Aaron Bentley
Merge from bzr.dev
120
        self.assertEqualDiff(subst_dates(out), '''\
1694.2.3 by Martin Pool
Add -p0, -p1 options for diff.
121
=== modified file 'hello'
1740.2.5 by Aaron Bentley
Merge from bzr.dev
122
--- hello\tYYYY-MM-DD HH:MM:SS +ZZZZ
123
+++ hello\tYYYY-MM-DD HH:MM:SS +ZZZZ
1694.2.2 by Martin Pool
Add test for diff --diff-prefix, which was previously untested.
124
@@ -1,1 +1,1 @@
125
-foo
126
+hello world!
127
128
''')
129
1658.1.9 by Martin Pool
Give an error for bzr diff on an nonexistent file (Malone #3619)
130
    def test_diff_nonexistent(self):
131
        # Get an error from a file that does not exist at all
132
        # (Malone #3619)
133
        self.make_example_branch()
3872.2.1 by Marius Kruger
add tests for diff with non-existing revno's
134
        out, err = self.run_bzr('diff does-not-exist', retcode=3,
7143.15.2 by Jelmer Vernooij
Run autopep8.
135
                                error_regexes=('not versioned.*does-not-exist',))
1658.1.9 by Martin Pool
Give an error for bzr diff on an nonexistent file (Malone #3619)
136
2197.2.1 by Martin Pool
Refactor cmd_diff
137
    def test_diff_illegal_revision_specifiers(self):
3872.2.1 by Marius Kruger
add tests for diff with non-existing revno's
138
        out, err = self.run_bzr('diff -r 1..23..123', retcode=3,
7143.15.2 by Jelmer Vernooij
Run autopep8.
139
                                error_regexes=('one or two revision specifiers',))
3872.2.1 by Marius Kruger
add tests for diff with non-existing revno's
140
5131.1.2 by Jelmer Vernooij
Refuse both --using and --format to 'bzr diff'.
141
    def test_diff_using_and_format(self):
142
        out, err = self.run_bzr('diff --format=default --using=mydi', retcode=3,
7143.15.2 by Jelmer Vernooij
Run autopep8.
143
                                error_regexes=('are mutually exclusive',))
5131.1.2 by Jelmer Vernooij
Refuse both --using and --format to 'bzr diff'.
144
3872.2.1 by Marius Kruger
add tests for diff with non-existing revno's
145
    def test_diff_nonexistent_revision(self):
146
        out, err = self.run_bzr('diff -r 123', retcode=3,
7143.15.2 by Jelmer Vernooij
Run autopep8.
147
                                error_regexes=("Requested revision: '123' does not "
148
                                               "exist in branch:",))
3872.2.1 by Marius Kruger
add tests for diff with non-existing revno's
149
150
    def test_diff_nonexistent_dotted_revision(self):
151
        out, err = self.run_bzr('diff -r 1.1', retcode=3)
3872.2.3 by Marius Kruger
since the -c case seems to be fixed, add a test for it
152
        self.assertContainsRe(err,
7143.15.2 by Jelmer Vernooij
Run autopep8.
153
                              "Requested revision: '1.1' does not exist in branch:")
3872.2.3 by Marius Kruger
since the -c case seems to be fixed, add a test for it
154
155
    def test_diff_nonexistent_dotted_revision_change(self):
156
        out, err = self.run_bzr('diff -c 1.1', retcode=3)
3872.2.1 by Marius Kruger
add tests for diff with non-existing revno's
157
        self.assertContainsRe(err,
7143.15.2 by Jelmer Vernooij
Run autopep8.
158
                              "Requested revision: '1.1' does not exist in branch:")
2197.2.1 by Martin Pool
Refactor cmd_diff
159
1658.1.10 by Martin Pool
diff on unversiond files should give an error (Malone #3619)
160
    def test_diff_unversioned(self):
161
        # Get an error when diffing a non-versioned file.
162
        # (Malone #3619)
163
        self.make_example_branch()
164
        self.build_tree(['unversioned-file'])
2530.3.1 by Martin Pool
Cleanup old variations on run_bzr in the test suite
165
        out, err = self.run_bzr('diff unversioned-file', retcode=3)
7027.4.1 by Jelmer Vernooij
Use StringIOWithEncoding on Python3.
166
        self.assertContainsRe(err, 'not versioned.*unversioned-file')
1658.1.10 by Martin Pool
diff on unversiond files should give an error (Malone #3619)
167
168
    # TODO: What should diff say for a file deleted in working tree?
1658.1.9 by Martin Pool
Give an error for bzr diff on an nonexistent file (Malone #3619)
169
1551.2.13 by Aaron Bentley
Got diff working properly with checkouts
170
    def example_branches(self):
2664.10.1 by Daniel Watkins
tests.blackbox.test_diff now uses internals where appropriate.
171
        branch1_tree = self.make_branch_and_tree('branch1')
172
        self.build_tree(['branch1/file'], line_endings='binary')
3072.1.1 by Ian Clatworthy
Improved diff based on feedback from abentley
173
        self.build_tree(['branch1/file2'], line_endings='binary')
2664.10.1 by Daniel Watkins
tests.blackbox.test_diff now uses internals where appropriate.
174
        branch1_tree.add('file')
3072.1.1 by Ian Clatworthy
Improved diff based on feedback from abentley
175
        branch1_tree.add('file2')
176
        branch1_tree.commit(message='add file and file2')
7143.15.2 by Jelmer Vernooij
Run autopep8.
177
        branch2_tree = branch1_tree.controldir.sprout(
178
            'branch2').open_workingtree()
6855.4.1 by Jelmer Vernooij
Yet more bees.
179
        self.build_tree_contents([('branch2/file', b'new content\n')])
2664.10.1 by Daniel Watkins
tests.blackbox.test_diff now uses internals where appropriate.
180
        branch2_tree.commit(message='update file')
181
        return branch1_tree, branch2_tree
1551.2.13 by Aaron Bentley
Got diff working properly with checkouts
182
3072.1.2 by Ian Clatworthy
Test various --old and --new combinations
183
    def check_b2_vs_b1(self, cmd):
184
        # Compare branch2 vs branch1 using cmd and check the result
185
        out, err = self.run_bzr(cmd, retcode=1)
7027.4.1 by Jelmer Vernooij
Use StringIOWithEncoding on Python3.
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))
3072.1.2 by Ian Clatworthy
Test various --old and --new combinations
194
3072.1.3 by Ian Clatworthy
Test more --old and --new combinations
195
    def check_b1_vs_b2(self, cmd):
196
        # Compare branch1 vs branch2 using cmd and check the result
197
        out, err = self.run_bzr(cmd, retcode=1)
7027.4.1 by Jelmer Vernooij
Use StringIOWithEncoding on Python3.
198
        self.assertEqual('', err)
199
        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))
3072.1.3 by Ian Clatworthy
Test more --old and --new combinations
206
3072.1.2 by Ian Clatworthy
Test various --old and --new combinations
207
    def check_no_diffs(self, cmd):
208
        # Check that running cmd returns an empty diff
209
        out, err = self.run_bzr(cmd, retcode=0)
7027.4.1 by Jelmer Vernooij
Use StringIOWithEncoding on Python3.
210
        self.assertEqual('', err)
211
        self.assertEqual('', out)
3072.1.2 by Ian Clatworthy
Test various --old and --new combinations
212
213
    def test_diff_branches(self):
214
        self.example_branches()
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
215
        # should open branch1 and diff against branch2,
3072.1.2 by Ian Clatworthy
Test various --old and --new combinations
216
        self.check_b2_vs_b1('diff -r branch:branch2 branch1')
217
        # Compare two working trees using various syntax forms
218
        self.check_b2_vs_b1('diff --old branch2 --new branch1')
219
        self.check_b2_vs_b1('diff --old branch2 branch1')
220
        self.check_b2_vs_b1('diff branch2 --new branch1')
3072.1.1 by Ian Clatworthy
Improved diff based on feedback from abentley
221
        # Test with a selected file that was changed
3072.1.2 by Ian Clatworthy
Test various --old and --new combinations
222
        self.check_b2_vs_b1('diff --old branch2 --new branch1 file')
223
        self.check_b2_vs_b1('diff --old branch2 branch1/file')
224
        self.check_b2_vs_b1('diff branch2/file --new branch1')
3072.1.1 by Ian Clatworthy
Improved diff based on feedback from abentley
225
        # Test with a selected file that was not changed
3072.1.2 by Ian Clatworthy
Test various --old and --new combinations
226
        self.check_no_diffs('diff --old branch2 --new branch1 file2')
227
        self.check_no_diffs('diff --old branch2 branch1/file2')
228
        self.check_no_diffs('diff branch2/file2 --new branch1')
3072.1.1 by Ian Clatworthy
Improved diff based on feedback from abentley
229
230
    def test_diff_branches_no_working_trees(self):
231
        branch1_tree, branch2_tree = self.example_branches()
232
        # Compare a working tree to a branch without a WT
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
233
        dir1 = branch1_tree.controldir
3072.1.1 by Ian Clatworthy
Improved diff based on feedback from abentley
234
        dir1.destroy_workingtree()
235
        self.assertFalse(dir1.has_workingtree())
3072.1.3 by Ian Clatworthy
Test more --old and --new combinations
236
        self.check_b2_vs_b1('diff --old branch2 --new branch1')
237
        self.check_b2_vs_b1('diff --old branch2 branch1')
238
        self.check_b2_vs_b1('diff branch2 --new branch1')
3072.1.1 by Ian Clatworthy
Improved diff based on feedback from abentley
239
        # Compare a branch without a WT to one with a WT
3072.1.3 by Ian Clatworthy
Test more --old and --new combinations
240
        self.check_b1_vs_b2('diff --old branch1 --new branch2')
241
        self.check_b1_vs_b2('diff --old branch1 branch2')
242
        self.check_b1_vs_b2('diff branch1 --new branch2')
3072.1.1 by Ian Clatworthy
Improved diff based on feedback from abentley
243
        # Compare a branch with a WT against another without a WT
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
244
        dir2 = branch2_tree.controldir
3072.1.1 by Ian Clatworthy
Improved diff based on feedback from abentley
245
        dir2.destroy_workingtree()
246
        self.assertFalse(dir2.has_workingtree())
3072.1.3 by Ian Clatworthy
Test more --old and --new combinations
247
        self.check_b1_vs_b2('diff --old branch1 --new branch2')
248
        self.check_b1_vs_b2('diff --old branch1 branch2')
249
        self.check_b1_vs_b2('diff branch1 --new branch2')
1185.50.44 by John Arbash Meinel
[patch] Robey Pointer: diff -r 1.. should diff against working tree.
250
1732.3.3 by Matthieu Moy
Testcases for revno:N:path
251
    def test_diff_revno_branches(self):
252
        self.example_branches()
2664.10.1 by Daniel Watkins
tests.blackbox.test_diff now uses internals where appropriate.
253
        branch2_tree = workingtree.WorkingTree.open_containing('branch2')[0]
6855.4.1 by Jelmer Vernooij
Yet more bees.
254
        self.build_tree_contents([('branch2/file', b'even newer content')])
2664.10.1 by Daniel Watkins
tests.blackbox.test_diff now uses internals where appropriate.
255
        branch2_tree.commit(message='update file once more')
1732.3.3 by Matthieu Moy
Testcases for revno:N:path
256
2552.2.2 by Vincent Ladeuil
Enforce run_bzr(string) where possible.
257
        out, err = self.run_bzr('diff -r revno:1:branch2..revno:1:branch1',
2581.1.2 by Martin Pool
Remove unnecessary retcode=0 to run_bzr calls
258
                                )
7027.4.1 by Jelmer Vernooij
Use StringIOWithEncoding on Python3.
259
        self.assertEqual('', err)
260
        self.assertEqual('', out)
2552.2.2 by Vincent Ladeuil
Enforce run_bzr(string) where possible.
261
        out, err = self.run_bzr('diff -r revno:2:branch2..revno:1:branch1',
262
                                retcode=1)
7027.4.1 by Jelmer Vernooij
Use StringIOWithEncoding on Python3.
263
        self.assertEqual('', err)
264
        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))
1732.3.3 by Matthieu Moy
Testcases for revno:N:path
271
7406.4.5 by Jelmer Vernooij
Add a test.
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)
7406.4.7 by Jelmer Vernooij
Fix test.
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))
7406.4.5 by Jelmer Vernooij
Add a test.
293
1551.2.13 by Aaron Bentley
Got diff working properly with checkouts
294
    def example_branch2(self):
2664.10.1 by Daniel Watkins
tests.blackbox.test_diff now uses internals where appropriate.
295
        branch1_tree = self.make_branch_and_tree('branch1')
6855.4.1 by Jelmer Vernooij
Yet more bees.
296
        self.build_tree_contents([('branch1/file1', b'original line\n')])
2664.10.1 by Daniel Watkins
tests.blackbox.test_diff now uses internals where appropriate.
297
        branch1_tree.add('file1')
298
        branch1_tree.commit(message='first commit')
6855.4.1 by Jelmer Vernooij
Yet more bees.
299
        self.build_tree_contents([('branch1/file1', b'repo line\n')])
2664.10.1 by Daniel Watkins
tests.blackbox.test_diff now uses internals where appropriate.
300
        branch1_tree.commit(message='second commit')
301
        return branch1_tree
1551.2.13 by Aaron Bentley
Got diff working properly with checkouts
302
303
    def test_diff_to_working_tree(self):
304
        self.example_branch2()
6855.4.1 by Jelmer Vernooij
Yet more bees.
305
        self.build_tree_contents([('branch1/file1', b'new line')])
2664.10.1 by Daniel Watkins
tests.blackbox.test_diff now uses internals where appropriate.
306
        output = self.run_bzr('diff -r 1.. branch1', retcode=1)
2598.5.12 by Aaron Bentley
Merge bzr.dev
307
        self.assertContainsRe(output[0], '\n\\-original line\n\\+new line\n')
1551.2.13 by Aaron Bentley
Got diff working properly with checkouts
308
3164.1.1 by Ian Clatworthy
diff without arguments means the current tree, not the current directory
309
    def test_diff_to_working_tree_in_subdir(self):
310
        self.example_branch2()
6855.4.1 by Jelmer Vernooij
Yet more bees.
311
        self.build_tree_contents([('branch1/file1', b'new line')])
3164.1.1 by Ian Clatworthy
diff without arguments means the current tree, not the current directory
312
        os.mkdir('branch1/dir1')
6423.1.1 by Vincent Ladeuil
Cleanup old blackbox tests and then some. Remove os.chdir() calls, caught a few bugs, make sure we don't leave file handles opened.
313
        output = self.run_bzr('diff -r 1..', retcode=1,
314
                              working_dir='branch1/dir1')
3164.1.1 by Ian Clatworthy
diff without arguments means the current tree, not the current directory
315
        self.assertContainsRe(output[0], '\n\\-original line\n\\+new line\n')
316
1551.7.19 by Aaron Bentley
Always include working tree when calculating file ids for diff
317
    def test_diff_across_rename(self):
318
        """The working tree path should always be considered for diffing"""
2738.1.2 by Daniel Watkins
Tests now run on both branches and checkouts.
319
        tree = self.make_example_branch()
2552.2.2 by Vincent Ladeuil
Enforce run_bzr(string) where possible.
320
        self.run_bzr('diff -r 0..1 hello', retcode=1)
2664.10.1 by Daniel Watkins
tests.blackbox.test_diff now uses internals where appropriate.
321
        tree.rename_one('hello', 'hello1')
2552.2.2 by Vincent Ladeuil
Enforce run_bzr(string) where possible.
322
        self.run_bzr('diff hello1', retcode=1)
323
        self.run_bzr('diff -r 0..1 hello1', retcode=1)
1551.7.19 by Aaron Bentley
Always include working tree when calculating file ids for diff
324
3072.1.1 by Ian Clatworthy
Improved diff based on feedback from abentley
325
    def test_diff_to_branch_no_working_tree(self):
326
        branch1_tree = self.example_branch2()
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
327
        dir1 = branch1_tree.controldir
3072.1.1 by Ian Clatworthy
Improved diff based on feedback from abentley
328
        dir1.destroy_workingtree()
329
        self.assertFalse(dir1.has_workingtree())
330
        output = self.run_bzr('diff -r 1.. branch1', retcode=1)
331
        self.assertContainsRe(output[0], '\n\\-original line\n\\+repo line\n')
332
5131.1.4 by Jelmer Vernooij
Add test for custom diff format.
333
    def test_custom_format(self):
334
        class BooDiffTree(DiffTree):
335
336
            def show_diff(self, specific_files, extra_trees=None):
337
                self.to_file.write("BOO!\n")
338
                return super(BooDiffTree, self).show_diff(specific_files,
7143.15.2 by Jelmer Vernooij
Run autopep8.
339
                                                          extra_trees)
5131.1.4 by Jelmer Vernooij
Add test for custom diff format.
340
6423.1.1 by Vincent Ladeuil
Cleanup old blackbox tests and then some. Remove os.chdir() calls, caught a few bugs, make sure we don't leave file handles opened.
341
        diff_format_registry.register("boo", BooDiffTree, "Scary diff format")
5131.1.4 by Jelmer Vernooij
Add test for custom diff format.
342
        self.addCleanup(diff_format_registry.remove, "boo")
343
        self.make_example_branch()
6855.4.1 by Jelmer Vernooij
Yet more bees.
344
        self.build_tree_contents([('hello', b'hello world!\n')])
5131.1.4 by Jelmer Vernooij
Add test for custom diff format.
345
        output = self.run_bzr('diff --format=boo', retcode=1)
346
        self.assertTrue("BOO!" in output[0])
5574.9.5 by Jelmer Vernooij
Fix test.
347
        output = self.run_bzr('diff -Fboo', retcode=1)
5574.9.2 by Jelmer Vernooij
Add test.
348
        self.assertTrue("BOO!" in output[0])
349
7358.8.1 by Jelmer Vernooij
Don't include datestamps in filenames when reporting on binary files.
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(
7358.8.5 by Jelmer Vernooij
Fix tests.
358
            "=== removed file 'a'\nBinary files old/a and new/a differ\n",
7358.8.1 by Jelmer Vernooij
Don't include datestamps in filenames when reporting on binary files.
359
            output[0])
360
7490.35.1 by Jelmer Vernooij
Add test for bug 1880354.
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
5131.1.4 by Jelmer Vernooij
Add test for custom diff format.
381
1551.2.13 by Aaron Bentley
Got diff working properly with checkouts
382
class TestCheckoutDiff(TestDiff):
383
1658.1.9 by Martin Pool
Give an error for bzr diff on an nonexistent file (Malone #3619)
384
    def make_example_branch(self):
2664.10.1 by Daniel Watkins
tests.blackbox.test_diff now uses internals where appropriate.
385
        tree = super(TestCheckoutDiff, self).make_example_branch()
2738.1.2 by Daniel Watkins
Tests now run on both branches and checkouts.
386
        tree = tree.branch.create_checkout('checkout')
1551.2.13 by Aaron Bentley
Got diff working properly with checkouts
387
        os.chdir('checkout')
2738.1.2 by Daniel Watkins
Tests now run on both branches and checkouts.
388
        return tree
1551.2.13 by Aaron Bentley
Got diff working properly with checkouts
389
390
    def example_branch2(self):
2664.10.1 by Daniel Watkins
tests.blackbox.test_diff now uses internals where appropriate.
391
        tree = super(TestCheckoutDiff, self).example_branch2()
1551.2.13 by Aaron Bentley
Got diff working properly with checkouts
392
        os.mkdir('checkouts')
2738.1.2 by Daniel Watkins
Tests now run on both branches and checkouts.
393
        tree = tree.branch.create_checkout('checkouts/branch1')
1551.2.13 by Aaron Bentley
Got diff working properly with checkouts
394
        os.chdir('checkouts')
2738.1.2 by Daniel Watkins
Tests now run on both branches and checkouts.
395
        return tree
1551.2.13 by Aaron Bentley
Got diff working properly with checkouts
396
397
    def example_branches(self):
6423.1.1 by Vincent Ladeuil
Cleanup old blackbox tests and then some. Remove os.chdir() calls, caught a few bugs, make sure we don't leave file handles opened.
398
        branch1_tree, branch2_tree = super(TestCheckoutDiff,
399
                                           self).example_branches()
1551.2.13 by Aaron Bentley
Got diff working properly with checkouts
400
        os.mkdir('checkouts')
2738.1.2 by Daniel Watkins
Tests now run on both branches and checkouts.
401
        branch1_tree = branch1_tree.branch.create_checkout('checkouts/branch1')
402
        branch2_tree = branch2_tree.branch.create_checkout('checkouts/branch2')
1551.2.13 by Aaron Bentley
Got diff working properly with checkouts
403
        os.chdir('checkouts')
2738.1.2 by Daniel Watkins
Tests now run on both branches and checkouts.
404
        return branch1_tree, branch2_tree
1583.1.1 by Michael Ellerman
Change to -p1 format diffs. Update existing tests to cope, and add some
405
1658.1.9 by Martin Pool
Give an error for bzr diff on an nonexistent file (Malone #3619)
406
2172.2.1 by Alexander Belchenko
Remove unnecessary duplication of tests in blackbox/test_diff.py
407
class TestDiffLabels(DiffBase):
1583.1.1 by Michael Ellerman
Change to -p1 format diffs. Update existing tests to cope, and add some
408
409
    def test_diff_label_removed(self):
2664.10.1 by Daniel Watkins
tests.blackbox.test_diff now uses internals where appropriate.
410
        tree = super(TestDiffLabels, self).make_example_branch()
411
        tree.remove('hello', keep_files=False)
2552.2.2 by Vincent Ladeuil
Enforce run_bzr(string) where possible.
412
        diff = self.run_bzr('diff', retcode=1)
1694.2.1 by Martin Pool
Remove 'a/', 'b/' default prefixes on diff output.
413
        self.assertTrue("=== removed file 'hello'" in diff[0])
1583.1.1 by Michael Ellerman
Change to -p1 format diffs. Update existing tests to cope, and add some
414
415
    def test_diff_label_added(self):
2664.10.1 by Daniel Watkins
tests.blackbox.test_diff now uses internals where appropriate.
416
        tree = super(TestDiffLabels, self).make_example_branch()
6855.4.1 by Jelmer Vernooij
Yet more bees.
417
        self.build_tree_contents([('barbar', b'barbar')])
2664.10.1 by Daniel Watkins
tests.blackbox.test_diff now uses internals where appropriate.
418
        tree.add('barbar')
2552.2.2 by Vincent Ladeuil
Enforce run_bzr(string) where possible.
419
        diff = self.run_bzr('diff', retcode=1)
1694.2.1 by Martin Pool
Remove 'a/', 'b/' default prefixes on diff output.
420
        self.assertTrue("=== added file 'barbar'" in diff[0])
1583.1.1 by Michael Ellerman
Change to -p1 format diffs. Update existing tests to cope, and add some
421
422
    def test_diff_label_modified(self):
1658.1.9 by Martin Pool
Give an error for bzr diff on an nonexistent file (Malone #3619)
423
        super(TestDiffLabels, self).make_example_branch()
6855.4.1 by Jelmer Vernooij
Yet more bees.
424
        self.build_tree_contents([('hello', b'barbar')])
2552.2.2 by Vincent Ladeuil
Enforce run_bzr(string) where possible.
425
        diff = self.run_bzr('diff', retcode=1)
1694.2.1 by Martin Pool
Remove 'a/', 'b/' default prefixes on diff output.
426
        self.assertTrue("=== modified file 'hello'" in diff[0])
1583.1.1 by Michael Ellerman
Change to -p1 format diffs. Update existing tests to cope, and add some
427
428
    def test_diff_label_renamed(self):
2664.10.1 by Daniel Watkins
tests.blackbox.test_diff now uses internals where appropriate.
429
        tree = super(TestDiffLabels, self).make_example_branch()
430
        tree.rename_one('hello', 'gruezi')
2552.2.2 by Vincent Ladeuil
Enforce run_bzr(string) where possible.
431
        diff = self.run_bzr('diff', retcode=1)
1694.2.1 by Martin Pool
Remove 'a/', 'b/' default prefixes on diff output.
432
        self.assertTrue("=== renamed file 'hello' => 'gruezi'" in diff[0])
1899.1.2 by John Arbash Meinel
Add a test that we can use an external diff program.
433
434
2172.2.1 by Alexander Belchenko
Remove unnecessary duplication of tests in blackbox/test_diff.py
435
class TestExternalDiff(DiffBase):
1899.1.2 by John Arbash Meinel
Add a test that we can use an external diff program.
436
437
    def test_external_diff(self):
438
        """Test that we can spawn an external diff process"""
4695.3.2 by Vincent Ladeuil
Simplified and claried as per Robert's review.
439
        self.disable_missing_extensions_warning()
1899.1.2 by John Arbash Meinel
Add a test that we can use an external diff program.
440
        # We have to use run_bzr_subprocess, because we need to
441
        # test writing directly to stdout, (there was a bug in
442
        # subprocess.py that we had to workaround).
443
        # However, if 'diff' may not be available
444
        self.make_example_branch()
6561.2.1 by Vincent Ladeuil
Add a ``progress_bar`` config option.
445
        out, err = self.run_bzr_subprocess(
446
            'diff -Oprogress_bar=none -r 1 --diff-options -ub',
447
            universal_newlines=True,
448
            retcode=None)
7027.10.1 by Jelmer Vernooij
Various blackbox test fixes.
449
        if b'Diff is not installed on this machine' in err:
4695.3.1 by Vincent Ladeuil
Fix test failures with no C extensions loaded.
450
            raise tests.TestSkipped("No external 'diff' is available")
7045.1.1 by Jelmer Vernooij
Fix another 300 tests.
451
        self.assertEqual(b'', err)
1899.1.2 by John Arbash Meinel
Add a test that we can use an external diff program.
452
        # We have to skip the stuff in the middle, because it depends
453
        # on time.time()
6645.3.1 by Jelmer Vernooij
Make 'bzr diff' default to -p1.
454
        self.assertStartsWith(
455
            out,
7045.1.1 by Jelmer Vernooij
Fix another 300 tests.
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")
2178.4.1 by Alexander Belchenko
Provide tests to illustrate bug #55276 on win32.
461
5349.1.4 by Matthäus G. Chajdas
Allow both --using and --diff-options.
462
    def test_external_diff_options_and_using(self):
463
        """Test that the options are passed correctly to an external diff process"""
5349.1.5 by Matthäus G. Chajdas
Fix issues raised by Vincent Ladeuil.
464
        self.requireFeature(features.diff_feature)
5349.1.4 by Matthäus G. Chajdas
Allow both --using and --diff-options.
465
        self.make_example_branch()
6855.4.1 by Jelmer Vernooij
Yet more bees.
466
        self.build_tree_contents([('hello', b'Foo\n')])
5349.1.5 by Matthäus G. Chajdas
Fix issues raised by Vincent Ladeuil.
467
        out, err = self.run_bzr('diff --diff-options -i --using diff',
7143.15.2 by Jelmer Vernooij
Run autopep8.
468
                                retcode=1)
7027.4.1 by Jelmer Vernooij
Use StringIOWithEncoding on Python3.
469
        self.assertEqual("=== modified file 'hello'\n", out)
470
        self.assertEqual('', err)
5349.1.4 by Matthäus G. Chajdas
Allow both --using and --diff-options.
471
2178.4.1 by Alexander Belchenko
Provide tests to illustrate bug #55276 on win32.
472
473
class TestDiffOutput(DiffBase):
474
475
    def test_diff_output(self):
2178.4.5 by Alexander Belchenko
Spell-checking (thanks to Aaron)
476
        # check that output doesn't mangle line-endings
2178.4.1 by Alexander Belchenko
Provide tests to illustrate bug #55276 on win32.
477
        self.make_example_branch()
6855.4.1 by Jelmer Vernooij
Yet more bees.
478
        self.build_tree_contents([('hello', b'hello world!\n')])
2178.4.1 by Alexander Belchenko
Provide tests to illustrate bug #55276 on win32.
479
        output = self.run_bzr_subprocess('diff', retcode=1)[0]
7027.10.1 by Jelmer Vernooij
Various blackbox test fixes.
480
        self.assertTrue(b'\n+hello world!\n' in output)