~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to tests/shelf_tests.py

  • Committer: Aaron Bentley
  • Date: 2013-08-20 03:02:43 UTC
  • Revision ID: aaron@aaronbentley.com-20130820030243-r8v1xfbcnd8f10p4
Fix zap command for 2.6/7

Show diffs side-by-side

added added

removed removed

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