~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to tests/shelf_tests.py

  • Committer: Aaron Bentley
  • Date: 2008-10-22 09:11:39 UTC
  • Revision ID: aaron@aaronbentley.com-20081022091139-28q2r3wcun52zz4u
Use lazy command-loading in bzrtools

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/python
2
 
 
 
1
import os.path
 
2
from bzrlib.diff import _patch_header_date
3
3
import bzrlib.tests
4
 
import os.path
 
4
 
 
5
from bzrlib.plugins.bzrtools.hunk_selector import (
 
6
    ShelveHunkSelector,
 
7
    UnshelveHunkSelector,
 
8
    )
 
9
from bzrlib.plugins.bzrtools.errors import NoColor
 
10
from bzrlib.plugins.bzrtools.command_classes import cmd_shelf
 
11
 
5
12
 
6
13
class ShelfTests(bzrlib.tests.TestCaseWithTransport):
7
14
    ORIGINAL = '\n\nhello test world\n\n'
8
15
    MODIFIED = '\n\ngoodbye test world\n\n'
9
16
    DIFF_HEADER = "=== modified file '%(filename)s'\n"
10
 
    DIFF_1 = """--- %(filename)s\t
11
 
+++ %(filename)s\t
 
17
    DIFF_1 = """--- %(filename)s\t%(old_date)s
 
18
+++ %(filename)s\t%(new_date)s
12
19
@@ -1,4 +1,4 @@
13
20
 
14
21
 
16
23
+goodbye test world
17
24
 
18
25
"""
19
 
    DIFF_2 = """--- test_file\t
20
 
+++ test_file\t
 
26
    DIFF_2 = """--- test_file\t%(old_date)s
 
27
+++ test_file\t%(new_date)s
21
28
@@ -1,4 +1,4 @@
22
29
 
23
30
 
26
33
 
27
34
"""
28
35
    def _check_diff(self, diff=DIFF_1, filename='test_file'):
29
 
        keys = { 'filename' : filename }
 
36
        old_tree = self.tree.basis_tree()
 
37
        old_tree.lock_read()
 
38
        self.tree.lock_read()
 
39
        try:
 
40
            old_date = _patch_header_date(old_tree,
 
41
                                          old_tree.inventory.path2id(filename),
 
42
                                          filename)
 
43
            new_date = _patch_header_date(self.tree,
 
44
                self.tree.inventory.path2id(filename), filename)
 
45
        finally:
 
46
            self.tree.unlock()
 
47
            old_tree.unlock()
 
48
        keys = { 'filename' : filename , 'old_date': old_date,
 
49
                 'new_date': new_date}
30
50
        hdr  = self.DIFF_HEADER % keys
31
51
        diff = diff % keys
32
 
        self.assertEqual(self.capture('diff', retcode=1), hdr + diff + '\n')
 
52
        self.assertEqual(self.run_bzr('diff', retcode=1)[0], hdr + diff + '\n')
33
53
 
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,
 
54
    def _check_shelf(self, idx, diff=DIFF_1, filename='test_file',
 
55
                     new_date=None):
 
56
        old_tree = self.tree.basis_tree()
 
57
        old_tree.lock_read()
 
58
        try:
 
59
            old_date = _patch_header_date(old_tree,
 
60
                                          old_tree.inventory.path2id(filename),
 
61
                                          filename)
 
62
        finally:
 
63
            old_tree.unlock()
 
64
        diff = diff % { 'filename' : filename, 'old_date': old_date,
 
65
                        'new_date': new_date}
 
66
        shelf = open(os.path.join(self.tree.basedir,
37
67
                '.shelf/shelves/default/' + idx)).read()
38
68
        shelf = shelf[shelf.index('\n') + 1:] # skip the message
39
69
        self.assertEqual(shelf, diff)
47
77
    def __test_loop(self, count):
48
78
        self.tree = self.make_branch_and_tree('.')
49
79
        self.__create_and_add_test_file()
50
 
 
51
 
        while count > 0:
52
 
            count -= 1
53
 
 
 
80
        for counter in range(count):
54
81
            # Modify the test file
55
 
            file('test_file', 'w').write(self.MODIFIED)
 
82
            # write in binary mode because on win32 line-endings should be
 
83
            # LF
 
84
            f = file('test_file', 'wb')
 
85
            f.write(self.MODIFIED)
 
86
            f.close()
56
87
 
57
88
            self._check_diff()
58
89
 
 
90
            self.tree.lock_write()
 
91
            try:
 
92
                new_date = _patch_header_date(self.tree,
 
93
                    self.tree.inventory.path2id('test_file'), 'test_file')
 
94
            finally:
 
95
                self.tree.unlock()
 
96
 
59
97
            # Shelve the changes
60
 
            self.run_bzr('shelve', '--all', retcode=0)
 
98
            self.run_bzr('shelve --all', retcode=0)
61
99
 
62
100
            # Make sure there is no diff anymore
63
 
            self.assertEqual(self.capture('diff', retcode=0), '')
 
101
            self.assertEqual(self.run_bzr('diff', retcode=0)[0], '')
64
102
 
65
103
            # Make sure the file is actually back the way it was
66
104
            self.assertEqual(file('test_file').read(), self.ORIGINAL)
67
105
 
68
 
            self._check_shelf('00')
 
106
            self._check_shelf('00', new_date=new_date)
69
107
 
70
108
            # Unshelve
71
 
            self.run_bzr('unshelve', '--all', retcode=0)
 
109
            self.run_bzr('unshelve --all', retcode=0)
72
110
 
73
111
            self._check_diff()
74
112
 
75
113
            # Check the shelved patch was backed up
76
 
            self._check_shelf('00~')
 
114
            self._check_shelf('00~', new_date=new_date)
77
115
 
78
116
            # Make sure the file is back the way it should be
79
117
            self.assertEqual(file('test_file').read(), self.MODIFIED)
84
122
        self.__create_and_add_test_file()
85
123
 
86
124
        # Shelve the changes
87
 
        self.run_bzr('shelve', '--all', retcode=3)
 
125
        self.run_bzr('shelve --all', retcode=3)
88
126
 
89
127
        if os.path.exists(os.path.join(self.tree.branch.base,
90
128
                '.shelf/shelves/default/00')):
91
129
            self.fail("Shelf exists, but it shouldn't")
92
130
 
93
131
    def __create_and_add_test_file(self, filename='test_file'):
94
 
        f = open(filename, 'w')
 
132
        # write in binary mode because on win32 line-endings should be LF
 
133
        f = open(filename, 'wb')
95
134
        f.write(self.ORIGINAL)
96
135
        f.close()
97
136
        self.tree.add(self.tree.relpath(os.path.join(os.getcwd(), filename)))
107
146
        self.tree.commit(message='update test_file')
108
147
 
109
148
        # Shelve the changes
110
 
        self.run_bzr('shelve', '--all', '-r', '1', retcode=0)
 
149
        self.run_bzr('shelve --all -r 1', retcode=0)
111
150
 
112
151
        self._check_diff(self.DIFF_2)
113
152
 
115
154
        self.assertEqual(file('test_file').read(), self.ORIGINAL)
116
155
 
117
156
        # Unshelve
118
 
        self.run_bzr('unshelve', '--all', retcode=0)
 
157
        self.run_bzr('unshelve --all', retcode=0)
119
158
 
120
159
        # Make sure the file is back the way it should be
121
160
        self.assertEqual(file('test_file').read(), self.MODIFIED)
123
162
    def test_shelf_with_two_revisions(self):
124
163
        self.tree = self.make_branch_and_tree('.')
125
164
 
126
 
        cmd = 'shelve --all -r 1..2'
127
 
        (stdout, stderr) = self.run_bzr_captured(cmd.split(), retcode=None)
 
165
        stdout, stderr = self.run_bzr('shelve --all -r 1..2', retcode=None)
128
166
 
129
167
        self.assertEqual(stderr.split('\n')[0],
130
168
            'bzr: ERROR: shelve only accepts a single revision parameter.')
139
177
        self.build_tree_contents([('test_file', 'patch %s\n' % patch)])
140
178
 
141
179
        # Shelve the changes
142
 
        self.run_bzr('shelve', '--all', retcode=0)
 
180
        self.run_bzr('shelve --all', retcode=0)
143
181
 
144
182
        # Make sure there is no diff anymore
145
 
        self.assertEqual(self.capture('diff', retcode=0), '')
 
183
        self.assertEqual(self.run_bzr('diff', retcode=0)[0], '')
146
184
 
147
185
        # Check the shelf is right
148
 
        shelf = open(os.path.join(self.tree.branch.base,
 
186
        shelf = open(os.path.join(self.tree.basedir,
149
187
                    '.shelf/shelves/default', patch)).read()
150
188
        self.assertTrue('patch %s' % patch in shelf)
151
189
 
152
190
        # Check the shown output is right
153
 
        shown = self.capture('shelf show %s' % patch, retcode=0)
 
191
        shown = self.run_bzr('shelf show %s' % patch, retcode=0)[0]
154
192
        self.assertEqual(shown, shelf)
155
193
 
156
194
    def test_shelf_show_multi(self):
161
199
        self.__test_show(self.tree, '02')
162
200
 
163
201
        # Now check we can show a previously shelved patch
164
 
        shelf = open(os.path.join(self.tree.branch.base,
 
202
        shelf = open(os.path.join(self.tree.basedir,
165
203
                    '.shelf/shelves/default/00')).read()
166
204
        self.assertTrue('patch 00' in shelf)
167
205
 
168
206
        # Check the shown output is right
169
 
        shown = self.capture('shelf show 00', retcode=0)
 
207
        shown = self.run_bzr('shelf show 00', retcode=0)[0]
170
208
        self.assertEqual(shown, shelf)
171
209
 
172
210
    def test_shelf_show_unspecified(self):
177
215
        self.__test_show(self.tree, '02')
178
216
 
179
217
        # Check that not specifying at patch gets us the most recent
180
 
        shelf = open(os.path.join(self.tree.branch.base,
 
218
        shelf = open(os.path.join(self.tree.basedir,
181
219
                    '.shelf/shelves/default/02')).read()
182
220
        self.assertTrue('patch 02' in shelf)
183
221
 
184
222
        # Check the shown output is right
185
 
        shown = self.capture('shelf show', retcode=0)
 
223
        shown = self.run_bzr('shelf show', retcode=0)[0]
186
224
        self.assertEqual(shown, shelf)
187
225
 
188
226
    def test_shelf_show_with_no_patch(self):
189
227
        self.tree = self.make_branch_and_tree('.')
190
 
        stderr = self.run_bzr_captured(['shelf', 'show', '00'], retcode=None)[1]
 
228
        stderr = self.run_bzr('shelf show 00', retcode=None)[1]
191
229
        self.assertTrue("Patch '00' doesn't exist on shelf default!" in stderr)
192
230
 
193
231
    def test_shelf_unshelve_failure(self):
199
237
        file('test_file', 'w').write(self.MODIFIED)
200
238
 
201
239
        # Shelve the changes
202
 
        self.run_bzr('shelve', '--all', retcode=0)
 
240
        self.run_bzr('shelve --all', retcode=0)
203
241
 
204
242
        # Write an unapplyable patch into the shelf
205
 
        shelf = open(os.path.join(self.tree.branch.base,
 
243
        shelf = open(os.path.join(self.tree.basedir,
206
244
                    '.shelf/shelves/default/00'), 'w')
207
245
        shelf.write(self.DIFF_2)
208
246
        shelf.close()
209
247
 
210
248
        # Unshelve, should fail
211
 
        self.run_bzr('unshelve', '--all', retcode=3)
 
249
        self.run_bzr('unshelve --all', retcode=3)
212
250
 
213
251
        # Make sure the patch is still there, eventhough it's broken
214
 
        shelf = open(os.path.join(self.tree.branch.base,
 
252
        shelf = open(os.path.join(self.tree.basedir,
215
253
                    '.shelf/shelves/default/00')).read()
216
254
        self.assertEqual(shelf, self.DIFF_2)
217
255
 
218
256
        # Working tree should be unchanged
219
 
        diff = self.capture('diff', retcode=0)
 
257
        diff = self.run_bzr('diff', retcode=0)[0]
220
258
        self.assertEqual(diff, '')
221
259
 
222
260
    def test_shelf_unshelve_failure_two_hunks(self):
230
268
        file('test_file2', 'w').write(self.MODIFIED)
231
269
 
232
270
        # Shelve the changes
233
 
        self.run_bzr('shelve', '--all', retcode=0)
 
271
        self.run_bzr('shelve --all', retcode=0)
234
272
 
235
273
        # Put the changes to test_file back, the shelved patch won't apply now
236
274
        file('test_file', 'w').write(self.MODIFIED)
237
275
        self.tree.commit(message='screw up test_file')
238
276
 
239
277
        # Unshelve, should fail
240
 
        self.run_bzr('unshelve', '--all', retcode=3)
 
278
        self.run_bzr('unshelve --all', retcode=3)
241
279
 
242
280
        # Working tree should be unchanged
243
 
        diff = self.capture('diff', retcode=0)
 
281
        diff = self.run_bzr('diff', retcode=0)[0]
244
282
        self.assertEqual(diff, '')
245
283
 
246
284
        # Force should succeed and modify test_file2, but leave shelf
247
 
        self.run_bzr('unshelve', '--force', '--all', retcode=0)
 
285
        self.run_bzr('unshelve --force --all', retcode=0)
248
286
        self.assertEqual(open('test_file2').read(), self.MODIFIED)
249
287
        self.assertTrue(os.path.exists('.shelf/shelves/default/00'))
250
288
 
259
297
        file('test_file2', 'w').write(self.MODIFIED)
260
298
 
261
299
        # Shelve the changes
262
 
        self.run_bzr('shelve', '--all', 'test_file', retcode=0)
263
 
        self.run_bzr('shelve', '--all', 'test_file2', retcode=0)
 
300
        self.run_bzr('shelve --all test_file', retcode=0)
 
301
        self.run_bzr('shelve --all test_file2', retcode=0)
264
302
 
265
303
        # Unshelve
266
 
        self.run_bzr('unshelve', '--all', retcode=0)
 
304
        self.run_bzr('unshelve --all', retcode=0)
267
305
 
268
306
        # We should now have 00 and 01~
269
307
        self.assertTrue(os.path.exists('.shelf/shelves/default/00'))
270
308
        self.assertTrue(os.path.exists('.shelf/shelves/default/01~'))
271
309
 
272
310
        # Check ls works
273
 
        list = self.capture('shelf ls', retcode=0).split('\n')
274
 
        for line in list:
 
311
        lines = self.run_bzr('shelf ls', retcode=0)[0].split('\n')
 
312
        for line in lines:
275
313
            self.assertFalse(line.startswith(' 01'))
276
314
 
277
315
        # Unshelve, if unshelve is confused by the backup it will fail
278
 
        self.run_bzr('unshelve', '--all', retcode=0)
 
316
        self.run_bzr('unshelve --all', retcode=0)
279
317
 
280
318
    def test_shelf_delete(self):
281
319
        self.tree = self.make_branch_and_tree('.')
282
 
 
283
 
        self.__create_and_add_test_file()
284
 
        self.__create_and_add_test_file(filename='test_file2')
285
 
 
286
 
        # Modify the test files
287
 
        file('test_file', 'w').write(self.MODIFIED)
288
 
        file('test_file2', 'w').write(self.MODIFIED)
 
320
        self.tree.lock_write()
 
321
        try:
 
322
            self.__create_and_add_test_file()
 
323
            self.__create_and_add_test_file(filename='test_file2')
 
324
 
 
325
            # Modify the test files
 
326
            # write in binary mode because on win32 line-endings should be LF
 
327
            f = file('test_file', 'wb')
 
328
            f.write(self.MODIFIED)
 
329
            f.close()
 
330
            f = file('test_file2', 'wb')
 
331
            f.write(self.MODIFIED)
 
332
            f.close()
 
333
            new_date = _patch_header_date(self.tree,
 
334
                self.tree.inventory.path2id('test_file'), 'test_file')
 
335
        finally:
 
336
            self.tree.unlock()
289
337
 
290
338
        # Shelve the changes
291
 
        self.run_bzr('shelve', '--all', 'test_file', retcode=0)
292
 
        self.run_bzr('shelve', '--all', 'test_file2', retcode=0)
 
339
        self.run_bzr('shelve --all test_file', retcode=0)
 
340
        self.run_bzr('shelve --all test_file2', retcode=0)
293
341
 
294
 
        self._check_shelf('00')
 
342
        self._check_shelf('00', new_date=new_date)
295
343
 
296
344
        # Delete 00
297
 
        self.run_bzr('shelf', 'delete', '00', retcode=0)
 
345
        self.run_bzr('shelf delete 00', retcode=0)
298
346
 
299
347
        # We should now have 01 but not 00, but we should have 00~
300
348
        self.assertFalse(os.path.exists('.shelf/shelves/default/00'))
302
350
        self.assertTrue(os.path.exists('.shelf/shelves/default/01'))
303
351
 
304
352
        # Check the backup is right
305
 
        self._check_shelf('00~')
 
353
        self._check_shelf('00~', new_date=new_date)
306
354
 
307
355
        # Check ls works
308
 
        list = self.capture('shelf ls', retcode=0).split('\n')
309
 
        for line in list:
 
356
        lines = self.run_bzr('shelf ls', retcode=0)[0].split('\n')
 
357
        for line in lines:
310
358
            self.assertFalse(line.startswith(' 00'))
311
359
 
312
360
        # Unshelve should unshelve 01
313
 
        self.run_bzr('unshelve', '--all', retcode=0)
 
361
        self.run_bzr('unshelve --all', retcode=0)
314
362
        self.assertEqual(file('test_file2').read(), self.MODIFIED)
315
363
 
316
364
    def test_shelf_gaps(self):
317
365
        self.tree = self.make_branch_and_tree('.')
318
366
        self.__create_and_add_test_file()
319
367
        file('test_file', 'w').write(self.MODIFIED)
320
 
        self.run_bzr('shelve', '--all', 'test_file', retcode=0)
 
368
        self.run_bzr('shelve --all test_file', retcode=0)
321
369
        file('test_file', 'w').write(self.MODIFIED)
322
 
        self.run_bzr('shelve', '--all', 'test_file', retcode=0)
 
370
        self.run_bzr('shelve --all test_file', retcode=0)
323
371
 
324
372
        # Now delete 00, leaving 01, next shelve should go into 02
325
 
        self.run_bzr('shelf', 'delete', '0', retcode=0)
 
373
        self.run_bzr('shelf delete 0', retcode=0)
326
374
        self.assertFalse(os.path.exists('.shelf/shelves/default/00'))
327
375
        self.assertFalse(os.path.exists('.shelf/shelves/default/02'))
328
376
        file('test_file', 'w').write(self.MODIFIED)
329
 
        self.run_bzr('shelve', '--all', 'test_file', retcode=0)
 
377
        self.run_bzr('shelve --all test_file', retcode=0)
330
378
        self.assertFalse(os.path.exists('.shelf/shelves/default/00'))
331
379
        self.assertTrue(os.path.exists('.shelf/shelves/default/02'))
332
380
 
337
385
 
338
386
        # Modify then shelve, so we're not upgrading to 00, just for kicks
339
387
        file('test_file', 'w').write(self.MODIFIED)
340
 
        self.run_bzr('shelve', '--all', 'test_file', retcode=0)
 
388
        self.run_bzr('shelve --all test_file', retcode=0)
341
389
 
342
390
        open('.bzr-shelf', 'w').write('First old shelf')
343
391
        open('.bzr-shelf-1', 'w').write('Second old shelf')
345
393
 
346
394
        # shelve and unshelve should bitch and do nothing
347
395
        file('test_file', 'w').write('blah blah blah')
348
 
        self.run_bzr('shelve', '--all', retcode=3)
 
396
        self.run_bzr('shelve --all', retcode=3)
349
397
        self.assertFalse(os.path.exists('.shelf/shelves/default/01'))
350
398
        self.assertEqual(file('test_file').read(), 'blah blah blah')
351
 
        self.run_bzr('unshelve', '--all', retcode=3)
 
399
        self.run_bzr('unshelve --all', retcode=3)
352
400
        self.assertTrue(os.path.exists('.shelf/shelves/default/00'))
353
401
 
354
402
        # Upgrade, make sure it worked
355
 
        self.run_bzr('shelf', 'upgrade', retcode=0)
 
403
        self.run_bzr('shelf upgrade', retcode=0)
356
404
        self.assertEqual(open('.shelf/shelves/default/01').read(),
357
405
                'First old shelf')
358
406
        self.assertEqual(open('.shelf/shelves/default/02').read(),
369
417
        self.assertFalse(os.path.exists('.bzr-shelf-3'))
370
418
 
371
419
        # Shelve should work now
372
 
        self.run_bzr('shelve', '--all', retcode=0)
 
420
        self.run_bzr('shelve --all', retcode=0)
373
421
 
374
422
    def test_shelf_p1_patch(self):
375
423
        self.tree = self.make_branch_and_tree('.')
377
425
        self.__create_and_add_test_file()
378
426
 
379
427
        # Run a benign shelf command to setup .shelf for us
380
 
        self.run_bzr('shelf', 'ls', retcode=0)
 
428
        self.run_bzr('shelf ls', retcode=0)
381
429
 
 
430
        old_tree = self.tree.basis_tree()
 
431
        old_tree.lock_read()
 
432
        self.tree.lock_read()
 
433
        try:
 
434
            old_date = _patch_header_date(old_tree,
 
435
                old_tree.inventory.path2id('test_file'),
 
436
                                          'test_file')
 
437
            new_date = _patch_header_date(self.tree,
 
438
                self.tree.inventory.path2id('test_file'), 'test_file')
 
439
        finally:
 
440
            old_tree.unlock()
 
441
            self.tree.unlock()
382
442
        # Fake a -p0 shelved patch
383
 
        diff = self.DIFF_1 % { 'filename' : 'test_file' }
 
443
        diff = self.DIFF_1 % { 'filename' : 'test_file', 'old_date': old_date,
 
444
                               'new_date' : new_date}
384
445
        diff = diff.replace('--- ', '--- a/')
385
446
        diff = diff.replace('+++ ', '+++ b/')
386
447
        open('.shelf/shelves/default/00', 'w').write(diff)
387
448
 
388
449
        # This should work
389
 
        self.run_bzr('unshelve', '--all', retcode=0)
 
450
        self.run_bzr('unshelve --all', retcode=0)
390
451
 
391
452
        self._check_diff()
392
453
 
401
462
        self.__create_and_add_test_file()
402
463
 
403
464
        # Modify the test file
404
 
        file('test_file', 'w').write(self.MODIFIED)
 
465
        # write in binary mode because on win32 line-endings should be LF
 
466
        f = file('test_file', 'wb')
 
467
        f.write(self.MODIFIED)
 
468
        f.close()
405
469
 
406
470
        # Shelve the changes
407
 
        self.run_bzr('shelve', '--all', retcode=0)
 
471
        self.run_bzr('shelve --all', retcode=0)
408
472
 
409
473
        # Working tree should be unchanged
410
 
        diff = self.capture('diff', retcode=0)
 
474
        diff = self.run_bzr('diff', retcode=0)[0]
411
475
        self.assertEqual(diff, '')
412
476
 
413
477
        # Unshelve, should succeed
414
 
        self.run_bzr('unshelve', '--all', retcode=0)
 
478
        self.run_bzr('unshelve --all', retcode=0)
415
479
 
416
480
        self._check_diff(filename='subdir/test_file')
417
481
 
418
482
        # Make sure relative filenames work ok
419
 
        self.run_bzr('shelve', 'test_file', '--all', retcode=0)
 
483
        self.run_bzr('shelve test_file --all', retcode=0)
420
484
 
421
485
    def test_shelf_shelf_bogus_subcommand(self):
422
486
        self.tree = self.make_branch_and_tree('.')
423
 
        self.run_bzr('shelf', 'foo', retcode=3) # <- retcode == 3
 
487
        self.run_bzr('shelf foo', retcode=3) # <- retcode == 3
 
488
 
 
489
    def test_shelf_OOO_unshelve(self):
 
490
        self.tree = self.make_branch_and_tree('.')
 
491
 
 
492
        for i in range(1, 5):
 
493
            self.__create_and_add_test_file(filename='test_file%d' % i)
 
494
 
 
495
        # Modify the test files
 
496
        for i in range(1, 5):
 
497
            file('test_file%d' % i, 'w').write(self.MODIFIED)
 
498
 
 
499
        # Shelve the changes
 
500
        for i in range(1, 5):
 
501
            self.run_bzr(['shelve', '--all', 'test_file%d' % i], retcode=0)
 
502
 
 
503
        # Check shelving worked
 
504
        for i in range(1, 5):
 
505
            self.assertEqual(file('test_file%d' % i).read(), self.ORIGINAL)
 
506
 
 
507
        # We should now have 00-03
 
508
        for i in range(0, 4):
 
509
            self.assertTrue(os.path.exists('.shelf/shelves/default/0%d' % i))
 
510
 
 
511
        # Unshelve 00
 
512
        self.run_bzr('unshelve --all 00', retcode=0)
 
513
        self.assertEqual(file('test_file1').read(), self.MODIFIED)
 
514
 
 
515
        # Check ls works
 
516
        lines = self.run_bzr('shelf ls', retcode=0)[0].split('\n')
 
517
        for line in lines:
 
518
            self.assertFalse(line.startswith(' 00'))
 
519
 
 
520
        # Check we can reshelve once we've unshelved out of order, should be 04
 
521
        self.assertFalse(os.path.exists('.shelf/shelves/default/04'))
 
522
        self.run_bzr('shelve --all')
 
523
        self.assertTrue(os.path.exists('.shelf/shelves/default/04'))
 
524
 
 
525
        # Check ls works
 
526
        text = self.run_bzr('shelf ls', retcode=0)[0]
 
527
        for line in text.split('\n'):
 
528
            self.assertFalse(line.startswith(' 00'))
 
529
 
 
530
        # We now have 01,02,03,04
 
531
        # Unshelve 02
 
532
        self.run_bzr('unshelve --all 02', retcode=0)
 
533
        self.assertEqual(file('test_file3').read(), self.MODIFIED)
 
534
 
 
535
        # Unshelve the default, this is the reshelved 00, hence modifies file 1
 
536
        self.run_bzr('unshelve --all', retcode=0)
 
537
        self.assertEqual(file('test_file1').read(), self.MODIFIED)
 
538
 
 
539
    def test_shelf_switch_basic(self):
 
540
        self.tree = self.make_branch_and_tree('.')
 
541
        self.__create_and_add_test_file()
 
542
 
 
543
        # This should go to "default"
 
544
        file('test_file', 'w').write(self.MODIFIED)
 
545
        self.run_bzr('shelve --all test_file', retcode=0)
 
546
 
 
547
        # Switch to "other"
 
548
        self.run_bzr('shelf switch other', retcode=0)
 
549
        file('test_file', 'w').write(self.MODIFIED)
 
550
        self.run_bzr('shelve --all test_file', retcode=0)
 
551
 
 
552
        # Check it worked
 
553
        self.assertTrue(os.path.exists('.shelf/shelves/default/00'))
 
554
        self.assertFalse(os.path.exists('.shelf/shelves/default/01'))
 
555
        self.assertTrue(os.path.exists('.shelf/shelves/other/00'))
 
556
 
 
557
        # Switch back
 
558
        self.run_bzr('shelf switch default', retcode=0)
 
559
        file('test_file', 'w').write(self.MODIFIED)
 
560
        self.run_bzr('shelve --all test_file', retcode=0)
 
561
 
 
562
        # Check that worked
 
563
        self.assertTrue(os.path.exists('.shelf/shelves/default/01'))
 
564
        self.assertFalse(os.path.exists('.shelf/shelves/other/01'))
 
565
 
 
566
    def test_shelf_binary(self):
 
567
        self.tree = self.make_branch_and_tree('.')
 
568
        self.build_tree_contents([('file', '\x00')])
 
569
        self.tree.add('file')
 
570
        self.run_bzr_error(['Changes involve binary files.'], 'shelve --all')
 
571
 
 
572
    def test_shelf_bad_patch_arg(self):
 
573
        self.tree = self.make_branch_and_tree('.')
 
574
 
 
575
        # Check the bad arg handling
 
576
        stdout, error = self.run_bzr('unshelve 01', retcode=3)
 
577
        self.assertTrue("Patch '01' doesn't exist on shelf" in error)
 
578
 
 
579
        stdout, error = self.run_bzr('unshelve foo', retcode=3)
 
580
        self.assertTrue("Invalid patch name 'foo'" in error)
 
581
 
 
582
        # Hex and is cracky, so it shouldn't work
 
583
        stdout, error = self.run_bzr(['unshelve', '0x00'], retcode=3)
 
584
        self.assertTrue("Invalid patch name '0x00'" in error)
 
585
 
 
586
    def test_color_hunk_selector(self):
 
587
        """Make sure NoColor is raised iff terminal.has_ansi_colors is False"""
 
588
        hs = ShelveHunkSelector([])
 
589
        hs = UnshelveHunkSelector([])
 
590
        try:
 
591
            from bzrlib.plugins.bzrtools import terminal
 
592
        except ImportError:
 
593
            from bzrtools import terminal
 
594
        old_has_ansi_colors = terminal.has_ansi_colors
 
595
        terminal.has_ansi_colors = lambda: False
 
596
        try:
 
597
            self.assertRaises(NoColor, ShelveHunkSelector, [], True)
 
598
            self.assertRaises(NoColor, UnshelveHunkSelector, [], True)
 
599
            terminal.has_ansi_colors = lambda: True
 
600
            hs = ShelveHunkSelector([], color=True)
 
601
            hs = UnshelveHunkSelector([], color=True)
 
602
        finally:
 
603
            terminal.has_ansi_colors = old_has_ansi_colors
 
604
 
 
605
    def test_no_color(self):
 
606
        """Ensure --no-color switch can be passed"""
 
607
        self.tree = self.make_branch_and_tree('.')
 
608
 
 
609
        subdir = 'subdir'
 
610
        os.mkdir(subdir)
 
611
        self.tree.add(subdir)
 
612
        os.chdir(subdir)
 
613
 
 
614
        self.__create_and_add_test_file()
 
615
 
 
616
        # Modify the test file
 
617
        # write in binary mode because on win32 line-endings should be LF
 
618
        f = file('test_file', 'wb')
 
619
        f.write(self.MODIFIED)
 
620
        f.close()
 
621
        stdout, error = self.run_bzr('shelve --all --no-color')
 
622
        stdout, error = self.run_bzr('unshelve --all --no-color')
 
623
 
 
624
    def test_shelf_help(self):
 
625
        self.assertContainsRe(cmd_shelf().help(),
 
626
                              'list\n.*List the patches on the current shelf')
 
627
 
 
628
    def test_show_empty_shelf(self):
 
629
        self.tree = self.make_branch_and_tree('.')
 
630
        self.run_bzr_error(('No patches on shelf.',), 'shelf show')