/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
6619.2.1 by Vincent Ladeuil
Fix test failure for recent versions of diff
1
# Copyright (C) 2005-2012, 2014, 2016, 2017 Canonical Ltd
1711.2.16 by John Arbash Meinel
test_diff needs a copyright statement
2
#
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.
7
#
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.
2052.3.1 by John Arbash Meinel
Add tests to cleanup the copyright of all source files
12
#
1711.2.16 by John Arbash Meinel
test_diff needs a copyright statement
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
1711.2.16 by John Arbash Meinel
test_diff needs a copyright statement
16
1740.2.5 by Aaron Bentley
Merge from bzr.dev
17
import os
7031.1.1 by Jelmer Vernooij
Fix breezy.tests.test_diff.
18
import re
1920.1.1 by John Arbash Meinel
fix bug #56307, handle binary files even when LANG is not english
19
import subprocess
7141.1.1 by Jelmer Vernooij
Use sys.executable rather than python for ad-hoc tests.
20
import sys
5168.1.2 by Vincent Ladeuil
Ckeanup some more imports.
21
import tempfile
1558.15.2 by Aaron Bentley
Implemented binary file handling for diff
22
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
23
from .. import (
5168.1.4 by Vincent Ladeuil
Final import fixes for bt.test_diff.
24
    diff,
5168.1.2 by Vincent Ladeuil
Ckeanup some more imports.
25
    errors,
26
    osutils,
27
    revision as _mod_revision,
28
    revisionspec,
29
    revisiontree,
30
    tests,
31
    )
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
32
from ..sixish import (
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
33
    BytesIO,
7031.1.1 by Jelmer Vernooij
Fix breezy.tests.test_diff.
34
    unichr,
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
35
    )
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
36
from ..tests import (
5967.12.1 by Martin Pool
Move all test features into bzrlib.tests.features
37
    features,
6597.2.2 by Vincent Ladeuil
Split the diff tests to get finer grained failures. Also cleaned up some unused imports.
38
    EncodingAdapter,
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
39
    )
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
40
from ..tests.scenarios import load_tests_apply_scenarios
6597.2.2 by Vincent Ladeuil
Split the diff tests to get finer grained failures. Also cleaned up some unused imports.
41
42
43
load_tests = load_tests_apply_scenarios
2781.1.1 by Martin Pool
merge cpatiencediff from Lukas
44
45
7031.1.1 by Jelmer Vernooij
Fix breezy.tests.test_diff.
46
def subst_dates(string):
47
    """Replace date strings with constant values."""
48
    return re.sub(br'\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2} [-\+]\d{4}',
49
                  b'YYYY-MM-DD HH:MM:SS +ZZZZ', string)
50
51
1558.15.11 by Aaron Bentley
Apply merge review suggestions
52
def udiff_lines(old, new, allow_binary=False):
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
53
    output = BytesIO()
5168.1.4 by Vincent Ladeuil
Final import fixes for bt.test_diff.
54
    diff.internal_diff('old', old, 'new', new, output, allow_binary)
974.1.6 by Aaron Bentley
Added unit tests
55
    output.seek(0, 0)
56
    return output.readlines()
57
1711.2.54 by John Arbash Meinel
Use mkstemp instead of NamedTemporary file for external diff.
58
1711.2.57 by John Arbash Meinel
Allow external diff to write to a file without a fileno.
59
def external_udiff_lines(old, new, use_stringio=False):
60
    if use_stringio:
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
61
        # BytesIO has no fileno, so it tests a different codepath
62
        output = BytesIO()
1711.2.57 by John Arbash Meinel
Allow external diff to write to a file without a fileno.
63
    else:
5168.1.2 by Vincent Ladeuil
Ckeanup some more imports.
64
        output = tempfile.TemporaryFile()
1692.8.7 by James Henstridge
changes suggested by John Meinel
65
    try:
5168.1.4 by Vincent Ladeuil
Final import fixes for bt.test_diff.
66
        diff.external_diff('old', old, 'new', new, output, diff_opts=['-u'])
5168.1.2 by Vincent Ladeuil
Ckeanup some more imports.
67
    except errors.NoDiff:
68
        raise tests.TestSkipped('external "diff" not present to test')
1692.8.2 by James Henstridge
add a test for sending external diff output to a file
69
    output.seek(0, 0)
70
    lines = output.readlines()
71
    output.close()
72
    return lines
73
74
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
75
class StubO(object):
76
    """Simple file-like object that allows writes with any type and records."""
77
78
    def __init__(self):
79
        self.write_record = []
80
81
    def write(self, data):
82
        self.write_record.append(data)
83
84
    def check_types(self, testcase, expected_type):
85
        testcase.assertFalse(
86
            any(not isinstance(o, expected_type) for o in self.write_record),
87
            "Not all writes of type %s: %r" % (
88
                expected_type.__name__, self.write_record))
89
90
6597.2.2 by Vincent Ladeuil
Split the diff tests to get finer grained failures. Also cleaned up some unused imports.
91
class TestDiffOptions(tests.TestCase):
92
93
    def test_unified_added(self):
94
        """Check for default style '-u' only if no other style specified
95
        in 'diff-options'.
96
        """
97
        # Verify that style defaults to unified, id est '-u' appended
98
        # to option list, in the absence of an alternative style.
99
        self.assertEqual(['-a', '-u'], diff.default_style_unified(['-a']))
100
101
102
class TestDiffOptionsScenarios(tests.TestCase):
103
104
    scenarios = [(s, dict(style=s)) for s in diff.style_option_list]
7143.15.2 by Jelmer Vernooij
Run autopep8.
105
    style = None  # Set by load_tests_apply_scenarios from scenarios
6597.2.2 by Vincent Ladeuil
Split the diff tests to get finer grained failures. Also cleaned up some unused imports.
106
107
    def test_unified_not_added(self):
108
        # Verify that for all valid style options, '-u' is not
109
        # appended to option list.
110
        ret_opts = diff.default_style_unified(diff_opts=["%s" % (self.style,)])
111
        self.assertEqual(["%s" % (self.style,)], ret_opts)
112
113
5168.1.2 by Vincent Ladeuil
Ckeanup some more imports.
114
class TestDiff(tests.TestCase):
1185.81.25 by Aaron Bentley
Clean up test_diff
115
1102 by Martin Pool
- merge test refactoring from robertc
116
    def test_add_nl(self):
117
        """diff generates a valid diff for patches that add a newline"""
7031.1.1 by Jelmer Vernooij
Fix breezy.tests.test_diff.
118
        lines = udiff_lines([b'boo'], [b'boo\n'])
1185.16.145 by Martin Pool
Remove all assert statements from test cases.
119
        self.check_patch(lines)
7031.1.1 by Jelmer Vernooij
Fix breezy.tests.test_diff.
120
        self.assertEqual(lines[4], b'\\ No newline at end of file\n')
7143.15.2 by Jelmer Vernooij
Run autopep8.
121
        ## "expected no-nl, got %r" % lines[4]
974.1.6 by Aaron Bentley
Added unit tests
122
1102 by Martin Pool
- merge test refactoring from robertc
123
    def test_add_nl_2(self):
124
        """diff generates a valid diff for patches that change last line and
125
        add a newline.
126
        """
7031.1.1 by Jelmer Vernooij
Fix breezy.tests.test_diff.
127
        lines = udiff_lines([b'boo'], [b'goo\n'])
1185.16.145 by Martin Pool
Remove all assert statements from test cases.
128
        self.check_patch(lines)
7031.1.1 by Jelmer Vernooij
Fix breezy.tests.test_diff.
129
        self.assertEqual(lines[4], b'\\ No newline at end of file\n')
7143.15.2 by Jelmer Vernooij
Run autopep8.
130
        ## "expected no-nl, got %r" % lines[4]
974.1.6 by Aaron Bentley
Added unit tests
131
1102 by Martin Pool
- merge test refactoring from robertc
132
    def test_remove_nl(self):
133
        """diff generates a valid diff for patches that change last line and
134
        add a newline.
135
        """
7031.1.1 by Jelmer Vernooij
Fix breezy.tests.test_diff.
136
        lines = udiff_lines([b'boo\n'], [b'boo'])
1185.16.145 by Martin Pool
Remove all assert statements from test cases.
137
        self.check_patch(lines)
7031.1.1 by Jelmer Vernooij
Fix breezy.tests.test_diff.
138
        self.assertEqual(lines[5], b'\\ No newline at end of file\n')
7143.15.2 by Jelmer Vernooij
Run autopep8.
139
        ## "expected no-nl, got %r" % lines[5]
1185.16.145 by Martin Pool
Remove all assert statements from test cases.
140
141
    def check_patch(self, lines):
6614.1.1 by Vincent Ladeuil
Fix assert_ being deprecated by using assertTrue.
142
        self.assertTrue(len(lines) > 1)
7143.15.2 by Jelmer Vernooij
Run autopep8.
143
        ## "Not enough lines for a file header for patch:\n%s" % "".join(lines)
144
        self.assertTrue(lines[0].startswith(b'---'))
145
        ## 'No orig line for patch:\n%s' % "".join(lines)
146
        self.assertTrue(lines[1].startswith(b'+++'))
147
        ## 'No mod line for patch:\n%s' % "".join(lines)
6614.1.1 by Vincent Ladeuil
Fix assert_ being deprecated by using assertTrue.
148
        self.assertTrue(len(lines) > 2)
7143.15.2 by Jelmer Vernooij
Run autopep8.
149
        ## "No hunks for patch:\n%s" % "".join(lines)
7031.1.1 by Jelmer Vernooij
Fix breezy.tests.test_diff.
150
        self.assertTrue(lines[2].startswith(b'@@'))
7143.15.2 by Jelmer Vernooij
Run autopep8.
151
        ## "No hunk header for patch:\n%s" % "".join(lines)
7031.1.1 by Jelmer Vernooij
Fix breezy.tests.test_diff.
152
        self.assertTrue(b'@@' in lines[2][2:])
7143.15.2 by Jelmer Vernooij
Run autopep8.
153
        ## "Unterminated hunk header for patch:\n%s" % "".join(lines)
1185.16.145 by Martin Pool
Remove all assert statements from test cases.
154
1558.15.2 by Aaron Bentley
Implemented binary file handling for diff
155
    def test_binary_lines(self):
5168.1.2 by Vincent Ladeuil
Ckeanup some more imports.
156
        empty = []
7031.1.1 by Jelmer Vernooij
Fix breezy.tests.test_diff.
157
        uni_lines = [1023 * b'a' + b'\x00']
6809.1.1 by Martin
Apply 2to3 ws_comma fixer
158
        self.assertRaises(errors.BinaryFile, udiff_lines, uni_lines, empty)
5168.1.2 by Vincent Ladeuil
Ckeanup some more imports.
159
        self.assertRaises(errors.BinaryFile, udiff_lines, empty, uni_lines)
6809.1.1 by Martin
Apply 2to3 ws_comma fixer
160
        udiff_lines(uni_lines, empty, allow_binary=True)
5168.1.2 by Vincent Ladeuil
Ckeanup some more imports.
161
        udiff_lines(empty, uni_lines, allow_binary=True)
1692.8.2 by James Henstridge
add a test for sending external diff output to a file
162
163
    def test_external_diff(self):
7031.1.1 by Jelmer Vernooij
Fix breezy.tests.test_diff.
164
        lines = external_udiff_lines([b'boo\n'], [b'goo\n'])
1692.8.2 by James Henstridge
add a test for sending external diff output to a file
165
        self.check_patch(lines)
7031.1.1 by Jelmer Vernooij
Fix breezy.tests.test_diff.
166
        self.assertEqual(b'\n', lines[-1])
1711.2.57 by John Arbash Meinel
Allow external diff to write to a file without a fileno.
167
168
    def test_external_diff_no_fileno(self):
169
        # Make sure that we can handle not having a fileno, even
170
        # if the diff is large
7143.15.2 by Jelmer Vernooij
Run autopep8.
171
        lines = external_udiff_lines([b'boo\n'] * 10000,
172
                                     [b'goo\n'] * 10000,
1711.2.57 by John Arbash Meinel
Allow external diff to write to a file without a fileno.
173
                                     use_stringio=True)
174
        self.check_patch(lines)
1899.1.1 by John Arbash Meinel
Fix the bug in the NoDiff exception class, and add a test
175
1920.1.1 by John Arbash Meinel
fix bug #56307, handle binary files even when LANG is not english
176
    def test_external_diff_binary_lang_c(self):
2321.2.5 by Alexander Belchenko
external diff: no need for special code path for win32 (suggested by John Meinel)
177
        for lang in ('LANG', 'LC_ALL', 'LANGUAGE'):
5570.3.9 by Vincent Ladeuil
More use cases for overrideEnv, _cleanEnvironment *may* contain too much variables now.
178
            self.overrideEnv(lang, 'C')
7031.1.1 by Jelmer Vernooij
Fix breezy.tests.test_diff.
179
        lines = external_udiff_lines([b'\x00foobar\n'], [b'foo\x00bar\n'])
5570.3.9 by Vincent Ladeuil
More use cases for overrideEnv, _cleanEnvironment *may* contain too much variables now.
180
        # Older versions of diffutils say "Binary files", newer
181
        # versions just say "Files".
7143.15.2 by Jelmer Vernooij
Run autopep8.
182
        self.assertContainsRe(
183
            lines[0], b'(Binary f|F)iles old and new differ\n')
7031.1.1 by Jelmer Vernooij
Fix breezy.tests.test_diff.
184
        self.assertEqual(lines[1:], [b'\n'])
1899.1.4 by John Arbash Meinel
Just swallow a return code of 2
185
1899.1.1 by John Arbash Meinel
Fix the bug in the NoDiff exception class, and add a test
186
    def test_no_external_diff(self):
187
        """Check that NoDiff is raised when diff is not available"""
5570.3.9 by Vincent Ladeuil
More use cases for overrideEnv, _cleanEnvironment *may* contain too much variables now.
188
        # Make sure no 'diff' command is available
189
        # XXX: Weird, using None instead of '' breaks the test -- vila 20101216
190
        self.overrideEnv('PATH', '')
191
        self.assertRaises(errors.NoDiff, diff.external_diff,
7031.1.1 by Jelmer Vernooij
Fix breezy.tests.test_diff.
192
                          b'old', [b'boo\n'], b'new', [b'goo\n'],
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
193
                          BytesIO(), diff_opts=['-u'])
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
194
1711.2.30 by John Arbash Meinel
Fix bug in internal_diff handling of unicode paths
195
    def test_internal_diff_default(self):
196
        # Default internal diff encoding is utf8
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
197
        output = BytesIO()
7031.1.1 by Jelmer Vernooij
Fix breezy.tests.test_diff.
198
        diff.internal_diff(u'old_\xb5', [b'old_text\n'],
199
                           u'new_\xe5', [b'new_text\n'], output)
1711.2.30 by John Arbash Meinel
Fix bug in internal_diff handling of unicode paths
200
        lines = output.getvalue().splitlines(True)
201
        self.check_patch(lines)
7031.1.1 by Jelmer Vernooij
Fix breezy.tests.test_diff.
202
        self.assertEqual([b'--- old_\xc2\xb5\n',
7143.15.2 by Jelmer Vernooij
Run autopep8.
203
                          b'+++ new_\xc3\xa5\n',
204
                          b'@@ -1,1 +1,1 @@\n',
205
                          b'-old_text\n',
206
                          b'+new_text\n',
207
                          b'\n',
208
                          ], lines)
1711.2.30 by John Arbash Meinel
Fix bug in internal_diff handling of unicode paths
209
210
    def test_internal_diff_utf8(self):
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
211
        output = BytesIO()
7031.1.1 by Jelmer Vernooij
Fix breezy.tests.test_diff.
212
        diff.internal_diff(u'old_\xb5', [b'old_text\n'],
213
                           u'new_\xe5', [b'new_text\n'], output,
5168.1.4 by Vincent Ladeuil
Final import fixes for bt.test_diff.
214
                           path_encoding='utf8')
1711.2.30 by John Arbash Meinel
Fix bug in internal_diff handling of unicode paths
215
        lines = output.getvalue().splitlines(True)
216
        self.check_patch(lines)
7031.1.1 by Jelmer Vernooij
Fix breezy.tests.test_diff.
217
        self.assertEqual([b'--- old_\xc2\xb5\n',
7143.15.2 by Jelmer Vernooij
Run autopep8.
218
                          b'+++ new_\xc3\xa5\n',
219
                          b'@@ -1,1 +1,1 @@\n',
220
                          b'-old_text\n',
221
                          b'+new_text\n',
222
                          b'\n',
223
                          ], lines)
1711.2.30 by John Arbash Meinel
Fix bug in internal_diff handling of unicode paths
224
225
    def test_internal_diff_iso_8859_1(self):
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
226
        output = BytesIO()
6973.13.2 by Jelmer Vernooij
Fix some more tests.
227
        diff.internal_diff(u'old_\xb5', [b'old_text\n'],
228
                           u'new_\xe5', [b'new_text\n'], output,
5168.1.4 by Vincent Ladeuil
Final import fixes for bt.test_diff.
229
                           path_encoding='iso-8859-1')
1711.2.30 by John Arbash Meinel
Fix bug in internal_diff handling of unicode paths
230
        lines = output.getvalue().splitlines(True)
231
        self.check_patch(lines)
6973.13.2 by Jelmer Vernooij
Fix some more tests.
232
        self.assertEqual([b'--- old_\xb5\n',
233
                          b'+++ new_\xe5\n',
234
                          b'@@ -1,1 +1,1 @@\n',
235
                          b'-old_text\n',
236
                          b'+new_text\n',
237
                          b'\n',
7143.15.2 by Jelmer Vernooij
Run autopep8.
238
                          ], lines)
1711.2.30 by John Arbash Meinel
Fix bug in internal_diff handling of unicode paths
239
3085.1.1 by John Arbash Meinel
Fix internal_diff to not fail when the texts are identical.
240
    def test_internal_diff_no_content(self):
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
241
        output = BytesIO()
5168.1.4 by Vincent Ladeuil
Final import fixes for bt.test_diff.
242
        diff.internal_diff(u'old', [], u'new', [], output)
6973.13.2 by Jelmer Vernooij
Fix some more tests.
243
        self.assertEqual(b'', output.getvalue())
3085.1.1 by John Arbash Meinel
Fix internal_diff to not fail when the texts are identical.
244
245
    def test_internal_diff_no_changes(self):
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
246
        output = BytesIO()
6973.13.2 by Jelmer Vernooij
Fix some more tests.
247
        diff.internal_diff(u'old', [b'text\n', b'contents\n'],
248
                           u'new', [b'text\n', b'contents\n'],
5168.1.4 by Vincent Ladeuil
Final import fixes for bt.test_diff.
249
                           output)
6973.13.2 by Jelmer Vernooij
Fix some more tests.
250
        self.assertEqual(b'', output.getvalue())
3085.1.1 by John Arbash Meinel
Fix internal_diff to not fail when the texts are identical.
251
1711.2.30 by John Arbash Meinel
Fix bug in internal_diff handling of unicode paths
252
    def test_internal_diff_returns_bytes(self):
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
253
        output = StubO()
6973.13.2 by Jelmer Vernooij
Fix some more tests.
254
        diff.internal_diff(u'old_\xb5', [b'old_text\n'],
7031.1.1 by Jelmer Vernooij
Fix breezy.tests.test_diff.
255
                           u'new_\xe5', [b'new_text\n'], output)
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
256
        output.check_types(self, bytes)
1711.2.30 by John Arbash Meinel
Fix bug in internal_diff handling of unicode paths
257
6524.5.5 by Paul Nixon
Added tests of configurable context
258
    def test_internal_diff_default_context(self):
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
259
        output = BytesIO()
7031.1.1 by Jelmer Vernooij
Fix breezy.tests.test_diff.
260
        diff.internal_diff('old', [b'same_text\n', b'same_text\n', b'same_text\n',
7143.15.2 by Jelmer Vernooij
Run autopep8.
261
                                   b'same_text\n', b'same_text\n', b'old_text\n'],
7031.1.1 by Jelmer Vernooij
Fix breezy.tests.test_diff.
262
                           'new', [b'same_text\n', b'same_text\n', b'same_text\n',
7143.15.2 by Jelmer Vernooij
Run autopep8.
263
                                   b'same_text\n', b'same_text\n', b'new_text\n'], output)
6524.5.5 by Paul Nixon
Added tests of configurable context
264
        lines = output.getvalue().splitlines(True)
265
        self.check_patch(lines)
7031.1.1 by Jelmer Vernooij
Fix breezy.tests.test_diff.
266
        self.assertEqual([b'--- old\n',
7143.15.2 by Jelmer Vernooij
Run autopep8.
267
                          b'+++ new\n',
268
                          b'@@ -3,4 +3,4 @@\n',
269
                          b' same_text\n',
270
                          b' same_text\n',
271
                          b' same_text\n',
272
                          b'-old_text\n',
273
                          b'+new_text\n',
274
                          b'\n',
275
                          ], lines)
6524.5.5 by Paul Nixon
Added tests of configurable context
276
277
    def test_internal_diff_no_context(self):
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
278
        output = BytesIO()
7031.1.1 by Jelmer Vernooij
Fix breezy.tests.test_diff.
279
        diff.internal_diff('old', [b'same_text\n', b'same_text\n', b'same_text\n',
7143.15.2 by Jelmer Vernooij
Run autopep8.
280
                                   b'same_text\n', b'same_text\n', b'old_text\n'],
7031.1.1 by Jelmer Vernooij
Fix breezy.tests.test_diff.
281
                           'new', [b'same_text\n', b'same_text\n', b'same_text\n',
7143.15.2 by Jelmer Vernooij
Run autopep8.
282
                                   b'same_text\n', b'same_text\n', b'new_text\n'], output,
6524.5.5 by Paul Nixon
Added tests of configurable context
283
                           context_lines=0)
284
        lines = output.getvalue().splitlines(True)
285
        self.check_patch(lines)
7031.1.1 by Jelmer Vernooij
Fix breezy.tests.test_diff.
286
        self.assertEqual([b'--- old\n',
7143.15.2 by Jelmer Vernooij
Run autopep8.
287
                          b'+++ new\n',
288
                          b'@@ -6,1 +6,1 @@\n',
289
                          b'-old_text\n',
290
                          b'+new_text\n',
291
                          b'\n',
292
                          ], lines)
6524.5.5 by Paul Nixon
Added tests of configurable context
293
294
    def test_internal_diff_more_context(self):
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
295
        output = BytesIO()
7031.1.1 by Jelmer Vernooij
Fix breezy.tests.test_diff.
296
        diff.internal_diff('old', [b'same_text\n', b'same_text\n', b'same_text\n',
7143.15.2 by Jelmer Vernooij
Run autopep8.
297
                                   b'same_text\n', b'same_text\n', b'old_text\n'],
7031.1.1 by Jelmer Vernooij
Fix breezy.tests.test_diff.
298
                           'new', [b'same_text\n', b'same_text\n', b'same_text\n',
7143.15.2 by Jelmer Vernooij
Run autopep8.
299
                                   b'same_text\n', b'same_text\n', b'new_text\n'], output,
6524.5.5 by Paul Nixon
Added tests of configurable context
300
                           context_lines=4)
301
        lines = output.getvalue().splitlines(True)
302
        self.check_patch(lines)
7031.1.1 by Jelmer Vernooij
Fix breezy.tests.test_diff.
303
        self.assertEqual([b'--- old\n',
304
                          b'+++ new\n',
305
                          b'@@ -2,5 +2,5 @@\n',
306
                          b' same_text\n',
307
                          b' same_text\n',
308
                          b' same_text\n',
309
                          b' same_text\n',
310
                          b'-old_text\n',
311
                          b'+new_text\n',
312
                          b'\n',
7143.15.2 by Jelmer Vernooij
Run autopep8.
313
                          ], lines)
6524.5.5 by Paul Nixon
Added tests of configurable context
314
315
5168.1.2 by Vincent Ladeuil
Ckeanup some more imports.
316
class TestDiffFiles(tests.TestCaseInTempDir):
1920.1.1 by John Arbash Meinel
fix bug #56307, handle binary files even when LANG is not english
317
318
    def test_external_diff_binary(self):
319
        """The output when using external diff should use diff's i18n error"""
6792.1.2 by Jelmer Vernooij
Alternative approach.
320
        for lang in ('LANG', 'LC_ALL', 'LANGUAGE'):
321
            self.overrideEnv(lang, 'C')
1920.1.1 by John Arbash Meinel
fix bug #56307, handle binary files even when LANG is not english
322
        # Make sure external_diff doesn't fail in the current LANG
7031.1.1 by Jelmer Vernooij
Fix breezy.tests.test_diff.
323
        lines = external_udiff_lines([b'\x00foobar\n'], [b'foo\x00bar\n'])
1920.1.1 by John Arbash Meinel
fix bug #56307, handle binary files even when LANG is not english
324
2240.1.1 by Alexander Belchenko
test_external_diff_binary: run external diff with --binary flag
325
        cmd = ['diff', '-u', '--binary', 'old', 'new']
7143.15.2 by Jelmer Vernooij
Run autopep8.
326
        with open('old', 'wb') as f:
327
            f.write(b'\x00foobar\n')
328
        with open('new', 'wb') as f:
329
            f.write(b'foo\x00bar\n')
6792.1.3 by Jelmer Vernooij
Alternative approach.
330
        pipe = subprocess.Popen(cmd, stdout=subprocess.PIPE,
7143.15.2 by Jelmer Vernooij
Run autopep8.
331
                                stdin=subprocess.PIPE)
1920.1.1 by John Arbash Meinel
fix bug #56307, handle binary files even when LANG is not english
332
        out, err = pipe.communicate()
333
        # We should output whatever diff tells us, plus a trailing newline
7031.1.1 by Jelmer Vernooij
Fix breezy.tests.test_diff.
334
        self.assertEqual(out.splitlines(True) + [b'\n'], lines)
1920.1.1 by John Arbash Meinel
fix bug #56307, handle binary files even when LANG is not english
335
336
5784.3.1 by Martin Pool
Remove unnecessary TestShowDiffTreesHelper and just use a function
337
def get_diff_as_string(tree1, tree2, specific_files=None, working_tree=None):
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
338
    output = BytesIO()
5784.3.1 by Martin Pool
Remove unnecessary TestShowDiffTreesHelper and just use a function
339
    if working_tree is not None:
340
        extra_trees = (working_tree,)
341
    else:
342
        extra_trees = ()
343
    diff.show_diff_trees(tree1, tree2, output,
7143.15.2 by Jelmer Vernooij
Run autopep8.
344
                         specific_files=specific_files,
345
                         extra_trees=extra_trees, old_label='old/',
346
                         new_label='new/')
5784.3.1 by Martin Pool
Remove unnecessary TestShowDiffTreesHelper and just use a function
347
    return output.getvalue()
348
349
350
class TestDiffDates(tests.TestCaseWithTransport):
1740.2.5 by Aaron Bentley
Merge from bzr.dev
351
352
    def setUp(self):
353
        super(TestDiffDates, self).setUp()
354
        self.wt = self.make_branch_and_tree('.')
355
        self.b = self.wt.branch
356
        self.build_tree_contents([
6855.4.1 by Jelmer Vernooij
Yet more bees.
357
            ('file1', b'file1 contents at rev 1\n'),
358
            ('file2', b'file2 contents at rev 1\n')
1740.2.5 by Aaron Bentley
Merge from bzr.dev
359
            ])
360
        self.wt.add(['file1', 'file2'])
361
        self.wt.commit(
362
            message='Revision 1',
7143.15.2 by Jelmer Vernooij
Run autopep8.
363
            timestamp=1143849600,  # 2006-04-01 00:00:00 UTC
1740.2.5 by Aaron Bentley
Merge from bzr.dev
364
            timezone=0,
6855.4.1 by Jelmer Vernooij
Yet more bees.
365
            rev_id=b'rev-1')
366
        self.build_tree_contents([('file1', b'file1 contents at rev 2\n')])
1740.2.5 by Aaron Bentley
Merge from bzr.dev
367
        self.wt.commit(
368
            message='Revision 2',
7143.15.2 by Jelmer Vernooij
Run autopep8.
369
            timestamp=1143936000,  # 2006-04-02 00:00:00 UTC
1740.2.5 by Aaron Bentley
Merge from bzr.dev
370
            timezone=28800,
6855.4.1 by Jelmer Vernooij
Yet more bees.
371
            rev_id=b'rev-2')
372
        self.build_tree_contents([('file2', b'file2 contents at rev 3\n')])
1740.2.5 by Aaron Bentley
Merge from bzr.dev
373
        self.wt.commit(
374
            message='Revision 3',
7143.15.2 by Jelmer Vernooij
Run autopep8.
375
            timestamp=1144022400,  # 2006-04-03 00:00:00 UTC
1740.2.5 by Aaron Bentley
Merge from bzr.dev
376
            timezone=-3600,
6855.4.1 by Jelmer Vernooij
Yet more bees.
377
            rev_id=b'rev-3')
1740.2.5 by Aaron Bentley
Merge from bzr.dev
378
        self.wt.remove(['file2'])
379
        self.wt.commit(
380
            message='Revision 4',
7143.15.2 by Jelmer Vernooij
Run autopep8.
381
            timestamp=1144108800,  # 2006-04-04 00:00:00 UTC
1740.2.5 by Aaron Bentley
Merge from bzr.dev
382
            timezone=0,
6855.4.1 by Jelmer Vernooij
Yet more bees.
383
            rev_id=b'rev-4')
1740.2.5 by Aaron Bentley
Merge from bzr.dev
384
        self.build_tree_contents([
6855.4.1 by Jelmer Vernooij
Yet more bees.
385
            ('file1', b'file1 contents in working tree\n')
1740.2.5 by Aaron Bentley
Merge from bzr.dev
386
            ])
387
        # set the date stamps for files in the working tree to known values
7143.15.2 by Jelmer Vernooij
Run autopep8.
388
        os.utime('file1', (1144195200, 1144195200))  # 2006-04-05 00:00:00 UTC
1740.2.5 by Aaron Bentley
Merge from bzr.dev
389
390
    def test_diff_rev_tree_working_tree(self):
5784.3.1 by Martin Pool
Remove unnecessary TestShowDiffTreesHelper and just use a function
391
        output = get_diff_as_string(self.wt.basis_tree(), self.wt)
1740.2.5 by Aaron Bentley
Merge from bzr.dev
392
        # note that the date for old/file1 is from rev 2 rather than from
393
        # the basis revision (rev 4)
7031.1.1 by Jelmer Vernooij
Fix breezy.tests.test_diff.
394
        self.assertEqualDiff(output, b'''\
1740.2.5 by Aaron Bentley
Merge from bzr.dev
395
=== modified file 'file1'
396
--- old/file1\t2006-04-02 00:00:00 +0000
397
+++ new/file1\t2006-04-05 00:00:00 +0000
398
@@ -1,1 +1,1 @@
399
-file1 contents at rev 2
400
+file1 contents in working tree
401
402
''')
403
404
    def test_diff_rev_tree_rev_tree(self):
6973.5.2 by Jelmer Vernooij
Add more bees.
405
        tree1 = self.b.repository.revision_tree(b'rev-2')
406
        tree2 = self.b.repository.revision_tree(b'rev-3')
5784.3.1 by Martin Pool
Remove unnecessary TestShowDiffTreesHelper and just use a function
407
        output = get_diff_as_string(tree1, tree2)
7031.1.1 by Jelmer Vernooij
Fix breezy.tests.test_diff.
408
        self.assertEqualDiff(output, b'''\
1740.2.5 by Aaron Bentley
Merge from bzr.dev
409
=== modified file 'file2'
410
--- old/file2\t2006-04-01 00:00:00 +0000
411
+++ new/file2\t2006-04-03 00:00:00 +0000
412
@@ -1,1 +1,1 @@
413
-file2 contents at rev 1
414
+file2 contents at rev 3
415
416
''')
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
417
1740.2.5 by Aaron Bentley
Merge from bzr.dev
418
    def test_diff_add_files(self):
3668.5.1 by Jelmer Vernooij
Use NULL_REVISION rather than None for Repository.revision_tree().
419
        tree1 = self.b.repository.revision_tree(_mod_revision.NULL_REVISION)
6973.5.2 by Jelmer Vernooij
Add more bees.
420
        tree2 = self.b.repository.revision_tree(b'rev-1')
5784.3.1 by Martin Pool
Remove unnecessary TestShowDiffTreesHelper and just use a function
421
        output = get_diff_as_string(tree1, tree2)
1740.2.5 by Aaron Bentley
Merge from bzr.dev
422
        # the files have the epoch time stamp for the tree in which
423
        # they don't exist.
7031.1.1 by Jelmer Vernooij
Fix breezy.tests.test_diff.
424
        self.assertEqualDiff(output, b'''\
1740.2.5 by Aaron Bentley
Merge from bzr.dev
425
=== added file 'file1'
426
--- old/file1\t1970-01-01 00:00:00 +0000
427
+++ new/file1\t2006-04-01 00:00:00 +0000
428
@@ -0,0 +1,1 @@
429
+file1 contents at rev 1
430
431
=== added file 'file2'
432
--- old/file2\t1970-01-01 00:00:00 +0000
433
+++ new/file2\t2006-04-01 00:00:00 +0000
434
@@ -0,0 +1,1 @@
435
+file2 contents at rev 1
436
437
''')
438
439
    def test_diff_remove_files(self):
6973.5.2 by Jelmer Vernooij
Add more bees.
440
        tree1 = self.b.repository.revision_tree(b'rev-3')
441
        tree2 = self.b.repository.revision_tree(b'rev-4')
5784.3.1 by Martin Pool
Remove unnecessary TestShowDiffTreesHelper and just use a function
442
        output = get_diff_as_string(tree1, tree2)
1740.2.5 by Aaron Bentley
Merge from bzr.dev
443
        # the file has the epoch time stamp for the tree in which
444
        # it doesn't exist.
7031.1.1 by Jelmer Vernooij
Fix breezy.tests.test_diff.
445
        self.assertEqualDiff(output, b'''\
1740.2.5 by Aaron Bentley
Merge from bzr.dev
446
=== removed file 'file2'
447
--- old/file2\t2006-04-03 00:00:00 +0000
448
+++ new/file2\t1970-01-01 00:00:00 +0000
449
@@ -1,1 +0,0 @@
450
-file2 contents at rev 3
451
452
''')
453
1551.7.17 by Aaron Bentley
Switch to PathsNotVersioned, accept extra_trees
454
    def test_show_diff_specified(self):
1551.7.22 by Aaron Bentley
Changes from review
455
        """A working tree filename can be used to identify a file"""
1551.7.17 by Aaron Bentley
Switch to PathsNotVersioned, accept extra_trees
456
        self.wt.rename_one('file1', 'file1b')
6973.5.2 by Jelmer Vernooij
Add more bees.
457
        old_tree = self.b.repository.revision_tree(b'rev-1')
458
        new_tree = self.b.repository.revision_tree(b'rev-4')
5784.3.1 by Martin Pool
Remove unnecessary TestShowDiffTreesHelper and just use a function
459
        out = get_diff_as_string(old_tree, new_tree, specific_files=['file1b'],
7143.15.2 by Jelmer Vernooij
Run autopep8.
460
                                 working_tree=self.wt)
7031.1.1 by Jelmer Vernooij
Fix breezy.tests.test_diff.
461
        self.assertContainsRe(out, b'file1\t')
1551.7.17 by Aaron Bentley
Switch to PathsNotVersioned, accept extra_trees
462
1551.7.22 by Aaron Bentley
Changes from review
463
    def test_recursive_diff(self):
464
        """Children of directories are matched"""
465
        os.mkdir('dir1')
466
        os.mkdir('dir2')
467
        self.wt.add(['dir1', 'dir2'])
468
        self.wt.rename_one('file1', 'dir1/file1')
6973.5.2 by Jelmer Vernooij
Add more bees.
469
        old_tree = self.b.repository.revision_tree(b'rev-1')
470
        new_tree = self.b.repository.revision_tree(b'rev-4')
5784.3.1 by Martin Pool
Remove unnecessary TestShowDiffTreesHelper and just use a function
471
        out = get_diff_as_string(old_tree, new_tree, specific_files=['dir1'],
7143.15.2 by Jelmer Vernooij
Run autopep8.
472
                                 working_tree=self.wt)
6973.14.7 by Jelmer Vernooij
Bees bees bees.
473
        self.assertContainsRe(out, b'file1\t')
5784.3.1 by Martin Pool
Remove unnecessary TestShowDiffTreesHelper and just use a function
474
        out = get_diff_as_string(old_tree, new_tree, specific_files=['dir2'],
7143.15.2 by Jelmer Vernooij
Run autopep8.
475
                                 working_tree=self.wt)
6973.14.7 by Jelmer Vernooij
Bees bees bees.
476
        self.assertNotContainsRe(out, b'file1\t')
1740.2.5 by Aaron Bentley
Merge from bzr.dev
477
1899.1.1 by John Arbash Meinel
Fix the bug in the NoDiff exception class, and add a test
478
5784.3.1 by Martin Pool
Remove unnecessary TestShowDiffTreesHelper and just use a function
479
class TestShowDiffTrees(tests.TestCaseWithTransport):
2405.1.1 by John Arbash Meinel
Add a bunch of direct tests for 'show_diff_trees'
480
    """Direct tests for show_diff_trees"""
481
482
    def test_modified_file(self):
483
        """Test when a file is modified."""
484
        tree = self.make_branch_and_tree('tree')
6855.4.1 by Jelmer Vernooij
Yet more bees.
485
        self.build_tree_contents([('tree/file', b'contents\n')])
486
        tree.add(['file'], [b'file-id'])
487
        tree.commit('one', rev_id=b'rev-1')
2405.1.1 by John Arbash Meinel
Add a bunch of direct tests for 'show_diff_trees'
488
6855.4.1 by Jelmer Vernooij
Yet more bees.
489
        self.build_tree_contents([('tree/file', b'new contents\n')])
5784.3.1 by Martin Pool
Remove unnecessary TestShowDiffTreesHelper and just use a function
490
        d = get_diff_as_string(tree.basis_tree(), tree)
7031.1.1 by Jelmer Vernooij
Fix breezy.tests.test_diff.
491
        self.assertContainsRe(d, b"=== modified file 'file'\n")
492
        self.assertContainsRe(d, b'--- old/file\t')
493
        self.assertContainsRe(d, b'\\+\\+\\+ new/file\t')
494
        self.assertContainsRe(d, b'-contents\n'
495
                                 b'\\+new contents\n')
2405.1.1 by John Arbash Meinel
Add a bunch of direct tests for 'show_diff_trees'
496
2405.1.2 by John Arbash Meinel
Fix bug #103870 by passing None instead of a (sometimes wrong) path
497
    def test_modified_file_in_renamed_dir(self):
498
        """Test when a file is modified in a renamed directory."""
499
        tree = self.make_branch_and_tree('tree')
500
        self.build_tree(['tree/dir/'])
6855.4.1 by Jelmer Vernooij
Yet more bees.
501
        self.build_tree_contents([('tree/dir/file', b'contents\n')])
502
        tree.add(['dir', 'dir/file'], [b'dir-id', b'file-id'])
503
        tree.commit('one', rev_id=b'rev-1')
2405.1.2 by John Arbash Meinel
Fix bug #103870 by passing None instead of a (sometimes wrong) path
504
505
        tree.rename_one('dir', 'other')
6855.4.1 by Jelmer Vernooij
Yet more bees.
506
        self.build_tree_contents([('tree/other/file', b'new contents\n')])
5784.3.1 by Martin Pool
Remove unnecessary TestShowDiffTreesHelper and just use a function
507
        d = get_diff_as_string(tree.basis_tree(), tree)
7031.1.1 by Jelmer Vernooij
Fix breezy.tests.test_diff.
508
        self.assertContainsRe(d, b"=== renamed directory 'dir' => 'other'\n")
509
        self.assertContainsRe(d, b"=== modified file 'other/file'\n")
2405.1.2 by John Arbash Meinel
Fix bug #103870 by passing None instead of a (sometimes wrong) path
510
        # XXX: This is technically incorrect, because it used to be at another
511
        # location. What to do?
7031.1.1 by Jelmer Vernooij
Fix breezy.tests.test_diff.
512
        self.assertContainsRe(d, b'--- old/dir/file\t')
513
        self.assertContainsRe(d, b'\\+\\+\\+ new/other/file\t')
514
        self.assertContainsRe(d, b'-contents\n'
515
                                 b'\\+new contents\n')
2405.1.2 by John Arbash Meinel
Fix bug #103870 by passing None instead of a (sometimes wrong) path
516
2405.1.1 by John Arbash Meinel
Add a bunch of direct tests for 'show_diff_trees'
517
    def test_renamed_directory(self):
518
        """Test when only a directory is only renamed."""
519
        tree = self.make_branch_and_tree('tree')
520
        self.build_tree(['tree/dir/'])
6855.4.1 by Jelmer Vernooij
Yet more bees.
521
        self.build_tree_contents([('tree/dir/file', b'contents\n')])
522
        tree.add(['dir', 'dir/file'], [b'dir-id', b'file-id'])
523
        tree.commit('one', rev_id=b'rev-1')
2405.1.1 by John Arbash Meinel
Add a bunch of direct tests for 'show_diff_trees'
524
525
        tree.rename_one('dir', 'newdir')
5784.3.1 by Martin Pool
Remove unnecessary TestShowDiffTreesHelper and just use a function
526
        d = get_diff_as_string(tree.basis_tree(), tree)
2405.1.1 by John Arbash Meinel
Add a bunch of direct tests for 'show_diff_trees'
527
        # Renaming a directory should be a single "you renamed this dir" even
528
        # when there are files inside.
7031.1.1 by Jelmer Vernooij
Fix breezy.tests.test_diff.
529
        self.assertEqual(d, b"=== renamed directory 'dir' => 'newdir'\n")
2405.1.1 by John Arbash Meinel
Add a bunch of direct tests for 'show_diff_trees'
530
531
    def test_renamed_file(self):
532
        """Test when a file is only renamed."""
533
        tree = self.make_branch_and_tree('tree')
6855.4.1 by Jelmer Vernooij
Yet more bees.
534
        self.build_tree_contents([('tree/file', b'contents\n')])
535
        tree.add(['file'], [b'file-id'])
536
        tree.commit('one', rev_id=b'rev-1')
2405.1.1 by John Arbash Meinel
Add a bunch of direct tests for 'show_diff_trees'
537
538
        tree.rename_one('file', 'newname')
5784.3.1 by Martin Pool
Remove unnecessary TestShowDiffTreesHelper and just use a function
539
        d = get_diff_as_string(tree.basis_tree(), tree)
7031.1.1 by Jelmer Vernooij
Fix breezy.tests.test_diff.
540
        self.assertContainsRe(d, b"=== renamed file 'file' => 'newname'\n")
2405.1.1 by John Arbash Meinel
Add a bunch of direct tests for 'show_diff_trees'
541
        # We shouldn't have a --- or +++ line, because there is no content
542
        # change
7031.1.1 by Jelmer Vernooij
Fix breezy.tests.test_diff.
543
        self.assertNotContainsRe(d, b'---')
2405.1.1 by John Arbash Meinel
Add a bunch of direct tests for 'show_diff_trees'
544
545
    def test_renamed_and_modified_file(self):
546
        """Test when a file is only renamed."""
547
        tree = self.make_branch_and_tree('tree')
6855.4.1 by Jelmer Vernooij
Yet more bees.
548
        self.build_tree_contents([('tree/file', b'contents\n')])
549
        tree.add(['file'], [b'file-id'])
550
        tree.commit('one', rev_id=b'rev-1')
2405.1.1 by John Arbash Meinel
Add a bunch of direct tests for 'show_diff_trees'
551
552
        tree.rename_one('file', 'newname')
6855.4.1 by Jelmer Vernooij
Yet more bees.
553
        self.build_tree_contents([('tree/newname', b'new contents\n')])
5784.3.1 by Martin Pool
Remove unnecessary TestShowDiffTreesHelper and just use a function
554
        d = get_diff_as_string(tree.basis_tree(), tree)
7031.1.1 by Jelmer Vernooij
Fix breezy.tests.test_diff.
555
        self.assertContainsRe(d, b"=== renamed file 'file' => 'newname'\n")
556
        self.assertContainsRe(d, b'--- old/file\t')
557
        self.assertContainsRe(d, b'\\+\\+\\+ new/newname\t')
558
        self.assertContainsRe(d, b'-contents\n'
559
                                 b'\\+new contents\n')
2405.1.1 by John Arbash Meinel
Add a bunch of direct tests for 'show_diff_trees'
560
3268.1.1 by C Miller
Describe the property changes in diffs. Currently, this is the executable-bit
561
    def test_internal_diff_exec_property(self):
562
        tree = self.make_branch_and_tree('tree')
563
7350.3.2 by Jelmer Vernooij
Use Tree.get_transform.
564
        tt = tree.get_transform()
6973.13.2 by Jelmer Vernooij
Fix some more tests.
565
        tt.new_file('a', tt.root, [b'contents\n'], b'a-id', True)
566
        tt.new_file('b', tt.root, [b'contents\n'], b'b-id', False)
567
        tt.new_file('c', tt.root, [b'contents\n'], b'c-id', True)
568
        tt.new_file('d', tt.root, [b'contents\n'], b'd-id', False)
569
        tt.new_file('e', tt.root, [b'contents\n'], b'control-e-id', True)
570
        tt.new_file('f', tt.root, [b'contents\n'], b'control-f-id', False)
3268.1.1 by C Miller
Describe the property changes in diffs. Currently, this is the executable-bit
571
        tt.apply()
6855.4.1 by Jelmer Vernooij
Yet more bees.
572
        tree.commit('one', rev_id=b'rev-1')
3268.1.1 by C Miller
Describe the property changes in diffs. Currently, this is the executable-bit
573
7350.3.2 by Jelmer Vernooij
Use Tree.get_transform.
574
        tt = tree.get_transform()
6973.13.2 by Jelmer Vernooij
Fix some more tests.
575
        tt.set_executability(False, tt.trans_id_file_id(b'a-id'))
576
        tt.set_executability(True, tt.trans_id_file_id(b'b-id'))
577
        tt.set_executability(False, tt.trans_id_file_id(b'c-id'))
578
        tt.set_executability(True, tt.trans_id_file_id(b'd-id'))
3268.1.1 by C Miller
Describe the property changes in diffs. Currently, this is the executable-bit
579
        tt.apply()
580
        tree.rename_one('c', 'new-c')
581
        tree.rename_one('d', 'new-d')
582
5784.3.1 by Martin Pool
Remove unnecessary TestShowDiffTreesHelper and just use a function
583
        d = get_diff_as_string(tree.basis_tree(), tree)
3268.1.1 by C Miller
Describe the property changes in diffs. Currently, this is the executable-bit
584
7031.1.1 by Jelmer Vernooij
Fix breezy.tests.test_diff.
585
        self.assertContainsRe(d, br"file 'a'.*\(properties changed:"
586
                                 br".*\+x to -x.*\)")
587
        self.assertContainsRe(d, br"file 'b'.*\(properties changed:"
588
                                 br".*-x to \+x.*\)")
589
        self.assertContainsRe(d, br"file 'c'.*\(properties changed:"
590
                                 br".*\+x to -x.*\)")
591
        self.assertContainsRe(d, br"file 'd'.*\(properties changed:"
592
                                 br".*-x to \+x.*\)")
593
        self.assertNotContainsRe(d, br"file 'e'")
594
        self.assertNotContainsRe(d, br"file 'f'")
3268.1.1 by C Miller
Describe the property changes in diffs. Currently, this is the executable-bit
595
2592.2.1 by Jonathan Lange
Reproduce and fix bug 110092.
596
    def test_binary_unicode_filenames(self):
2592.2.2 by Jonathan Lange
Apply jam's comments to test_binary_unicode_filenames. Change the
597
        """Test that contents of files are *not* encoded in UTF-8 when there
598
        is a binary file in the diff.
2592.2.1 by Jonathan Lange
Reproduce and fix bug 110092.
599
        """
600
        # See https://bugs.launchpad.net/bugs/110092.
5967.12.1 by Martin Pool
Move all test features into bzrlib.tests.features
601
        self.requireFeature(features.UnicodeFilenameFeature)
2592.2.1 by Jonathan Lange
Reproduce and fix bug 110092.
602
603
        tree = self.make_branch_and_tree('tree')
2592.2.2 by Jonathan Lange
Apply jam's comments to test_binary_unicode_filenames. Change the
604
        alpha, omega = u'\u03b1', u'\u03c9'
605
        alpha_utf8, omega_utf8 = alpha.encode('utf8'), omega.encode('utf8')
2592.2.1 by Jonathan Lange
Reproduce and fix bug 110092.
606
        self.build_tree_contents(
7031.1.1 by Jelmer Vernooij
Fix breezy.tests.test_diff.
607
            [('tree/' + alpha, b'\0'),
2592.2.2 by Jonathan Lange
Apply jam's comments to test_binary_unicode_filenames. Change the
608
             ('tree/' + omega,
7031.1.1 by Jelmer Vernooij
Fix breezy.tests.test_diff.
609
              (b'The %s and the %s\n' % (alpha_utf8, omega_utf8)))])
6855.4.1 by Jelmer Vernooij
Yet more bees.
610
        tree.add([alpha], [b'file-id'])
611
        tree.add([omega], [b'file-id-2'])
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
612
        diff_content = StubO()
5168.1.4 by Vincent Ladeuil
Final import fixes for bt.test_diff.
613
        diff.show_diff_trees(tree.basis_tree(), tree, diff_content)
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
614
        diff_content.check_types(self, bytes)
615
        d = b''.join(diff_content.write_record)
7031.1.1 by Jelmer Vernooij
Fix breezy.tests.test_diff.
616
        self.assertContainsRe(d, br"=== added file '%s'" % alpha_utf8)
617
        self.assertContainsRe(d, b"Binary files a/%s.*and b/%s.* differ\n"
5168.1.4 by Vincent Ladeuil
Final import fixes for bt.test_diff.
618
                              % (alpha_utf8, alpha_utf8))
7031.1.1 by Jelmer Vernooij
Fix breezy.tests.test_diff.
619
        self.assertContainsRe(d, br"=== added file '%s'" % omega_utf8)
620
        self.assertContainsRe(d, br"--- a/%s" % (omega_utf8,))
621
        self.assertContainsRe(d, br"\+\+\+ b/%s" % (omega_utf8,))
2592.2.1 by Jonathan Lange
Reproduce and fix bug 110092.
622
2725.2.1 by ghigo
When a unicode filename is renamed, in the diff is showed a wrong result
623
    def test_unicode_filename(self):
624
        """Test when the filename are unicode."""
5967.12.1 by Martin Pool
Move all test features into bzrlib.tests.features
625
        self.requireFeature(features.UnicodeFilenameFeature)
2725.2.1 by ghigo
When a unicode filename is renamed, in the diff is showed a wrong result
626
627
        alpha, omega = u'\u03b1', u'\u03c9'
628
        autf8, outf8 = alpha.encode('utf8'), omega.encode('utf8')
629
630
        tree = self.make_branch_and_tree('tree')
7143.15.2 by Jelmer Vernooij
Run autopep8.
631
        self.build_tree_contents([('tree/ren_' + alpha, b'contents\n')])
632
        tree.add(['ren_' + alpha], [b'file-id-2'])
633
        self.build_tree_contents([('tree/del_' + alpha, b'contents\n')])
634
        tree.add(['del_' + alpha], [b'file-id-3'])
635
        self.build_tree_contents([('tree/mod_' + alpha, b'contents\n')])
636
        tree.add(['mod_' + alpha], [b'file-id-4'])
2725.2.1 by ghigo
When a unicode filename is renamed, in the diff is showed a wrong result
637
6855.4.1 by Jelmer Vernooij
Yet more bees.
638
        tree.commit('one', rev_id=b'rev-1')
2725.2.1 by ghigo
When a unicode filename is renamed, in the diff is showed a wrong result
639
7143.15.2 by Jelmer Vernooij
Run autopep8.
640
        tree.rename_one('ren_' + alpha, 'ren_' + omega)
641
        tree.remove('del_' + alpha)
642
        self.build_tree_contents([('tree/add_' + alpha, b'contents\n')])
643
        tree.add(['add_' + alpha], [b'file-id'])
644
        self.build_tree_contents([('tree/mod_' + alpha, b'contents_mod\n')])
2725.2.1 by ghigo
When a unicode filename is renamed, in the diff is showed a wrong result
645
5784.3.1 by Martin Pool
Remove unnecessary TestShowDiffTreesHelper and just use a function
646
        d = get_diff_as_string(tree.basis_tree(), tree)
5168.1.4 by Vincent Ladeuil
Final import fixes for bt.test_diff.
647
        self.assertContainsRe(d,
7143.15.2 by Jelmer Vernooij
Run autopep8.
648
                              b"=== renamed file 'ren_%s' => 'ren_%s'\n" % (autf8, outf8))
649
        self.assertContainsRe(d, b"=== added file 'add_%s'" % autf8)
650
        self.assertContainsRe(d, b"=== modified file 'mod_%s'" % autf8)
651
        self.assertContainsRe(d, b"=== removed file 'del_%s'" % autf8)
5168.1.4 by Vincent Ladeuil
Final import fixes for bt.test_diff.
652
4797.57.6 by Alexander Belchenko
added whitebox test for path_encoding in diff.
653
    def test_unicode_filename_path_encoding(self):
654
        """Test for bug #382699: unicode filenames on Windows should be shown
655
        in user encoding.
656
        """
5967.12.1 by Martin Pool
Move all test features into bzrlib.tests.features
657
        self.requireFeature(features.UnicodeFilenameFeature)
4797.57.6 by Alexander Belchenko
added whitebox test for path_encoding in diff.
658
        # The word 'test' in Russian
659
        _russian_test = u'\u0422\u0435\u0441\u0442'
660
        directory = _russian_test + u'/'
661
        test_txt = _russian_test + u'.txt'
662
        u1234 = u'\u1234.txt'
663
664
        tree = self.make_branch_and_tree('.')
665
        self.build_tree_contents([
6855.4.1 by Jelmer Vernooij
Yet more bees.
666
            (test_txt, b'foo\n'),
667
            (u1234, b'foo\n'),
4797.57.6 by Alexander Belchenko
added whitebox test for path_encoding in diff.
668
            (directory, None),
669
            ])
670
        tree.add([test_txt, u1234, directory])
671
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
672
        sio = BytesIO()
5258.1.1 by Alexander Belchenko
merge diff header work from my 2.1 branch
673
        diff.show_diff_trees(tree.basis_tree(), tree, sio,
7143.15.2 by Jelmer Vernooij
Run autopep8.
674
                             path_encoding='cp1251')
4797.57.6 by Alexander Belchenko
added whitebox test for path_encoding in diff.
675
676
        output = subst_dates(sio.getvalue())
7031.1.1 by Jelmer Vernooij
Fix breezy.tests.test_diff.
677
        shouldbe = (b'''\
4797.57.6 by Alexander Belchenko
added whitebox test for path_encoding in diff.
678
=== added directory '%(directory)s'
679
=== added file '%(test_txt)s'
680
--- a/%(test_txt)s\tYYYY-MM-DD HH:MM:SS +ZZZZ
681
+++ b/%(test_txt)s\tYYYY-MM-DD HH:MM:SS +ZZZZ
682
@@ -0,0 +1,1 @@
683
+foo
684
685
=== added file '?.txt'
686
--- a/?.txt\tYYYY-MM-DD HH:MM:SS +ZZZZ
687
+++ b/?.txt\tYYYY-MM-DD HH:MM:SS +ZZZZ
688
@@ -0,0 +1,1 @@
689
+foo
690
7031.1.1 by Jelmer Vernooij
Fix breezy.tests.test_diff.
691
''' % {b'directory': _russian_test.encode('cp1251'),
7143.15.2 by Jelmer Vernooij
Run autopep8.
692
            b'test_txt': test_txt.encode('cp1251'),
693
       })
4797.57.6 by Alexander Belchenko
added whitebox test for path_encoding in diff.
694
        self.assertEqualDiff(output, shouldbe)
695
5168.1.4 by Vincent Ladeuil
Final import fixes for bt.test_diff.
696
697
class DiffWasIs(diff.DiffPath):
3009.2.15 by Aaron Bentley
Test differ registration
698
7206.6.2 by Jelmer Vernooij
Remove file_id from diff API.
699
    def diff(self, old_path, new_path, old_kind, new_kind):
7031.1.1 by Jelmer Vernooij
Fix breezy.tests.test_diff.
700
        self.to_file.write(b'was: ')
6809.4.5 by Jelmer Vernooij
Swap arguments for get_file_*.
701
        self.to_file.write(self.old_tree.get_file(old_path).read())
7031.1.1 by Jelmer Vernooij
Fix breezy.tests.test_diff.
702
        self.to_file.write(b'is: ')
6809.4.5 by Jelmer Vernooij
Swap arguments for get_file_*.
703
        self.to_file.write(self.new_tree.get_file(new_path).read())
3009.2.15 by Aaron Bentley
Test differ registration
704
705
5168.1.2 by Vincent Ladeuil
Ckeanup some more imports.
706
class TestDiffTree(tests.TestCaseWithTransport):
3009.2.9 by Aaron Bentley
Add tests for Differ
707
708
    def setUp(self):
5168.1.2 by Vincent Ladeuil
Ckeanup some more imports.
709
        super(TestDiffTree, self).setUp()
3009.2.9 by Aaron Bentley
Add tests for Differ
710
        self.old_tree = self.make_branch_and_tree('old-tree')
711
        self.old_tree.lock_write()
712
        self.addCleanup(self.old_tree.unlock)
713
        self.new_tree = self.make_branch_and_tree('new-tree')
714
        self.new_tree.lock_write()
715
        self.addCleanup(self.new_tree.unlock)
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
716
        self.differ = diff.DiffTree(self.old_tree, self.new_tree, BytesIO())
3009.2.9 by Aaron Bentley
Add tests for Differ
717
718
    def test_diff_text(self):
719
        self.build_tree_contents([('old-tree/olddir/',),
6855.4.1 by Jelmer Vernooij
Yet more bees.
720
                                  ('old-tree/olddir/oldfile', b'old\n')])
3009.2.9 by Aaron Bentley
Add tests for Differ
721
        self.old_tree.add('olddir')
6855.4.1 by Jelmer Vernooij
Yet more bees.
722
        self.old_tree.add('olddir/oldfile', b'file-id')
3009.2.9 by Aaron Bentley
Add tests for Differ
723
        self.build_tree_contents([('new-tree/newdir/',),
6855.4.1 by Jelmer Vernooij
Yet more bees.
724
                                  ('new-tree/newdir/newfile', b'new\n')])
3009.2.9 by Aaron Bentley
Add tests for Differ
725
        self.new_tree.add('newdir')
6855.4.1 by Jelmer Vernooij
Yet more bees.
726
        self.new_tree.add('newdir/newfile', b'file-id')
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
727
        differ = diff.DiffText(self.old_tree, self.new_tree, BytesIO())
7206.6.2 by Jelmer Vernooij
Remove file_id from diff API.
728
        differ.diff_text('olddir/oldfile', None, 'old label', 'new label')
3009.2.9 by Aaron Bentley
Add tests for Differ
729
        self.assertEqual(
7031.1.1 by Jelmer Vernooij
Fix breezy.tests.test_diff.
730
            b'--- old label\n+++ new label\n@@ -1,1 +0,0 @@\n-old\n\n',
3009.2.11 by Aaron Bentley
Refactor diff to be more pluggable
731
            differ.to_file.getvalue())
732
        differ.to_file.seek(0)
6809.4.9 by Jelmer Vernooij
Fix some more tests.
733
        differ.diff_text(None, 'newdir/newfile',
7206.6.2 by Jelmer Vernooij
Remove file_id from diff API.
734
                         'old label', 'new label')
3009.2.9 by Aaron Bentley
Add tests for Differ
735
        self.assertEqual(
7031.1.1 by Jelmer Vernooij
Fix breezy.tests.test_diff.
736
            b'--- old label\n+++ new label\n@@ -0,0 +1,1 @@\n+new\n\n',
3009.2.11 by Aaron Bentley
Refactor diff to be more pluggable
737
            differ.to_file.getvalue())
738
        differ.to_file.seek(0)
6809.4.9 by Jelmer Vernooij
Fix some more tests.
739
        differ.diff_text('olddir/oldfile', 'newdir/newfile',
7206.6.2 by Jelmer Vernooij
Remove file_id from diff API.
740
                         'old label', 'new label')
3009.2.9 by Aaron Bentley
Add tests for Differ
741
        self.assertEqual(
7031.1.1 by Jelmer Vernooij
Fix breezy.tests.test_diff.
742
            b'--- old label\n+++ new label\n@@ -1,1 +1,1 @@\n-old\n+new\n\n',
3009.2.11 by Aaron Bentley
Refactor diff to be more pluggable
743
            differ.to_file.getvalue())
3009.2.9 by Aaron Bentley
Add tests for Differ
744
3087.1.1 by Aaron Bentley
Diff handles missing files correctly, with no tracebacks
745
    def test_diff_deletion(self):
6855.4.1 by Jelmer Vernooij
Yet more bees.
746
        self.build_tree_contents([('old-tree/file', b'contents'),
747
                                  ('new-tree/file', b'contents')])
748
        self.old_tree.add('file', b'file-id')
749
        self.new_tree.add('file', b'file-id')
3087.1.1 by Aaron Bentley
Diff handles missing files correctly, with no tracebacks
750
        os.unlink('new-tree/file')
751
        self.differ.show_diff(None)
7031.1.1 by Jelmer Vernooij
Fix breezy.tests.test_diff.
752
        self.assertContainsRe(self.differ.to_file.getvalue(), b'-contents')
3087.1.1 by Aaron Bentley
Diff handles missing files correctly, with no tracebacks
753
754
    def test_diff_creation(self):
6855.4.1 by Jelmer Vernooij
Yet more bees.
755
        self.build_tree_contents([('old-tree/file', b'contents'),
756
                                  ('new-tree/file', b'contents')])
757
        self.old_tree.add('file', b'file-id')
758
        self.new_tree.add('file', b'file-id')
3087.1.1 by Aaron Bentley
Diff handles missing files correctly, with no tracebacks
759
        os.unlink('old-tree/file')
760
        self.differ.show_diff(None)
7031.1.1 by Jelmer Vernooij
Fix breezy.tests.test_diff.
761
        self.assertContainsRe(self.differ.to_file.getvalue(), br'\+contents')
3087.1.1 by Aaron Bentley
Diff handles missing files correctly, with no tracebacks
762
3009.2.9 by Aaron Bentley
Add tests for Differ
763
    def test_diff_symlink(self):
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
764
        differ = diff.DiffSymlink(self.old_tree, self.new_tree, BytesIO())
3009.2.11 by Aaron Bentley
Refactor diff to be more pluggable
765
        differ.diff_symlink('old target', None)
7031.1.1 by Jelmer Vernooij
Fix breezy.tests.test_diff.
766
        self.assertEqual(b"=== target was 'old target'\n",
3009.2.11 by Aaron Bentley
Refactor diff to be more pluggable
767
                         differ.to_file.getvalue())
3009.2.9 by Aaron Bentley
Add tests for Differ
768
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
769
        differ = diff.DiffSymlink(self.old_tree, self.new_tree, BytesIO())
3009.2.11 by Aaron Bentley
Refactor diff to be more pluggable
770
        differ.diff_symlink(None, 'new target')
7031.1.1 by Jelmer Vernooij
Fix breezy.tests.test_diff.
771
        self.assertEqual(b"=== target is 'new target'\n",
3009.2.11 by Aaron Bentley
Refactor diff to be more pluggable
772
                         differ.to_file.getvalue())
773
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
774
        differ = diff.DiffSymlink(self.old_tree, self.new_tree, BytesIO())
3009.2.11 by Aaron Bentley
Refactor diff to be more pluggable
775
        differ.diff_symlink('old target', 'new target')
7031.1.1 by Jelmer Vernooij
Fix breezy.tests.test_diff.
776
        self.assertEqual(b"=== target changed 'old target' => 'new target'\n",
3009.2.11 by Aaron Bentley
Refactor diff to be more pluggable
777
                         differ.to_file.getvalue())
3009.2.9 by Aaron Bentley
Add tests for Differ
778
779
    def test_diff(self):
780
        self.build_tree_contents([('old-tree/olddir/',),
6855.4.1 by Jelmer Vernooij
Yet more bees.
781
                                  ('old-tree/olddir/oldfile', b'old\n')])
3009.2.9 by Aaron Bentley
Add tests for Differ
782
        self.old_tree.add('olddir')
6855.4.1 by Jelmer Vernooij
Yet more bees.
783
        self.old_tree.add('olddir/oldfile', b'file-id')
3009.2.9 by Aaron Bentley
Add tests for Differ
784
        self.build_tree_contents([('new-tree/newdir/',),
6855.4.1 by Jelmer Vernooij
Yet more bees.
785
                                  ('new-tree/newdir/newfile', b'new\n')])
3009.2.9 by Aaron Bentley
Add tests for Differ
786
        self.new_tree.add('newdir')
6855.4.1 by Jelmer Vernooij
Yet more bees.
787
        self.new_tree.add('newdir/newfile', b'file-id')
7206.6.2 by Jelmer Vernooij
Remove file_id from diff API.
788
        self.differ.diff('olddir/oldfile', 'newdir/newfile')
3009.2.9 by Aaron Bentley
Add tests for Differ
789
        self.assertContainsRe(
790
            self.differ.to_file.getvalue(),
7031.1.1 by Jelmer Vernooij
Fix breezy.tests.test_diff.
791
            br'--- olddir/oldfile.*\n\+\+\+ newdir/newfile.*\n\@\@ -1,1 \+1,1'
792
            br' \@\@\n-old\n\+new\n\n')
3009.2.9 by Aaron Bentley
Add tests for Differ
793
794
    def test_diff_kind_change(self):
5967.12.1 by Martin Pool
Move all test features into bzrlib.tests.features
795
        self.requireFeature(features.SymlinkFeature)
3009.2.9 by Aaron Bentley
Add tests for Differ
796
        self.build_tree_contents([('old-tree/olddir/',),
6855.4.1 by Jelmer Vernooij
Yet more bees.
797
                                  ('old-tree/olddir/oldfile', b'old\n')])
3009.2.9 by Aaron Bentley
Add tests for Differ
798
        self.old_tree.add('olddir')
6855.4.1 by Jelmer Vernooij
Yet more bees.
799
        self.old_tree.add('olddir/oldfile', b'file-id')
3009.2.9 by Aaron Bentley
Add tests for Differ
800
        self.build_tree(['new-tree/newdir/'])
801
        os.symlink('new', 'new-tree/newdir/newfile')
802
        self.new_tree.add('newdir')
6855.4.1 by Jelmer Vernooij
Yet more bees.
803
        self.new_tree.add('newdir/newfile', b'file-id')
7206.6.2 by Jelmer Vernooij
Remove file_id from diff API.
804
        self.differ.diff('olddir/oldfile', 'newdir/newfile')
3009.2.9 by Aaron Bentley
Add tests for Differ
805
        self.assertContainsRe(
806
            self.differ.to_file.getvalue(),
7031.1.1 by Jelmer Vernooij
Fix breezy.tests.test_diff.
807
            br'--- olddir/oldfile.*\n\+\+\+ newdir/newfile.*\n\@\@ -1,1 \+0,0'
808
            br' \@\@\n-old\n\n')
3009.2.9 by Aaron Bentley
Add tests for Differ
809
        self.assertContainsRe(self.differ.to_file.getvalue(),
7031.1.1 by Jelmer Vernooij
Fix breezy.tests.test_diff.
810
                              b"=== target is 'new'\n")
3009.2.9 by Aaron Bentley
Add tests for Differ
811
3009.2.19 by Aaron Bentley
Implement directory diffing
812
    def test_diff_directory(self):
813
        self.build_tree(['new-tree/new-dir/'])
6855.4.1 by Jelmer Vernooij
Yet more bees.
814
        self.new_tree.add('new-dir', b'new-dir-id')
7206.6.2 by Jelmer Vernooij
Remove file_id from diff API.
815
        self.differ.diff(None, 'new-dir')
6973.12.3 by Jelmer Vernooij
Fixes.
816
        self.assertEqual(self.differ.to_file.getvalue(), b'')
3009.2.19 by Aaron Bentley
Implement directory diffing
817
3009.2.16 by Aaron Bentley
Test support for extra differs
818
    def create_old_new(self):
819
        self.build_tree_contents([('old-tree/olddir/',),
6855.4.1 by Jelmer Vernooij
Yet more bees.
820
                                  ('old-tree/olddir/oldfile', b'old\n')])
3009.2.16 by Aaron Bentley
Test support for extra differs
821
        self.old_tree.add('olddir')
6855.4.1 by Jelmer Vernooij
Yet more bees.
822
        self.old_tree.add('olddir/oldfile', b'file-id')
3009.2.16 by Aaron Bentley
Test support for extra differs
823
        self.build_tree_contents([('new-tree/newdir/',),
6855.4.1 by Jelmer Vernooij
Yet more bees.
824
                                  ('new-tree/newdir/newfile', b'new\n')])
3009.2.16 by Aaron Bentley
Test support for extra differs
825
        self.new_tree.add('newdir')
6855.4.1 by Jelmer Vernooij
Yet more bees.
826
        self.new_tree.add('newdir/newfile', b'file-id')
3009.2.16 by Aaron Bentley
Test support for extra differs
827
3009.2.27 by Aaron Bentley
Use extra_factories instead of extra_diffs
828
    def test_register_diff(self):
3009.2.16 by Aaron Bentley
Test support for extra differs
829
        self.create_old_new()
5168.1.4 by Vincent Ladeuil
Final import fixes for bt.test_diff.
830
        old_diff_factories = diff.DiffTree.diff_factories
7143.15.2 by Jelmer Vernooij
Run autopep8.
831
        diff.DiffTree.diff_factories = old_diff_factories[:]
5168.1.4 by Vincent Ladeuil
Final import fixes for bt.test_diff.
832
        diff.DiffTree.diff_factories.insert(0, DiffWasIs.from_diff_tree)
3009.2.16 by Aaron Bentley
Test support for extra differs
833
        try:
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
834
            differ = diff.DiffTree(self.old_tree, self.new_tree, BytesIO())
3009.2.16 by Aaron Bentley
Test support for extra differs
835
        finally:
5168.1.4 by Vincent Ladeuil
Final import fixes for bt.test_diff.
836
            diff.DiffTree.diff_factories = old_diff_factories
7206.6.2 by Jelmer Vernooij
Remove file_id from diff API.
837
        differ.diff('olddir/oldfile', 'newdir/newfile')
3009.2.16 by Aaron Bentley
Test support for extra differs
838
        self.assertNotContainsRe(
839
            differ.to_file.getvalue(),
7031.1.1 by Jelmer Vernooij
Fix breezy.tests.test_diff.
840
            br'--- olddir/oldfile.*\n\+\+\+ newdir/newfile.*\n\@\@ -1,1 \+1,1'
841
            br' \@\@\n-old\n\+new\n\n')
3009.2.16 by Aaron Bentley
Test support for extra differs
842
        self.assertContainsRe(differ.to_file.getvalue(),
7031.1.1 by Jelmer Vernooij
Fix breezy.tests.test_diff.
843
                              b'was: old\nis: new\n')
3009.2.16 by Aaron Bentley
Test support for extra differs
844
3009.2.27 by Aaron Bentley
Use extra_factories instead of extra_diffs
845
    def test_extra_factories(self):
3009.2.16 by Aaron Bentley
Test support for extra differs
846
        self.create_old_new()
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
847
        differ = diff.DiffTree(self.old_tree, self.new_tree, BytesIO(),
5168.1.4 by Vincent Ladeuil
Final import fixes for bt.test_diff.
848
                               extra_factories=[DiffWasIs.from_diff_tree])
7206.6.2 by Jelmer Vernooij
Remove file_id from diff API.
849
        differ.diff('olddir/oldfile', 'newdir/newfile')
3009.2.16 by Aaron Bentley
Test support for extra differs
850
        self.assertNotContainsRe(
851
            differ.to_file.getvalue(),
7031.1.1 by Jelmer Vernooij
Fix breezy.tests.test_diff.
852
            br'--- olddir/oldfile.*\n\+\+\+ newdir/newfile.*\n\@\@ -1,1 \+1,1'
853
            br' \@\@\n-old\n\+new\n\n')
3009.2.16 by Aaron Bentley
Test support for extra differs
854
        self.assertContainsRe(differ.to_file.getvalue(),
7031.1.1 by Jelmer Vernooij
Fix breezy.tests.test_diff.
855
                              b'was: old\nis: new\n')
3009.2.16 by Aaron Bentley
Test support for extra differs
856
3123.4.1 by Aaron Bentley
Diff sorts files in alphabetical order
857
    def test_alphabetical_order(self):
858
        self.build_tree(['new-tree/a-file'])
859
        self.new_tree.add('a-file')
860
        self.build_tree(['old-tree/b-file'])
861
        self.old_tree.add('b-file')
862
        self.differ.show_diff(None)
863
        self.assertContainsRe(self.differ.to_file.getvalue(),
7143.15.2 by Jelmer Vernooij
Run autopep8.
864
                              b'.*a-file(.|\n)*b-file')
3123.4.1 by Aaron Bentley
Diff sorts files in alphabetical order
865
3009.2.9 by Aaron Bentley
Add tests for Differ
866
5168.1.2 by Vincent Ladeuil
Ckeanup some more imports.
867
class TestDiffFromTool(tests.TestCaseWithTransport):
3123.6.2 by Aaron Bentley
Implement diff --using natively
868
869
    def test_from_string(self):
7358.6.1 by Jelmer Vernooij
Use standard syntax for the ``change_editor`` configuration option.
870
        diff_obj = diff.DiffFromTool.from_string(
871
            ['diff', '{old_path}', '{new_path}'],
872
            None, None, None)
3123.6.2 by Aaron Bentley
Implement diff --using natively
873
        self.addCleanup(diff_obj.finish)
7358.6.1 by Jelmer Vernooij
Use standard syntax for the ``change_editor`` configuration option.
874
        self.assertEqual(['diff', '{old_path}', '{new_path}'],
7143.15.2 by Jelmer Vernooij
Run autopep8.
875
                         diff_obj.command_template)
3199.1.6 by Vincent Ladeuil
Fiz last leaking tmp dir.
876
877
    def test_from_string_u5(self):
7358.6.1 by Jelmer Vernooij
Use standard syntax for the ``change_editor`` configuration option.
878
        diff_obj = diff.DiffFromTool.from_string(
7358.6.3 by Jelmer Vernooij
Fix formatting.
879
            ['diff', "-u 5", '{old_path}', '{new_path}'], None, None, None)
3199.1.6 by Vincent Ladeuil
Fiz last leaking tmp dir.
880
        self.addCleanup(diff_obj.finish)
7358.6.1 by Jelmer Vernooij
Use standard syntax for the ``change_editor`` configuration option.
881
        self.assertEqual(['diff', '-u 5', '{old_path}', '{new_path}'],
3123.6.2 by Aaron Bentley
Implement diff --using natively
882
                         diff_obj.command_template)
883
        self.assertEqual(['diff', '-u 5', 'old-path', 'new-path'],
884
                         diff_obj._get_command('old-path', 'new-path'))
5168.1.2 by Vincent Ladeuil
Ckeanup some more imports.
885
4913.5.8 by Gordon Tyler
Added test_from_string_path_with_backslashes, which tests the actual scenario in bug 392428.
886
    def test_from_string_path_with_backslashes(self):
5241.2.2 by Robert Collins
Missed one test.
887
        self.requireFeature(features.backslashdir_feature)
7358.6.1 by Jelmer Vernooij
Use standard syntax for the ``change_editor`` configuration option.
888
        tool = ['C:\\Tools\\Diff.exe', '{old_path}', '{new_path}']
5168.1.4 by Vincent Ladeuil
Final import fixes for bt.test_diff.
889
        diff_obj = diff.DiffFromTool.from_string(tool, None, None, None)
4913.5.8 by Gordon Tyler
Added test_from_string_path_with_backslashes, which tests the actual scenario in bug 392428.
890
        self.addCleanup(diff_obj.finish)
7358.6.1 by Jelmer Vernooij
Use standard syntax for the ``change_editor`` configuration option.
891
        self.assertEqual(['C:\\Tools\\Diff.exe', '{old_path}', '{new_path}'],
4913.5.8 by Gordon Tyler
Added test_from_string_path_with_backslashes, which tests the actual scenario in bug 392428.
892
                         diff_obj.command_template)
893
        self.assertEqual(['C:\\Tools\\Diff.exe', 'old-path', 'new-path'],
894
                         diff_obj._get_command('old-path', 'new-path'))
3123.6.2 by Aaron Bentley
Implement diff --using natively
895
896
    def test_execute(self):
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
897
        output = BytesIO()
7141.1.1 by Jelmer Vernooij
Use sys.executable rather than python for ad-hoc tests.
898
        diff_obj = diff.DiffFromTool([sys.executable, '-c',
7358.6.1 by Jelmer Vernooij
Use standard syntax for the ``change_editor`` configuration option.
899
                                      'print("{old_path} {new_path}")'],
5168.1.4 by Vincent Ladeuil
Final import fixes for bt.test_diff.
900
                                     None, None, output)
3123.6.2 by Aaron Bentley
Implement diff --using natively
901
        self.addCleanup(diff_obj.finish)
902
        diff_obj._execute('old', 'new')
6973.12.3 by Jelmer Vernooij
Fixes.
903
        self.assertEqual(output.getvalue().rstrip(), b'old new')
3123.6.2 by Aaron Bentley
Implement diff --using natively
904
6597.2.1 by Richard Wilbur
Split diff format option parser into a separate function, update to include all format options for GNU diff v3.2, and test parser.
905
    def test_execute_missing(self):
5168.1.4 by Vincent Ladeuil
Final import fixes for bt.test_diff.
906
        diff_obj = diff.DiffFromTool(['a-tool-which-is-unlikely-to-exist'],
907
                                     None, None, None)
3145.1.1 by Aaron Bentley
Handle missing tools gracefully in diff --using
908
        self.addCleanup(diff_obj.finish)
5168.1.2 by Vincent Ladeuil
Ckeanup some more imports.
909
        e = self.assertRaises(errors.ExecutableMissing, diff_obj._execute,
910
                              'old', 'new')
3145.1.1 by Aaron Bentley
Handle missing tools gracefully in diff --using
911
        self.assertEqual('a-tool-which-is-unlikely-to-exist could not be found'
912
                         ' on this machine', str(e))
913
3287.18.22 by Matt McClure
Reverts to prior decomposition of exercise and verification, as suggested
914
    def test_prepare_files_creates_paths_readable_by_windows_tool(self):
5967.12.1 by Martin Pool
Move all test features into bzrlib.tests.features
915
        self.requireFeature(features.AttribFeature)
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
916
        output = BytesIO()
3287.18.10 by Matt McClure
Uses TestSkipped for test_execute_windows_tool on non-Windows platforms.
917
        tree = self.make_branch_and_tree('tree')
6855.4.1 by Jelmer Vernooij
Yet more bees.
918
        self.build_tree_contents([('tree/file', b'content')])
919
        tree.add('file', b'file-id')
3287.18.11 by Matt McClure
Removed unnecessary timestamp parameter.
920
        tree.commit('old tree')
3287.18.10 by Matt McClure
Uses TestSkipped for test_execute_windows_tool on non-Windows platforms.
921
        tree.lock_read()
922
        self.addCleanup(tree.unlock)
4873.3.1 by John Arbash Meinel
Now that we return files directly from the working tree
923
        basis_tree = tree.basis_tree()
924
        basis_tree.lock_read()
925
        self.addCleanup(basis_tree.unlock)
7141.1.1 by Jelmer Vernooij
Use sys.executable rather than python for ad-hoc tests.
926
        diff_obj = diff.DiffFromTool([sys.executable, '-c',
7358.6.1 by Jelmer Vernooij
Use standard syntax for the ``change_editor`` configuration option.
927
                                      'print "{old_path} {new_path}"'],
5168.1.4 by Vincent Ladeuil
Final import fixes for bt.test_diff.
928
                                     basis_tree, tree, output)
6855.4.1 by Jelmer Vernooij
Yet more bees.
929
        diff_obj._prepare_files('file', 'file', file_id=b'file-id')
4873.3.1 by John Arbash Meinel
Now that we return files directly from the working tree
930
        # The old content should be readonly
931
        self.assertReadableByAttrib(diff_obj._root, 'old\\file',
932
                                    r'R.*old\\file$')
933
        # The new content should use the tree object, not a 'new' file anymore
934
        self.assertEndsWith(tree.basedir, 'work/tree')
935
        self.assertReadableByAttrib(tree.basedir, 'file', r'work\\tree\\file$')
3287.18.22 by Matt McClure
Reverts to prior decomposition of exercise and verification, as suggested
936
937
    def assertReadableByAttrib(self, cwd, relpath, regex):
938
        proc = subprocess.Popen(['attrib', relpath],
939
                                stdout=subprocess.PIPE,
940
                                cwd=cwd)
4873.3.1 by John Arbash Meinel
Now that we return files directly from the working tree
941
        (result, err) = proc.communicate()
942
        self.assertContainsRe(result.replace('\r\n', '\n'), regex)
3287.18.9 by Matt McClure
Adds a test asserting that a Windows tool that understands forward slashes
943
3123.6.2 by Aaron Bentley
Implement diff --using natively
944
    def test_prepare_files(self):
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
945
        output = BytesIO()
3123.6.2 by Aaron Bentley
Implement diff --using natively
946
        tree = self.make_branch_and_tree('tree')
6855.4.1 by Jelmer Vernooij
Yet more bees.
947
        self.build_tree_contents([('tree/oldname', b'oldcontent')])
948
        self.build_tree_contents([('tree/oldname2', b'oldcontent2')])
949
        tree.add('oldname', b'file-id')
950
        tree.add('oldname2', b'file2-id')
5151.3.1 by Martin
Fix os.utime test failures, three on FAT filesystems and one with readonly files
951
        # Earliest allowable date on FAT32 filesystems is 1980-01-01
952
        tree.commit('old tree', timestamp=315532800)
3123.6.5 by Aaron Bentley
Symlink to real files if possible
953
        tree.rename_one('oldname', 'newname')
3287.18.23 by Matt McClure
Adds comments that document my understanding of
954
        tree.rename_one('oldname2', 'newname2')
6855.4.1 by Jelmer Vernooij
Yet more bees.
955
        self.build_tree_contents([('tree/newname', b'newcontent')])
956
        self.build_tree_contents([('tree/newname2', b'newcontent2')])
3123.6.2 by Aaron Bentley
Implement diff --using natively
957
        old_tree = tree.basis_tree()
958
        old_tree.lock_read()
959
        self.addCleanup(old_tree.unlock)
3123.6.4 by Aaron Bentley
Set mtime (and atime) on files for --using
960
        tree.lock_read()
961
        self.addCleanup(tree.unlock)
7141.1.1 by Jelmer Vernooij
Use sys.executable rather than python for ad-hoc tests.
962
        diff_obj = diff.DiffFromTool([sys.executable, '-c',
7358.6.1 by Jelmer Vernooij
Use standard syntax for the ``change_editor`` configuration option.
963
                                      'print "{old_path} {new_path}"'],
5168.1.4 by Vincent Ladeuil
Final import fixes for bt.test_diff.
964
                                     old_tree, tree, output)
3123.6.2 by Aaron Bentley
Implement diff --using natively
965
        self.addCleanup(diff_obj.finish)
6681.2.10 by Jelmer Vernooij
Fix failures.
966
        self.assertContainsRe(diff_obj._root, 'brz-diff-[^/]*')
6809.4.15 by Jelmer Vernooij
Fix some more tests.
967
        old_path, new_path = diff_obj._prepare_files(
7206.6.2 by Jelmer Vernooij
Remove file_id from diff API.
968
            'oldname', 'newname')
3123.6.2 by Aaron Bentley
Implement diff --using natively
969
        self.assertContainsRe(old_path, 'old/oldname$')
5151.3.1 by Martin
Fix os.utime test failures, three on FAT filesystems and one with readonly files
970
        self.assertEqual(315532800, os.stat(old_path).st_mtime)
4845.2.1 by Gary van der Merwe
When launching an external diff app, don't write temporary files for a working tree.
971
        self.assertContainsRe(new_path, 'tree/newname$')
7029.4.2 by Jelmer Vernooij
Fix more merge tests.
972
        self.assertFileEqual(b'oldcontent', old_path)
973
        self.assertFileEqual(b'newcontent', new_path)
3287.18.14 by Matt McClure
Extracted a host_os_dereferences_symlinks method.
974
        if osutils.host_os_dereferences_symlinks():
3123.6.5 by Aaron Bentley
Symlink to real files if possible
975
            self.assertTrue(os.path.samefile('tree/newname', new_path))
3123.6.2 by Aaron Bentley
Implement diff --using natively
976
        # make sure we can create files with the same parent directories
7206.6.2 by Jelmer Vernooij
Remove file_id from diff API.
977
        diff_obj._prepare_files('oldname2', 'newname2')
4705.1.2 by Gary van der Merwe
Start on tests for get_trees_and_branches_to_diff.
978
979
5074.5.4 by INADA Naoki
fix easy bug.
980
class TestDiffFromToolEncodedFilename(tests.TestCaseWithTransport):
5074.5.2 by INADA Naoki
Add test for encoded filenames
981
982
    def test_encodable_filename(self):
5074.5.9 by INADA Naoki
Make additional comments to clarify
983
        # Just checks file path for external diff tool.
984
        # We cannot change CPython's internal encoding used by os.exec*.
7358.6.1 by Jelmer Vernooij
Use standard syntax for the ``change_editor`` configuration option.
985
        diffobj = diff.DiffFromTool(['dummy', '{old_path}', '{new_path}'],
5074.5.7 by INADA Naoki
Test for filename encoding can't test subprocess execution because
986
                                    None, None, None)
5074.5.2 by INADA Naoki
Add test for encoded filenames
987
        for _, scenario in EncodingAdapter.encoding_scenarios:
988
            encoding = scenario['encoding']
6614.1.1 by Vincent Ladeuil
Fix assert_ being deprecated by using assertTrue.
989
            dirname = scenario['info']['directory']
5074.5.2 by INADA Naoki
Add test for encoded filenames
990
            filename = scenario['info']['filename']
5074.5.6 by INADA Naoki
Change directry name for each check.
991
5074.5.8 by INADA Naoki
Use tempfile when filepath in tree is not be able to encode with fsencoding.
992
            self.overrideAttr(diffobj, '_fenc', lambda: encoding)
993
            relpath = dirname + u'/' + filename
994
            fullpath = diffobj._safe_filename('safe', relpath)
6614.1.1 by Vincent Ladeuil
Fix assert_ being deprecated by using assertTrue.
995
            self.assertEqual(fullpath,
996
                             fullpath.encode(encoding).decode(encoding))
997
            self.assertTrue(fullpath.startswith(diffobj._root + '/safe'))
5074.5.2 by INADA Naoki
Add test for encoded filenames
998
999
    def test_unencodable_filename(self):
7358.6.1 by Jelmer Vernooij
Use standard syntax for the ``change_editor`` configuration option.
1000
        diffobj = diff.DiffFromTool(['dummy', '{old_path}', '{new_path}'],
5074.5.7 by INADA Naoki
Test for filename encoding can't test subprocess execution because
1001
                                    None, None, None)
5074.5.2 by INADA Naoki
Add test for encoded filenames
1002
        for _, scenario in EncodingAdapter.encoding_scenarios:
1003
            encoding = scenario['encoding']
6614.1.1 by Vincent Ladeuil
Fix assert_ being deprecated by using assertTrue.
1004
            dirname = scenario['info']['directory']
5074.5.2 by INADA Naoki
Add test for encoded filenames
1005
            filename = scenario['info']['filename']
1006
1007
            if encoding == 'iso-8859-1':
1008
                encoding = 'iso-8859-2'
1009
            else:
1010
                encoding = 'iso-8859-1'
5074.5.7 by INADA Naoki
Test for filename encoding can't test subprocess execution because
1011
5074.5.8 by INADA Naoki
Use tempfile when filepath in tree is not be able to encode with fsencoding.
1012
            self.overrideAttr(diffobj, '_fenc', lambda: encoding)
1013
            relpath = dirname + u'/' + filename
1014
            fullpath = diffobj._safe_filename('safe', relpath)
6614.1.1 by Vincent Ladeuil
Fix assert_ being deprecated by using assertTrue.
1015
            self.assertEqual(fullpath,
1016
                             fullpath.encode(encoding).decode(encoding))
1017
            self.assertTrue(fullpath.startswith(diffobj._root + '/safe'))
5074.5.2 by INADA Naoki
Add test for encoded filenames
1018
1019
5168.1.2 by Vincent Ladeuil
Ckeanup some more imports.
1020
class TestGetTreesAndBranchesToDiffLocked(tests.TestCaseWithTransport):
5147.3.7 by Andrew Bennetts
Expect DeprecationWarnings for get_trees_and_branches_to_diff in test_diff, and add corresponding test coverage for get_trees_and_branches_to_diff_locked.
1021
1022
    def call_gtabtd(self, path_list, revision_specs, old_url, new_url):
6027.1.4 by Vincent Ladeuil
Remove ``diff.get_trees_and_branches_to_diff`` deprecated in 2.2.0 and the corrsponding tests.
1023
        """Call get_trees_and_branches_to_diff_locked."""
5168.1.4 by Vincent Ladeuil
Final import fixes for bt.test_diff.
1024
        return diff.get_trees_and_branches_to_diff_locked(
5147.3.7 by Andrew Bennetts
Expect DeprecationWarnings for get_trees_and_branches_to_diff in test_diff, and add corresponding test coverage for get_trees_and_branches_to_diff_locked.
1025
            path_list, revision_specs, old_url, new_url, self.addCleanup)
4732.1.2 by Vincent Ladeuil
(trivial) Fix some PEP8 issues
1026
4705.1.2 by Gary van der Merwe
Start on tests for get_trees_and_branches_to_diff.
1027
    def test_basic(self):
1028
        tree = self.make_branch_and_tree('tree')
1029
        (old_tree, new_tree,
1030
         old_branch, new_branch,
5147.3.7 by Andrew Bennetts
Expect DeprecationWarnings for get_trees_and_branches_to_diff in test_diff, and add corresponding test coverage for get_trees_and_branches_to_diff_locked.
1031
         specific_files, extra_trees) = self.call_gtabtd(
1032
             ['tree'], None, None, None)
4732.1.2 by Vincent Ladeuil
(trivial) Fix some PEP8 issues
1033
5168.1.2 by Vincent Ladeuil
Ckeanup some more imports.
1034
        self.assertIsInstance(old_tree, revisiontree.RevisionTree)
1035
        self.assertEqual(_mod_revision.NULL_REVISION,
1036
                         old_tree.get_revision_id())
4705.1.2 by Gary van der Merwe
Start on tests for get_trees_and_branches_to_diff.
1037
        self.assertEqual(tree.basedir, new_tree.basedir)
1038
        self.assertEqual(tree.branch.base, old_branch.base)
1039
        self.assertEqual(tree.branch.base, new_branch.base)
1040
        self.assertIs(None, specific_files)
1041
        self.assertIs(None, extra_trees)
1042
1043
    def test_with_rev_specs(self):
1044
        tree = self.make_branch_and_tree('tree')
6855.4.1 by Jelmer Vernooij
Yet more bees.
1045
        self.build_tree_contents([('tree/file', b'oldcontent')])
1046
        tree.add('file', b'file-id')
1047
        tree.commit('old tree', timestamp=0, rev_id=b"old-id")
1048
        self.build_tree_contents([('tree/file', b'newcontent')])
6973.13.2 by Jelmer Vernooij
Fix some more tests.
1049
        tree.commit('new tree', timestamp=0, rev_id=b"new-id")
4732.1.2 by Vincent Ladeuil
(trivial) Fix some PEP8 issues
1050
5168.1.2 by Vincent Ladeuil
Ckeanup some more imports.
1051
        revisions = [revisionspec.RevisionSpec.from_string('1'),
1052
                     revisionspec.RevisionSpec.from_string('2')]
4705.1.2 by Gary van der Merwe
Start on tests for get_trees_and_branches_to_diff.
1053
        (old_tree, new_tree,
1054
         old_branch, new_branch,
5147.3.7 by Andrew Bennetts
Expect DeprecationWarnings for get_trees_and_branches_to_diff in test_diff, and add corresponding test coverage for get_trees_and_branches_to_diff_locked.
1055
         specific_files, extra_trees) = self.call_gtabtd(
1056
            ['tree'], revisions, None, None)
4732.1.2 by Vincent Ladeuil
(trivial) Fix some PEP8 issues
1057
5168.1.2 by Vincent Ladeuil
Ckeanup some more imports.
1058
        self.assertIsInstance(old_tree, revisiontree.RevisionTree)
6973.13.2 by Jelmer Vernooij
Fix some more tests.
1059
        self.assertEqual(b"old-id", old_tree.get_revision_id())
5168.1.2 by Vincent Ladeuil
Ckeanup some more imports.
1060
        self.assertIsInstance(new_tree, revisiontree.RevisionTree)
6973.13.2 by Jelmer Vernooij
Fix some more tests.
1061
        self.assertEqual(b"new-id", new_tree.get_revision_id())
4705.1.2 by Gary van der Merwe
Start on tests for get_trees_and_branches_to_diff.
1062
        self.assertEqual(tree.branch.base, old_branch.base)
1063
        self.assertEqual(tree.branch.base, new_branch.base)
1064
        self.assertIs(None, specific_files)
4705.1.4 by Gary van der Merwe
Add newline to end of test_diff.py
1065
        self.assertEqual(tree.basedir, extra_trees[0].basedir)