~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

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

  • Committer: Alexander Belchenko
  • Date: 2006-07-30 16:43:12 UTC
  • mto: (1711.2.111 jam-integration)
  • mto: This revision was merged to the branch mainline in revision 1906.
  • Revision ID: bialix@ukr.net-20060730164312-b025fd3ff0cee59e
rename  gpl.txt => COPYING.txt

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006 Canonical Ltd
 
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
22
22
import re
23
23
 
24
24
import bzrlib
 
25
from bzrlib.branch import Branch
25
26
from bzrlib import workingtree
26
 
from bzrlib.branch import Branch
27
 
from bzrlib.tests import TestSkipped
28
27
from bzrlib.tests.blackbox import ExternalBase
29
28
 
30
29
 
34
33
                  'YYYY-MM-DD HH:MM:SS +ZZZZ', string)
35
34
 
36
35
 
37
 
class DiffBase(ExternalBase):
38
 
    """Base class with common setup method"""
 
36
class TestDiff(ExternalBase):
39
37
 
40
 
    def make_example_branch(self):
 
38
    def make_example_branch(test):
41
39
        # 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):
 
40
        test.runbzr('init')
 
41
        file('hello', 'wb').write('foo\n')
 
42
        test.runbzr('add hello')
 
43
        test.runbzr('commit -m setup hello')
 
44
        file('goodbye', 'wb').write('baz\n')
 
45
        test.runbzr('add goodbye')
 
46
        test.runbzr('commit -m setup goodbye')
52
47
 
53
48
    def test_diff(self):
54
49
        self.make_example_branch()
55
50
        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]
 
51
        self.runbzr('commit -m fixing hello')
 
52
        output = self.runbzr('diff -r 2..3', backtick=1, retcode=1)
58
53
        self.assert_('\n+hello world!' in output)
59
 
        output = self.run_bzr('diff -r last:3..last:1',
60
 
                retcode=1)[0]
 
54
        output = self.runbzr('diff -r last:3..last:1', backtick=1, retcode=1)
61
55
        self.assert_('\n+baz' in output)
62
 
        self.build_tree(['moo'])
63
 
        self.run_bzr('add moo')
 
56
        file('moo', 'wb').write('moo')
 
57
        self.runbzr('add moo')
64
58
        os.unlink('moo')
65
 
        self.run_bzr('diff')
 
59
        self.runbzr('diff')
66
60
 
67
61
    def test_diff_prefix(self):
68
62
        """diff --prefix appends to filenames in output"""
69
63
        self.make_example_branch()
70
64
        file('hello', 'wb').write('hello world!\n')
71
 
        out, err = self.run_bzr('diff --prefix old/:new/', retcode=1)
 
65
        out, err = self.runbzr('diff --prefix old/:new/', retcode=1)
72
66
        self.assertEquals(err, '')
73
67
        self.assertEqualDiff(subst_dates(out), '''\
74
68
=== modified file 'hello'
80
74
 
81
75
''')
82
76
 
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
77
    def test_diff_p1(self):
90
78
        """diff -p1 produces lkml-style diffs"""
91
79
        self.make_example_branch()
92
80
        file('hello', 'wb').write('hello world!\n')
93
 
        out, err = self.run_bzr('diff -p1', retcode=1)
 
81
        out, err = self.runbzr('diff -p1', retcode=1)
94
82
        self.assertEquals(err, '')
95
83
        self.assertEqualDiff(subst_dates(out), '''\
96
84
=== modified file 'hello'
106
94
        """diff -p0 produces diffs with no prefix"""
107
95
        self.make_example_branch()
108
96
        file('hello', 'wb').write('hello world!\n')
109
 
        out, err = self.run_bzr('diff -p0', retcode=1)
 
97
        out, err = self.runbzr('diff -p0', retcode=1)
110
98
        self.assertEquals(err, '')
111
99
        self.assertEqualDiff(subst_dates(out), '''\
112
100
=== modified file 'hello'
122
110
        # Get an error from a file that does not exist at all
123
111
        # (Malone #3619)
124
112
        self.make_example_branch()
125
 
        out, err = self.run_bzr('diff does-not-exist', retcode=3)
 
113
        out, err = self.runbzr('diff does-not-exist', retcode=3)
126
114
        self.assertContainsRe(err, 'not versioned.*does-not-exist')
127
115
 
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
116
    def test_diff_unversioned(self):
133
117
        # Get an error when diffing a non-versioned file.
134
118
        # (Malone #3619)
135
119
        self.make_example_branch()
136
120
        self.build_tree(['unversioned-file'])
137
 
        out, err = self.run_bzr('diff unversioned-file', retcode=3)
 
121
        out, err = self.runbzr('diff unversioned-file', retcode=3)
138
122
        self.assertContainsRe(err, 'not versioned.*unversioned-file')
139
123
 
140
124
    # TODO: What should diff say for a file deleted in working tree?
141
125
 
142
126
    def example_branches(self):
143
127
        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'])
 
128
        self.capture('init branch1')
 
129
        self.capture('add branch1/file')
 
130
        self.run_bzr_captured(['commit', '-m', 'add file', 'branch1'])
 
131
        self.capture('branch branch1 branch2')
 
132
        print >> open('branch2/file', 'wb'), 'new content'
 
133
        self.run_bzr_captured(['commit', '-m', 'update file', 'branch2'])
150
134
 
151
135
    def test_diff_branches(self):
152
136
        self.example_branches()
153
137
        # should open branch1 and diff against branch2, 
154
 
        out, err = self.run_bzr('diff -r branch:branch2 branch1',
155
 
                                retcode=1)
 
138
        out, err = self.run_bzr_captured(['diff', '-r', 'branch:branch2', 
 
139
                                          'branch1'],
 
140
                                         retcode=1)
156
141
        self.assertEquals('', err)
157
142
        self.assertEquals("=== modified file 'file'\n"
158
143
                          "--- file\tYYYY-MM-DD HH:MM:SS +ZZZZ\n"
161
146
                          "-new content\n"
162
147
                          "+contents of branch1/file\n"
163
148
                          "\n", subst_dates(out))
164
 
        out, err = self.run_bzr('diff branch2 branch1',
 
149
        out, err = self.run_bzr_captured(['diff', 'branch2', 'branch1'],
165
150
                                         retcode=1)
166
151
        self.assertEquals('', err)
167
152
        self.assertEqualDiff("=== modified file 'file'\n"
175
160
    def test_diff_revno_branches(self):
176
161
        self.example_branches()
177
162
        print >> open('branch2/file', 'wb'), 'even newer content'
178
 
        self.run_bzr(['commit', '-m',
179
 
                      'update file once more', 'branch2'])
 
163
        self.run_bzr_captured(['commit', '-m', 
 
164
                               'update file once more', 'branch2'])
180
165
 
181
 
        out, err = self.run_bzr('diff -r revno:1:branch2..revno:1:branch1',
182
 
                                )
 
166
        out, err = self.run_bzr_captured(['diff', '-r',
 
167
                                          'revno:1:branch2..revno:1:branch1'],
 
168
                                         retcode=0)
183
169
        self.assertEquals('', err)
184
170
        self.assertEquals('', out)
185
 
        out, err = self.run_bzr('diff -r revno:2:branch2..revno:1:branch1',
186
 
                                retcode=1)
 
171
        out, err = self.run_bzr_captured(['diff', '-r', 
 
172
                                          'revno:2:branch2..revno:1:branch1'],
 
173
                                         retcode=1)
187
174
        self.assertEquals('', err)
188
175
        self.assertEqualDiff("=== modified file 'file'\n"
189
176
                              "--- file\tYYYY-MM-DD HH:MM:SS +ZZZZ\n"
195
182
 
196
183
    def example_branch2(self):
197
184
        self.build_tree(['branch1/', 'branch1/file1'], line_endings='binary')
198
 
        self.run_bzr('init branch1')[0]
199
 
        self.run_bzr('add branch1/file1')[0]
 
185
        self.capture('init branch1')
 
186
        self.capture('add branch1/file1')
200
187
        print >> open('branch1/file1', 'wb'), 'original line'
201
 
        self.run_bzr(['commit', '-m', 'first commit', 'branch1'])
 
188
        self.run_bzr_captured(['commit', '-m', 'first commit', 'branch1'])
202
189
        
203
190
        print >> open('branch1/file1', 'wb'), 'repo line'
204
 
        self.run_bzr(['commit', '-m', 'second commit', 'branch1'])
 
191
        self.run_bzr_captured(['commit', '-m', 'second commit', 'branch1'])
205
192
 
206
193
    def test_diff_to_working_tree(self):
207
194
        self.example_branch2()
208
195
        
209
196
        print >> open('branch1/file1', 'wb'), 'new line'
210
 
        output = self.run_bzr('diff -r 1.. branch1',
211
 
                              retcode=1)
 
197
        output = self.run_bzr_captured(['diff', '-r', '1..', 'branch1'],
 
198
                                       retcode=1)
212
199
        self.assertTrue('\n-original line\n+new line\n' in output[0])
213
200
 
214
201
    def test_diff_across_rename(self):
215
202
        """The working tree path should always be considered for diffing"""
216
203
        self.make_example_branch()
217
 
        self.run_bzr('diff -r 0..1 hello', retcode=1)
 
204
        self.run_bzr('diff', '-r', '0..1', 'hello', retcode=1)
218
205
        wt = workingtree.WorkingTree.open_containing('.')[0]
219
206
        wt.rename_one('hello', 'hello1')
220
 
        self.run_bzr('diff hello1', retcode=1)
221
 
        self.run_bzr('diff -r 0..1 hello1', retcode=1)
 
207
        self.run_bzr('diff', 'hello1', retcode=1)
 
208
        self.run_bzr('diff', '-r', '0..1', 'hello1', retcode=1)
222
209
 
223
210
 
224
211
class TestCheckoutDiff(TestDiff):
225
212
 
226
213
    def make_example_branch(self):
227
214
        super(TestCheckoutDiff, self).make_example_branch()
228
 
        self.run_bzr('checkout . checkout')
 
215
        self.runbzr('checkout . checkout')
229
216
        os.chdir('checkout')
230
217
 
231
218
    def example_branch2(self):
232
219
        super(TestCheckoutDiff, self).example_branch2()
233
220
        os.mkdir('checkouts')
234
 
        self.run_bzr('checkout branch1 checkouts/branch1')
 
221
        self.runbzr('checkout branch1 checkouts/branch1')
235
222
        os.chdir('checkouts')
236
223
 
237
224
    def example_branches(self):
238
225
        super(TestCheckoutDiff, self).example_branches()
239
226
        os.mkdir('checkouts')
240
 
        self.run_bzr('checkout branch1 checkouts/branch1')
241
 
        self.run_bzr('checkout branch2 checkouts/branch2')
 
227
        self.runbzr('checkout branch1 checkouts/branch1')
 
228
        self.runbzr('checkout branch2 checkouts/branch2')
242
229
        os.chdir('checkouts')
243
230
 
244
231
 
245
 
class TestDiffLabels(DiffBase):
 
232
class TestDiffLabels(TestDiff):
246
233
 
247
234
    def test_diff_label_removed(self):
248
235
        super(TestDiffLabels, self).make_example_branch()
249
 
        self.run_bzr('remove hello')
250
 
        diff = self.run_bzr('diff', retcode=1)
 
236
        self.runbzr('remove hello')
 
237
        diff = self.run_bzr_captured(['diff'], retcode=1)
251
238
        self.assertTrue("=== removed file 'hello'" in diff[0])
252
239
 
253
240
    def test_diff_label_added(self):
254
241
        super(TestDiffLabels, self).make_example_branch()
255
242
        file('barbar', 'wt').write('barbar')
256
 
        self.run_bzr('add barbar')
257
 
        diff = self.run_bzr('diff', retcode=1)
 
243
        self.runbzr('add barbar')
 
244
        diff = self.run_bzr_captured(['diff'], retcode=1)
258
245
        self.assertTrue("=== added file 'barbar'" in diff[0])
259
246
 
260
247
    def test_diff_label_modified(self):
261
248
        super(TestDiffLabels, self).make_example_branch()
262
249
        file('hello', 'wt').write('barbar')
263
 
        diff = self.run_bzr('diff', retcode=1)
 
250
        diff = self.run_bzr_captured(['diff'], retcode=1)
264
251
        self.assertTrue("=== modified file 'hello'" in diff[0])
265
252
 
266
253
    def test_diff_label_renamed(self):
267
254
        super(TestDiffLabels, self).make_example_branch()
268
 
        self.run_bzr('rename hello gruezi')
269
 
        diff = self.run_bzr('diff', retcode=1)
 
255
        self.runbzr('rename hello gruezi')
 
256
        diff = self.run_bzr_captured(['diff'], retcode=1)
270
257
        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)