~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

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

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2006-07-06 03:15:29 UTC
  • mfrom: (1711.2.78 jam-integration)
  • Revision ID: pqm@pqm.ubuntu.com-20060706031529-e189d8c3f42076be
(jam) allow plugins to include benchmarks

Show diffs side-by-side

added added

removed removed

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