7
8
ORIGINAL = '\n\nhello test world\n\n'
8
9
MODIFIED = '\n\ngoodbye test world\n\n'
9
10
DIFF_HEADER = "=== modified file '%(filename)s'\n"
10
DIFF_1 = """--- %(filename)s\t
11
DIFF_1 = """--- %(filename)s\t%(old_date)s
12
+++ %(filename)s\t%(new_date)s
28
29
def _check_diff(self, diff=DIFF_1, filename='test_file'):
29
keys = { 'filename' : filename }
30
old_tree = self.tree.basis_tree()
31
old_date = _patch_header_date(old_tree,
32
old_tree.inventory.path2id(filename),
34
new_date = _patch_header_date(self.tree,
35
self.tree.inventory.path2id(filename),
37
keys = { 'filename' : filename , 'old_date': old_date,
30
39
hdr = self.DIFF_HEADER % keys
32
41
self.assertEqual(self.capture('diff', retcode=1), hdr + diff + '\n')
34
def _check_shelf(self, idx, diff=DIFF_1, filename='test_file'):
35
diff = diff % { 'filename' : filename }
36
shelf = open(os.path.join(self.tree.branch.base,
43
def _check_shelf(self, idx, diff=DIFF_1, filename='test_file',
45
old_tree = self.tree.basis_tree()
46
old_date = _patch_header_date(old_tree,
47
old_tree.inventory.path2id(filename),
49
diff = diff % { 'filename' : filename, 'old_date': old_date,
51
shelf = open(os.path.join(self.tree.basedir,
37
52
'.shelf/shelves/default/' + idx)).read()
38
53
shelf = shelf[shelf.index('\n') + 1:] # skip the message
39
54
self.assertEqual(shelf, diff)
55
70
file('test_file', 'w').write(self.MODIFIED)
74
new_date = _patch_header_date(self.tree,
75
self.tree.inventory.path2id('test_file'), 'test_file')
59
77
# Shelve the changes
60
78
self.run_bzr('shelve', '--all', retcode=0)
65
83
# Make sure the file is actually back the way it was
66
84
self.assertEqual(file('test_file').read(), self.ORIGINAL)
68
self._check_shelf('00')
86
self._check_shelf('00', new_date=new_date)
71
89
self.run_bzr('unshelve', '--all', retcode=0)
75
93
# Check the shelved patch was backed up
76
self._check_shelf('00~')
94
self._check_shelf('00~', new_date=new_date)
78
96
# Make sure the file is back the way it should be
79
97
self.assertEqual(file('test_file').read(), self.MODIFIED)
145
163
self.assertEqual(self.capture('diff', retcode=0), '')
147
165
# Check the shelf is right
148
shelf = open(os.path.join(self.tree.branch.base,
166
shelf = open(os.path.join(self.tree.basedir,
149
167
'.shelf/shelves/default', patch)).read()
150
168
self.assertTrue('patch %s' % patch in shelf)
161
179
self.__test_show(self.tree, '02')
163
181
# Now check we can show a previously shelved patch
164
shelf = open(os.path.join(self.tree.branch.base,
182
shelf = open(os.path.join(self.tree.basedir,
165
183
'.shelf/shelves/default/00')).read()
166
184
self.assertTrue('patch 00' in shelf)
177
195
self.__test_show(self.tree, '02')
179
197
# Check that not specifying at patch gets us the most recent
180
shelf = open(os.path.join(self.tree.branch.base,
198
shelf = open(os.path.join(self.tree.basedir,
181
199
'.shelf/shelves/default/02')).read()
182
200
self.assertTrue('patch 02' in shelf)
202
220
self.run_bzr('shelve', '--all', retcode=0)
204
222
# Write an unapplyable patch into the shelf
205
shelf = open(os.path.join(self.tree.branch.base,
223
shelf = open(os.path.join(self.tree.basedir,
206
224
'.shelf/shelves/default/00'), 'w')
207
225
shelf.write(self.DIFF_2)
211
229
self.run_bzr('unshelve', '--all', retcode=3)
213
231
# Make sure the patch is still there, eventhough it's broken
214
shelf = open(os.path.join(self.tree.branch.base,
232
shelf = open(os.path.join(self.tree.basedir,
215
233
'.shelf/shelves/default/00')).read()
216
234
self.assertEqual(shelf, self.DIFF_2)
286
304
# Modify the test files
287
305
file('test_file', 'w').write(self.MODIFIED)
288
306
file('test_file2', 'w').write(self.MODIFIED)
307
new_date = _patch_header_date(self.tree,
308
self.tree.inventory.path2id('test_file'), 'test_file')
290
310
# Shelve the changes
291
311
self.run_bzr('shelve', '--all', 'test_file', retcode=0)
292
312
self.run_bzr('shelve', '--all', 'test_file2', retcode=0)
294
self._check_shelf('00')
314
self._check_shelf('00', new_date=new_date)
297
317
self.run_bzr('shelf', 'delete', '00', retcode=0)
302
322
self.assertTrue(os.path.exists('.shelf/shelves/default/01'))
304
324
# Check the backup is right
305
self._check_shelf('00~')
325
self._check_shelf('00~', new_date=new_date)
308
328
lines = self.capture('shelf ls', retcode=0).split('\n')
379
399
# Run a benign shelf command to setup .shelf for us
380
400
self.run_bzr('shelf', 'ls', retcode=0)
402
old_tree = self.tree.basis_tree()
403
old_date = _patch_header_date(old_tree,
404
old_tree.inventory.path2id('test_file'),
406
new_date = _patch_header_date(self.tree,
407
self.tree.inventory.path2id('test_file'),
382
409
# Fake a -p0 shelved patch
383
diff = self.DIFF_1 % { 'filename' : 'test_file' }
410
diff = self.DIFF_1 % { 'filename' : 'test_file', 'old_date': old_date,
411
'new_date' : new_date}
384
412
diff = diff.replace('--- ', '--- a/')
385
413
diff = diff.replace('+++ ', '+++ b/')
386
414
open('.shelf/shelves/default/00', 'w').write(diff)