~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to tests/shelf_tests.py

  • Committer: Aaron Bentley
  • Date: 2006-03-24 19:01:30 UTC
  • Revision ID: abentley@panoramicfeedback.com-20060324190130-2208c693486a8b33
Added apache index scraping to the branches command

Show diffs side-by-side

added added

removed removed

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