~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to tests/shelf_tests.py

  • Committer: Aaron Bentley
  • Date: 2006-03-18 23:29:32 UTC
  • Revision ID: aaron.bentley@utoronto.ca-20060318232932-4981a8efa6e028c9
Fixed patch on checkouts

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