~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to tests/shelf_tests.py

  • Committer: Aaron Bentley
  • Date: 2006-11-23 00:05:45 UTC
  • mfrom: (0.8.7 show_paths)
  • Revision ID: aaron.bentley@utoronto.ca-20061123000545-vk3id8fxuzzclvsh
Add show-paths command from Alexander Belchenko

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
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
 
6
19
class ShelfTests(bzrlib.tests.TestCaseWithTransport):
7
20
    ORIGINAL = '\n\nhello test world\n\n'
8
21
    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
 
22
    DIFF_HEADER = "=== modified file '%(filename)s'\n"
 
23
    DIFF_1 = """--- %(filename)s\t%(old_date)s
 
24
+++ %(filename)s\t%(new_date)s
12
25
@@ -1,4 +1,4 @@
13
26
 
14
27
 
16
29
+goodbye test world
17
30
 
18
31
"""
19
 
    DIFF_2 = """--- a/test_file\t
20
 
+++ b/test_file\t
 
32
    DIFF_2 = """--- test_file\t%(old_date)s
 
33
+++ test_file\t%(new_date)s
21
34
@@ -1,4 +1,4 @@
22
35
 
23
36
 
25
38
+hello test world
26
39
 
27
40
"""
 
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
 
28
68
    def test_shelf(self):
29
69
        self.__test_loop(1)
30
70
 
32
72
        self.__test_loop(10)
33
73
 
34
74
    def __test_loop(self, count):
35
 
        tree = self.make_branch_and_tree('.')
36
 
        self.__create_and_add_test_file(tree)
 
75
        self.tree = self.make_branch_and_tree('.')
 
76
        self.__create_and_add_test_file()
37
77
 
38
78
        while count > 0:
39
79
            count -= 1
40
80
 
41
81
            # Modify the test file
42
 
            file('test_file', 'w').write(self.MODIFIED)
 
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()
43
86
 
44
 
            # Check the diff is right
45
 
            self.assertEqual(self.capture('diff', retcode=1),
46
 
                self.DIFF_HEADER + self.DIFF_1 + '\n')
 
87
            self._check_diff()
 
88
            
 
89
            new_date = _patch_header_date(self.tree, 
 
90
                self.tree.inventory.path2id('test_file'), 'test_file')
47
91
 
48
92
            # Shelve the changes
49
93
            self.run_bzr('shelve', '--all', retcode=0)
54
98
            # Make sure the file is actually back the way it was
55
99
            self.assertEqual(file('test_file').read(), self.ORIGINAL)
56
100
 
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)
 
101
            self._check_shelf('00', new_date=new_date)
62
102
 
63
103
            # Unshelve
64
104
            self.run_bzr('unshelve', '--all', retcode=0)
65
105
 
66
 
            # Check the diff is right again
67
 
            self.assertEqual(self.capture('diff', retcode=1),
68
 
                self.DIFF_HEADER + self.DIFF_1 + '\n')
 
106
            self._check_diff()
69
107
 
70
108
            # 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)
 
109
            self._check_shelf('00~', new_date=new_date)
75
110
 
76
111
            # Make sure the file is back the way it should be
77
112
            self.assertEqual(file('test_file').read(), self.MODIFIED)
78
113
 
79
114
    def test_shelf_nothing_to_shelve(self):
80
115
        import os.path
81
 
        tree = self.make_branch_and_tree('.')
82
 
        self.__create_and_add_test_file(tree)
 
116
        self.tree = self.make_branch_and_tree('.')
 
117
        self.__create_and_add_test_file()
83
118
 
84
119
        # Shelve the changes
85
120
        self.run_bzr('shelve', '--all', retcode=3)
86
121
 
87
 
        if os.path.exists(os.path.join(tree.branch.base,
 
122
        if os.path.exists(os.path.join(self.tree.branch.base,
88
123
                '.shelf/shelves/default/00')):
89
124
            self.fail("Shelf exists, but it shouldn't")
90
125
 
91
 
    def __create_and_add_test_file(self, tree, filename='test_file'):
92
 
        f = open(filename, 'w')
 
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')
93
129
        f.write(self.ORIGINAL)
94
130
        f.close()
95
 
        tree.add(tree.relpath(os.path.join(os.getcwd(), filename)))
96
 
        tree.commit(message='add %s' % filename)
 
131
        self.tree.add(self.tree.relpath(os.path.join(os.getcwd(), filename)))
 
132
        self.tree.commit(message='add %s' % filename)
97
133
 
98
134
    def test_shelf_with_revision(self):
99
 
        tree = self.make_branch_and_tree('.')
 
135
        self.tree = self.make_branch_and_tree('.')
100
136
 
101
 
        self.__create_and_add_test_file(tree)
 
137
        self.__create_and_add_test_file()
102
138
 
103
139
        # Modify the test file and commit it
104
140
        self.build_tree_contents([('test_file', self.MODIFIED)])
105
 
        tree.commit(message='update test_file')
 
141
        self.tree.commit(message='update test_file')
106
142
 
107
143
        # Shelve the changes
108
144
        self.run_bzr('shelve', '--all', '-r', '1', retcode=0)
109
145
 
110
 
        # Check the diff is right
111
 
        self.assertEqual(self.capture('diff', retcode=1),
112
 
            self.DIFF_HEADER + self.DIFF_2 + '\n')
 
146
        self._check_diff(self.DIFF_2)
113
147
 
114
148
        # Make sure the file is the way it should be
115
149
        self.assertEqual(file('test_file').read(), self.ORIGINAL)
121
155
        self.assertEqual(file('test_file').read(), self.MODIFIED)
122
156
 
123
157
    def test_shelf_with_two_revisions(self):
124
 
        tree = self.make_branch_and_tree('.')
 
158
        self.tree = self.make_branch_and_tree('.')
125
159
 
126
160
        cmd = 'shelve --all -r 1..2'
127
161
        (stdout, stderr) = self.run_bzr_captured(cmd.split(), retcode=None)
130
164
            'bzr: ERROR: shelve only accepts a single revision parameter.')
131
165
 
132
166
    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')
 
167
        self.tree = self.make_branch_and_tree('.')
 
168
        self.__create_and_add_test_file()
 
169
        self.__test_show(self.tree, '00')
136
170
 
137
171
    def __test_show(self, tree, patch):
138
172
        # Modify the test file
145
179
        self.assertEqual(self.capture('diff', retcode=0), '')
146
180
 
147
181
        # Check the shelf is right
148
 
        shelf = open(os.path.join(tree.branch.base,
 
182
        shelf = open(os.path.join(self.tree.basedir,
149
183
                    '.shelf/shelves/default', patch)).read()
150
184
        self.assertTrue('patch %s' % patch in shelf)
151
185
 
154
188
        self.assertEqual(shown, shelf)
155
189
 
156
190
    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')
 
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')
162
196
 
163
197
        # Now check we can show a previously shelved patch
164
 
        shelf = open(os.path.join(tree.branch.base,
 
198
        shelf = open(os.path.join(self.tree.basedir,
165
199
                    '.shelf/shelves/default/00')).read()
166
200
        self.assertTrue('patch 00' in shelf)
167
201
 
169
203
        shown = self.capture('shelf show 00', retcode=0)
170
204
        self.assertEqual(shown, shelf)
171
205
 
 
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
 
172
222
    def test_shelf_show_with_no_patch(self):
173
 
        tree = self.make_branch_and_tree('.')
 
223
        self.tree = self.make_branch_and_tree('.')
174
224
        stderr = self.run_bzr_captured(['shelf', 'show', '00'], retcode=None)[1]
175
225
        self.assertTrue("Patch '00' doesn't exist on shelf default!" in stderr)
176
226
 
177
227
    def test_shelf_unshelve_failure(self):
178
 
        tree = self.make_branch_and_tree('.')
 
228
        self.tree = self.make_branch_and_tree('.')
179
229
 
180
 
        self.__create_and_add_test_file(tree)
 
230
        self.__create_and_add_test_file()
181
231
 
182
232
        # Modify the test file
183
233
        file('test_file', 'w').write(self.MODIFIED)
186
236
        self.run_bzr('shelve', '--all', retcode=0)
187
237
 
188
238
        # Write an unapplyable patch into the shelf
189
 
        shelf = open(os.path.join(tree.branch.base,
 
239
        shelf = open(os.path.join(self.tree.basedir,
190
240
                    '.shelf/shelves/default/00'), 'w')
191
241
        shelf.write(self.DIFF_2)
192
242
        shelf.close()
195
245
        self.run_bzr('unshelve', '--all', retcode=3)
196
246
 
197
247
        # Make sure the patch is still there, eventhough it's broken
198
 
        shelf = open(os.path.join(tree.branch.base,
 
248
        shelf = open(os.path.join(self.tree.basedir,
199
249
                    '.shelf/shelves/default/00')).read()
200
250
        self.assertEqual(shelf, self.DIFF_2)
201
251
 
204
254
        self.assertEqual(diff, '')
205
255
 
206
256
    def test_shelf_unshelve_failure_two_hunks(self):
207
 
        tree = self.make_branch_and_tree('.')
 
257
        self.tree = self.make_branch_and_tree('.')
208
258
 
209
 
        self.__create_and_add_test_file(tree)
210
 
        self.__create_and_add_test_file(tree, filename='test_file2')
 
259
        self.__create_and_add_test_file()
 
260
        self.__create_and_add_test_file(filename='test_file2')
211
261
 
212
262
        # Modify the test files
213
263
        file('test_file', 'w').write(self.MODIFIED)
218
268
 
219
269
        # Put the changes to test_file back, the shelved patch won't apply now
220
270
        file('test_file', 'w').write(self.MODIFIED)
221
 
        tree.commit(message='screw up test_file')
 
271
        self.tree.commit(message='screw up test_file')
222
272
 
223
273
        # Unshelve, should fail
224
274
        self.run_bzr('unshelve', '--all', retcode=3)
233
283
        self.assertTrue(os.path.exists('.shelf/shelves/default/00'))
234
284
 
235
285
    def test_shelf_after_unshelve(self):
236
 
        tree = self.make_branch_and_tree('.')
 
286
        self.tree = self.make_branch_and_tree('.')
237
287
 
238
 
        self.__create_and_add_test_file(tree)
239
 
        self.__create_and_add_test_file(tree, filename='test_file2')
 
288
        self.__create_and_add_test_file()
 
289
        self.__create_and_add_test_file(filename='test_file2')
240
290
 
241
291
        # Modify the test files
242
292
        file('test_file', 'w').write(self.MODIFIED)
254
304
        self.assertTrue(os.path.exists('.shelf/shelves/default/01~'))
255
305
 
256
306
        # Check ls works
257
 
        list = self.capture('shelf ls', retcode=0).split('\n')
258
 
        for line in list:
 
307
        lines = self.capture('shelf ls', retcode=0).split('\n')
 
308
        for line in lines:
259
309
            self.assertFalse(line.startswith(' 01'))
260
310
 
261
311
        # Unshelve, if unshelve is confused by the backup it will fail
262
312
        self.run_bzr('unshelve', '--all', retcode=0)
263
313
 
264
314
    def test_shelf_delete(self):
265
 
        tree = self.make_branch_and_tree('.')
 
315
        self.tree = self.make_branch_and_tree('.')
266
316
 
267
 
        self.__create_and_add_test_file(tree)
268
 
        self.__create_and_add_test_file(tree, filename='test_file2')
 
317
        self.__create_and_add_test_file()
 
318
        self.__create_and_add_test_file(filename='test_file2')
269
319
 
270
320
        # Modify the test files
271
 
        file('test_file', 'w').write(self.MODIFIED)
272
 
        file('test_file2', 'w').write(self.MODIFIED)
 
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')
273
330
 
274
331
        # Shelve the changes
275
332
        self.run_bzr('shelve', '--all', 'test_file', retcode=0)
276
333
        self.run_bzr('shelve', '--all', 'test_file2', retcode=0)
277
334
 
 
335
        self._check_shelf('00', new_date=new_date)
 
336
 
278
337
        # Delete 00
279
338
        self.run_bzr('shelf', 'delete', '00', retcode=0)
280
339
 
281
 
        # We should now have 01 but not 00
 
340
        # We should now have 01 but not 00, but we should have 00~
282
341
        self.assertFalse(os.path.exists('.shelf/shelves/default/00'))
 
342
        self.assertTrue(os.path.exists('.shelf/shelves/default/00~'))
283
343
        self.assertTrue(os.path.exists('.shelf/shelves/default/01'))
284
344
 
 
345
        # Check the backup is right
 
346
        self._check_shelf('00~', new_date=new_date)
 
347
 
285
348
        # Check ls works
286
 
        list = self.capture('shelf ls', retcode=0).split('\n')
287
 
        for line in list:
 
349
        lines = self.capture('shelf ls', retcode=0).split('\n')
 
350
        for line in lines:
288
351
            self.assertFalse(line.startswith(' 00'))
289
352
 
290
353
        # Unshelve should unshelve 01
292
355
        self.assertEqual(file('test_file2').read(), self.MODIFIED)
293
356
 
294
357
    def test_shelf_gaps(self):
295
 
        tree = self.make_branch_and_tree('.')
296
 
        self.__create_and_add_test_file(tree)
 
358
        self.tree = self.make_branch_and_tree('.')
 
359
        self.__create_and_add_test_file()
297
360
        file('test_file', 'w').write(self.MODIFIED)
298
361
        self.run_bzr('shelve', '--all', 'test_file', retcode=0)
299
362
        file('test_file', 'w').write(self.MODIFIED)
309
372
        self.assertTrue(os.path.exists('.shelf/shelves/default/02'))
310
373
 
311
374
    def test_shelf_upgrade(self):
312
 
        tree = self.make_branch_and_tree('.')
 
375
        self.tree = self.make_branch_and_tree('.')
313
376
 
314
 
        self.__create_and_add_test_file(tree)
 
377
        self.__create_and_add_test_file()
315
378
 
316
379
        # Modify then shelve, so we're not upgrading to 00, just for kicks
317
380
        file('test_file', 'w').write(self.MODIFIED)
349
412
        # Shelve should work now
350
413
        self.run_bzr('shelve', '--all', retcode=0)
351
414
 
352
 
    def test_shelf_p0_patch(self):
353
 
        tree = self.make_branch_and_tree('.')
 
415
    def test_shelf_p1_patch(self):
 
416
        self.tree = self.make_branch_and_tree('.')
354
417
 
355
 
        self.__create_and_add_test_file(tree)
 
418
        self.__create_and_add_test_file()
356
419
 
357
420
        # Run a benign shelf command to setup .shelf for us
358
421
        self.run_bzr('shelf', 'ls', retcode=0)
359
422
 
 
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')
360
430
        # Fake a -p0 shelved patch
361
 
        diff = self.DIFF_1
362
 
        diff = diff.replace('a/', '')
363
 
        diff = diff.replace('b/', '')
 
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/')
364
435
        open('.shelf/shelves/default/00', 'w').write(diff)
365
436
 
366
437
        # This should work
367
438
        self.run_bzr('unshelve', '--all', retcode=0)
368
439
 
369
 
        # Check the diff is right
370
 
        self.assertEqual(self.capture('diff', retcode=1),
371
 
            self.DIFF_HEADER + self.DIFF_1 + '\n')
 
440
        self._check_diff()
372
441
 
373
442
    def test_shelf_shelve_in_subdir(self):
374
 
        tree = self.make_branch_and_tree('.')
375
 
 
376
 
        self.__create_and_add_test_file(tree)
 
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()
377
451
 
378
452
        # Modify the test file
379
 
        file('test_file', 'w').write(self.MODIFIED)
 
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()
380
457
 
381
458
        # Shelve the changes
382
459
        self.run_bzr('shelve', '--all', retcode=0)
388
465
        # Unshelve, should succeed
389
466
        self.run_bzr('unshelve', '--all', retcode=0)
390
467
 
391
 
        # Check the diff is right
392
 
        self.assertEqual(self.capture('diff', retcode=1),
393
 
            self.DIFF_HEADER + self.DIFF_1 + '\n')
 
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'])