1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
|
#!/usr/bin/python
import bzrlib.tests
import os.path
class ShelfTests(bzrlib.tests.TestCaseWithTransport):
ORIGINAL = '\n\nhello test world\n\n'
MODIFIED = '\n\ngoodbye test world\n\n'
DIFF_HEADER = "=== modified file 'a/test_file'\n"
DIFF_1 = """--- a/test_file\t
+++ b/test_file\t
@@ -1,4 +1,4 @@
-hello test world
+goodbye test world
"""
DIFF_2 = """--- a/test_file\t
+++ b/test_file\t
@@ -1,4 +1,4 @@
-goodbye test world
+hello test world
"""
def test_shelf(self):
self.__test_loop(1)
def test_shelf_multi(self):
self.__test_loop(10)
def __test_loop(self, count):
tree = self.make_branch_and_tree('.')
self.__create_and_add_test_file(tree)
while count > 0:
count -= 1
# Modify the test file
file('test_file', 'w').write(self.MODIFIED)
# Check the diff is right
self.assertEqual(self.capture('diff', retcode=1),
self.DIFF_HEADER + self.DIFF_1 + '\n')
# Shelve the changes
self.run_bzr('shelve', retcode=0)
# Make sure there is no diff anymore
self.assertEqual(self.capture('diff', retcode=0), '')
# Make sure the file is actually back the way it was
self.assertEqual(file('test_file').read(), self.ORIGINAL)
# Check the shelf is right
shelf = open(os.path.join(tree.branch.base,
'.shelf/shelves/default/00')).read()
shelf = shelf[shelf.index('\n') + 1:] # skip the message
self.assertEqual(shelf, self.DIFF_1)
# Unshelve
self.run_bzr('unshelve', retcode=0)
# Check the diff is right again
self.assertEqual(self.capture('diff', retcode=1),
self.DIFF_HEADER + self.DIFF_1 + '\n')
# Make sure the file is back the way it should be
self.assertEqual(file('test_file').read(), self.MODIFIED)
def test_shelf_nothing_to_shelve(self):
import os.path
tree = self.make_branch_and_tree('.')
self.__create_and_add_test_file(tree)
# Shelve the changes
self.run_bzr('shelve', retcode=3)
if os.path.exists(os.path.join(tree.branch.base,
'.shelf/shelves/default/00')):
self.fail("Shelf exists, but it shouldn't")
def __create_and_add_test_file(self, tree, filename='test_file'):
self.build_tree_contents([(filename, self.ORIGINAL)])
tree.add(filename)
tree.commit(message='add %s' % filename)
def test_shelf_with_revision(self):
tree = self.make_branch_and_tree('.')
self.__create_and_add_test_file(tree)
# Modify the test file and commit it
self.build_tree_contents([('test_file', self.MODIFIED)])
tree.commit(message='update test_file')
# Shelve the changes
self.run_bzr('shelve', '-r', '1', retcode=0)
# Check the diff is right
self.assertEqual(self.capture('diff', retcode=1),
self.DIFF_HEADER + self.DIFF_2 + '\n')
# Make sure the file is the way it should be
self.assertEqual(file('test_file').read(), self.ORIGINAL)
# Unshelve
self.run_bzr('unshelve', retcode=0)
# Make sure the file is back the way it should be
self.assertEqual(file('test_file').read(), self.MODIFIED)
def test_shelf_with_two_revisions(self):
tree = self.make_branch_and_tree('.')
cmd = 'shelve -r 1..2'
(stdout, stderr) = self.run_bzr_captured(cmd.split(), retcode=None)
self.assertEqual(stderr.split('\n')[0],
'bzr: ERROR: shelve only accepts a single revision parameter.')
def test_shelf_long_filename(self):
"""Regression test for diffstat with long filenames.
Create a branch with two files, one of which has a long name. Commit.
Modify both files. Shelve the file with the short name. If diffstat
has regressed, it will generate a diffstat of the file with the long
name, and break.
"""
self.run_bzr('init')
filename = 'a' * 80
file(filename, 'wb').write('hello')
file('foo', 'wb').write('bar')
self.run_bzr('add')
self.run_bzr('commit', '-m', 'added')
file(filename, 'wb').write('goodbye')
file('foo', 'wb').write('baz')
self.run_bzr('shelve', 'foo', retcode=0)
def test_shelf_show_basic(self):
tree = self.make_branch_and_tree('.')
self.__create_and_add_test_file(tree)
self.__test_show(tree, '00')
def __test_show(self, tree, patch):
# Modify the test file
self.build_tree_contents([('test_file', 'patch %s\n' % patch)])
# Shelve the changes
self.run_bzr('shelve', retcode=0)
# Make sure there is no diff anymore
self.assertEqual(self.capture('diff', retcode=0), '')
# Check the shelf is right
shelf = open(os.path.join(tree.branch.base,
'.shelf/shelves/default', patch)).read()
self.assertTrue('patch %s' % patch in shelf)
# Check the shown output is right
shown = self.capture('shelf show %s' % patch, retcode=0)
self.assertEqual(shown, shelf)
def test_shelf_show_multi(self):
tree = self.make_branch_and_tree('.')
self.__create_and_add_test_file(tree)
self.__test_show(tree, '00')
self.__test_show(tree, '01')
self.__test_show(tree, '02')
# Now check we can show a previously shelved patch
shelf = open(os.path.join(tree.branch.base,
'.shelf/shelves/default/00')).read()
self.assertTrue('patch 00' in shelf)
# Check the shown output is right
shown = self.capture('shelf show 00', retcode=0)
self.assertEqual(shown, shelf)
def test_shelf_show_with_no_patch(self):
tree = self.make_branch_and_tree('.')
stderr = self.run_bzr_captured(['shelf', 'show', '00'], retcode=None)[1]
self.assertTrue("Patch '00' doesn't exist on shelf default!" in stderr)
def test_shelf_unshelve_failure(self):
tree = self.make_branch_and_tree('.')
self.__create_and_add_test_file(tree)
# Modify the test file
file('test_file', 'w').write(self.MODIFIED)
# Shelve the changes
self.run_bzr('shelve', retcode=0)
# Write an unapplyable patch into the shelf
shelf = open(os.path.join(tree.branch.base,
'.shelf/shelves/default/00'), 'w')
shelf.write(self.DIFF_2)
shelf.close()
# Unshelve, should fail
self.run_bzr('unshelve', retcode=3)
# Make sure the patch is still there, eventhough it's broken
shelf = open(os.path.join(tree.branch.base,
'.shelf/shelves/default/00')).read()
self.assertEqual(shelf, self.DIFF_2)
# Working tree should be unchanged
diff = self.capture('diff', retcode=0)
self.assertEqual(diff, '')
def test_shelf_unshelve_failure_two_hunks(self):
tree = self.make_branch_and_tree('.')
self.__create_and_add_test_file(tree)
self.__create_and_add_test_file(tree, filename='test_file2')
# Modify the test files
file('test_file', 'w').write(self.MODIFIED)
file('test_file2', 'w').write(self.MODIFIED)
# Shelve the changes
self.run_bzr('shelve', retcode=0)
# Put the changes to test_file back, the shelved patch won't apply now
file('test_file', 'w').write(self.MODIFIED)
tree.commit(message='screw up test_file')
# Unshelve, should fail
self.run_bzr('unshelve', retcode=3)
# Working tree should be unchanged
diff = self.capture('diff', retcode=0)
self.assertEqual(diff, '')
|