24
25
from bzrlib.branch import Branch
26
from bzrlib import workingtree
25
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)
28
36
class TestDiff(ExternalBase):
30
38
def make_example_branch(test):
31
39
# FIXME: copied from test_too_much -- share elsewhere?
32
40
test.runbzr('init')
33
file('hello', 'wt').write('foo\n')
41
file('hello', 'wb').write('foo\n')
34
42
test.runbzr('add hello')
35
43
test.runbzr('commit -m setup hello')
36
file('goodbye', 'wt').write('baz\n')
44
file('goodbye', 'wb').write('baz\n')
37
45
test.runbzr('add goodbye')
38
46
test.runbzr('commit -m setup goodbye')
53
61
def test_diff_prefix(self):
54
62
"""diff --prefix appends to filenames in output"""
55
63
self.make_example_branch()
56
file('hello', 'wt').write('hello world!\n')
64
file('hello', 'wb').write('hello world!\n')
57
65
out, err = self.runbzr('diff --prefix old/:new/', retcode=1)
58
66
self.assertEquals(err, '')
59
self.assertEqualDiff(out, '''\
67
self.assertEqualDiff(subst_dates(out), '''\
60
68
=== modified file 'hello'
69
--- old/hello\tYYYY-MM-DD HH:MM:SS +ZZZZ
70
+++ new/hello\tYYYY-MM-DD HH:MM:SS +ZZZZ
69
77
def test_diff_p1(self):
70
78
"""diff -p1 produces lkml-style diffs"""
71
79
self.make_example_branch()
72
file('hello', 'wt').write('hello world!\n')
80
file('hello', 'wb').write('hello world!\n')
73
81
out, err = self.runbzr('diff -p1', retcode=1)
74
82
self.assertEquals(err, '')
75
self.assertEqualDiff(out, '''\
83
self.assertEqualDiff(subst_dates(out), '''\
76
84
=== modified file 'hello'
85
--- old/hello\tYYYY-MM-DD HH:MM:SS +ZZZZ
86
+++ new/hello\tYYYY-MM-DD HH:MM:SS +ZZZZ
85
93
def test_diff_p0(self):
86
94
"""diff -p0 produces diffs with no prefix"""
87
95
self.make_example_branch()
88
file('hello', 'wt').write('hello world!\n')
96
file('hello', 'wb').write('hello world!\n')
89
97
out, err = self.runbzr('diff -p0', retcode=1)
90
98
self.assertEquals(err, '')
91
self.assertEqualDiff(out, '''\
99
self.assertEqualDiff(subst_dates(out), '''\
92
100
=== modified file 'hello'
101
--- hello\tYYYY-MM-DD HH:MM:SS +ZZZZ
102
+++ hello\tYYYY-MM-DD HH:MM:SS +ZZZZ
127
135
def test_diff_branches(self):
128
136
self.example_branches()
129
137
# 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"
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 test_diff_revno_branches(self):
161
self.example_branches()
162
print >> open('branch2/file', 'wb'), 'even newer content'
163
self.run_bzr_captured(['commit', '-m', 'update file once more', 'branch2'])
165
out, err = self.run_bzr_captured(['diff', '-r', 'revno:1:branch2..revno:1:branch1'],
167
self.assertEquals('', err)
168
self.assertEquals('', out)
169
out, ett = self.run_bzr_captured(['diff', '-r', 'revno:2:branch2..revno:1:branch1'],
171
self.assertEquals('', err)
172
self.assertEqualDiff("=== modified file 'file'\n"
173
"--- file\tYYYY-MM-DD HH:MM:SS +ZZZZ\n"
174
"+++ file\tYYYY-MM-DD HH:MM:SS +ZZZZ\n"
177
"+contents of branch1/file\n"
178
"\n", subst_dates(out))
150
180
def example_branch2(self):
151
181
self.build_tree(['branch1/', 'branch1/file1'], line_endings='binary')
164
194
output = self.run_bzr_captured(['diff', '-r', '1..', 'branch1'], retcode=1)
165
195
self.assertTrue('\n-original line\n+new line\n' in output[0])
197
def test_diff_across_rename(self):
198
"""The working tree path should always be considered for diffing"""
199
self.make_example_branch()
200
self.run_bzr('diff', '-r', '0..1', 'hello', retcode=1)
201
wt = workingtree.WorkingTree.open_containing('.')[0]
202
wt.rename_one('hello', 'hello1')
203
self.run_bzr('diff', 'hello1', retcode=1)
204
self.run_bzr('diff', '-r', '0..1', 'hello1', retcode=1)
168
207
class TestCheckoutDiff(TestDiff):