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.
25
from bzrlib.branch import Branch
26
from bzrlib import workingtree
27
from bzrlib.tests.blackbox import ExternalBase
30
def subst_dates(string):
31
"""Replace date strings with constant values."""
32
return re.sub(r'\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2} [-\+]\d{4}',
33
'YYYY-MM-DD HH:MM:SS +ZZZZ', string)
36
class TestDiff(ExternalBase):
38
def make_example_branch(test):
39
# FIXME: copied from test_too_much -- share elsewhere?
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')
49
self.make_example_branch()
50
file('hello', 'wt').write('hello world!')
51
self.runbzr('commit -m fixing hello')
52
output = self.runbzr('diff -r 2..3', backtick=1, retcode=1)
53
self.assert_('\n+hello world!' in output)
54
output = self.runbzr('diff -r last:3..last:1', backtick=1, retcode=1)
55
self.assert_('\n+baz' in output)
56
file('moo', 'wb').write('moo')
57
self.runbzr('add moo')
61
def test_diff_prefix(self):
62
"""diff --prefix appends to filenames in output"""
63
self.make_example_branch()
64
file('hello', 'wb').write('hello world!\n')
65
out, err = self.runbzr('diff --prefix old/:new/', retcode=1)
66
self.assertEquals(err, '')
67
self.assertEqualDiff(subst_dates(out), '''\
68
=== modified file 'hello'
69
--- old/hello\tYYYY-MM-DD HH:MM:SS +ZZZZ
70
+++ new/hello\tYYYY-MM-DD HH:MM:SS +ZZZZ
77
def test_diff_p1(self):
78
"""diff -p1 produces lkml-style diffs"""
79
self.make_example_branch()
80
file('hello', 'wb').write('hello world!\n')
81
out, err = self.runbzr('diff -p1', retcode=1)
82
self.assertEquals(err, '')
83
self.assertEqualDiff(subst_dates(out), '''\
84
=== modified file 'hello'
85
--- old/hello\tYYYY-MM-DD HH:MM:SS +ZZZZ
86
+++ new/hello\tYYYY-MM-DD HH:MM:SS +ZZZZ
93
def test_diff_p0(self):
94
"""diff -p0 produces diffs with no prefix"""
95
self.make_example_branch()
96
file('hello', 'wb').write('hello world!\n')
97
out, err = self.runbzr('diff -p0', retcode=1)
98
self.assertEquals(err, '')
99
self.assertEqualDiff(subst_dates(out), '''\
100
=== modified file 'hello'
101
--- hello\tYYYY-MM-DD HH:MM:SS +ZZZZ
102
+++ hello\tYYYY-MM-DD HH:MM:SS +ZZZZ
109
def test_diff_nonexistent(self):
110
# Get an error from a file that does not exist at all
112
self.make_example_branch()
113
out, err = self.runbzr('diff does-not-exist', retcode=3)
114
self.assertContainsRe(err, 'not versioned.*does-not-exist')
116
def test_diff_unversioned(self):
117
# Get an error when diffing a non-versioned file.
119
self.make_example_branch()
120
self.build_tree(['unversioned-file'])
121
out, err = self.runbzr('diff unversioned-file', retcode=3)
122
self.assertContainsRe(err, 'not versioned.*unversioned-file')
124
# TODO: What should diff say for a file deleted in working tree?
126
def example_branches(self):
127
self.build_tree(['branch1/', 'branch1/file'], line_endings='binary')
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'])
135
def test_diff_branches(self):
136
self.example_branches()
137
# should open branch1 and diff against branch2,
138
out, err = self.run_bzr_captured(['diff', '-r', 'branch:branch2',
141
self.assertEquals('', err)
142
self.assertEquals("=== modified file 'file'\n"
143
"--- file\tYYYY-MM-DD HH:MM:SS +ZZZZ\n"
144
"+++ file\tYYYY-MM-DD HH:MM:SS +ZZZZ\n"
147
"+contents of branch1/file\n"
148
"\n", subst_dates(out))
149
out, ett = self.run_bzr_captured(['diff', 'branch2', 'branch1'],
151
self.assertEquals('', err)
152
self.assertEqualDiff("=== modified file 'file'\n"
153
"--- file\tYYYY-MM-DD HH:MM:SS +ZZZZ\n"
154
"+++ file\tYYYY-MM-DD HH:MM:SS +ZZZZ\n"
157
"+contents of branch1/file\n"
158
"\n", subst_dates(out))
160
def example_branch2(self):
161
self.build_tree(['branch1/', 'branch1/file1'], line_endings='binary')
162
self.capture('init branch1')
163
self.capture('add branch1/file1')
164
print >> open('branch1/file1', 'wb'), 'original line'
165
self.run_bzr_captured(['commit', '-m', 'first commit', 'branch1'])
167
print >> open('branch1/file1', 'wb'), 'repo line'
168
self.run_bzr_captured(['commit', '-m', 'second commit', 'branch1'])
170
def test_diff_to_working_tree(self):
171
self.example_branch2()
173
print >> open('branch1/file1', 'wb'), 'new line'
174
output = self.run_bzr_captured(['diff', '-r', '1..', 'branch1'], retcode=1)
175
self.assertTrue('\n-original line\n+new line\n' in output[0])
177
def test_diff_across_rename(self):
178
"""The working tree path should always be considered for diffing"""
179
self.make_example_branch()
180
self.run_bzr('diff', '-r', '0..1', 'hello', retcode=1)
181
wt = workingtree.WorkingTree.open_containing('.')[0]
182
wt.rename_one('hello', 'hello1')
183
self.run_bzr('diff', 'hello1', retcode=1)
184
self.run_bzr('diff', '-r', '0..1', 'hello1', retcode=1)
187
class TestCheckoutDiff(TestDiff):
189
def make_example_branch(self):
190
super(TestCheckoutDiff, self).make_example_branch()
191
self.runbzr('checkout . checkout')
194
def example_branch2(self):
195
super(TestCheckoutDiff, self).example_branch2()
196
os.mkdir('checkouts')
197
self.runbzr('checkout branch1 checkouts/branch1')
198
os.chdir('checkouts')
200
def example_branches(self):
201
super(TestCheckoutDiff, self).example_branches()
202
os.mkdir('checkouts')
203
self.runbzr('checkout branch1 checkouts/branch1')
204
self.runbzr('checkout branch2 checkouts/branch2')
205
os.chdir('checkouts')
208
class TestDiffLabels(TestDiff):
210
def test_diff_label_removed(self):
211
super(TestDiffLabels, self).make_example_branch()
212
self.runbzr('remove hello')
213
diff = self.run_bzr_captured(['diff'], retcode=1)
214
self.assertTrue("=== removed file 'hello'" in diff[0])
216
def test_diff_label_added(self):
217
super(TestDiffLabels, self).make_example_branch()
218
file('barbar', 'wt').write('barbar')
219
self.runbzr('add barbar')
220
diff = self.run_bzr_captured(['diff'], retcode=1)
221
self.assertTrue("=== added file 'barbar'" in diff[0])
223
def test_diff_label_modified(self):
224
super(TestDiffLabels, self).make_example_branch()
225
file('hello', 'wt').write('barbar')
226
diff = self.run_bzr_captured(['diff'], retcode=1)
227
self.assertTrue("=== modified file 'hello'" in diff[0])
229
def test_diff_label_renamed(self):
230
super(TestDiffLabels, self).make_example_branch()
231
self.runbzr('rename hello gruezi')
232
diff = self.run_bzr_captured(['diff'], retcode=1)
233
self.assertTrue("=== renamed file 'hello' => 'gruezi'" in diff[0])