~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to tests/shelf_tests.py

  • Committer: Aaron Bentley
  • Date: 2006-08-07 03:07:35 UTC
  • mto: This revision was merged to the branch mainline in revision 425.
  • Revision ID: aaron.bentley@utoronto.ca-20060807030735-0a9f8330ce1a836a
Add --no-color option to shelf

Show diffs side-by-side

added added

removed removed

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