25
25
from bzrlib.branch import Branch
26
26
from bzrlib.tests.blackbox import ExternalBase
29
def subst_dates(string):
30
"""Replace date strings with constant values."""
31
return re.sub(r'\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2} [-\+]\d{4}',
32
'YYYY-MM-DD HH:MM:SS +ZZZZ', string)
35
29
class TestDiff(ExternalBase):
37
def make_example_branch(test):
30
def example_branch(test):
38
31
# FIXME: copied from test_too_much -- share elsewhere?
39
32
test.runbzr('init')
40
file('hello', 'wt').write('foo\n')
33
file('hello', 'wt').write('foo')
41
34
test.runbzr('add hello')
42
35
test.runbzr('commit -m setup hello')
43
file('goodbye', 'wt').write('baz\n')
36
file('goodbye', 'wt').write('baz')
44
37
test.runbzr('add goodbye')
45
38
test.runbzr('commit -m setup goodbye')
47
40
def test_diff(self):
48
self.make_example_branch()
49
42
file('hello', 'wt').write('hello world!')
50
43
self.runbzr('commit -m fixing hello')
51
44
output = self.runbzr('diff -r 2..3', backtick=1, retcode=1)
58
51
self.runbzr('diff')
60
def test_diff_prefix(self):
61
"""diff --prefix appends to filenames in output"""
62
self.make_example_branch()
63
file('hello', 'wt').write('hello world!\n')
64
out, err = self.runbzr('diff --prefix old/:new/', retcode=1)
65
self.assertEquals(err, '')
66
self.assertEqualDiff(subst_dates(out), '''\
67
=== modified file 'hello'
68
--- old/hello\tYYYY-MM-DD HH:MM:SS +ZZZZ
69
+++ new/hello\tYYYY-MM-DD HH:MM:SS +ZZZZ
76
def test_diff_p1(self):
77
"""diff -p1 produces lkml-style diffs"""
78
self.make_example_branch()
79
file('hello', 'wt').write('hello world!\n')
80
out, err = self.runbzr('diff -p1', retcode=1)
81
self.assertEquals(err, '')
82
self.assertEqualDiff(subst_dates(out), '''\
83
=== modified file 'hello'
84
--- old/hello\tYYYY-MM-DD HH:MM:SS +ZZZZ
85
+++ new/hello\tYYYY-MM-DD HH:MM:SS +ZZZZ
92
def test_diff_p0(self):
93
"""diff -p0 produces diffs with no prefix"""
94
self.make_example_branch()
95
file('hello', 'wt').write('hello world!\n')
96
out, err = self.runbzr('diff -p0', retcode=1)
97
self.assertEquals(err, '')
98
self.assertEqualDiff(subst_dates(out), '''\
99
=== modified file 'hello'
100
--- hello\tYYYY-MM-DD HH:MM:SS +ZZZZ
101
+++ hello\tYYYY-MM-DD HH:MM:SS +ZZZZ
108
def test_diff_nonexistent(self):
109
# Get an error from a file that does not exist at all
111
self.make_example_branch()
112
out, err = self.runbzr('diff does-not-exist', retcode=3)
113
self.assertContainsRe(err, 'not versioned.*does-not-exist')
115
def test_diff_unversioned(self):
116
# Get an error when diffing a non-versioned file.
118
self.make_example_branch()
119
self.build_tree(['unversioned-file'])
120
out, err = self.runbzr('diff unversioned-file', retcode=3)
121
self.assertContainsRe(err, 'not versioned.*unversioned-file')
123
# TODO: What should diff say for a file deleted in working tree?
125
def example_branches(self):
53
def test_diff_branches(self):
126
54
self.build_tree(['branch1/', 'branch1/file'], line_endings='binary')
127
55
self.capture('init branch1')
128
56
self.capture('add branch1/file')
130
58
self.capture('branch branch1 branch2')
131
59
print >> open('branch2/file', 'wb'), 'new content'
132
60
self.run_bzr_captured(['commit', '-m', 'update file', 'branch2'])
134
def test_diff_branches(self):
135
self.example_branches()
136
61
# should open branch1 and diff against branch2,
137
out, err = self.run_bzr_captured(['diff', '-r', 'branch:branch2',
140
self.assertEquals('', err)
141
self.assertEquals("=== modified file 'file'\n"
142
"--- file\tYYYY-MM-DD HH:MM:SS +ZZZZ\n"
143
"+++ file\tYYYY-MM-DD HH:MM:SS +ZZZZ\n"
146
"+contents of branch1/file\n"
147
"\n", subst_dates(out))
148
out, ett = self.run_bzr_captured(['diff', 'branch2', 'branch1'],
150
self.assertEquals('', err)
151
self.assertEqualDiff("=== modified file 'file'\n"
152
"--- file\tYYYY-MM-DD HH:MM:SS +ZZZZ\n"
153
"+++ file\tYYYY-MM-DD HH:MM:SS +ZZZZ\n"
62
output = self.run_bzr_captured(['diff', '-r', 'branch:branch2',
65
self.assertEquals(("=== modified file 'file'\n"
70
"+contents of branch1/file\n"
72
output = self.run_bzr_captured(['diff', 'branch2', 'branch1'],
74
self.assertEqualDiff(("=== modified file 'file'\n"
154
77
"@@ -1,1 +1,1 @@\n"
156
79
"+contents of branch1/file\n"
157
"\n", subst_dates(out))
159
def example_branch2(self):
82
def test_diff_to_working_tree(self):
160
83
self.build_tree(['branch1/', 'branch1/file1'], line_endings='binary')
161
84
self.capture('init branch1')
162
85
self.capture('add branch1/file1')
166
89
print >> open('branch1/file1', 'wb'), 'repo line'
167
90
self.run_bzr_captured(['commit', '-m', 'second commit', 'branch1'])
169
def test_diff_to_working_tree(self):
170
self.example_branch2()
172
92
print >> open('branch1/file1', 'wb'), 'new line'
173
93
output = self.run_bzr_captured(['diff', '-r', '1..', 'branch1'], retcode=1)
174
94
self.assertTrue('\n-original line\n+new line\n' in output[0])
177
class TestCheckoutDiff(TestDiff):
179
def make_example_branch(self):
180
super(TestCheckoutDiff, self).make_example_branch()
181
self.runbzr('checkout . checkout')
184
def example_branch2(self):
185
super(TestCheckoutDiff, self).example_branch2()
186
os.mkdir('checkouts')
187
self.runbzr('checkout branch1 checkouts/branch1')
188
os.chdir('checkouts')
190
def example_branches(self):
191
super(TestCheckoutDiff, self).example_branches()
192
os.mkdir('checkouts')
193
self.runbzr('checkout branch1 checkouts/branch1')
194
self.runbzr('checkout branch2 checkouts/branch2')
195
os.chdir('checkouts')
198
class TestDiffLabels(TestDiff):
200
def test_diff_label_removed(self):
201
super(TestDiffLabels, self).make_example_branch()
202
self.runbzr('remove hello')
203
diff = self.run_bzr_captured(['diff'], retcode=1)
204
self.assertTrue("=== removed file 'hello'" in diff[0])
206
def test_diff_label_added(self):
207
super(TestDiffLabels, self).make_example_branch()
208
file('barbar', 'wt').write('barbar')
209
self.runbzr('add barbar')
210
diff = self.run_bzr_captured(['diff'], retcode=1)
211
self.assertTrue("=== added file 'barbar'" in diff[0])
213
def test_diff_label_modified(self):
214
super(TestDiffLabels, self).make_example_branch()
215
file('hello', 'wt').write('barbar')
216
diff = self.run_bzr_captured(['diff'], retcode=1)
217
self.assertTrue("=== modified file 'hello'" in diff[0])
219
def test_diff_label_renamed(self):
220
super(TestDiffLabels, self).make_example_branch()
221
self.runbzr('rename hello gruezi')
222
diff = self.run_bzr_captured(['diff'], retcode=1)
223
self.assertTrue("=== renamed file 'hello' => 'gruezi'" in diff[0])