~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to tests/shelf_tests.py

  • Committer: Robert Collins
  • Date: 2006-04-24 01:35:08 UTC
  • mto: (364.1.3 bzrtools)
  • mto: This revision was merged to the branch mainline in revision 366.
  • Revision ID: robertc@robertcollins.net-20060424013508-2d37e47814d9493e
Support importing into empty branches.

Show diffs side-by-side

added added

removed removed

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