1
# Copyright (C) 2005, 2006 by Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
"""Black-box tests for bzr diff.
24
from bzrlib.branch import Branch
25
from bzrlib.tests.blackbox import ExternalBase
28
class TestDiff(ExternalBase):
30
def make_example_branch(test):
31
# FIXME: copied from test_too_much -- share elsewhere?
33
file('hello', 'wt').write('foo\n')
34
test.runbzr('add hello')
35
test.runbzr('commit -m setup hello')
36
file('goodbye', 'wt').write('baz\n')
37
test.runbzr('add goodbye')
38
test.runbzr('commit -m setup goodbye')
41
self.make_example_branch()
42
file('hello', 'wt').write('hello world!')
43
self.runbzr('commit -m fixing hello')
44
output = self.runbzr('diff -r 2..3', backtick=1, retcode=1)
45
self.assert_('\n+hello world!' in output)
46
output = self.runbzr('diff -r last:3..last:1', backtick=1, retcode=1)
47
self.assert_('\n+baz' in output)
48
file('moo', 'wb').write('moo')
49
self.runbzr('add moo')
53
def test_diff_prefix(self):
54
"""diff --prefix appends to filenames in output"""
55
self.make_example_branch()
56
file('hello', 'wt').write('hello world!\n')
57
out, err = self.runbzr('diff --prefix old/:new/', retcode=1)
58
self.assertEquals(err, '')
59
self.assertEqualDiff(out, '''\
60
=== modified file 'hello'
69
def test_diff_p1(self):
70
"""diff -p1 produces lkml-style diffs"""
71
self.make_example_branch()
72
file('hello', 'wt').write('hello world!\n')
73
out, err = self.runbzr('diff -p1', retcode=1)
74
self.assertEquals(err, '')
75
self.assertEqualDiff(out, '''\
76
=== modified file 'hello'
85
def test_diff_p0(self):
86
"""diff -p0 produces diffs with no prefix"""
87
self.make_example_branch()
88
file('hello', 'wt').write('hello world!\n')
89
out, err = self.runbzr('diff -p0', retcode=1)
90
self.assertEquals(err, '')
91
self.assertEqualDiff(out, '''\
92
=== modified file 'hello'
101
def test_diff_nonexistent(self):
102
# Get an error from a file that does not exist at all
104
self.make_example_branch()
105
out, err = self.runbzr('diff does-not-exist', retcode=3)
106
self.assertContainsRe(err, 'not versioned.*does-not-exist')
108
def test_diff_unversioned(self):
109
# Get an error when diffing a non-versioned file.
111
self.make_example_branch()
112
self.build_tree(['unversioned-file'])
113
out, err = self.runbzr('diff unversioned-file', retcode=3)
114
self.assertContainsRe(err, 'not versioned.*unversioned-file')
116
# TODO: What should diff say for a file deleted in working tree?
118
def example_branches(self):
119
self.build_tree(['branch1/', 'branch1/file'], line_endings='binary')
120
self.capture('init branch1')
121
self.capture('add branch1/file')
122
self.run_bzr_captured(['commit', '-m', 'add file', 'branch1'])
123
self.capture('branch branch1 branch2')
124
print >> open('branch2/file', 'wb'), 'new content'
125
self.run_bzr_captured(['commit', '-m', 'update file', 'branch2'])
127
def test_diff_branches(self):
128
self.example_branches()
129
# should open branch1 and diff against branch2,
130
output = self.run_bzr_captured(['diff', '-r', 'branch:branch2',
133
self.assertEquals(("=== modified file 'file'\n"
138
"+contents of branch1/file\n"
140
output = self.run_bzr_captured(['diff', 'branch2', 'branch1'],
142
self.assertEqualDiff(("=== modified file 'file'\n"
147
"+contents of branch1/file\n"
150
def example_branch2(self):
151
self.build_tree(['branch1/', 'branch1/file1'], line_endings='binary')
152
self.capture('init branch1')
153
self.capture('add branch1/file1')
154
print >> open('branch1/file1', 'wb'), 'original line'
155
self.run_bzr_captured(['commit', '-m', 'first commit', 'branch1'])
157
print >> open('branch1/file1', 'wb'), 'repo line'
158
self.run_bzr_captured(['commit', '-m', 'second commit', 'branch1'])
160
def test_diff_to_working_tree(self):
161
self.example_branch2()
163
print >> open('branch1/file1', 'wb'), 'new line'
164
output = self.run_bzr_captured(['diff', '-r', '1..', 'branch1'], retcode=1)
165
self.assertTrue('\n-original line\n+new line\n' in output[0])
168
class TestCheckoutDiff(TestDiff):
170
def make_example_branch(self):
171
super(TestCheckoutDiff, self).make_example_branch()
172
self.runbzr('checkout . checkout')
175
def example_branch2(self):
176
super(TestCheckoutDiff, self).example_branch2()
177
os.mkdir('checkouts')
178
self.runbzr('checkout branch1 checkouts/branch1')
179
os.chdir('checkouts')
181
def example_branches(self):
182
super(TestCheckoutDiff, self).example_branches()
183
os.mkdir('checkouts')
184
self.runbzr('checkout branch1 checkouts/branch1')
185
self.runbzr('checkout branch2 checkouts/branch2')
186
os.chdir('checkouts')
189
class TestDiffLabels(TestDiff):
191
def test_diff_label_removed(self):
192
super(TestDiffLabels, self).make_example_branch()
193
self.runbzr('remove hello')
194
diff = self.run_bzr_captured(['diff'], retcode=1)
195
self.assertTrue("=== removed file 'hello'" in diff[0])
197
def test_diff_label_added(self):
198
super(TestDiffLabels, self).make_example_branch()
199
file('barbar', 'wt').write('barbar')
200
self.runbzr('add barbar')
201
diff = self.run_bzr_captured(['diff'], retcode=1)
202
self.assertTrue("=== added file 'barbar'" in diff[0])
204
def test_diff_label_modified(self):
205
super(TestDiffLabels, self).make_example_branch()
206
file('hello', 'wt').write('barbar')
207
diff = self.run_bzr_captured(['diff'], retcode=1)
208
self.assertTrue("=== modified file 'hello'" in diff[0])
210
def test_diff_label_renamed(self):
211
super(TestDiffLabels, self).make_example_branch()
212
self.runbzr('rename hello gruezi')
213
diff = self.run_bzr_captured(['diff'], retcode=1)
214
self.assertTrue("=== renamed file 'hello' => 'gruezi'" in diff[0])