~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to tests/shelf_tests.py

  • Committer: Aaron Bentley
  • Date: 2007-03-09 18:25:10 UTC
  • mto: This revision was merged to the branch mainline in revision 520.
  • Revision ID: abentley@panoramicfeedback.com-20070309182510-fkj1j6t119d1cj5q
Handle broken python tar implementations

Show diffs side-by-side

added added

removed removed

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