~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

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

Late bind to PatienceSequenceMatcher to allow plugin to override.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
# Copyright (C) 2005, 2006 by Canonical Ltd
2
 
#
 
2
 
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
5
5
# the Free Software Foundation; either version 2 of the License, or
6
6
# (at your option) any later version.
7
 
#
 
7
 
8
8
# This program is distributed in the hope that it will be useful,
9
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
11
# GNU General Public License for more details.
12
 
#
 
12
 
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
19
"""
20
20
 
21
21
import os
22
 
import re
23
22
 
24
23
import bzrlib
25
 
from bzrlib import workingtree
26
24
from bzrlib.branch import Branch
27
 
from bzrlib.tests import TestSkipped
28
25
from bzrlib.tests.blackbox import ExternalBase
29
26
 
30
27
 
31
 
def subst_dates(string):
32
 
    """Replace date strings with constant values."""
33
 
    return re.sub(r'\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2} [-\+]\d{4}',
34
 
                  'YYYY-MM-DD HH:MM:SS +ZZZZ', string)
35
 
 
36
 
 
37
28
class TestDiff(ExternalBase):
38
29
 
39
 
    def make_example_branch(self):
 
30
    def make_example_branch(test):
40
31
        # FIXME: copied from test_too_much -- share elsewhere?
41
 
        tree = self.make_branch_and_tree('.')
42
 
        open('hello', 'wb').write('foo\n')
43
 
        tree.add(['hello'])
44
 
        tree.commit('setup')
45
 
        open('goodbye', 'wb').write('baz\n')
46
 
        tree.add(['goodbye'])
47
 
        tree.commit('setup')
 
32
        test.runbzr('init')
 
33
        file('hello', 'wt').write('foo\n')
 
34
        test.runbzr('add hello')
 
35
        test.runbzr('commit -m setup hello')
 
36
        file('goodbye', 'wt').write('baz\n')
 
37
        test.runbzr('add goodbye')
 
38
        test.runbzr('commit -m setup goodbye')
48
39
 
49
40
    def test_diff(self):
50
41
        self.make_example_branch()
62
53
    def test_diff_prefix(self):
63
54
        """diff --prefix appends to filenames in output"""
64
55
        self.make_example_branch()
65
 
        file('hello', 'wb').write('hello world!\n')
 
56
        file('hello', 'wt').write('hello world!\n')
66
57
        out, err = self.runbzr('diff --prefix old/:new/', retcode=1)
67
58
        self.assertEquals(err, '')
68
 
        self.assertEqualDiff(subst_dates(out), '''\
 
59
        self.assertEqualDiff(out, '''\
69
60
=== modified file 'hello'
70
 
--- old/hello\tYYYY-MM-DD HH:MM:SS +ZZZZ
71
 
+++ new/hello\tYYYY-MM-DD HH:MM:SS +ZZZZ
 
61
--- old/hello\t
 
62
+++ new/hello\t
72
63
@@ -1,1 +1,1 @@
73
64
-foo
74
65
+hello world!
78
69
    def test_diff_p1(self):
79
70
        """diff -p1 produces lkml-style diffs"""
80
71
        self.make_example_branch()
81
 
        file('hello', 'wb').write('hello world!\n')
 
72
        file('hello', 'wt').write('hello world!\n')
82
73
        out, err = self.runbzr('diff -p1', retcode=1)
83
74
        self.assertEquals(err, '')
84
 
        self.assertEqualDiff(subst_dates(out), '''\
 
75
        self.assertEqualDiff(out, '''\
85
76
=== modified file 'hello'
86
 
--- old/hello\tYYYY-MM-DD HH:MM:SS +ZZZZ
87
 
+++ new/hello\tYYYY-MM-DD HH:MM:SS +ZZZZ
 
77
--- old/hello\t
 
78
+++ new/hello\t
88
79
@@ -1,1 +1,1 @@
89
80
-foo
90
81
+hello world!
94
85
    def test_diff_p0(self):
95
86
        """diff -p0 produces diffs with no prefix"""
96
87
        self.make_example_branch()
97
 
        file('hello', 'wb').write('hello world!\n')
 
88
        file('hello', 'wt').write('hello world!\n')
98
89
        out, err = self.runbzr('diff -p0', retcode=1)
99
90
        self.assertEquals(err, '')
100
 
        self.assertEqualDiff(subst_dates(out), '''\
 
91
        self.assertEqualDiff(out, '''\
101
92
=== modified file 'hello'
102
 
--- hello\tYYYY-MM-DD HH:MM:SS +ZZZZ
103
 
+++ hello\tYYYY-MM-DD HH:MM:SS +ZZZZ
 
93
--- hello\t
 
94
+++ hello\t
104
95
@@ -1,1 +1,1 @@
105
96
-foo
106
97
+hello world!
136
127
    def test_diff_branches(self):
137
128
        self.example_branches()
138
129
        # should open branch1 and diff against branch2, 
139
 
        out, err = self.run_bzr_captured(['diff', '-r', 'branch:branch2', 
140
 
                                          'branch1'],
141
 
                                         retcode=1)
142
 
        self.assertEquals('', err)
143
 
        self.assertEquals("=== modified file 'file'\n"
144
 
                          "--- file\tYYYY-MM-DD HH:MM:SS +ZZZZ\n"
145
 
                          "+++ file\tYYYY-MM-DD HH:MM:SS +ZZZZ\n"
146
 
                          "@@ -1,1 +1,1 @@\n"
147
 
                          "-new content\n"
148
 
                          "+contents of branch1/file\n"
149
 
                          "\n", subst_dates(out))
150
 
        out, err = self.run_bzr_captured(['diff', 'branch2', 'branch1'],
151
 
                                         retcode=1)
152
 
        self.assertEquals('', err)
153
 
        self.assertEqualDiff("=== modified file 'file'\n"
154
 
                              "--- file\tYYYY-MM-DD HH:MM:SS +ZZZZ\n"
155
 
                              "+++ file\tYYYY-MM-DD HH:MM:SS +ZZZZ\n"
156
 
                              "@@ -1,1 +1,1 @@\n"
157
 
                              "-new content\n"
158
 
                              "+contents of branch1/file\n"
159
 
                              "\n", subst_dates(out))
160
 
 
161
 
    def test_diff_revno_branches(self):
162
 
        self.example_branches()
163
 
        print >> open('branch2/file', 'wb'), 'even newer content'
164
 
        self.run_bzr_captured(['commit', '-m', 
165
 
                               'update file once more', 'branch2'])
166
 
 
167
 
        out, err = self.run_bzr_captured(['diff', '-r',
168
 
                                          'revno:1:branch2..revno:1:branch1'],
169
 
                                         retcode=0)
170
 
        self.assertEquals('', err)
171
 
        self.assertEquals('', out)
172
 
        out, err = self.run_bzr_captured(['diff', '-r', 
173
 
                                          'revno:2:branch2..revno:1:branch1'],
174
 
                                         retcode=1)
175
 
        self.assertEquals('', err)
176
 
        self.assertEqualDiff("=== modified file 'file'\n"
177
 
                              "--- file\tYYYY-MM-DD HH:MM:SS +ZZZZ\n"
178
 
                              "+++ file\tYYYY-MM-DD HH:MM:SS +ZZZZ\n"
179
 
                              "@@ -1,1 +1,1 @@\n"
180
 
                              "-new content\n"
181
 
                              "+contents of branch1/file\n"
182
 
                              "\n", subst_dates(out))
 
130
        output = self.run_bzr_captured(['diff', '-r', 'branch:branch2', 
 
131
                                        'branch1'],
 
132
                                       retcode=1)
 
133
        self.assertEquals(("=== modified file 'file'\n"
 
134
                           "--- file\t\n"
 
135
                           "+++ file\t\n"
 
136
                           "@@ -1,1 +1,1 @@\n"
 
137
                           "-new content\n"
 
138
                           "+contents of branch1/file\n"
 
139
                           "\n", ''), output)
 
140
        output = self.run_bzr_captured(['diff', 'branch2', 'branch1'],
 
141
                                       retcode=1)
 
142
        self.assertEqualDiff(("=== modified file 'file'\n"
 
143
                              "--- file\t\n"
 
144
                              "+++ file\t\n"
 
145
                              "@@ -1,1 +1,1 @@\n"
 
146
                              "-new content\n"
 
147
                              "+contents of branch1/file\n"
 
148
                              "\n", ''), output)
183
149
 
184
150
    def example_branch2(self):
185
151
        self.build_tree(['branch1/', 'branch1/file1'], line_endings='binary')
195
161
        self.example_branch2()
196
162
        
197
163
        print >> open('branch1/file1', 'wb'), 'new line'
198
 
        output = self.run_bzr_captured(['diff', '-r', '1..', 'branch1'],
199
 
                                       retcode=1)
 
164
        output = self.run_bzr_captured(['diff', '-r', '1..', 'branch1'], retcode=1)
200
165
        self.assertTrue('\n-original line\n+new line\n' in output[0])
201
166
 
202
 
    def test_diff_across_rename(self):
203
 
        """The working tree path should always be considered for diffing"""
204
 
        self.make_example_branch()
205
 
        self.run_bzr('diff', '-r', '0..1', 'hello', retcode=1)
206
 
        wt = workingtree.WorkingTree.open_containing('.')[0]
207
 
        wt.rename_one('hello', 'hello1')
208
 
        self.run_bzr('diff', 'hello1', retcode=1)
209
 
        self.run_bzr('diff', '-r', '0..1', 'hello1', retcode=1)
210
 
 
211
167
 
212
168
class TestCheckoutDiff(TestDiff):
213
169
 
256
212
        self.runbzr('rename hello gruezi')
257
213
        diff = self.run_bzr_captured(['diff'], retcode=1)
258
214
        self.assertTrue("=== renamed file 'hello' => 'gruezi'" in diff[0])
259
 
 
260
 
 
261
 
class TestExternalDiff(TestDiff):
262
 
 
263
 
    def test_external_diff(self):
264
 
        """Test that we can spawn an external diff process"""
265
 
        # We have to use run_bzr_subprocess, because we need to
266
 
        # test writing directly to stdout, (there was a bug in
267
 
        # subprocess.py that we had to workaround).
268
 
        # However, if 'diff' may not be available
269
 
        self.make_example_branch()
270
 
        orig_progress = os.environ.get('BZR_PROGRESS_BAR')
271
 
        try:
272
 
            os.environ['BZR_PROGRESS_BAR'] = 'none'
273
 
            out, err = self.run_bzr_subprocess('diff', '-r', '1',
274
 
                                               '--diff-options', '-ub',
275
 
                                               retcode=None)
276
 
        finally:
277
 
            if orig_progress is None:
278
 
                del os.environ['BZR_PROGRESS_BAR']
279
 
            else:
280
 
                os.environ['BZR_PROGRESS_BAR'] = orig_progress
281
 
            
282
 
        if 'Diff is not installed on this machine' in err:
283
 
            raise TestSkipped("No external 'diff' is available")
284
 
        self.assertEqual('', err)
285
 
        # We have to skip the stuff in the middle, because it depends
286
 
        # on time.time()
287
 
        self.assertStartsWith(out, "=== added file 'goodbye'\n"
288
 
                                   "--- goodbye\t1970-01-01 00:00:00 +0000\n"
289
 
                                   "+++ goodbye\t")
290
 
        self.assertEndsWith(out, "\n@@ -0,0 +1 @@\n"
291
 
                                 "+baz\n\n")