~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-08-17 07:52:09 UTC
  • mfrom: (1910.3.4 trivial)
  • Revision ID: pqm@pqm.ubuntu.com-20060817075209-e85a1f9e05ff8b87
(andrew) Trivial fixes to NotImplemented errors.

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