~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

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

  • Committer: Robert Collins
  • Date: 2007-04-30 05:13:58 UTC
  • mfrom: (2470 +trunk)
  • mto: This revision was merged to the branch mainline in revision 2471.
  • Revision ID: robertc@robertcollins.net-20070430051358-8cp7kvp1q0tqhxx0
Merge Johns fix for bug 110399.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006 by Canonical Ltd
2
 
 
 
1
# Copyright (C) 2005, 2006 Canonical Ltd
 
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
22
23
 
23
24
import bzrlib
 
25
from bzrlib import workingtree
24
26
from bzrlib.branch import Branch
 
27
from bzrlib.tests import TestSkipped
25
28
from bzrlib.tests.blackbox import ExternalBase
26
29
 
27
30
 
28
 
class TestDiff(ExternalBase):
29
 
 
30
 
    def make_example_branch(test):
 
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
class DiffBase(ExternalBase):
 
38
    """Base class with common setup method"""
 
39
 
 
40
    def make_example_branch(self):
31
41
        # FIXME: copied from test_too_much -- share elsewhere?
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')
 
42
        tree = self.make_branch_and_tree('.')
 
43
        open('hello', 'wb').write('foo\n')
 
44
        tree.add(['hello'])
 
45
        tree.commit('setup')
 
46
        open('goodbye', 'wb').write('baz\n')
 
47
        tree.add(['goodbye'])
 
48
        tree.commit('setup')
 
49
 
 
50
 
 
51
class TestDiff(DiffBase):
39
52
 
40
53
    def test_diff(self):
41
54
        self.make_example_branch()
53
66
    def test_diff_prefix(self):
54
67
        """diff --prefix appends to filenames in output"""
55
68
        self.make_example_branch()
56
 
        file('hello', 'wt').write('hello world!\n')
 
69
        file('hello', 'wb').write('hello world!\n')
57
70
        out, err = self.runbzr('diff --prefix old/:new/', retcode=1)
58
71
        self.assertEquals(err, '')
59
 
        self.assertEqualDiff(out, '''\
 
72
        self.assertEqualDiff(subst_dates(out), '''\
60
73
=== modified file 'hello'
61
 
--- old/hello\t
62
 
+++ new/hello\t
 
74
--- old/hello\tYYYY-MM-DD HH:MM:SS +ZZZZ
 
75
+++ new/hello\tYYYY-MM-DD HH:MM:SS +ZZZZ
63
76
@@ -1,1 +1,1 @@
64
77
-foo
65
78
+hello world!
66
79
 
67
80
''')
68
81
 
 
82
    def test_diff_illegal_prefix_value(self):
 
83
        # There was an error in error reporting for this option
 
84
        out, err = self.runbzr('diff --prefix old/', retcode=3)
 
85
        self.assertContainsRe(err,
 
86
            '--prefix expects two values separated by a colon')
 
87
 
69
88
    def test_diff_p1(self):
70
89
        """diff -p1 produces lkml-style diffs"""
71
90
        self.make_example_branch()
72
 
        file('hello', 'wt').write('hello world!\n')
 
91
        file('hello', 'wb').write('hello world!\n')
73
92
        out, err = self.runbzr('diff -p1', retcode=1)
74
93
        self.assertEquals(err, '')
75
 
        self.assertEqualDiff(out, '''\
 
94
        self.assertEqualDiff(subst_dates(out), '''\
76
95
=== modified file 'hello'
77
 
--- old/hello\t
78
 
+++ new/hello\t
 
96
--- old/hello\tYYYY-MM-DD HH:MM:SS +ZZZZ
 
97
+++ new/hello\tYYYY-MM-DD HH:MM:SS +ZZZZ
79
98
@@ -1,1 +1,1 @@
80
99
-foo
81
100
+hello world!
85
104
    def test_diff_p0(self):
86
105
        """diff -p0 produces diffs with no prefix"""
87
106
        self.make_example_branch()
88
 
        file('hello', 'wt').write('hello world!\n')
 
107
        file('hello', 'wb').write('hello world!\n')
89
108
        out, err = self.runbzr('diff -p0', retcode=1)
90
109
        self.assertEquals(err, '')
91
 
        self.assertEqualDiff(out, '''\
 
110
        self.assertEqualDiff(subst_dates(out), '''\
92
111
=== modified file 'hello'
93
 
--- hello\t
94
 
+++ hello\t
 
112
--- hello\tYYYY-MM-DD HH:MM:SS +ZZZZ
 
113
+++ hello\tYYYY-MM-DD HH:MM:SS +ZZZZ
95
114
@@ -1,1 +1,1 @@
96
115
-foo
97
116
+hello world!
105
124
        out, err = self.runbzr('diff does-not-exist', retcode=3)
106
125
        self.assertContainsRe(err, 'not versioned.*does-not-exist')
107
126
 
 
127
    def test_diff_illegal_revision_specifiers(self):
 
128
        out, err = self.runbzr('diff -r 1..23..123', retcode=3)
 
129
        self.assertContainsRe(err, 'one or two revision specifiers')
 
130
 
108
131
    def test_diff_unversioned(self):
109
132
        # Get an error when diffing a non-versioned file.
110
133
        # (Malone #3619)
127
150
    def test_diff_branches(self):
128
151
        self.example_branches()
129
152
        # should open branch1 and diff against branch2, 
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)
 
153
        out, err = self.run_bzr_captured(['diff', '-r', 'branch:branch2', 
 
154
                                          'branch1'],
 
155
                                         retcode=1)
 
156
        self.assertEquals('', err)
 
157
        self.assertEquals("=== modified file 'file'\n"
 
158
                          "--- file\tYYYY-MM-DD HH:MM:SS +ZZZZ\n"
 
159
                          "+++ file\tYYYY-MM-DD HH:MM:SS +ZZZZ\n"
 
160
                          "@@ -1,1 +1,1 @@\n"
 
161
                          "-new content\n"
 
162
                          "+contents of branch1/file\n"
 
163
                          "\n", subst_dates(out))
 
164
        out, err = self.run_bzr_captured(['diff', 'branch2', 'branch1'],
 
165
                                         retcode=1)
 
166
        self.assertEquals('', err)
 
167
        self.assertEqualDiff("=== modified file 'file'\n"
 
168
                              "--- file\tYYYY-MM-DD HH:MM:SS +ZZZZ\n"
 
169
                              "+++ file\tYYYY-MM-DD HH:MM:SS +ZZZZ\n"
 
170
                              "@@ -1,1 +1,1 @@\n"
 
171
                              "-new content\n"
 
172
                              "+contents of branch1/file\n"
 
173
                              "\n", subst_dates(out))
 
174
 
 
175
    def test_diff_revno_branches(self):
 
176
        self.example_branches()
 
177
        print >> open('branch2/file', 'wb'), 'even newer content'
 
178
        self.run_bzr_captured(['commit', '-m', 
 
179
                               'update file once more', 'branch2'])
 
180
 
 
181
        out, err = self.run_bzr_captured(['diff', '-r',
 
182
                                          'revno:1:branch2..revno:1:branch1'],
 
183
                                         retcode=0)
 
184
        self.assertEquals('', err)
 
185
        self.assertEquals('', out)
 
186
        out, err = self.run_bzr_captured(['diff', '-r', 
 
187
                                          'revno:2:branch2..revno:1:branch1'],
 
188
                                         retcode=1)
 
189
        self.assertEquals('', err)
 
190
        self.assertEqualDiff("=== modified file 'file'\n"
 
191
                              "--- file\tYYYY-MM-DD HH:MM:SS +ZZZZ\n"
 
192
                              "+++ file\tYYYY-MM-DD HH:MM:SS +ZZZZ\n"
 
193
                              "@@ -1,1 +1,1 @@\n"
 
194
                              "-new content\n"
 
195
                              "+contents of branch1/file\n"
 
196
                              "\n", subst_dates(out))
149
197
 
150
198
    def example_branch2(self):
151
199
        self.build_tree(['branch1/', 'branch1/file1'], line_endings='binary')
161
209
        self.example_branch2()
162
210
        
163
211
        print >> open('branch1/file1', 'wb'), 'new line'
164
 
        output = self.run_bzr_captured(['diff', '-r', '1..', 'branch1'], retcode=1)
 
212
        output = self.run_bzr_captured(['diff', '-r', '1..', 'branch1'],
 
213
                                       retcode=1)
165
214
        self.assertTrue('\n-original line\n+new line\n' in output[0])
166
215
 
 
216
    def test_diff_across_rename(self):
 
217
        """The working tree path should always be considered for diffing"""
 
218
        self.make_example_branch()
 
219
        self.run_bzr('diff', '-r', '0..1', 'hello', retcode=1)
 
220
        wt = workingtree.WorkingTree.open_containing('.')[0]
 
221
        wt.rename_one('hello', 'hello1')
 
222
        self.run_bzr('diff', 'hello1', retcode=1)
 
223
        self.run_bzr('diff', '-r', '0..1', 'hello1', retcode=1)
 
224
 
167
225
 
168
226
class TestCheckoutDiff(TestDiff):
169
227
 
186
244
        os.chdir('checkouts')
187
245
 
188
246
 
189
 
class TestDiffLabels(TestDiff):
 
247
class TestDiffLabels(DiffBase):
190
248
 
191
249
    def test_diff_label_removed(self):
192
250
        super(TestDiffLabels, self).make_example_branch()
212
270
        self.runbzr('rename hello gruezi')
213
271
        diff = self.run_bzr_captured(['diff'], retcode=1)
214
272
        self.assertTrue("=== renamed file 'hello' => 'gruezi'" in diff[0])
 
273
 
 
274
 
 
275
class TestExternalDiff(DiffBase):
 
276
 
 
277
    def test_external_diff(self):
 
278
        """Test that we can spawn an external diff process"""
 
279
        # We have to use run_bzr_subprocess, because we need to
 
280
        # test writing directly to stdout, (there was a bug in
 
281
        # subprocess.py that we had to workaround).
 
282
        # However, if 'diff' may not be available
 
283
        self.make_example_branch()
 
284
        orig_progress = os.environ.get('BZR_PROGRESS_BAR')
 
285
        try:
 
286
            os.environ['BZR_PROGRESS_BAR'] = 'none'
 
287
            out, err = self.run_bzr_subprocess('diff', '-r', '1',
 
288
                                               '--diff-options', '-ub',
 
289
                                               universal_newlines=True,
 
290
                                               retcode=None)
 
291
        finally:
 
292
            if orig_progress is None:
 
293
                del os.environ['BZR_PROGRESS_BAR']
 
294
            else:
 
295
                os.environ['BZR_PROGRESS_BAR'] = orig_progress
 
296
            
 
297
        if 'Diff is not installed on this machine' in err:
 
298
            raise TestSkipped("No external 'diff' is available")
 
299
        self.assertEqual('', err)
 
300
        # We have to skip the stuff in the middle, because it depends
 
301
        # on time.time()
 
302
        self.assertStartsWith(out, "=== added file 'goodbye'\n"
 
303
                                   "--- goodbye\t1970-01-01 00:00:00 +0000\n"
 
304
                                   "+++ goodbye\t")
 
305
        self.assertEndsWith(out, "\n@@ -0,0 +1 @@\n"
 
306
                                 "+baz\n\n")
 
307
 
 
308
 
 
309
class TestDiffOutput(DiffBase):
 
310
 
 
311
    def test_diff_output(self):
 
312
        # check that output doesn't mangle line-endings
 
313
        self.make_example_branch()
 
314
        file('hello', 'wb').write('hello world!\n')
 
315
        output = self.run_bzr_subprocess('diff', retcode=1)[0]
 
316
        self.assert_('\n+hello world!\n' in output)