~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: 2007-10-24 12:49:17 UTC
  • mfrom: (2935.1.1 ianc-integration)
  • Revision ID: pqm@pqm.ubuntu.com-20071024124917-xb75eckyxx6vkrlg
Makefile fixes - hooks.html generation & allow python to be overridden (Ian Clatworthy)

Show diffs side-by-side

added added

removed removed

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