~bzr-pqm/bzr/bzr.dev

4763.2.4 by John Arbash Meinel
merge bzr.2.1 in preparation for NEWS entry.
1
# Copyright (C) 2005-2010 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
3123.6.5 by Aaron Bentley
Symlink to real files if possible
18
import os.path
1558.15.2 by Aaron Bentley
Implemented binary file handling for diff
19
from cStringIO import StringIO
1692.8.7 by James Henstridge
changes suggested by John Meinel
20
import errno
1920.1.1 by John Arbash Meinel
fix bug #56307, handle binary files even when LANG is not english
21
import subprocess
3287.18.6 by Matt McClure
Snapshot of unfinished work.
22
import sys
1920.1.3 by John Arbash Meinel
Remove spurious import
23
from tempfile import TemporaryFile
1558.15.2 by Aaron Bentley
Implemented binary file handling for diff
24
3146.4.3 by Aaron Bentley
Add missing Symlink requirement
25
from bzrlib import tests
3009.2.9 by Aaron Bentley
Add tests for Differ
26
from bzrlib.diff import (
3123.6.2 by Aaron Bentley
Implement diff --using natively
27
    DiffFromTool,
3009.2.22 by Aaron Bentley
Update names & docstring
28
    DiffPath,
29
    DiffSymlink,
30
    DiffTree,
31
    DiffText,
3123.6.2 by Aaron Bentley
Implement diff --using natively
32
    external_diff,
33
    internal_diff,
34
    show_diff_trees,
4705.1.2 by Gary van der Merwe
Start on tests for get_trees_and_branches_to_diff.
35
    get_trees_and_branches_to_diff,
3009.2.9 by Aaron Bentley
Add tests for Differ
36
    )
3145.1.1 by Aaron Bentley
Handle missing tools gracefully in diff --using
37
from bzrlib.errors import BinaryFile, NoDiff, ExecutableMissing
2321.2.5 by Alexander Belchenko
external diff: no need for special code path for win32 (suggested by John Meinel)
38
import bzrlib.osutils as osutils
3668.5.1 by Jelmer Vernooij
Use NULL_REVISION rather than None for Repository.revision_tree().
39
import bzrlib.revision as _mod_revision
3268.1.1 by C Miller
Describe the property changes in diffs. Currently, this is the executable-bit
40
import bzrlib.transform as transform
1711.2.20 by John Arbash Meinel
Late bind to patiencediff objects to make it easier to plug-in
41
import bzrlib.patiencediff
2781.1.1 by Martin Pool
merge cpatiencediff from Lukas
42
import bzrlib._patiencediff_py
2592.2.4 by Jonathan Lange
Skip the unicode filename test if the platform doesn't support unicode
43
from bzrlib.tests import (Feature, TestCase, TestCaseWithTransport,
1711.2.54 by John Arbash Meinel
Use mkstemp instead of NamedTemporary file for external diff.
44
                          TestCaseInTempDir, TestSkipped)
4705.1.2 by Gary van der Merwe
Start on tests for get_trees_and_branches_to_diff.
45
from bzrlib.revisiontree import RevisionTree
46
from bzrlib.revisionspec import RevisionSpec
1558.15.2 by Aaron Bentley
Implemented binary file handling for diff
47
4913.5.9 by Gordon Tyler
Use test_win32utils' BackslashDirSeparatorFeature instead of checking sys.platform for win32.
48
from bzrlib.tests.test_win32utils import BackslashDirSeparatorFeature
49
1558.15.2 by Aaron Bentley
Implemented binary file handling for diff
50
3287.18.20 by Matt McClure
Introduces a Feature subclass to encapsulate the availability of 'attrib'.
51
class _AttribFeature(Feature):
52
53
    def _probe(self):
54
        if (sys.platform not in ('cygwin', 'win32')):
3287.18.21 by Matt McClure
Fixes capitalization of False.
55
            return False
3287.18.20 by Matt McClure
Introduces a Feature subclass to encapsulate the availability of 'attrib'.
56
        try:
57
            proc = subprocess.Popen(['attrib', '.'], stdout=subprocess.PIPE)
58
        except OSError, e:
3287.18.21 by Matt McClure
Fixes capitalization of False.
59
            return False
3287.18.20 by Matt McClure
Introduces a Feature subclass to encapsulate the availability of 'attrib'.
60
        return (0 == proc.wait())
3287.18.24 by Matt McClure
Cleans up for submission. Removes comments.
61
3287.18.20 by Matt McClure
Introduces a Feature subclass to encapsulate the availability of 'attrib'.
62
    def feature_name(self):
63
        return 'attrib Windows command-line tool'
64
65
AttribFeature = _AttribFeature()
66
67
4913.2.20 by John Arbash Meinel
Change all of the compiled_foo to compiled_foo_feature
68
compiled_patiencediff_feature = tests.ModuleAvailableFeature(
69
                                    'bzrlib._patiencediff_c')
2781.1.1 by Martin Pool
merge cpatiencediff from Lukas
70
71
1558.15.11 by Aaron Bentley
Apply merge review suggestions
72
def udiff_lines(old, new, allow_binary=False):
974.1.6 by Aaron Bentley
Added unit tests
73
    output = StringIO()
1558.15.11 by Aaron Bentley
Apply merge review suggestions
74
    internal_diff('old', old, 'new', new, output, allow_binary)
974.1.6 by Aaron Bentley
Added unit tests
75
    output.seek(0, 0)
76
    return output.readlines()
77
1711.2.54 by John Arbash Meinel
Use mkstemp instead of NamedTemporary file for external diff.
78
1711.2.57 by John Arbash Meinel
Allow external diff to write to a file without a fileno.
79
def external_udiff_lines(old, new, use_stringio=False):
80
    if use_stringio:
81
        # StringIO has no fileno, so it tests a different codepath
82
        output = StringIO()
83
    else:
84
        output = TemporaryFile()
1692.8.7 by James Henstridge
changes suggested by John Meinel
85
    try:
86
        external_diff('old', old, 'new', new, output, diff_opts=['-u'])
1711.2.58 by John Arbash Meinel
Use osutils.pumpfile so we don't have to buffer everything in ram
87
    except NoDiff:
1711.2.56 by John Arbash Meinel
Raise NoDiff if 'diff' not present.
88
        raise TestSkipped('external "diff" not present to test')
1692.8.2 by James Henstridge
add a test for sending external diff output to a file
89
    output.seek(0, 0)
90
    lines = output.readlines()
91
    output.close()
92
    return lines
93
94
1102 by Martin Pool
- merge test refactoring from robertc
95
class TestDiff(TestCase):
1185.81.25 by Aaron Bentley
Clean up test_diff
96
1102 by Martin Pool
- merge test refactoring from robertc
97
    def test_add_nl(self):
98
        """diff generates a valid diff for patches that add a newline"""
974.1.6 by Aaron Bentley
Added unit tests
99
        lines = udiff_lines(['boo'], ['boo\n'])
1185.16.145 by Martin Pool
Remove all assert statements from test cases.
100
        self.check_patch(lines)
101
        self.assertEquals(lines[4], '\\ No newline at end of file\n')
102
            ## "expected no-nl, got %r" % lines[4]
974.1.6 by Aaron Bentley
Added unit tests
103
1102 by Martin Pool
- merge test refactoring from robertc
104
    def test_add_nl_2(self):
105
        """diff generates a valid diff for patches that change last line and
106
        add a newline.
107
        """
974.1.6 by Aaron Bentley
Added unit tests
108
        lines = udiff_lines(['boo'], ['goo\n'])
1185.16.145 by Martin Pool
Remove all assert statements from test cases.
109
        self.check_patch(lines)
110
        self.assertEquals(lines[4], '\\ No newline at end of file\n')
111
            ## "expected no-nl, got %r" % lines[4]
974.1.6 by Aaron Bentley
Added unit tests
112
1102 by Martin Pool
- merge test refactoring from robertc
113
    def test_remove_nl(self):
114
        """diff generates a valid diff for patches that change last line and
115
        add a newline.
116
        """
974.1.6 by Aaron Bentley
Added unit tests
117
        lines = udiff_lines(['boo\n'], ['boo'])
1185.16.145 by Martin Pool
Remove all assert statements from test cases.
118
        self.check_patch(lines)
119
        self.assertEquals(lines[5], '\\ No newline at end of file\n')
120
            ## "expected no-nl, got %r" % lines[5]
121
122
    def check_patch(self, lines):
123
        self.assert_(len(lines) > 1)
124
            ## "Not enough lines for a file header for patch:\n%s" % "".join(lines)
125
        self.assert_(lines[0].startswith ('---'))
126
            ## 'No orig line for patch:\n%s' % "".join(lines)
127
        self.assert_(lines[1].startswith ('+++'))
128
            ## 'No mod line for patch:\n%s' % "".join(lines)
129
        self.assert_(len(lines) > 2)
130
            ## "No hunks for patch:\n%s" % "".join(lines)
131
        self.assert_(lines[2].startswith('@@'))
132
            ## "No hunk header for patch:\n%s" % "".join(lines)
133
        self.assert_('@@' in lines[2][2:])
134
            ## "Unterminated hunk header for patch:\n%s" % "".join(lines)
135
1558.15.2 by Aaron Bentley
Implemented binary file handling for diff
136
    def test_binary_lines(self):
137
        self.assertRaises(BinaryFile, udiff_lines, [1023 * 'a' + '\x00'], [])
138
        self.assertRaises(BinaryFile, udiff_lines, [], [1023 * 'a' + '\x00'])
1558.15.11 by Aaron Bentley
Apply merge review suggestions
139
        udiff_lines([1023 * 'a' + '\x00'], [], allow_binary=True)
140
        udiff_lines([], [1023 * 'a' + '\x00'], allow_binary=True)
1692.8.2 by James Henstridge
add a test for sending external diff output to a file
141
142
    def test_external_diff(self):
143
        lines = external_udiff_lines(['boo\n'], ['goo\n'])
144
        self.check_patch(lines)
1899.1.6 by John Arbash Meinel
internal_diff always adds a trailing \n, make sure external_diff does too
145
        self.assertEqual('\n', lines[-1])
1711.2.57 by John Arbash Meinel
Allow external diff to write to a file without a fileno.
146
147
    def test_external_diff_no_fileno(self):
148
        # Make sure that we can handle not having a fileno, even
149
        # if the diff is large
150
        lines = external_udiff_lines(['boo\n']*10000,
151
                                     ['goo\n']*10000,
152
                                     use_stringio=True)
153
        self.check_patch(lines)
1899.1.1 by John Arbash Meinel
Fix the bug in the NoDiff exception class, and add a test
154
1920.1.1 by John Arbash Meinel
fix bug #56307, handle binary files even when LANG is not english
155
    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)
156
        old_env = {}
157
        for lang in ('LANG', 'LC_ALL', 'LANGUAGE'):
158
            old_env[lang] = osutils.set_or_unset_env(lang, 'C')
1920.1.1 by John Arbash Meinel
fix bug #56307, handle binary files even when LANG is not english
159
        try:
160
            lines = external_udiff_lines(['\x00foobar\n'], ['foo\x00bar\n'])
1959.1.1 by Marien Zwart
merge.
161
            # Older versions of diffutils say "Binary files", newer
162
            # versions just say "Files".
163
            self.assertContainsRe(lines[0],
164
                                  '(Binary f|F)iles old and new differ\n')
165
            self.assertEquals(lines[1:], ['\n'])
1920.1.1 by John Arbash Meinel
fix bug #56307, handle binary files even when LANG is not english
166
        finally:
2321.2.5 by Alexander Belchenko
external diff: no need for special code path for win32 (suggested by John Meinel)
167
            for lang, old_val in old_env.iteritems():
168
                osutils.set_or_unset_env(lang, old_val)
1899.1.4 by John Arbash Meinel
Just swallow a return code of 2
169
1899.1.1 by John Arbash Meinel
Fix the bug in the NoDiff exception class, and add a test
170
    def test_no_external_diff(self):
171
        """Check that NoDiff is raised when diff is not available"""
172
        # Use os.environ['PATH'] to make sure no 'diff' command is available
173
        orig_path = os.environ['PATH']
174
        try:
175
            os.environ['PATH'] = ''
176
            self.assertRaises(NoDiff, external_diff,
177
                              'old', ['boo\n'], 'new', ['goo\n'],
178
                              StringIO(), diff_opts=['-u'])
179
        finally:
180
            os.environ['PATH'] = orig_path
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
181
1711.2.30 by John Arbash Meinel
Fix bug in internal_diff handling of unicode paths
182
    def test_internal_diff_default(self):
183
        # Default internal diff encoding is utf8
184
        output = StringIO()
185
        internal_diff(u'old_\xb5', ['old_text\n'],
186
                    u'new_\xe5', ['new_text\n'], output)
187
        lines = output.getvalue().splitlines(True)
188
        self.check_patch(lines)
1740.2.5 by Aaron Bentley
Merge from bzr.dev
189
        self.assertEquals(['--- old_\xc2\xb5\n',
190
                           '+++ new_\xc3\xa5\n',
1711.2.30 by John Arbash Meinel
Fix bug in internal_diff handling of unicode paths
191
                           '@@ -1,1 +1,1 @@\n',
192
                           '-old_text\n',
193
                           '+new_text\n',
194
                           '\n',
195
                          ]
196
                          , lines)
197
198
    def test_internal_diff_utf8(self):
199
        output = StringIO()
200
        internal_diff(u'old_\xb5', ['old_text\n'],
201
                    u'new_\xe5', ['new_text\n'], output,
202
                    path_encoding='utf8')
203
        lines = output.getvalue().splitlines(True)
204
        self.check_patch(lines)
1740.2.5 by Aaron Bentley
Merge from bzr.dev
205
        self.assertEquals(['--- old_\xc2\xb5\n',
206
                           '+++ new_\xc3\xa5\n',
1711.2.30 by John Arbash Meinel
Fix bug in internal_diff handling of unicode paths
207
                           '@@ -1,1 +1,1 @@\n',
208
                           '-old_text\n',
209
                           '+new_text\n',
210
                           '\n',
211
                          ]
212
                          , lines)
213
214
    def test_internal_diff_iso_8859_1(self):
215
        output = StringIO()
216
        internal_diff(u'old_\xb5', ['old_text\n'],
217
                    u'new_\xe5', ['new_text\n'], output,
218
                    path_encoding='iso-8859-1')
219
        lines = output.getvalue().splitlines(True)
220
        self.check_patch(lines)
1740.2.5 by Aaron Bentley
Merge from bzr.dev
221
        self.assertEquals(['--- old_\xb5\n',
222
                           '+++ new_\xe5\n',
1711.2.30 by John Arbash Meinel
Fix bug in internal_diff handling of unicode paths
223
                           '@@ -1,1 +1,1 @@\n',
224
                           '-old_text\n',
225
                           '+new_text\n',
226
                           '\n',
227
                          ]
228
                          , lines)
229
3085.1.1 by John Arbash Meinel
Fix internal_diff to not fail when the texts are identical.
230
    def test_internal_diff_no_content(self):
231
        output = StringIO()
232
        internal_diff(u'old', [], u'new', [], output)
233
        self.assertEqual('', output.getvalue())
234
235
    def test_internal_diff_no_changes(self):
236
        output = StringIO()
237
        internal_diff(u'old', ['text\n', 'contents\n'],
238
                      u'new', ['text\n', 'contents\n'],
239
                      output)
240
        self.assertEqual('', output.getvalue())
241
1711.2.30 by John Arbash Meinel
Fix bug in internal_diff handling of unicode paths
242
    def test_internal_diff_returns_bytes(self):
243
        import StringIO
244
        output = StringIO.StringIO()
245
        internal_diff(u'old_\xb5', ['old_text\n'],
246
                    u'new_\xe5', ['new_text\n'], output)
247
        self.failUnless(isinstance(output.getvalue(), str),
248
            'internal_diff should return bytestrings')
249
1185.81.25 by Aaron Bentley
Clean up test_diff
250
1920.1.1 by John Arbash Meinel
fix bug #56307, handle binary files even when LANG is not english
251
class TestDiffFiles(TestCaseInTempDir):
252
253
    def test_external_diff_binary(self):
254
        """The output when using external diff should use diff's i18n error"""
255
        # Make sure external_diff doesn't fail in the current LANG
256
        lines = external_udiff_lines(['\x00foobar\n'], ['foo\x00bar\n'])
257
2240.1.1 by Alexander Belchenko
test_external_diff_binary: run external diff with --binary flag
258
        cmd = ['diff', '-u', '--binary', 'old', 'new']
1920.1.1 by John Arbash Meinel
fix bug #56307, handle binary files even when LANG is not english
259
        open('old', 'wb').write('\x00foobar\n')
260
        open('new', 'wb').write('foo\x00bar\n')
261
        pipe = subprocess.Popen(cmd, stdout=subprocess.PIPE,
262
                                     stdin=subprocess.PIPE)
263
        out, err = pipe.communicate()
264
        # Diff returns '2' on Binary files.
265
        self.assertEqual(2, pipe.returncode)
266
        # We should output whatever diff tells us, plus a trailing newline
267
        self.assertEqual(out.splitlines(True) + ['\n'], lines)
268
269
2405.1.1 by John Arbash Meinel
Add a bunch of direct tests for 'show_diff_trees'
270
class TestShowDiffTreesHelper(TestCaseWithTransport):
271
    """Has a helper for running show_diff_trees"""
272
273
    def get_diff(self, tree1, tree2, specific_files=None, working_tree=None):
274
        output = StringIO()
275
        if working_tree is not None:
276
            extra_trees = (working_tree,)
277
        else:
278
            extra_trees = ()
279
        show_diff_trees(tree1, tree2, output, specific_files=specific_files,
280
                        extra_trees=extra_trees, old_label='old/',
281
                        new_label='new/')
282
        return output.getvalue()
283
284
285
class TestDiffDates(TestShowDiffTreesHelper):
1740.2.5 by Aaron Bentley
Merge from bzr.dev
286
287
    def setUp(self):
288
        super(TestDiffDates, self).setUp()
289
        self.wt = self.make_branch_and_tree('.')
290
        self.b = self.wt.branch
291
        self.build_tree_contents([
292
            ('file1', 'file1 contents at rev 1\n'),
293
            ('file2', 'file2 contents at rev 1\n')
294
            ])
295
        self.wt.add(['file1', 'file2'])
296
        self.wt.commit(
297
            message='Revision 1',
298
            timestamp=1143849600, # 2006-04-01 00:00:00 UTC
299
            timezone=0,
300
            rev_id='rev-1')
301
        self.build_tree_contents([('file1', 'file1 contents at rev 2\n')])
302
        self.wt.commit(
303
            message='Revision 2',
304
            timestamp=1143936000, # 2006-04-02 00:00:00 UTC
305
            timezone=28800,
306
            rev_id='rev-2')
307
        self.build_tree_contents([('file2', 'file2 contents at rev 3\n')])
308
        self.wt.commit(
309
            message='Revision 3',
310
            timestamp=1144022400, # 2006-04-03 00:00:00 UTC
311
            timezone=-3600,
312
            rev_id='rev-3')
313
        self.wt.remove(['file2'])
314
        self.wt.commit(
315
            message='Revision 4',
316
            timestamp=1144108800, # 2006-04-04 00:00:00 UTC
317
            timezone=0,
318
            rev_id='rev-4')
319
        self.build_tree_contents([
320
            ('file1', 'file1 contents in working tree\n')
321
            ])
322
        # set the date stamps for files in the working tree to known values
323
        os.utime('file1', (1144195200, 1144195200)) # 2006-04-05 00:00:00 UTC
324
325
    def test_diff_rev_tree_working_tree(self):
326
        output = self.get_diff(self.wt.basis_tree(), self.wt)
327
        # note that the date for old/file1 is from rev 2 rather than from
328
        # the basis revision (rev 4)
329
        self.assertEqualDiff(output, '''\
330
=== modified file 'file1'
331
--- old/file1\t2006-04-02 00:00:00 +0000
332
+++ new/file1\t2006-04-05 00:00:00 +0000
333
@@ -1,1 +1,1 @@
334
-file1 contents at rev 2
335
+file1 contents in working tree
336
337
''')
338
339
    def test_diff_rev_tree_rev_tree(self):
340
        tree1 = self.b.repository.revision_tree('rev-2')
341
        tree2 = self.b.repository.revision_tree('rev-3')
342
        output = self.get_diff(tree1, tree2)
343
        self.assertEqualDiff(output, '''\
344
=== modified file 'file2'
345
--- old/file2\t2006-04-01 00:00:00 +0000
346
+++ new/file2\t2006-04-03 00:00:00 +0000
347
@@ -1,1 +1,1 @@
348
-file2 contents at rev 1
349
+file2 contents at rev 3
350
351
''')
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
352
1740.2.5 by Aaron Bentley
Merge from bzr.dev
353
    def test_diff_add_files(self):
3668.5.1 by Jelmer Vernooij
Use NULL_REVISION rather than None for Repository.revision_tree().
354
        tree1 = self.b.repository.revision_tree(_mod_revision.NULL_REVISION)
1740.2.5 by Aaron Bentley
Merge from bzr.dev
355
        tree2 = self.b.repository.revision_tree('rev-1')
356
        output = self.get_diff(tree1, tree2)
357
        # the files have the epoch time stamp for the tree in which
358
        # they don't exist.
359
        self.assertEqualDiff(output, '''\
360
=== added file 'file1'
361
--- old/file1\t1970-01-01 00:00:00 +0000
362
+++ new/file1\t2006-04-01 00:00:00 +0000
363
@@ -0,0 +1,1 @@
364
+file1 contents at rev 1
365
366
=== added file 'file2'
367
--- old/file2\t1970-01-01 00:00:00 +0000
368
+++ new/file2\t2006-04-01 00:00:00 +0000
369
@@ -0,0 +1,1 @@
370
+file2 contents at rev 1
371
372
''')
373
374
    def test_diff_remove_files(self):
375
        tree1 = self.b.repository.revision_tree('rev-3')
376
        tree2 = self.b.repository.revision_tree('rev-4')
377
        output = self.get_diff(tree1, tree2)
378
        # the file has the epoch time stamp for the tree in which
379
        # it doesn't exist.
380
        self.assertEqualDiff(output, '''\
381
=== removed file 'file2'
382
--- old/file2\t2006-04-03 00:00:00 +0000
383
+++ new/file2\t1970-01-01 00:00:00 +0000
384
@@ -1,1 +0,0 @@
385
-file2 contents at rev 3
386
387
''')
388
1551.7.17 by Aaron Bentley
Switch to PathsNotVersioned, accept extra_trees
389
    def test_show_diff_specified(self):
1551.7.22 by Aaron Bentley
Changes from review
390
        """A working tree filename can be used to identify a file"""
1551.7.17 by Aaron Bentley
Switch to PathsNotVersioned, accept extra_trees
391
        self.wt.rename_one('file1', 'file1b')
392
        old_tree = self.b.repository.revision_tree('rev-1')
393
        new_tree = self.b.repository.revision_tree('rev-4')
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
394
        out = self.get_diff(old_tree, new_tree, specific_files=['file1b'],
1551.7.22 by Aaron Bentley
Changes from review
395
                            working_tree=self.wt)
396
        self.assertContainsRe(out, 'file1\t')
1551.7.17 by Aaron Bentley
Switch to PathsNotVersioned, accept extra_trees
397
1551.7.22 by Aaron Bentley
Changes from review
398
    def test_recursive_diff(self):
399
        """Children of directories are matched"""
400
        os.mkdir('dir1')
401
        os.mkdir('dir2')
402
        self.wt.add(['dir1', 'dir2'])
403
        self.wt.rename_one('file1', 'dir1/file1')
404
        old_tree = self.b.repository.revision_tree('rev-1')
405
        new_tree = self.b.repository.revision_tree('rev-4')
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
406
        out = self.get_diff(old_tree, new_tree, specific_files=['dir1'],
1551.7.22 by Aaron Bentley
Changes from review
407
                            working_tree=self.wt)
408
        self.assertContainsRe(out, 'file1\t')
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
409
        out = self.get_diff(old_tree, new_tree, specific_files=['dir2'],
1551.7.22 by Aaron Bentley
Changes from review
410
                            working_tree=self.wt)
411
        self.assertNotContainsRe(out, 'file1\t')
1740.2.5 by Aaron Bentley
Merge from bzr.dev
412
1899.1.1 by John Arbash Meinel
Fix the bug in the NoDiff exception class, and add a test
413
2405.1.1 by John Arbash Meinel
Add a bunch of direct tests for 'show_diff_trees'
414
415
class TestShowDiffTrees(TestShowDiffTreesHelper):
416
    """Direct tests for show_diff_trees"""
417
418
    def test_modified_file(self):
419
        """Test when a file is modified."""
420
        tree = self.make_branch_and_tree('tree')
421
        self.build_tree_contents([('tree/file', 'contents\n')])
422
        tree.add(['file'], ['file-id'])
423
        tree.commit('one', rev_id='rev-1')
424
425
        self.build_tree_contents([('tree/file', 'new contents\n')])
426
        diff = self.get_diff(tree.basis_tree(), tree)
427
        self.assertContainsRe(diff, "=== modified file 'file'\n")
428
        self.assertContainsRe(diff, '--- old/file\t')
429
        self.assertContainsRe(diff, '\\+\\+\\+ new/file\t')
430
        self.assertContainsRe(diff, '-contents\n'
431
                                    '\\+new contents\n')
432
2405.1.2 by John Arbash Meinel
Fix bug #103870 by passing None instead of a (sometimes wrong) path
433
    def test_modified_file_in_renamed_dir(self):
434
        """Test when a file is modified in a renamed directory."""
435
        tree = self.make_branch_and_tree('tree')
436
        self.build_tree(['tree/dir/'])
437
        self.build_tree_contents([('tree/dir/file', 'contents\n')])
438
        tree.add(['dir', 'dir/file'], ['dir-id', 'file-id'])
439
        tree.commit('one', rev_id='rev-1')
440
441
        tree.rename_one('dir', 'other')
442
        self.build_tree_contents([('tree/other/file', 'new contents\n')])
443
        diff = self.get_diff(tree.basis_tree(), tree)
444
        self.assertContainsRe(diff, "=== renamed directory 'dir' => 'other'\n")
445
        self.assertContainsRe(diff, "=== modified file 'other/file'\n")
446
        # XXX: This is technically incorrect, because it used to be at another
447
        # location. What to do?
2405.1.3 by John Arbash Meinel
Do a better fix, which recognizes that we should pass the correct old path.
448
        self.assertContainsRe(diff, '--- old/dir/file\t')
2405.1.2 by John Arbash Meinel
Fix bug #103870 by passing None instead of a (sometimes wrong) path
449
        self.assertContainsRe(diff, '\\+\\+\\+ new/other/file\t')
450
        self.assertContainsRe(diff, '-contents\n'
451
                                    '\\+new contents\n')
452
2405.1.1 by John Arbash Meinel
Add a bunch of direct tests for 'show_diff_trees'
453
    def test_renamed_directory(self):
454
        """Test when only a directory is only renamed."""
455
        tree = self.make_branch_and_tree('tree')
456
        self.build_tree(['tree/dir/'])
457
        self.build_tree_contents([('tree/dir/file', 'contents\n')])
458
        tree.add(['dir', 'dir/file'], ['dir-id', 'file-id'])
459
        tree.commit('one', rev_id='rev-1')
460
461
        tree.rename_one('dir', 'newdir')
462
        diff = self.get_diff(tree.basis_tree(), tree)
463
        # Renaming a directory should be a single "you renamed this dir" even
464
        # when there are files inside.
465
        self.assertEqual("=== renamed directory 'dir' => 'newdir'\n", diff)
466
467
    def test_renamed_file(self):
468
        """Test when a file is only renamed."""
469
        tree = self.make_branch_and_tree('tree')
470
        self.build_tree_contents([('tree/file', 'contents\n')])
471
        tree.add(['file'], ['file-id'])
472
        tree.commit('one', rev_id='rev-1')
473
474
        tree.rename_one('file', 'newname')
475
        diff = self.get_diff(tree.basis_tree(), tree)
476
        self.assertContainsRe(diff, "=== renamed file 'file' => 'newname'\n")
477
        # We shouldn't have a --- or +++ line, because there is no content
478
        # change
479
        self.assertNotContainsRe(diff, '---')
480
481
    def test_renamed_and_modified_file(self):
482
        """Test when a file is only renamed."""
483
        tree = self.make_branch_and_tree('tree')
484
        self.build_tree_contents([('tree/file', 'contents\n')])
485
        tree.add(['file'], ['file-id'])
486
        tree.commit('one', rev_id='rev-1')
487
488
        tree.rename_one('file', 'newname')
489
        self.build_tree_contents([('tree/newname', 'new contents\n')])
490
        diff = self.get_diff(tree.basis_tree(), tree)
491
        self.assertContainsRe(diff, "=== renamed file 'file' => 'newname'\n")
492
        self.assertContainsRe(diff, '--- old/file\t')
493
        self.assertContainsRe(diff, '\\+\\+\\+ new/newname\t')
494
        self.assertContainsRe(diff, '-contents\n'
495
                                    '\\+new contents\n')
496
3268.1.1 by C Miller
Describe the property changes in diffs. Currently, this is the executable-bit
497
498
    def test_internal_diff_exec_property(self):
499
        tree = self.make_branch_and_tree('tree')
500
501
        tt = transform.TreeTransform(tree)
502
        tt.new_file('a', tt.root, 'contents\n', 'a-id', True)
503
        tt.new_file('b', tt.root, 'contents\n', 'b-id', False)
504
        tt.new_file('c', tt.root, 'contents\n', 'c-id', True)
505
        tt.new_file('d', tt.root, 'contents\n', 'd-id', False)
506
        tt.new_file('e', tt.root, 'contents\n', 'control-e-id', True)
507
        tt.new_file('f', tt.root, 'contents\n', 'control-f-id', False)
508
        tt.apply()
509
        tree.commit('one', rev_id='rev-1')
510
511
        tt = transform.TreeTransform(tree)
512
        tt.set_executability(False, tt.trans_id_file_id('a-id'))
513
        tt.set_executability(True, tt.trans_id_file_id('b-id'))
514
        tt.set_executability(False, tt.trans_id_file_id('c-id'))
515
        tt.set_executability(True, tt.trans_id_file_id('d-id'))
516
        tt.apply()
517
        tree.rename_one('c', 'new-c')
518
        tree.rename_one('d', 'new-d')
519
520
        diff = self.get_diff(tree.basis_tree(), tree)
521
522
        self.assertContainsRe(diff, r"file 'a'.*\(properties changed:.*\+x to -x.*\)")
523
        self.assertContainsRe(diff, r"file 'b'.*\(properties changed:.*-x to \+x.*\)")
524
        self.assertContainsRe(diff, r"file 'c'.*\(properties changed:.*\+x to -x.*\)")
525
        self.assertContainsRe(diff, r"file 'd'.*\(properties changed:.*-x to \+x.*\)")
526
        self.assertNotContainsRe(diff, r"file 'e'")
527
        self.assertNotContainsRe(diff, r"file 'f'")
528
529
2592.2.1 by Jonathan Lange
Reproduce and fix bug 110092.
530
    def test_binary_unicode_filenames(self):
2592.2.2 by Jonathan Lange
Apply jam's comments to test_binary_unicode_filenames. Change the
531
        """Test that contents of files are *not* encoded in UTF-8 when there
532
        is a binary file in the diff.
2592.2.1 by Jonathan Lange
Reproduce and fix bug 110092.
533
        """
534
        # See https://bugs.launchpad.net/bugs/110092.
3477.1.2 by John Arbash Meinel
Rename UnicodeFilename => UnicodeFilenameFeature
535
        self.requireFeature(tests.UnicodeFilenameFeature)
2592.2.1 by Jonathan Lange
Reproduce and fix bug 110092.
536
537
        # This bug isn't triggered with cStringIO.
538
        from StringIO import StringIO
539
        tree = self.make_branch_and_tree('tree')
2592.2.2 by Jonathan Lange
Apply jam's comments to test_binary_unicode_filenames. Change the
540
        alpha, omega = u'\u03b1', u'\u03c9'
541
        alpha_utf8, omega_utf8 = alpha.encode('utf8'), omega.encode('utf8')
2592.2.1 by Jonathan Lange
Reproduce and fix bug 110092.
542
        self.build_tree_contents(
2592.2.2 by Jonathan Lange
Apply jam's comments to test_binary_unicode_filenames. Change the
543
            [('tree/' + alpha, chr(0)),
544
             ('tree/' + omega,
545
              ('The %s and the %s\n' % (alpha_utf8, omega_utf8)))])
546
        tree.add([alpha], ['file-id'])
547
        tree.add([omega], ['file-id-2'])
2592.2.1 by Jonathan Lange
Reproduce and fix bug 110092.
548
        diff_content = StringIO()
549
        show_diff_trees(tree.basis_tree(), tree, diff_content)
550
        diff = diff_content.getvalue()
2592.2.2 by Jonathan Lange
Apply jam's comments to test_binary_unicode_filenames. Change the
551
        self.assertContainsRe(diff, r"=== added file '%s'" % alpha_utf8)
2592.2.1 by Jonathan Lange
Reproduce and fix bug 110092.
552
        self.assertContainsRe(
2592.2.2 by Jonathan Lange
Apply jam's comments to test_binary_unicode_filenames. Change the
553
            diff, "Binary files a/%s.*and b/%s.* differ\n" % (alpha_utf8, alpha_utf8))
554
        self.assertContainsRe(diff, r"=== added file '%s'" % omega_utf8)
555
        self.assertContainsRe(diff, r"--- a/%s" % (omega_utf8,))
556
        self.assertContainsRe(diff, r"\+\+\+ b/%s" % (omega_utf8,))
2592.2.1 by Jonathan Lange
Reproduce and fix bug 110092.
557
2725.2.1 by ghigo
When a unicode filename is renamed, in the diff is showed a wrong result
558
    def test_unicode_filename(self):
559
        """Test when the filename are unicode."""
3477.1.2 by John Arbash Meinel
Rename UnicodeFilename => UnicodeFilenameFeature
560
        self.requireFeature(tests.UnicodeFilenameFeature)
2725.2.1 by ghigo
When a unicode filename is renamed, in the diff is showed a wrong result
561
562
        alpha, omega = u'\u03b1', u'\u03c9'
563
        autf8, outf8 = alpha.encode('utf8'), omega.encode('utf8')
564
565
        tree = self.make_branch_and_tree('tree')
566
        self.build_tree_contents([('tree/ren_'+alpha, 'contents\n')])
567
        tree.add(['ren_'+alpha], ['file-id-2'])
568
        self.build_tree_contents([('tree/del_'+alpha, 'contents\n')])
569
        tree.add(['del_'+alpha], ['file-id-3'])
570
        self.build_tree_contents([('tree/mod_'+alpha, 'contents\n')])
571
        tree.add(['mod_'+alpha], ['file-id-4'])
572
573
        tree.commit('one', rev_id='rev-1')
574
575
        tree.rename_one('ren_'+alpha, 'ren_'+omega)
576
        tree.remove('del_'+alpha)
577
        self.build_tree_contents([('tree/add_'+alpha, 'contents\n')])
578
        tree.add(['add_'+alpha], ['file-id'])
579
        self.build_tree_contents([('tree/mod_'+alpha, 'contents_mod\n')])
580
581
        diff = self.get_diff(tree.basis_tree(), tree)
582
        self.assertContainsRe(diff,
583
                "=== renamed file 'ren_%s' => 'ren_%s'\n"%(autf8, outf8))
584
        self.assertContainsRe(diff, "=== added file 'add_%s'"%autf8)
585
        self.assertContainsRe(diff, "=== modified file 'mod_%s'"%autf8)
586
        self.assertContainsRe(diff, "=== removed file 'del_%s'"%autf8)
2405.1.1 by John Arbash Meinel
Add a bunch of direct tests for 'show_diff_trees'
587
3009.2.9 by Aaron Bentley
Add tests for Differ
588
3009.2.22 by Aaron Bentley
Update names & docstring
589
class DiffWasIs(DiffPath):
3009.2.15 by Aaron Bentley
Test differ registration
590
591
    def diff(self, file_id, old_path, new_path, old_kind, new_kind):
592
        self.to_file.write('was: ')
593
        self.to_file.write(self.old_tree.get_file(file_id).read())
594
        self.to_file.write('is: ')
595
        self.to_file.write(self.new_tree.get_file(file_id).read())
596
        pass
597
598
3009.2.22 by Aaron Bentley
Update names & docstring
599
class TestDiffTree(TestCaseWithTransport):
3009.2.9 by Aaron Bentley
Add tests for Differ
600
601
    def setUp(self):
602
        TestCaseWithTransport.setUp(self)
603
        self.old_tree = self.make_branch_and_tree('old-tree')
604
        self.old_tree.lock_write()
605
        self.addCleanup(self.old_tree.unlock)
606
        self.new_tree = self.make_branch_and_tree('new-tree')
607
        self.new_tree.lock_write()
608
        self.addCleanup(self.new_tree.unlock)
3009.2.22 by Aaron Bentley
Update names & docstring
609
        self.differ = DiffTree(self.old_tree, self.new_tree, StringIO())
3009.2.9 by Aaron Bentley
Add tests for Differ
610
611
    def test_diff_text(self):
612
        self.build_tree_contents([('old-tree/olddir/',),
613
                                  ('old-tree/olddir/oldfile', 'old\n')])
614
        self.old_tree.add('olddir')
615
        self.old_tree.add('olddir/oldfile', 'file-id')
616
        self.build_tree_contents([('new-tree/newdir/',),
617
                                  ('new-tree/newdir/newfile', 'new\n')])
618
        self.new_tree.add('newdir')
619
        self.new_tree.add('newdir/newfile', 'file-id')
3009.2.22 by Aaron Bentley
Update names & docstring
620
        differ = DiffText(self.old_tree, self.new_tree, StringIO())
3009.2.11 by Aaron Bentley
Refactor diff to be more pluggable
621
        differ.diff_text('file-id', None, 'old label', 'new label')
3009.2.9 by Aaron Bentley
Add tests for Differ
622
        self.assertEqual(
623
            '--- 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
624
            differ.to_file.getvalue())
625
        differ.to_file.seek(0)
626
        differ.diff_text(None, 'file-id', 'old label', 'new label')
3009.2.9 by Aaron Bentley
Add tests for Differ
627
        self.assertEqual(
628
            '--- 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
629
            differ.to_file.getvalue())
630
        differ.to_file.seek(0)
631
        differ.diff_text('file-id', 'file-id', 'old label', 'new label')
3009.2.9 by Aaron Bentley
Add tests for Differ
632
        self.assertEqual(
633
            '--- 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
634
            differ.to_file.getvalue())
3009.2.9 by Aaron Bentley
Add tests for Differ
635
3087.1.1 by Aaron Bentley
Diff handles missing files correctly, with no tracebacks
636
    def test_diff_deletion(self):
637
        self.build_tree_contents([('old-tree/file', 'contents'),
638
                                  ('new-tree/file', 'contents')])
639
        self.old_tree.add('file', 'file-id')
640
        self.new_tree.add('file', 'file-id')
641
        os.unlink('new-tree/file')
642
        self.differ.show_diff(None)
643
        self.assertContainsRe(self.differ.to_file.getvalue(), '-contents')
644
645
    def test_diff_creation(self):
646
        self.build_tree_contents([('old-tree/file', 'contents'),
647
                                  ('new-tree/file', 'contents')])
648
        self.old_tree.add('file', 'file-id')
649
        self.new_tree.add('file', 'file-id')
650
        os.unlink('old-tree/file')
651
        self.differ.show_diff(None)
652
        self.assertContainsRe(self.differ.to_file.getvalue(), '\+contents')
653
3009.2.9 by Aaron Bentley
Add tests for Differ
654
    def test_diff_symlink(self):
3009.2.22 by Aaron Bentley
Update names & docstring
655
        differ = DiffSymlink(self.old_tree, self.new_tree, StringIO())
3009.2.11 by Aaron Bentley
Refactor diff to be more pluggable
656
        differ.diff_symlink('old target', None)
3009.2.9 by Aaron Bentley
Add tests for Differ
657
        self.assertEqual("=== target was 'old target'\n",
3009.2.11 by Aaron Bentley
Refactor diff to be more pluggable
658
                         differ.to_file.getvalue())
3009.2.9 by Aaron Bentley
Add tests for Differ
659
3009.2.22 by Aaron Bentley
Update names & docstring
660
        differ = DiffSymlink(self.old_tree, self.new_tree, StringIO())
3009.2.11 by Aaron Bentley
Refactor diff to be more pluggable
661
        differ.diff_symlink(None, 'new target')
3009.2.9 by Aaron Bentley
Add tests for Differ
662
        self.assertEqual("=== target is 'new target'\n",
3009.2.11 by Aaron Bentley
Refactor diff to be more pluggable
663
                         differ.to_file.getvalue())
664
3009.2.22 by Aaron Bentley
Update names & docstring
665
        differ = DiffSymlink(self.old_tree, self.new_tree, StringIO())
3009.2.11 by Aaron Bentley
Refactor diff to be more pluggable
666
        differ.diff_symlink('old target', 'new target')
3009.2.9 by Aaron Bentley
Add tests for Differ
667
        self.assertEqual("=== target changed 'old target' => 'new target'\n",
3009.2.11 by Aaron Bentley
Refactor diff to be more pluggable
668
                         differ.to_file.getvalue())
3009.2.9 by Aaron Bentley
Add tests for Differ
669
670
    def test_diff(self):
671
        self.build_tree_contents([('old-tree/olddir/',),
672
                                  ('old-tree/olddir/oldfile', 'old\n')])
673
        self.old_tree.add('olddir')
674
        self.old_tree.add('olddir/oldfile', 'file-id')
675
        self.build_tree_contents([('new-tree/newdir/',),
676
                                  ('new-tree/newdir/newfile', 'new\n')])
677
        self.new_tree.add('newdir')
678
        self.new_tree.add('newdir/newfile', 'file-id')
3009.2.12 by Aaron Bentley
Associate labels with text diffing only
679
        self.differ.diff('file-id', 'olddir/oldfile', 'newdir/newfile')
3009.2.9 by Aaron Bentley
Add tests for Differ
680
        self.assertContainsRe(
681
            self.differ.to_file.getvalue(),
682
            r'--- olddir/oldfile.*\n\+\+\+ newdir/newfile.*\n\@\@ -1,1 \+1,1'
683
             ' \@\@\n-old\n\+new\n\n')
684
685
    def test_diff_kind_change(self):
3146.4.3 by Aaron Bentley
Add missing Symlink requirement
686
        self.requireFeature(tests.SymlinkFeature)
3009.2.9 by Aaron Bentley
Add tests for Differ
687
        self.build_tree_contents([('old-tree/olddir/',),
688
                                  ('old-tree/olddir/oldfile', 'old\n')])
689
        self.old_tree.add('olddir')
690
        self.old_tree.add('olddir/oldfile', 'file-id')
691
        self.build_tree(['new-tree/newdir/'])
692
        os.symlink('new', 'new-tree/newdir/newfile')
693
        self.new_tree.add('newdir')
694
        self.new_tree.add('newdir/newfile', 'file-id')
3009.2.12 by Aaron Bentley
Associate labels with text diffing only
695
        self.differ.diff('file-id', 'olddir/oldfile', 'newdir/newfile')
3009.2.9 by Aaron Bentley
Add tests for Differ
696
        self.assertContainsRe(
697
            self.differ.to_file.getvalue(),
698
            r'--- olddir/oldfile.*\n\+\+\+ newdir/newfile.*\n\@\@ -1,1 \+0,0'
699
             ' \@\@\n-old\n\n')
700
        self.assertContainsRe(self.differ.to_file.getvalue(),
4216.3.1 by Robert Collins
Fix Tree.get_symlink_target to decode from the disk encoding to get a unicode encoded string.
701
                              "=== target is u'new'\n")
3009.2.9 by Aaron Bentley
Add tests for Differ
702
3009.2.19 by Aaron Bentley
Implement directory diffing
703
    def test_diff_directory(self):
704
        self.build_tree(['new-tree/new-dir/'])
705
        self.new_tree.add('new-dir', 'new-dir-id')
706
        self.differ.diff('new-dir-id', None, 'new-dir')
707
        self.assertEqual(self.differ.to_file.getvalue(), '')
708
3009.2.16 by Aaron Bentley
Test support for extra differs
709
    def create_old_new(self):
710
        self.build_tree_contents([('old-tree/olddir/',),
711
                                  ('old-tree/olddir/oldfile', 'old\n')])
712
        self.old_tree.add('olddir')
713
        self.old_tree.add('olddir/oldfile', 'file-id')
714
        self.build_tree_contents([('new-tree/newdir/',),
715
                                  ('new-tree/newdir/newfile', 'new\n')])
716
        self.new_tree.add('newdir')
717
        self.new_tree.add('newdir/newfile', 'file-id')
718
3009.2.27 by Aaron Bentley
Use extra_factories instead of extra_diffs
719
    def test_register_diff(self):
3009.2.16 by Aaron Bentley
Test support for extra differs
720
        self.create_old_new()
3009.2.27 by Aaron Bentley
Use extra_factories instead of extra_diffs
721
        old_diff_factories = DiffTree.diff_factories
722
        DiffTree.diff_factories=old_diff_factories[:]
3009.2.28 by Aaron Bentley
Add from_diff_tree factories
723
        DiffTree.diff_factories.insert(0, DiffWasIs.from_diff_tree)
3009.2.16 by Aaron Bentley
Test support for extra differs
724
        try:
3009.2.22 by Aaron Bentley
Update names & docstring
725
            differ = DiffTree(self.old_tree, self.new_tree, StringIO())
3009.2.16 by Aaron Bentley
Test support for extra differs
726
        finally:
3009.2.27 by Aaron Bentley
Use extra_factories instead of extra_diffs
727
            DiffTree.diff_factories = old_diff_factories
3009.2.16 by Aaron Bentley
Test support for extra differs
728
        differ.diff('file-id', 'olddir/oldfile', 'newdir/newfile')
729
        self.assertNotContainsRe(
730
            differ.to_file.getvalue(),
731
            r'--- olddir/oldfile.*\n\+\+\+ newdir/newfile.*\n\@\@ -1,1 \+1,1'
732
             ' \@\@\n-old\n\+new\n\n')
733
        self.assertContainsRe(differ.to_file.getvalue(),
734
                              'was: old\nis: new\n')
735
3009.2.27 by Aaron Bentley
Use extra_factories instead of extra_diffs
736
    def test_extra_factories(self):
3009.2.16 by Aaron Bentley
Test support for extra differs
737
        self.create_old_new()
3009.2.27 by Aaron Bentley
Use extra_factories instead of extra_diffs
738
        differ = DiffTree(self.old_tree, self.new_tree, StringIO(),
3009.2.28 by Aaron Bentley
Add from_diff_tree factories
739
                            extra_factories=[DiffWasIs.from_diff_tree])
3009.2.16 by Aaron Bentley
Test support for extra differs
740
        differ.diff('file-id', 'olddir/oldfile', 'newdir/newfile')
741
        self.assertNotContainsRe(
742
            differ.to_file.getvalue(),
743
            r'--- olddir/oldfile.*\n\+\+\+ newdir/newfile.*\n\@\@ -1,1 \+1,1'
744
             ' \@\@\n-old\n\+new\n\n')
745
        self.assertContainsRe(differ.to_file.getvalue(),
746
                              'was: old\nis: new\n')
747
3123.4.1 by Aaron Bentley
Diff sorts files in alphabetical order
748
    def test_alphabetical_order(self):
749
        self.build_tree(['new-tree/a-file'])
750
        self.new_tree.add('a-file')
751
        self.build_tree(['old-tree/b-file'])
752
        self.old_tree.add('b-file')
753
        self.differ.show_diff(None)
754
        self.assertContainsRe(self.differ.to_file.getvalue(),
755
            '.*a-file(.|\n)*b-file')
756
3009.2.9 by Aaron Bentley
Add tests for Differ
757
1711.2.15 by John Arbash Meinel
Found a couple CDV left
758
class TestPatienceDiffLib(TestCase):
1185.81.1 by John Arbash Meinel
Adding nofrillsprecisemerge's diff algorithm, wrapped in difflib.
759
2781.1.1 by Martin Pool
merge cpatiencediff from Lukas
760
    def setUp(self):
761
        super(TestPatienceDiffLib, self).setUp()
762
        self._unique_lcs = bzrlib._patiencediff_py.unique_lcs_py
763
        self._recurse_matches = bzrlib._patiencediff_py.recurse_matches_py
764
        self._PatienceSequenceMatcher = \
765
            bzrlib._patiencediff_py.PatienceSequenceMatcher_py
766
3628.1.3 by Lukáš Lalinský
Add a test
767
    def test_diff_unicode_string(self):
768
        a = ''.join([unichr(i) for i in range(4000, 4500, 3)])
769
        b = ''.join([unichr(i) for i in range(4300, 4800, 2)])
770
        sm = self._PatienceSequenceMatcher(None, a, b)
771
        mb = sm.get_matching_blocks()
772
        self.assertEquals(35, len(mb))
773
1185.81.9 by John Arbash Meinel
Added (failing) tests for cdv.recurse_matches with common sections,
774
    def test_unique_lcs(self):
2781.1.1 by Martin Pool
merge cpatiencediff from Lukas
775
        unique_lcs = self._unique_lcs
1185.81.9 by John Arbash Meinel
Added (failing) tests for cdv.recurse_matches with common sections,
776
        self.assertEquals(unique_lcs('', ''), [])
2781.1.1 by Martin Pool
merge cpatiencediff from Lukas
777
        self.assertEquals(unique_lcs('', 'a'), [])
778
        self.assertEquals(unique_lcs('a', ''), [])
1185.81.9 by John Arbash Meinel
Added (failing) tests for cdv.recurse_matches with common sections,
779
        self.assertEquals(unique_lcs('a', 'a'), [(0,0)])
780
        self.assertEquals(unique_lcs('a', 'b'), [])
781
        self.assertEquals(unique_lcs('ab', 'ab'), [(0,0), (1,1)])
782
        self.assertEquals(unique_lcs('abcde', 'cdeab'), [(2,0), (3,1), (4,2)])
783
        self.assertEquals(unique_lcs('cdeab', 'abcde'), [(0,2), (1,3), (2,4)])
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
784
        self.assertEquals(unique_lcs('abXde', 'abYde'), [(0,0), (1,1),
1185.81.9 by John Arbash Meinel
Added (failing) tests for cdv.recurse_matches with common sections,
785
                                                         (3,3), (4,4)])
786
        self.assertEquals(unique_lcs('acbac', 'abc'), [(2,1)])
787
788
    def test_recurse_matches(self):
789
        def test_one(a, b, matches):
790
            test_matches = []
2781.1.1 by Martin Pool
merge cpatiencediff from Lukas
791
            self._recurse_matches(
792
                a, b, 0, 0, len(a), len(b), test_matches, 10)
1185.81.9 by John Arbash Meinel
Added (failing) tests for cdv.recurse_matches with common sections,
793
            self.assertEquals(test_matches, matches)
794
1711.2.17 by John Arbash Meinel
Small cleanups to patience_diff code.
795
        test_one(['a', '', 'b', '', 'c'], ['a', 'a', 'b', 'c', 'c'],
1185.81.9 by John Arbash Meinel
Added (failing) tests for cdv.recurse_matches with common sections,
796
                 [(0, 0), (2, 2), (4, 4)])
797
        test_one(['a', 'c', 'b', 'a', 'c'], ['a', 'b', 'c'],
798
                 [(0, 0), (2, 1), (4, 2)])
2781.1.1 by Martin Pool
merge cpatiencediff from Lukas
799
        # Even though 'bc' is not unique globally, and is surrounded by
800
        # non-matching lines, we should still match, because they are locally
801
        # unique
802
        test_one('abcdbce', 'afbcgdbce', [(0,0), (1, 2), (2, 3), (3, 5),
803
                                          (4, 6), (5, 7), (6, 8)])
1185.81.9 by John Arbash Meinel
Added (failing) tests for cdv.recurse_matches with common sections,
804
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
805
        # recurse_matches doesn't match non-unique
1185.81.14 by John Arbash Meinel
Added a main function for running cdvdifflib manually, included tests for unified_diff interfaces
806
        # lines surrounded by bogus text.
1185.81.24 by Aaron Bentley
Reoganize patience-related code
807
        # The update has been done in patiencediff.SequenceMatcher instead
1185.81.14 by John Arbash Meinel
Added a main function for running cdvdifflib manually, included tests for unified_diff interfaces
808
809
        # This is what it could be
1185.81.9 by John Arbash Meinel
Added (failing) tests for cdv.recurse_matches with common sections,
810
        #test_one('aBccDe', 'abccde', [(0,0), (2,2), (3,3), (5,5)])
1185.81.14 by John Arbash Meinel
Added a main function for running cdvdifflib manually, included tests for unified_diff interfaces
811
1185.81.9 by John Arbash Meinel
Added (failing) tests for cdv.recurse_matches with common sections,
812
        # This is what it currently gives:
813
        test_one('aBccDe', 'abccde', [(0,0), (5,5)])
814
3074.2.10 by John Arbash Meinel
Cleanup the test cases (Andrew)
815
    def assertDiffBlocks(self, a, b, expected_blocks):
816
        """Check that the sequence matcher returns the correct blocks.
817
818
        :param a: A sequence to match
819
        :param b: Another sequence to match
820
        :param expected_blocks: The expected output, not including the final
821
            matching block (len(a), len(b), 0)
822
        """
823
        matcher = self._PatienceSequenceMatcher(None, a, b)
824
        blocks = matcher.get_matching_blocks()
825
        last = blocks.pop()
826
        self.assertEqual((len(a), len(b), 0), last)
827
        self.assertEqual(expected_blocks, blocks)
828
1185.81.1 by John Arbash Meinel
Adding nofrillsprecisemerge's diff algorithm, wrapped in difflib.
829
    def test_matching_blocks(self):
1185.81.2 by John Arbash Meinel
A couple small tests.
830
        # Some basic matching tests
3074.2.10 by John Arbash Meinel
Cleanup the test cases (Andrew)
831
        self.assertDiffBlocks('', '', [])
832
        self.assertDiffBlocks([], [], [])
833
        self.assertDiffBlocks('abc', '', [])
834
        self.assertDiffBlocks('', 'abc', [])
835
        self.assertDiffBlocks('abcd', 'abcd', [(0, 0, 4)])
836
        self.assertDiffBlocks('abcd', 'abce', [(0, 0, 3)])
837
        self.assertDiffBlocks('eabc', 'abce', [(1, 0, 3)])
838
        self.assertDiffBlocks('eabce', 'abce', [(1, 0, 4)])
839
        self.assertDiffBlocks('abcde', 'abXde', [(0, 0, 2), (3, 3, 2)])
840
        self.assertDiffBlocks('abcde', 'abXYZde', [(0, 0, 2), (3, 5, 2)])
841
        self.assertDiffBlocks('abde', 'abXYZde', [(0, 0, 2), (2, 5, 2)])
842
        # This may check too much, but it checks to see that
1185.81.3 by John Arbash Meinel
Adding tests for checking opcodes.
843
        # a copied block stays attached to the previous section,
844
        # not the later one.
845
        # difflib would tend to grab the trailing longest match
846
        # which would make the diff not look right
3074.2.10 by John Arbash Meinel
Cleanup the test cases (Andrew)
847
        self.assertDiffBlocks('abcdefghijklmnop', 'abcdefxydefghijklmnop',
848
                              [(0, 0, 6), (6, 11, 10)])
1185.81.1 by John Arbash Meinel
Adding nofrillsprecisemerge's diff algorithm, wrapped in difflib.
849
1185.81.2 by John Arbash Meinel
A couple small tests.
850
        # make sure it supports passing in lists
3074.2.10 by John Arbash Meinel
Cleanup the test cases (Andrew)
851
        self.assertDiffBlocks(
1185.81.29 by Aaron Bentley
Fix style issues and duplicated tests
852
                   ['hello there\n',
853
                    'world\n',
854
                    'how are you today?\n'],
855
                   ['hello there\n',
856
                    'how are you today?\n'],
1185.81.2 by John Arbash Meinel
A couple small tests.
857
                [(0, 0, 1), (2, 1, 1)])
1185.81.1 by John Arbash Meinel
Adding nofrillsprecisemerge's diff algorithm, wrapped in difflib.
858
1711.2.21 by John Arbash Meinel
Cleanup patiencediff, remove the use of difflib.SequenceMatcher.
859
        # non unique lines surrounded by non-matching lines
860
        # won't be found
3074.2.10 by John Arbash Meinel
Cleanup the test cases (Andrew)
861
        self.assertDiffBlocks('aBccDe', 'abccde', [(0,0,1), (5,5,1)])
1711.2.21 by John Arbash Meinel
Cleanup patiencediff, remove the use of difflib.SequenceMatcher.
862
863
        # But they only need to be locally unique
3074.2.10 by John Arbash Meinel
Cleanup the test cases (Andrew)
864
        self.assertDiffBlocks('aBcDec', 'abcdec', [(0,0,1), (2,2,1), (4,4,2)])
1711.2.21 by John Arbash Meinel
Cleanup patiencediff, remove the use of difflib.SequenceMatcher.
865
866
        # non unique blocks won't be matched
3074.2.10 by John Arbash Meinel
Cleanup the test cases (Andrew)
867
        self.assertDiffBlocks('aBcdEcdFg', 'abcdecdfg', [(0,0,1), (8,8,1)])
1711.2.21 by John Arbash Meinel
Cleanup patiencediff, remove the use of difflib.SequenceMatcher.
868
869
        # but locally unique ones will
3074.2.10 by John Arbash Meinel
Cleanup the test cases (Andrew)
870
        self.assertDiffBlocks('aBcdEeXcdFg', 'abcdecdfg', [(0,0,1), (2,2,2),
1711.2.21 by John Arbash Meinel
Cleanup patiencediff, remove the use of difflib.SequenceMatcher.
871
                                              (5,4,1), (7,5,2), (10,8,1)])
872
3074.2.10 by John Arbash Meinel
Cleanup the test cases (Andrew)
873
        self.assertDiffBlocks('abbabbXd', 'cabbabxd', [(7,7,1)])
874
        self.assertDiffBlocks('abbabbbb', 'cabbabbc', [])
875
        self.assertDiffBlocks('bbbbbbbb', 'cbbbbbbc', [])
1185.81.11 by John Arbash Meinel
Found some edge cases that weren't being matched.
876
3074.2.1 by John Arbash Meinel
Change the C PatienceDiff implementation to support arbitrary objects.
877
    def test_matching_blocks_tuples(self):
878
        # Some basic matching tests
3074.2.10 by John Arbash Meinel
Cleanup the test cases (Andrew)
879
        self.assertDiffBlocks([], [], [])
880
        self.assertDiffBlocks([('a',), ('b',), ('c,')], [], [])
881
        self.assertDiffBlocks([], [('a',), ('b',), ('c,')], [])
882
        self.assertDiffBlocks([('a',), ('b',), ('c,')],
883
                              [('a',), ('b',), ('c,')],
884
                              [(0, 0, 3)])
885
        self.assertDiffBlocks([('a',), ('b',), ('c,')],
886
                              [('a',), ('b',), ('d,')],
887
                              [(0, 0, 2)])
888
        self.assertDiffBlocks([('d',), ('b',), ('c,')],
889
                              [('a',), ('b',), ('c,')],
890
                              [(1, 1, 2)])
891
        self.assertDiffBlocks([('d',), ('a',), ('b',), ('c,')],
892
                              [('a',), ('b',), ('c,')],
893
                              [(1, 0, 3)])
894
        self.assertDiffBlocks([('a', 'b'), ('c', 'd'), ('e', 'f')],
895
                              [('a', 'b'), ('c', 'X'), ('e', 'f')],
896
                              [(0, 0, 1), (2, 2, 1)])
897
        self.assertDiffBlocks([('a', 'b'), ('c', 'd'), ('e', 'f')],
898
                              [('a', 'b'), ('c', 'dX'), ('e', 'f')],
899
                              [(0, 0, 1), (2, 2, 1)])
3074.2.1 by John Arbash Meinel
Change the C PatienceDiff implementation to support arbitrary objects.
900
1185.81.3 by John Arbash Meinel
Adding tests for checking opcodes.
901
    def test_opcodes(self):
1711.2.10 by John Arbash Meinel
Clarify the patience tests a little bit.
902
        def chk_ops(a, b, expected_codes):
2781.1.1 by Martin Pool
merge cpatiencediff from Lukas
903
            s = self._PatienceSequenceMatcher(None, a, b)
1711.2.10 by John Arbash Meinel
Clarify the patience tests a little bit.
904
            self.assertEquals(expected_codes, s.get_opcodes())
1185.81.3 by John Arbash Meinel
Adding tests for checking opcodes.
905
906
        chk_ops('', '', [])
907
        chk_ops([], [], [])
2781.1.1 by Martin Pool
merge cpatiencediff from Lukas
908
        chk_ops('abc', '', [('delete', 0,3, 0,0)])
909
        chk_ops('', 'abc', [('insert', 0,0, 0,3)])
1185.81.9 by John Arbash Meinel
Added (failing) tests for cdv.recurse_matches with common sections,
910
        chk_ops('abcd', 'abcd', [('equal',    0,4, 0,4)])
1185.81.29 by Aaron Bentley
Fix style issues and duplicated tests
911
        chk_ops('abcd', 'abce', [('equal',   0,3, 0,3),
912
                                 ('replace', 3,4, 3,4)
913
                                ])
914
        chk_ops('eabc', 'abce', [('delete', 0,1, 0,0),
915
                                 ('equal',  1,4, 0,3),
916
                                 ('insert', 4,4, 3,4)
917
                                ])
918
        chk_ops('eabce', 'abce', [('delete', 0,1, 0,0),
919
                                  ('equal',  1,5, 0,4)
1185.81.3 by John Arbash Meinel
Adding tests for checking opcodes.
920
                                 ])
1185.81.29 by Aaron Bentley
Fix style issues and duplicated tests
921
        chk_ops('abcde', 'abXde', [('equal',   0,2, 0,2),
922
                                   ('replace', 2,3, 2,3),
923
                                   ('equal',   3,5, 3,5)
1185.81.3 by John Arbash Meinel
Adding tests for checking opcodes.
924
                                  ])
1185.81.29 by Aaron Bentley
Fix style issues and duplicated tests
925
        chk_ops('abcde', 'abXYZde', [('equal',   0,2, 0,2),
926
                                     ('replace', 2,3, 2,5),
927
                                     ('equal',   3,5, 5,7)
1185.81.3 by John Arbash Meinel
Adding tests for checking opcodes.
928
                                    ])
1185.81.29 by Aaron Bentley
Fix style issues and duplicated tests
929
        chk_ops('abde', 'abXYZde', [('equal',  0,2, 0,2),
930
                                    ('insert', 2,2, 2,5),
931
                                    ('equal',  2,4, 5,7)
1185.81.3 by John Arbash Meinel
Adding tests for checking opcodes.
932
                                   ])
933
        chk_ops('abcdefghijklmnop', 'abcdefxydefghijklmnop',
1185.81.29 by Aaron Bentley
Fix style issues and duplicated tests
934
                [('equal',  0,6,  0,6),
935
                 ('insert', 6,6,  6,11),
936
                 ('equal',  6,16, 11,21)
1185.81.3 by John Arbash Meinel
Adding tests for checking opcodes.
937
                ])
938
        chk_ops(
939
                [ 'hello there\n'
940
                , 'world\n'
941
                , 'how are you today?\n'],
942
                [ 'hello there\n'
943
                , 'how are you today?\n'],
1185.81.29 by Aaron Bentley
Fix style issues and duplicated tests
944
                [('equal',  0,1, 0,1),
945
                 ('delete', 1,2, 1,1),
1711.2.21 by John Arbash Meinel
Cleanup patiencediff, remove the use of difflib.SequenceMatcher.
946
                 ('equal',  2,3, 1,2),
1185.81.9 by John Arbash Meinel
Added (failing) tests for cdv.recurse_matches with common sections,
947
                ])
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
948
        chk_ops('aBccDe', 'abccde',
1185.81.29 by Aaron Bentley
Fix style issues and duplicated tests
949
                [('equal',   0,1, 0,1),
1711.2.21 by John Arbash Meinel
Cleanup patiencediff, remove the use of difflib.SequenceMatcher.
950
                 ('replace', 1,5, 1,5),
951
                 ('equal',   5,6, 5,6),
952
                ])
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
953
        chk_ops('aBcDec', 'abcdec',
1711.2.21 by John Arbash Meinel
Cleanup patiencediff, remove the use of difflib.SequenceMatcher.
954
                [('equal',   0,1, 0,1),
1185.81.29 by Aaron Bentley
Fix style issues and duplicated tests
955
                 ('replace', 1,2, 1,2),
1711.2.21 by John Arbash Meinel
Cleanup patiencediff, remove the use of difflib.SequenceMatcher.
956
                 ('equal',   2,3, 2,3),
957
                 ('replace', 3,4, 3,4),
958
                 ('equal',   4,6, 4,6),
1185.81.3 by John Arbash Meinel
Adding tests for checking opcodes.
959
                ])
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
960
        chk_ops('aBcdEcdFg', 'abcdecdfg',
1185.81.29 by Aaron Bentley
Fix style issues and duplicated tests
961
                [('equal',   0,1, 0,1),
1711.2.21 by John Arbash Meinel
Cleanup patiencediff, remove the use of difflib.SequenceMatcher.
962
                 ('replace', 1,8, 1,8),
1185.81.29 by Aaron Bentley
Fix style issues and duplicated tests
963
                 ('equal',   8,9, 8,9)
1185.81.10 by John Arbash Meinel
Added some more test cases.
964
                ])
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
965
        chk_ops('aBcdEeXcdFg', 'abcdecdfg',
1711.2.21 by John Arbash Meinel
Cleanup patiencediff, remove the use of difflib.SequenceMatcher.
966
                [('equal',   0,1, 0,1),
967
                 ('replace', 1,2, 1,2),
968
                 ('equal',   2,4, 2,4),
969
                 ('delete', 4,5, 4,4),
970
                 ('equal',   5,6, 4,5),
971
                 ('delete', 6,7, 5,5),
972
                 ('equal',   7,9, 5,7),
973
                 ('replace', 9,10, 7,8),
974
                 ('equal',   10,11, 8,9)
975
                ])
1185.81.10 by John Arbash Meinel
Added some more test cases.
976
2781.1.1 by Martin Pool
merge cpatiencediff from Lukas
977
    def test_grouped_opcodes(self):
978
        def chk_ops(a, b, expected_codes, n=3):
979
            s = self._PatienceSequenceMatcher(None, a, b)
980
            self.assertEquals(expected_codes, list(s.get_grouped_opcodes(n)))
981
982
        chk_ops('', '', [])
983
        chk_ops([], [], [])
984
        chk_ops('abc', '', [[('delete', 0,3, 0,0)]])
985
        chk_ops('', 'abc', [[('insert', 0,0, 0,3)]])
986
        chk_ops('abcd', 'abcd', [])
987
        chk_ops('abcd', 'abce', [[('equal',   0,3, 0,3),
988
                                  ('replace', 3,4, 3,4)
989
                                 ]])
990
        chk_ops('eabc', 'abce', [[('delete', 0,1, 0,0),
991
                                 ('equal',  1,4, 0,3),
992
                                 ('insert', 4,4, 3,4)
993
                                ]])
994
        chk_ops('abcdefghijklmnop', 'abcdefxydefghijklmnop',
995
                [[('equal',  3,6, 3,6),
996
                  ('insert', 6,6, 6,11),
997
                  ('equal',  6,9, 11,14)
998
                  ]])
999
        chk_ops('abcdefghijklmnop', 'abcdefxydefghijklmnop',
1000
                [[('equal',  2,6, 2,6),
1001
                  ('insert', 6,6, 6,11),
1002
                  ('equal',  6,10, 11,15)
1003
                  ]], 4)
1004
        chk_ops('Xabcdef', 'abcdef',
1005
                [[('delete', 0,1, 0,0),
1006
                  ('equal',  1,4, 0,3)
1007
                  ]])
1008
        chk_ops('abcdef', 'abcdefX',
1009
                [[('equal',  3,6, 3,6),
1010
                  ('insert', 6,6, 6,7)
1011
                  ]])
1012
1013
1185.81.16 by John Arbash Meinel
Added tests, and an assert check to make sure ranges are always increasing.
1014
    def test_multiple_ranges(self):
1015
        # There was an earlier bug where we used a bad set of ranges,
1016
        # this triggers that specific bug, to make sure it doesn't regress
3074.2.10 by John Arbash Meinel
Cleanup the test cases (Andrew)
1017
        self.assertDiffBlocks('abcdefghijklmnop',
1018
                              'abcXghiYZQRSTUVWXYZijklmnop',
1019
                              [(0, 0, 3), (6, 4, 3), (9, 20, 7)])
1020
1021
        self.assertDiffBlocks('ABCd efghIjk  L',
1022
                              'AxyzBCn mo pqrstuvwI1 2  L',
1023
                              [(0,0,1), (1, 4, 2), (9, 19, 1), (12, 23, 3)])
1185.81.16 by John Arbash Meinel
Added tests, and an assert check to make sure ranges are always increasing.
1024
1711.2.8 by John Arbash Meinel
rot13 the code snippet to help with clarity.
1025
        # These are rot13 code snippets.
3074.2.10 by John Arbash Meinel
Cleanup the test cases (Andrew)
1026
        self.assertDiffBlocks('''\
1711.2.8 by John Arbash Meinel
rot13 the code snippet to help with clarity.
1027
    trg nqqrq jura lbh nqq n svyr va gur qverpgbel.
1028
    """
1029
    gnxrf_netf = ['svyr*']
1030
    gnxrf_bcgvbaf = ['ab-erphefr']
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1031
1711.2.8 by John Arbash Meinel
rot13 the code snippet to help with clarity.
1032
    qrs eha(frys, svyr_yvfg, ab_erphefr=Snyfr):
1033
        sebz omeyvo.nqq vzcbeg fzneg_nqq, nqq_ercbegre_cevag, nqq_ercbegre_ahyy
1034
        vs vf_dhvrg():
1035
            ercbegre = nqq_ercbegre_ahyy
1036
        ryfr:
1037
            ercbegre = nqq_ercbegre_cevag
1038
        fzneg_nqq(svyr_yvfg, abg ab_erphefr, ercbegre)
1039
1040
1041
pynff pzq_zxqve(Pbzznaq):
1042
'''.splitlines(True), '''\
1043
    trg nqqrq jura lbh nqq n svyr va gur qverpgbel.
1044
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1045
    --qel-eha jvyy fubj juvpu svyrf jbhyq or nqqrq, ohg abg npghnyyl
1711.2.8 by John Arbash Meinel
rot13 the code snippet to help with clarity.
1046
    nqq gurz.
1047
    """
1048
    gnxrf_netf = ['svyr*']
1049
    gnxrf_bcgvbaf = ['ab-erphefr', 'qel-eha']
1050
1051
    qrs eha(frys, svyr_yvfg, ab_erphefr=Snyfr, qel_eha=Snyfr):
1052
        vzcbeg omeyvo.nqq
1053
1054
        vs qel_eha:
1055
            vs vf_dhvrg():
1056
                # Guvf vf cbvagyrff, ohg V'q engure abg envfr na reebe
1057
                npgvba = omeyvo.nqq.nqq_npgvba_ahyy
1058
            ryfr:
1059
  npgvba = omeyvo.nqq.nqq_npgvba_cevag
1060
        ryvs vf_dhvrg():
1061
            npgvba = omeyvo.nqq.nqq_npgvba_nqq
1062
        ryfr:
1063
       npgvba = omeyvo.nqq.nqq_npgvba_nqq_naq_cevag
1064
1065
        omeyvo.nqq.fzneg_nqq(svyr_yvfg, abg ab_erphefr, npgvba)
1066
1067
1068
pynff pzq_zxqve(Pbzznaq):
1185.81.16 by John Arbash Meinel
Added tests, and an assert check to make sure ranges are always increasing.
1069
'''.splitlines(True)
1070
, [(0,0,1), (1, 4, 2), (9, 19, 1), (12, 23, 3)])
1071
1711.2.9 by John Arbash Meinel
Rename cdv => patience
1072
    def test_patience_unified_diff(self):
1185.81.29 by Aaron Bentley
Fix style issues and duplicated tests
1073
        txt_a = ['hello there\n',
1074
                 'world\n',
1075
                 'how are you today?\n']
1076
        txt_b = ['hello there\n',
1077
                 'how are you today?\n']
1711.2.20 by John Arbash Meinel
Late bind to patiencediff objects to make it easier to plug-in
1078
        unified_diff = bzrlib.patiencediff.unified_diff
2781.1.1 by Martin Pool
merge cpatiencediff from Lukas
1079
        psm = self._PatienceSequenceMatcher
3922.1.3 by John Arbash Meinel
fix some odd spacing.
1080
        self.assertEquals(['--- \n',
3922.1.2 by John Arbash Meinel
Update the test cases for the new patience diff code.
1081
                           '+++ \n',
1185.81.29 by Aaron Bentley
Fix style issues and duplicated tests
1082
                           '@@ -1,3 +1,2 @@\n',
1083
                           ' hello there\n',
1084
                           '-world\n',
1085
                           ' how are you today?\n'
1185.81.14 by John Arbash Meinel
Added a main function for running cdvdifflib manually, included tests for unified_diff interfaces
1086
                          ]
1711.2.20 by John Arbash Meinel
Late bind to patiencediff objects to make it easier to plug-in
1087
                          , list(unified_diff(txt_a, txt_b,
1088
                                 sequencematcher=psm)))
1185.81.14 by John Arbash Meinel
Added a main function for running cdvdifflib manually, included tests for unified_diff interfaces
1089
        txt_a = map(lambda x: x+'\n', 'abcdefghijklmnop')
1090
        txt_b = map(lambda x: x+'\n', 'abcdefxydefghijklmnop')
1091
        # This is the result with LongestCommonSubstring matching
3922.1.2 by John Arbash Meinel
Update the test cases for the new patience diff code.
1092
        self.assertEquals(['--- \n',
1093
                           '+++ \n',
1185.81.29 by Aaron Bentley
Fix style issues and duplicated tests
1094
                           '@@ -1,6 +1,11 @@\n',
1095
                           ' a\n',
1096
                           ' b\n',
1097
                           ' c\n',
1098
                           '+d\n',
1099
                           '+e\n',
1100
                           '+f\n',
1101
                           '+x\n',
1102
                           '+y\n',
1103
                           ' d\n',
1104
                           ' e\n',
1105
                           ' f\n']
1185.81.14 by John Arbash Meinel
Added a main function for running cdvdifflib manually, included tests for unified_diff interfaces
1106
                          , list(unified_diff(txt_a, txt_b)))
1711.2.9 by John Arbash Meinel
Rename cdv => patience
1107
        # And the patience diff
3922.1.2 by John Arbash Meinel
Update the test cases for the new patience diff code.
1108
        self.assertEquals(['--- \n',
1109
                           '+++ \n',
1185.81.29 by Aaron Bentley
Fix style issues and duplicated tests
1110
                           '@@ -4,6 +4,11 @@\n',
1111
                           ' d\n',
1112
                           ' e\n',
1113
                           ' f\n',
1114
                           '+x\n',
1115
                           '+y\n',
1116
                           '+d\n',
1117
                           '+e\n',
1118
                           '+f\n',
1119
                           ' g\n',
1120
                           ' h\n',
1121
                           ' i\n',
1185.81.14 by John Arbash Meinel
Added a main function for running cdvdifflib manually, included tests for unified_diff interfaces
1122
                          ]
1185.81.25 by Aaron Bentley
Clean up test_diff
1123
                          , list(unified_diff(txt_a, txt_b,
1711.2.20 by John Arbash Meinel
Late bind to patiencediff objects to make it easier to plug-in
1124
                                 sequencematcher=psm)))
1185.81.25 by Aaron Bentley
Clean up test_diff
1125
3922.1.2 by John Arbash Meinel
Update the test cases for the new patience diff code.
1126
    def test_patience_unified_diff_with_dates(self):
1127
        txt_a = ['hello there\n',
1128
                 'world\n',
1129
                 'how are you today?\n']
1130
        txt_b = ['hello there\n',
1131
                 'how are you today?\n']
1132
        unified_diff = bzrlib.patiencediff.unified_diff
1133
        psm = self._PatienceSequenceMatcher
3922.1.4 by John Arbash Meinel
It turns out that internal_diff worked around the trailing whitespace problem
1134
        self.assertEquals(['--- a\t2008-08-08\n',
1135
                           '+++ b\t2008-09-09\n',
3922.1.2 by John Arbash Meinel
Update the test cases for the new patience diff code.
1136
                           '@@ -1,3 +1,2 @@\n',
1137
                           ' hello there\n',
1138
                           '-world\n',
1139
                           ' how are you today?\n'
1140
                          ]
1141
                          , list(unified_diff(txt_a, txt_b,
1142
                                 fromfile='a', tofile='b',
1143
                                 fromfiledate='2008-08-08',
1144
                                 tofiledate='2008-09-09',
1145
                                 sequencematcher=psm)))
1146
1185.81.14 by John Arbash Meinel
Added a main function for running cdvdifflib manually, included tests for unified_diff interfaces
1147
2781.1.1 by Martin Pool
merge cpatiencediff from Lukas
1148
class TestPatienceDiffLib_c(TestPatienceDiffLib):
1149
4913.2.20 by John Arbash Meinel
Change all of the compiled_foo to compiled_foo_feature
1150
    _test_needs_features = [compiled_patiencediff_feature]
2781.1.1 by Martin Pool
merge cpatiencediff from Lukas
1151
1152
    def setUp(self):
1153
        super(TestPatienceDiffLib_c, self).setUp()
1154
        import bzrlib._patiencediff_c
1155
        self._unique_lcs = bzrlib._patiencediff_c.unique_lcs_c
1156
        self._recurse_matches = bzrlib._patiencediff_c.recurse_matches_c
1157
        self._PatienceSequenceMatcher = \
1158
            bzrlib._patiencediff_c.PatienceSequenceMatcher_c
1159
3074.2.3 by John Arbash Meinel
Enable some error checking, and small amount of code cleanup.
1160
    def test_unhashable(self):
1161
        """We should get a proper exception here."""
3074.2.10 by John Arbash Meinel
Cleanup the test cases (Andrew)
1162
        # We need to be able to hash items in the sequence, lists are
1163
        # unhashable, and thus cannot be diffed
3074.2.3 by John Arbash Meinel
Enable some error checking, and small amount of code cleanup.
1164
        e = self.assertRaises(TypeError, self._PatienceSequenceMatcher,
1165
                                         None, [[]], [])
3074.2.10 by John Arbash Meinel
Cleanup the test cases (Andrew)
1166
        e = self.assertRaises(TypeError, self._PatienceSequenceMatcher,
1167
                                         None, ['valid', []], [])
1168
        e = self.assertRaises(TypeError, self._PatienceSequenceMatcher,
1169
                                         None, ['valid'], [[]])
1170
        e = self.assertRaises(TypeError, self._PatienceSequenceMatcher,
1171
                                         None, ['valid'], ['valid', []])
3074.2.3 by John Arbash Meinel
Enable some error checking, and small amount of code cleanup.
1172
2781.1.1 by Martin Pool
merge cpatiencediff from Lukas
1173
1711.2.15 by John Arbash Meinel
Found a couple CDV left
1174
class TestPatienceDiffLibFiles(TestCaseInTempDir):
1185.81.14 by John Arbash Meinel
Added a main function for running cdvdifflib manually, included tests for unified_diff interfaces
1175
2781.1.1 by Martin Pool
merge cpatiencediff from Lukas
1176
    def setUp(self):
1177
        super(TestPatienceDiffLibFiles, self).setUp()
1178
        self._PatienceSequenceMatcher = \
1179
            bzrlib._patiencediff_py.PatienceSequenceMatcher_py
1180
1711.2.9 by John Arbash Meinel
Rename cdv => patience
1181
    def test_patience_unified_diff_files(self):
1185.81.29 by Aaron Bentley
Fix style issues and duplicated tests
1182
        txt_a = ['hello there\n',
1183
                 'world\n',
1184
                 'how are you today?\n']
1185
        txt_b = ['hello there\n',
1186
                 'how are you today?\n']
1185.81.14 by John Arbash Meinel
Added a main function for running cdvdifflib manually, included tests for unified_diff interfaces
1187
        open('a1', 'wb').writelines(txt_a)
1188
        open('b1', 'wb').writelines(txt_b)
1189
1711.2.20 by John Arbash Meinel
Late bind to patiencediff objects to make it easier to plug-in
1190
        unified_diff_files = bzrlib.patiencediff.unified_diff_files
2781.1.1 by Martin Pool
merge cpatiencediff from Lukas
1191
        psm = self._PatienceSequenceMatcher
3922.1.2 by John Arbash Meinel
Update the test cases for the new patience diff code.
1192
        self.assertEquals(['--- a1\n',
1193
                           '+++ b1\n',
1185.81.29 by Aaron Bentley
Fix style issues and duplicated tests
1194
                           '@@ -1,3 +1,2 @@\n',
1195
                           ' hello there\n',
1196
                           '-world\n',
1197
                           ' how are you today?\n',
1185.81.14 by John Arbash Meinel
Added a main function for running cdvdifflib manually, included tests for unified_diff interfaces
1198
                          ]
1185.81.25 by Aaron Bentley
Clean up test_diff
1199
                          , list(unified_diff_files('a1', 'b1',
1711.2.20 by John Arbash Meinel
Late bind to patiencediff objects to make it easier to plug-in
1200
                                 sequencematcher=psm)))
1185.81.14 by John Arbash Meinel
Added a main function for running cdvdifflib manually, included tests for unified_diff interfaces
1201
1202
        txt_a = map(lambda x: x+'\n', 'abcdefghijklmnop')
1203
        txt_b = map(lambda x: x+'\n', 'abcdefxydefghijklmnop')
1204
        open('a2', 'wb').writelines(txt_a)
1205
        open('b2', 'wb').writelines(txt_b)
1206
1207
        # This is the result with LongestCommonSubstring matching
3922.1.2 by John Arbash Meinel
Update the test cases for the new patience diff code.
1208
        self.assertEquals(['--- a2\n',
1209
                           '+++ b2\n',
1185.81.29 by Aaron Bentley
Fix style issues and duplicated tests
1210
                           '@@ -1,6 +1,11 @@\n',
1211
                           ' a\n',
1212
                           ' b\n',
1213
                           ' c\n',
1214
                           '+d\n',
1215
                           '+e\n',
1216
                           '+f\n',
1217
                           '+x\n',
1218
                           '+y\n',
1219
                           ' d\n',
1220
                           ' e\n',
1221
                           ' f\n']
1185.81.14 by John Arbash Meinel
Added a main function for running cdvdifflib manually, included tests for unified_diff interfaces
1222
                          , list(unified_diff_files('a2', 'b2')))
1223
1711.2.9 by John Arbash Meinel
Rename cdv => patience
1224
        # And the patience diff
3922.1.2 by John Arbash Meinel
Update the test cases for the new patience diff code.
1225
        self.assertEquals(['--- a2\n',
1226
                           '+++ b2\n',
1185.81.29 by Aaron Bentley
Fix style issues and duplicated tests
1227
                           '@@ -4,6 +4,11 @@\n',
1228
                           ' d\n',
1229
                           ' e\n',
1230
                           ' f\n',
1231
                           '+x\n',
1232
                           '+y\n',
1233
                           '+d\n',
1234
                           '+e\n',
1235
                           '+f\n',
1236
                           ' g\n',
1237
                           ' h\n',
1238
                           ' i\n',
1185.81.14 by John Arbash Meinel
Added a main function for running cdvdifflib manually, included tests for unified_diff interfaces
1239
                          ]
1185.81.25 by Aaron Bentley
Clean up test_diff
1240
                          , list(unified_diff_files('a2', 'b2',
1711.2.20 by John Arbash Meinel
Late bind to patiencediff objects to make it easier to plug-in
1241
                                 sequencematcher=psm)))
2781.1.1 by Martin Pool
merge cpatiencediff from Lukas
1242
1243
1244
class TestPatienceDiffLibFiles_c(TestPatienceDiffLibFiles):
1245
4913.2.20 by John Arbash Meinel
Change all of the compiled_foo to compiled_foo_feature
1246
    _test_needs_features = [compiled_patiencediff_feature]
2781.1.1 by Martin Pool
merge cpatiencediff from Lukas
1247
1248
    def setUp(self):
1249
        super(TestPatienceDiffLibFiles_c, self).setUp()
1250
        import bzrlib._patiencediff_c
1251
        self._PatienceSequenceMatcher = \
1252
            bzrlib._patiencediff_c.PatienceSequenceMatcher_c
1253
1254
1255
class TestUsingCompiledIfAvailable(TestCase):
1256
1257
    def test_PatienceSequenceMatcher(self):
4913.2.20 by John Arbash Meinel
Change all of the compiled_foo to compiled_foo_feature
1258
        if compiled_patiencediff_feature.available():
2781.1.1 by Martin Pool
merge cpatiencediff from Lukas
1259
            from bzrlib._patiencediff_c import PatienceSequenceMatcher_c
1260
            self.assertIs(PatienceSequenceMatcher_c,
1261
                          bzrlib.patiencediff.PatienceSequenceMatcher)
1262
        else:
1263
            from bzrlib._patiencediff_py import PatienceSequenceMatcher_py
1264
            self.assertIs(PatienceSequenceMatcher_py,
1265
                          bzrlib.patiencediff.PatienceSequenceMatcher)
1266
1267
    def test_unique_lcs(self):
4913.2.20 by John Arbash Meinel
Change all of the compiled_foo to compiled_foo_feature
1268
        if compiled_patiencediff_feature.available():
2781.1.1 by Martin Pool
merge cpatiencediff from Lukas
1269
            from bzrlib._patiencediff_c import unique_lcs_c
1270
            self.assertIs(unique_lcs_c,
1271
                          bzrlib.patiencediff.unique_lcs)
1272
        else:
1273
            from bzrlib._patiencediff_py import unique_lcs_py
1274
            self.assertIs(unique_lcs_py,
1275
                          bzrlib.patiencediff.unique_lcs)
1276
1277
    def test_recurse_matches(self):
4913.2.20 by John Arbash Meinel
Change all of the compiled_foo to compiled_foo_feature
1278
        if compiled_patiencediff_feature.available():
2781.1.1 by Martin Pool
merge cpatiencediff from Lukas
1279
            from bzrlib._patiencediff_c import recurse_matches_c
1280
            self.assertIs(recurse_matches_c,
1281
                          bzrlib.patiencediff.recurse_matches)
1282
        else:
1283
            from bzrlib._patiencediff_py import recurse_matches_py
1284
            self.assertIs(recurse_matches_py,
1285
                          bzrlib.patiencediff.recurse_matches)
3123.6.2 by Aaron Bentley
Implement diff --using natively
1286
1287
1288
class TestDiffFromTool(TestCaseWithTransport):
1289
1290
    def test_from_string(self):
1291
        diff_obj = DiffFromTool.from_string('diff', None, None, None)
1292
        self.addCleanup(diff_obj.finish)
4603.1.20 by Aaron Bentley
Use string.Template substitution with @ as delimiter.
1293
        self.assertEqual(['diff', '@old_path', '@new_path'],
3123.6.2 by Aaron Bentley
Implement diff --using natively
1294
            diff_obj.command_template)
3199.1.6 by Vincent Ladeuil
Fiz last leaking tmp dir.
1295
1296
    def test_from_string_u5(self):
4913.5.13 by Gordon Tyler
Fixed a few more tests.
1297
        diff_obj = DiffFromTool.from_string('diff "-u 5"', None, None, None)
3199.1.6 by Vincent Ladeuil
Fiz last leaking tmp dir.
1298
        self.addCleanup(diff_obj.finish)
4603.1.20 by Aaron Bentley
Use string.Template substitution with @ as delimiter.
1299
        self.assertEqual(['diff', '-u 5', '@old_path', '@new_path'],
3123.6.2 by Aaron Bentley
Implement diff --using natively
1300
                         diff_obj.command_template)
1301
        self.assertEqual(['diff', '-u 5', 'old-path', 'new-path'],
1302
                         diff_obj._get_command('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.
1303
        
1304
    def test_from_string_path_with_backslashes(self):
4913.5.9 by Gordon Tyler
Use test_win32utils' BackslashDirSeparatorFeature instead of checking sys.platform for win32.
1305
        self.requireFeature(BackslashDirSeparatorFeature)
4913.5.8 by Gordon Tyler
Added test_from_string_path_with_backslashes, which tests the actual scenario in bug 392428.
1306
        tool = 'C:\\Tools\\Diff.exe'
1307
        diff_obj = DiffFromTool.from_string(tool, None, None, None)
1308
        self.addCleanup(diff_obj.finish)
1309
        self.assertEqual(['C:\\Tools\\Diff.exe', '@old_path', '@new_path'],
1310
                         diff_obj.command_template)
1311
        self.assertEqual(['C:\\Tools\\Diff.exe', 'old-path', 'new-path'],
1312
                         diff_obj._get_command('old-path', 'new-path'))
3123.6.2 by Aaron Bentley
Implement diff --using natively
1313
1314
    def test_execute(self):
1315
        output = StringIO()
1316
        diff_obj = DiffFromTool(['python', '-c',
4603.1.20 by Aaron Bentley
Use string.Template substitution with @ as delimiter.
1317
                                 'print "@old_path @new_path"'],
3123.6.2 by Aaron Bentley
Implement diff --using natively
1318
                                None, None, output)
1319
        self.addCleanup(diff_obj.finish)
1320
        diff_obj._execute('old', 'new')
3146.4.2 by Aaron Bentley
Avoid assuming unix newline on output
1321
        self.assertEqual(output.getvalue().rstrip(), 'old new')
3123.6.2 by Aaron Bentley
Implement diff --using natively
1322
3145.1.1 by Aaron Bentley
Handle missing tools gracefully in diff --using
1323
    def test_excute_missing(self):
1324
        diff_obj = DiffFromTool(['a-tool-which-is-unlikely-to-exist'],
1325
                                None, None, None)
1326
        self.addCleanup(diff_obj.finish)
1327
        e = self.assertRaises(ExecutableMissing, diff_obj._execute, 'old',
1328
                              'new')
1329
        self.assertEqual('a-tool-which-is-unlikely-to-exist could not be found'
1330
                         ' on this machine', str(e))
1331
3287.18.22 by Matt McClure
Reverts to prior decomposition of exercise and verification, as suggested
1332
    def test_prepare_files_creates_paths_readable_by_windows_tool(self):
3287.18.20 by Matt McClure
Introduces a Feature subclass to encapsulate the availability of 'attrib'.
1333
        self.requireFeature(AttribFeature)
3287.18.10 by Matt McClure
Uses TestSkipped for test_execute_windows_tool on non-Windows platforms.
1334
        output = StringIO()
1335
        tree = self.make_branch_and_tree('tree')
1336
        self.build_tree_contents([('tree/file', 'content')])
1337
        tree.add('file', 'file-id')
3287.18.11 by Matt McClure
Removed unnecessary timestamp parameter.
1338
        tree.commit('old tree')
3287.18.10 by Matt McClure
Uses TestSkipped for test_execute_windows_tool on non-Windows platforms.
1339
        tree.lock_read()
1340
        self.addCleanup(tree.unlock)
4873.3.1 by John Arbash Meinel
Now that we return files directly from the working tree
1341
        basis_tree = tree.basis_tree()
1342
        basis_tree.lock_read()
1343
        self.addCleanup(basis_tree.unlock)
3287.18.10 by Matt McClure
Uses TestSkipped for test_execute_windows_tool on non-Windows platforms.
1344
        diff_obj = DiffFromTool(['python', '-c',
4603.1.20 by Aaron Bentley
Use string.Template substitution with @ as delimiter.
1345
                                 'print "@old_path @new_path"'],
4873.3.1 by John Arbash Meinel
Now that we return files directly from the working tree
1346
                                basis_tree, tree, output)
3287.18.22 by Matt McClure
Reverts to prior decomposition of exercise and verification, as suggested
1347
        diff_obj._prepare_files('file-id', 'file', 'file')
4873.3.1 by John Arbash Meinel
Now that we return files directly from the working tree
1348
        # The old content should be readonly
1349
        self.assertReadableByAttrib(diff_obj._root, 'old\\file',
1350
                                    r'R.*old\\file$')
1351
        # The new content should use the tree object, not a 'new' file anymore
1352
        self.assertEndsWith(tree.basedir, 'work/tree')
1353
        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
1354
1355
    def assertReadableByAttrib(self, cwd, relpath, regex):
1356
        proc = subprocess.Popen(['attrib', relpath],
1357
                                stdout=subprocess.PIPE,
1358
                                cwd=cwd)
4873.3.1 by John Arbash Meinel
Now that we return files directly from the working tree
1359
        (result, err) = proc.communicate()
1360
        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
1361
3123.6.2 by Aaron Bentley
Implement diff --using natively
1362
    def test_prepare_files(self):
1363
        output = StringIO()
1364
        tree = self.make_branch_and_tree('tree')
3123.6.5 by Aaron Bentley
Symlink to real files if possible
1365
        self.build_tree_contents([('tree/oldname', 'oldcontent')])
3287.18.23 by Matt McClure
Adds comments that document my understanding of
1366
        self.build_tree_contents([('tree/oldname2', 'oldcontent2')])
3123.6.5 by Aaron Bentley
Symlink to real files if possible
1367
        tree.add('oldname', 'file-id')
3287.18.23 by Matt McClure
Adds comments that document my understanding of
1368
        tree.add('oldname2', 'file2-id')
3123.6.4 by Aaron Bentley
Set mtime (and atime) on files for --using
1369
        tree.commit('old tree', timestamp=0)
3123.6.5 by Aaron Bentley
Symlink to real files if possible
1370
        tree.rename_one('oldname', 'newname')
3287.18.23 by Matt McClure
Adds comments that document my understanding of
1371
        tree.rename_one('oldname2', 'newname2')
3123.6.5 by Aaron Bentley
Symlink to real files if possible
1372
        self.build_tree_contents([('tree/newname', 'newcontent')])
3287.19.1 by Matt McClure
Fixes https://bugs.launchpad.net/bzr/+bug/212289. I submitted this patch.
1373
        self.build_tree_contents([('tree/newname2', 'newcontent2')])
3123.6.2 by Aaron Bentley
Implement diff --using natively
1374
        old_tree = tree.basis_tree()
1375
        old_tree.lock_read()
1376
        self.addCleanup(old_tree.unlock)
3123.6.4 by Aaron Bentley
Set mtime (and atime) on files for --using
1377
        tree.lock_read()
1378
        self.addCleanup(tree.unlock)
3123.6.2 by Aaron Bentley
Implement diff --using natively
1379
        diff_obj = DiffFromTool(['python', '-c',
4603.1.20 by Aaron Bentley
Use string.Template substitution with @ as delimiter.
1380
                                 'print "@old_path @new_path"'],
3123.6.2 by Aaron Bentley
Implement diff --using natively
1381
                                old_tree, tree, output)
1382
        self.addCleanup(diff_obj.finish)
1383
        self.assertContainsRe(diff_obj._root, 'bzr-diff-[^/]*')
1384
        old_path, new_path = diff_obj._prepare_files('file-id', 'oldname',
1385
                                                     'newname')
1386
        self.assertContainsRe(old_path, 'old/oldname$')
3123.6.4 by Aaron Bentley
Set mtime (and atime) on files for --using
1387
        self.assertEqual(0, 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.
1388
        self.assertContainsRe(new_path, 'tree/newname$')
3123.6.2 by Aaron Bentley
Implement diff --using natively
1389
        self.assertFileEqual('oldcontent', old_path)
1390
        self.assertFileEqual('newcontent', new_path)
3287.18.14 by Matt McClure
Extracted a host_os_dereferences_symlinks method.
1391
        if osutils.host_os_dereferences_symlinks():
3123.6.5 by Aaron Bentley
Symlink to real files if possible
1392
            self.assertTrue(os.path.samefile('tree/newname', new_path))
3123.6.2 by Aaron Bentley
Implement diff --using natively
1393
        # make sure we can create files with the same parent directories
3287.18.25 by Matt McClure
Uses the correct file_id as the argument to _prepare_files.
1394
        diff_obj._prepare_files('file2-id', 'oldname2', 'newname2')
4705.1.2 by Gary van der Merwe
Start on tests for get_trees_and_branches_to_diff.
1395
1396
1397
class TestGetTreesAndBranchesToDiff(TestCaseWithTransport):
4732.1.2 by Vincent Ladeuil
(trivial) Fix some PEP8 issues
1398
4705.1.2 by Gary van der Merwe
Start on tests for get_trees_and_branches_to_diff.
1399
    def test_basic(self):
1400
        tree = self.make_branch_and_tree('tree')
1401
        (old_tree, new_tree,
1402
         old_branch, new_branch,
1403
         specific_files, extra_trees) = \
1404
            get_trees_and_branches_to_diff(['tree'], None, None, None)
4732.1.2 by Vincent Ladeuil
(trivial) Fix some PEP8 issues
1405
4705.1.2 by Gary van der Merwe
Start on tests for get_trees_and_branches_to_diff.
1406
        self.assertIsInstance(old_tree, RevisionTree)
1407
        #print dir (old_tree)
1408
        self.assertEqual(_mod_revision.NULL_REVISION, old_tree.get_revision_id())
1409
        self.assertEqual(tree.basedir, new_tree.basedir)
1410
        self.assertEqual(tree.branch.base, old_branch.base)
1411
        self.assertEqual(tree.branch.base, new_branch.base)
1412
        self.assertIs(None, specific_files)
1413
        self.assertIs(None, extra_trees)
1414
1415
    def test_with_rev_specs(self):
1416
        tree = self.make_branch_and_tree('tree')
1417
        self.build_tree_contents([('tree/file', 'oldcontent')])
1418
        tree.add('file', 'file-id')
1419
        tree.commit('old tree', timestamp=0, rev_id="old-id")
1420
        self.build_tree_contents([('tree/file', 'newcontent')])
1421
        tree.commit('new tree', timestamp=0, rev_id="new-id")
4732.1.2 by Vincent Ladeuil
(trivial) Fix some PEP8 issues
1422
4705.1.2 by Gary van der Merwe
Start on tests for get_trees_and_branches_to_diff.
1423
        revisions = [RevisionSpec.from_string('1'),
1424
                     RevisionSpec.from_string('2')]
1425
        (old_tree, new_tree,
1426
         old_branch, new_branch,
1427
         specific_files, extra_trees) = \
1428
            get_trees_and_branches_to_diff(['tree'], revisions, None, None)
4732.1.2 by Vincent Ladeuil
(trivial) Fix some PEP8 issues
1429
4705.1.2 by Gary van der Merwe
Start on tests for get_trees_and_branches_to_diff.
1430
        self.assertIsInstance(old_tree, RevisionTree)
1431
        self.assertEqual("old-id", old_tree.get_revision_id())
1432
        self.assertIsInstance(new_tree, RevisionTree)
1433
        self.assertEqual("new-id", new_tree.get_revision_id())
1434
        self.assertEqual(tree.branch.base, old_branch.base)
1435
        self.assertEqual(tree.branch.base, new_branch.base)
1436
        self.assertIs(None, specific_files)
4705.1.4 by Gary van der Merwe
Add newline to end of test_diff.py
1437
        self.assertEqual(tree.basedir, extra_trees[0].basedir)