~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

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

  • Committer: Andrew Bennetts
  • Date: 2007-08-30 08:11:54 UTC
  • mfrom: (2766 +trunk)
  • mto: (2535.3.55 repo-refactor)
  • mto: This revision was merged to the branch mainline in revision 2772.
  • Revision ID: andrew.bennetts@canonical.com-20070830081154-16hebp2xwr15x2hc
Merge from bzr.dev.

Show diffs side-by-side

added added

removed removed

Lines of Context:
38
38
    """Base class with common setup method"""
39
39
 
40
40
    def make_example_branch(self):
41
 
        # FIXME: copied from test_too_much -- share elsewhere?
42
41
        tree = self.make_branch_and_tree('.')
43
 
        open('hello', 'wb').write('foo\n')
 
42
        self.build_tree_contents([
 
43
            ('hello', 'foo\n'),
 
44
            ('goodbye', 'baz\n')])
44
45
        tree.add(['hello'])
45
46
        tree.commit('setup')
46
 
        open('goodbye', 'wb').write('baz\n')
47
47
        tree.add(['goodbye'])
48
48
        tree.commit('setup')
 
49
        return tree
49
50
 
50
51
 
51
52
class TestDiff(DiffBase):
52
53
 
53
54
    def test_diff(self):
54
 
        self.make_example_branch()
55
 
        file('hello', 'wt').write('hello world!')
56
 
        self.run_bzr('commit -m fixing hello')
 
55
        tree = self.make_example_branch()
 
56
        self.build_tree_contents([('hello', 'hello world!')])
 
57
        tree.commit(message='fixing hello')
57
58
        output = self.run_bzr('diff -r 2..3', retcode=1)[0]
58
59
        self.assert_('\n+hello world!' in output)
59
60
        output = self.run_bzr('diff -r last:3..last:1',
60
61
                retcode=1)[0]
61
62
        self.assert_('\n+baz' in output)
62
63
        self.build_tree(['moo'])
63
 
        self.run_bzr('add moo')
 
64
        tree.add('moo')
64
65
        os.unlink('moo')
65
66
        self.run_bzr('diff')
66
67
 
67
68
    def test_diff_prefix(self):
68
69
        """diff --prefix appends to filenames in output"""
69
70
        self.make_example_branch()
70
 
        file('hello', 'wb').write('hello world!\n')
 
71
        self.build_tree_contents([('hello', 'hello world!\n')])
71
72
        out, err = self.run_bzr('diff --prefix old/:new/', retcode=1)
72
73
        self.assertEquals(err, '')
73
74
        self.assertEqualDiff(subst_dates(out), '''\
89
90
    def test_diff_p1(self):
90
91
        """diff -p1 produces lkml-style diffs"""
91
92
        self.make_example_branch()
92
 
        file('hello', 'wb').write('hello world!\n')
 
93
        self.build_tree_contents([('hello', 'hello world!\n')])
93
94
        out, err = self.run_bzr('diff -p1', retcode=1)
94
95
        self.assertEquals(err, '')
95
96
        self.assertEqualDiff(subst_dates(out), '''\
105
106
    def test_diff_p0(self):
106
107
        """diff -p0 produces diffs with no prefix"""
107
108
        self.make_example_branch()
108
 
        file('hello', 'wb').write('hello world!\n')
 
109
        self.build_tree_contents([('hello', 'hello world!\n')])
109
110
        out, err = self.run_bzr('diff -p0', retcode=1)
110
111
        self.assertEquals(err, '')
111
112
        self.assertEqualDiff(subst_dates(out), '''\
140
141
    # TODO: What should diff say for a file deleted in working tree?
141
142
 
142
143
    def example_branches(self):
143
 
        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]
 
144
        branch1_tree = self.make_branch_and_tree('branch1')
 
145
        self.build_tree(['branch1/file'], line_endings='binary')
 
146
        branch1_tree.add('file')
 
147
        branch1_tree.commit(message='add file')
 
148
        branch2_tree = branch1_tree.bzrdir.sprout('branch2').open_workingtree()
148
149
        self.build_tree_contents([('branch2/file', 'new content\n')])
149
 
        self.run_bzr(['commit', '-m', 'update file', 'branch2'])
 
150
        branch2_tree.commit(message='update file')
 
151
        return branch1_tree, branch2_tree
150
152
 
151
153
    def test_diff_branches(self):
152
154
        self.example_branches()
174
176
 
175
177
    def test_diff_revno_branches(self):
176
178
        self.example_branches()
177
 
        print >> open('branch2/file', 'wb'), 'even newer content'
178
 
        self.run_bzr(['commit', '-m',
179
 
                      'update file once more', 'branch2'])
 
179
        branch2_tree = workingtree.WorkingTree.open_containing('branch2')[0]
 
180
        self.build_tree_contents([('branch2/file', 'even newer content')])
 
181
        branch2_tree.commit(message='update file once more')
180
182
 
181
183
        out, err = self.run_bzr('diff -r revno:1:branch2..revno:1:branch1',
182
184
                                )
194
196
                              "\n", subst_dates(out))
195
197
 
196
198
    def example_branch2(self):
197
 
        self.build_tree(['branch1/', 'branch1/file1'], line_endings='binary')
198
 
        self.run_bzr('init branch1')[0]
199
 
        self.run_bzr('add branch1/file1')[0]
200
 
        print >> open('branch1/file1', 'wb'), 'original line'
201
 
        self.run_bzr(['commit', '-m', 'first commit', 'branch1'])
202
 
        
203
 
        print >> open('branch1/file1', 'wb'), 'repo line'
204
 
        self.run_bzr(['commit', '-m', 'second commit', 'branch1'])
 
199
        branch1_tree = self.make_branch_and_tree('branch1')
 
200
        self.build_tree_contents([('branch1/file1', 'original line\n')])
 
201
        branch1_tree.add('file1')
 
202
        branch1_tree.commit(message='first commit')
 
203
        self.build_tree_contents([('branch1/file1', 'repo line\n')])
 
204
        branch1_tree.commit(message='second commit')
 
205
        return branch1_tree
205
206
 
206
207
    def test_diff_to_working_tree(self):
207
208
        self.example_branch2()
208
 
        
209
 
        print >> open('branch1/file1', 'wb'), 'new line'
210
 
        output = self.run_bzr('diff -r 1.. branch1',
211
 
                              retcode=1)
212
 
        self.assertTrue('\n-original line\n+new line\n' in output[0])
 
209
        self.build_tree_contents([('branch1/file1', 'new line')])
 
210
        output = self.run_bzr('diff -r 1.. branch1', retcode=1)
 
211
        self.assertContainsRe(output[0], '\n\\-original line\n\\+new line\n')
213
212
 
214
213
    def test_diff_across_rename(self):
215
214
        """The working tree path should always be considered for diffing"""
216
 
        self.make_example_branch()
 
215
        tree = self.make_example_branch()
217
216
        self.run_bzr('diff -r 0..1 hello', retcode=1)
218
 
        wt = workingtree.WorkingTree.open_containing('.')[0]
219
 
        wt.rename_one('hello', 'hello1')
 
217
        tree.rename_one('hello', 'hello1')
220
218
        self.run_bzr('diff hello1', retcode=1)
221
219
        self.run_bzr('diff -r 0..1 hello1', retcode=1)
222
220
 
224
222
class TestCheckoutDiff(TestDiff):
225
223
 
226
224
    def make_example_branch(self):
227
 
        super(TestCheckoutDiff, self).make_example_branch()
228
 
        self.run_bzr('checkout . checkout')
 
225
        tree = super(TestCheckoutDiff, self).make_example_branch()
 
226
        tree = tree.branch.create_checkout('checkout')
229
227
        os.chdir('checkout')
 
228
        return tree
230
229
 
231
230
    def example_branch2(self):
232
 
        super(TestCheckoutDiff, self).example_branch2()
 
231
        tree = super(TestCheckoutDiff, self).example_branch2()
233
232
        os.mkdir('checkouts')
234
 
        self.run_bzr('checkout branch1 checkouts/branch1')
 
233
        tree = tree.branch.create_checkout('checkouts/branch1')
235
234
        os.chdir('checkouts')
 
235
        return tree
236
236
 
237
237
    def example_branches(self):
238
 
        super(TestCheckoutDiff, self).example_branches()
 
238
        branch1_tree, branch2_tree = super(TestCheckoutDiff, self).example_branches()
239
239
        os.mkdir('checkouts')
240
 
        self.run_bzr('checkout branch1 checkouts/branch1')
241
 
        self.run_bzr('checkout branch2 checkouts/branch2')
 
240
        branch1_tree = branch1_tree.branch.create_checkout('checkouts/branch1')
 
241
        branch2_tree = branch2_tree.branch.create_checkout('checkouts/branch2')
242
242
        os.chdir('checkouts')
 
243
        return branch1_tree, branch2_tree
243
244
 
244
245
 
245
246
class TestDiffLabels(DiffBase):
246
247
 
247
248
    def test_diff_label_removed(self):
248
 
        super(TestDiffLabels, self).make_example_branch()
249
 
        self.run_bzr('remove hello')
 
249
        tree = super(TestDiffLabels, self).make_example_branch()
 
250
        tree.remove('hello', keep_files=False)
250
251
        diff = self.run_bzr('diff', retcode=1)
251
252
        self.assertTrue("=== removed file 'hello'" in diff[0])
252
253
 
253
254
    def test_diff_label_added(self):
254
 
        super(TestDiffLabels, self).make_example_branch()
255
 
        file('barbar', 'wt').write('barbar')
256
 
        self.run_bzr('add barbar')
 
255
        tree = super(TestDiffLabels, self).make_example_branch()
 
256
        self.build_tree_contents([('barbar', 'barbar')])
 
257
        tree.add('barbar')
257
258
        diff = self.run_bzr('diff', retcode=1)
258
259
        self.assertTrue("=== added file 'barbar'" in diff[0])
259
260
 
260
261
    def test_diff_label_modified(self):
261
262
        super(TestDiffLabels, self).make_example_branch()
262
 
        file('hello', 'wt').write('barbar')
 
263
        self.build_tree_contents([('hello', 'barbar')])
263
264
        diff = self.run_bzr('diff', retcode=1)
264
265
        self.assertTrue("=== modified file 'hello'" in diff[0])
265
266
 
266
267
    def test_diff_label_renamed(self):
267
 
        super(TestDiffLabels, self).make_example_branch()
268
 
        self.run_bzr('rename hello gruezi')
 
268
        tree = super(TestDiffLabels, self).make_example_branch()
 
269
        tree.rename_one('hello', 'gruezi')
269
270
        diff = self.run_bzr('diff', retcode=1)
270
271
        self.assertTrue("=== renamed file 'hello' => 'gruezi'" in diff[0])
271
272
 
308
309
    def test_diff_output(self):
309
310
        # check that output doesn't mangle line-endings
310
311
        self.make_example_branch()
311
 
        file('hello', 'wb').write('hello world!\n')
 
312
        self.build_tree_contents([('hello', 'hello world!\n')])
312
313
        output = self.run_bzr_subprocess('diff', retcode=1)[0]
313
314
        self.assert_('\n+hello world!\n' in output)