25
from bzrlib import workingtree
26
24
from bzrlib.branch import Branch
27
from bzrlib.tests import TestSkipped
28
25
from bzrlib.tests.blackbox import ExternalBase
31
def subst_dates(string):
32
"""Replace date strings with constant values."""
33
return re.sub(r'\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2} [-\+]\d{4}',
34
'YYYY-MM-DD HH:MM:SS +ZZZZ', string)
37
class DiffBase(ExternalBase):
38
"""Base class with common setup method"""
40
def make_example_branch(self):
28
class TestDiff(ExternalBase):
30
def make_example_branch(test):
41
31
# FIXME: copied from test_too_much -- share elsewhere?
42
tree = self.make_branch_and_tree('.')
43
open('hello', 'wb').write('foo\n')
46
open('goodbye', 'wb').write('baz\n')
51
class TestDiff(DiffBase):
33
file('hello', 'wt').write('foo')
34
test.runbzr('add hello')
35
test.runbzr('commit -m setup hello')
36
file('goodbye', 'wt').write('baz')
37
test.runbzr('add goodbye')
38
test.runbzr('commit -m setup goodbye')
53
40
def test_diff(self):
54
41
self.make_example_branch()
55
42
file('hello', 'wt').write('hello world!')
56
self.run_bzr('commit -m fixing hello')
57
output = self.run_bzr('diff -r 2..3', retcode=1)[0]
43
self.runbzr('commit -m fixing hello')
44
output = self.runbzr('diff -r 2..3', backtick=1, retcode=1)
58
45
self.assert_('\n+hello world!' in output)
59
output = self.run_bzr('diff -r last:3..last:1',
46
output = self.runbzr('diff -r last:3..last:1', backtick=1, retcode=1)
61
47
self.assert_('\n+baz' in output)
62
self.build_tree(['moo'])
63
self.run_bzr('add moo')
48
file('moo', 'wb').write('moo')
49
self.runbzr('add moo')
67
def test_diff_prefix(self):
68
"""diff --prefix appends to filenames in output"""
69
self.make_example_branch()
70
file('hello', 'wb').write('hello world!\n')
71
out, err = self.run_bzr('diff --prefix old/:new/', retcode=1)
72
self.assertEquals(err, '')
73
self.assertEqualDiff(subst_dates(out), '''\
74
=== modified file 'hello'
75
--- old/hello\tYYYY-MM-DD HH:MM:SS +ZZZZ
76
+++ new/hello\tYYYY-MM-DD HH:MM:SS +ZZZZ
83
def test_diff_illegal_prefix_value(self):
84
# There was an error in error reporting for this option
85
out, err = self.run_bzr('diff --prefix old/', retcode=3)
86
self.assertContainsRe(err,
87
'--prefix expects two values separated by a colon')
89
def test_diff_p1(self):
90
"""diff -p1 produces lkml-style diffs"""
91
self.make_example_branch()
92
file('hello', 'wb').write('hello world!\n')
93
out, err = self.run_bzr('diff -p1', retcode=1)
94
self.assertEquals(err, '')
95
self.assertEqualDiff(subst_dates(out), '''\
96
=== modified file 'hello'
97
--- old/hello\tYYYY-MM-DD HH:MM:SS +ZZZZ
98
+++ new/hello\tYYYY-MM-DD HH:MM:SS +ZZZZ
105
def test_diff_p0(self):
106
"""diff -p0 produces diffs with no prefix"""
107
self.make_example_branch()
108
file('hello', 'wb').write('hello world!\n')
109
out, err = self.run_bzr('diff -p0', retcode=1)
110
self.assertEquals(err, '')
111
self.assertEqualDiff(subst_dates(out), '''\
112
=== modified file 'hello'
113
--- hello\tYYYY-MM-DD HH:MM:SS +ZZZZ
114
+++ hello\tYYYY-MM-DD HH:MM:SS +ZZZZ
121
53
def test_diff_nonexistent(self):
122
54
# Get an error from a file that does not exist at all
124
56
self.make_example_branch()
125
out, err = self.run_bzr('diff does-not-exist', retcode=3)
57
out, err = self.runbzr('diff does-not-exist', retcode=3)
126
58
self.assertContainsRe(err, 'not versioned.*does-not-exist')
128
def test_diff_illegal_revision_specifiers(self):
129
out, err = self.run_bzr('diff -r 1..23..123', retcode=3)
130
self.assertContainsRe(err, 'one or two revision specifiers')
132
60
def test_diff_unversioned(self):
133
61
# Get an error when diffing a non-versioned file.
135
63
self.make_example_branch()
136
64
self.build_tree(['unversioned-file'])
137
out, err = self.run_bzr('diff unversioned-file', retcode=3)
65
out, err = self.runbzr('diff unversioned-file', retcode=3)
138
66
self.assertContainsRe(err, 'not versioned.*unversioned-file')
140
68
# TODO: What should diff say for a file deleted in working tree?
142
70
def example_branches(self):
143
71
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]
148
self.build_tree_contents([('branch2/file', 'new content\n')])
149
self.run_bzr(['commit', '-m', 'update file', 'branch2'])
72
self.capture('init branch1')
73
self.capture('add branch1/file')
74
self.run_bzr_captured(['commit', '-m', 'add file', 'branch1'])
75
self.capture('branch branch1 branch2')
76
print >> open('branch2/file', 'wb'), 'new content'
77
self.run_bzr_captured(['commit', '-m', 'update file', 'branch2'])
151
79
def test_diff_branches(self):
152
80
self.example_branches()
153
81
# should open branch1 and diff against branch2,
154
out, err = self.run_bzr(['diff', '-r', 'branch:branch2',
157
self.assertEquals('', err)
158
self.assertEquals("=== modified file 'file'\n"
159
"--- file\tYYYY-MM-DD HH:MM:SS +ZZZZ\n"
160
"+++ file\tYYYY-MM-DD HH:MM:SS +ZZZZ\n"
163
"+contents of branch1/file\n"
164
"\n", subst_dates(out))
165
out, err = self.run_bzr(['diff', 'branch2', 'branch1'],
167
self.assertEquals('', err)
168
self.assertEqualDiff("=== modified file 'file'\n"
169
"--- file\tYYYY-MM-DD HH:MM:SS +ZZZZ\n"
170
"+++ file\tYYYY-MM-DD HH:MM:SS +ZZZZ\n"
173
"+contents of branch1/file\n"
174
"\n", subst_dates(out))
176
def test_diff_revno_branches(self):
177
self.example_branches()
178
print >> open('branch2/file', 'wb'), 'even newer content'
179
self.run_bzr(['commit', '-m',
180
'update file once more', 'branch2'])
182
out, err = self.run_bzr(['diff', '-r',
183
'revno:1:branch2..revno:1:branch1'],
185
self.assertEquals('', err)
186
self.assertEquals('', out)
187
out, err = self.run_bzr(['diff', '-r',
188
'revno:2:branch2..revno:1:branch1'],
190
self.assertEquals('', err)
191
self.assertEqualDiff("=== modified file 'file'\n"
192
"--- file\tYYYY-MM-DD HH:MM:SS +ZZZZ\n"
193
"+++ file\tYYYY-MM-DD HH:MM:SS +ZZZZ\n"
196
"+contents of branch1/file\n"
197
"\n", subst_dates(out))
82
output = self.run_bzr_captured(['diff', '-r', 'branch:branch2',
85
self.assertEquals(("=== modified file 'a/file'\n"
90
"+contents of branch1/file\n"
92
output = self.run_bzr_captured(['diff', 'branch2', 'branch1'],
94
self.assertEqualDiff(("=== modified file 'a/file'\n"
99
"+contents of branch1/file\n"
199
102
def example_branch2(self):
200
103
self.build_tree(['branch1/', 'branch1/file1'], line_endings='binary')
201
self.run_bzr(['init', 'branch1'])[0]
202
self.run_bzr(['add', 'branch1/file1'])[0]
104
self.capture('init branch1')
105
self.capture('add branch1/file1')
203
106
print >> open('branch1/file1', 'wb'), 'original line'
204
self.run_bzr(['commit', '-m', 'first commit', 'branch1'])
107
self.run_bzr_captured(['commit', '-m', 'first commit', 'branch1'])
206
109
print >> open('branch1/file1', 'wb'), 'repo line'
207
self.run_bzr(['commit', '-m', 'second commit', 'branch1'])
110
self.run_bzr_captured(['commit', '-m', 'second commit', 'branch1'])
209
112
def test_diff_to_working_tree(self):
210
113
self.example_branch2()
212
115
print >> open('branch1/file1', 'wb'), 'new line'
213
output = self.run_bzr(['diff', '-r', '1..', 'branch1'],
116
output = self.run_bzr_captured(['diff', '-r', '1..', 'branch1'], retcode=1)
215
117
self.assertTrue('\n-original line\n+new line\n' in output[0])
217
def test_diff_across_rename(self):
218
"""The working tree path should always be considered for diffing"""
219
self.make_example_branch()
220
self.run_bzr('diff', '-r', '0..1', 'hello', retcode=1)
221
wt = workingtree.WorkingTree.open_containing('.')[0]
222
wt.rename_one('hello', 'hello1')
223
self.run_bzr('diff', 'hello1', retcode=1)
224
self.run_bzr('diff', '-r', '0..1', 'hello1', retcode=1)
227
120
class TestCheckoutDiff(TestDiff):
229
122
def make_example_branch(self):
230
123
super(TestCheckoutDiff, self).make_example_branch()
231
self.run_bzr('checkout . checkout')
124
self.runbzr('checkout . checkout')
232
125
os.chdir('checkout')
234
127
def example_branch2(self):
235
128
super(TestCheckoutDiff, self).example_branch2()
236
129
os.mkdir('checkouts')
237
self.run_bzr('checkout branch1 checkouts/branch1')
130
self.runbzr('checkout branch1 checkouts/branch1')
238
131
os.chdir('checkouts')
240
133
def example_branches(self):
241
134
super(TestCheckoutDiff, self).example_branches()
242
135
os.mkdir('checkouts')
243
self.run_bzr('checkout branch1 checkouts/branch1')
244
self.run_bzr('checkout branch2 checkouts/branch2')
136
self.runbzr('checkout branch1 checkouts/branch1')
137
self.runbzr('checkout branch2 checkouts/branch2')
245
138
os.chdir('checkouts')
248
class TestDiffLabels(DiffBase):
141
class TestDiffLabels(TestDiff):
250
143
def test_diff_label_removed(self):
251
144
super(TestDiffLabels, self).make_example_branch()
252
self.run_bzr('remove hello')
253
diff = self.run_bzr(['diff'], retcode=1)
254
self.assertTrue("=== removed file 'hello'" in diff[0])
145
self.runbzr('remove hello')
146
diff = self.run_bzr_captured(['diff'], retcode=1)
147
self.assertTrue("=== removed file 'a/hello'" in diff[0])
256
149
def test_diff_label_added(self):
257
150
super(TestDiffLabels, self).make_example_branch()
258
151
file('barbar', 'wt').write('barbar')
259
self.run_bzr('add barbar')
260
diff = self.run_bzr(['diff'], retcode=1)
261
self.assertTrue("=== added file 'barbar'" in diff[0])
152
self.runbzr('add barbar')
153
diff = self.run_bzr_captured(['diff'], retcode=1)
154
self.assertTrue("=== added file 'b/barbar'" in diff[0])
263
156
def test_diff_label_modified(self):
264
157
super(TestDiffLabels, self).make_example_branch()
265
158
file('hello', 'wt').write('barbar')
266
diff = self.run_bzr(['diff'], retcode=1)
267
self.assertTrue("=== modified file 'hello'" in diff[0])
159
diff = self.run_bzr_captured(['diff'], retcode=1)
160
self.assertTrue("=== modified file 'a/hello'" in diff[0])
269
162
def test_diff_label_renamed(self):
270
163
super(TestDiffLabels, self).make_example_branch()
271
self.run_bzr('rename hello gruezi')
272
diff = self.run_bzr(['diff'], retcode=1)
273
self.assertTrue("=== renamed file 'hello' => 'gruezi'" in diff[0])
276
class TestExternalDiff(DiffBase):
278
def test_external_diff(self):
279
"""Test that we can spawn an external diff process"""
280
# We have to use run_bzr_subprocess, because we need to
281
# test writing directly to stdout, (there was a bug in
282
# subprocess.py that we had to workaround).
283
# However, if 'diff' may not be available
284
self.make_example_branch()
285
orig_progress = os.environ.get('BZR_PROGRESS_BAR')
287
os.environ['BZR_PROGRESS_BAR'] = 'none'
288
out, err = self.run_bzr_subprocess('diff', '-r', '1',
289
'--diff-options', '-ub',
290
universal_newlines=True,
293
if orig_progress is None:
294
del os.environ['BZR_PROGRESS_BAR']
296
os.environ['BZR_PROGRESS_BAR'] = orig_progress
298
if 'Diff is not installed on this machine' in err:
299
raise TestSkipped("No external 'diff' is available")
300
self.assertEqual('', err)
301
# We have to skip the stuff in the middle, because it depends
303
self.assertStartsWith(out, "=== added file 'goodbye'\n"
304
"--- goodbye\t1970-01-01 00:00:00 +0000\n"
306
self.assertEndsWith(out, "\n@@ -0,0 +1 @@\n"
310
class TestDiffOutput(DiffBase):
312
def test_diff_output(self):
313
# check that output doesn't mangle line-endings
314
self.make_example_branch()
315
file('hello', 'wb').write('hello world!\n')
316
output = self.run_bzr_subprocess('diff', retcode=1)[0]
317
self.assert_('\n+hello world!\n' in output)
164
self.runbzr('rename hello gruezi')
165
diff = self.run_bzr_captured(['diff'], retcode=1)
166
self.assertTrue("=== renamed file 'a/hello' => 'b/gruezi'" in diff[0])