~abentley/bzrtools/bzrtools.dev

514 by Aaron Bentley
Fix imports
1
import os.path
0.1.119 by Aaron Bentley
Fix tests for patches with dates
2
from bzrlib.diff import _patch_header_date
291 by Aaron Bentley
Adjusted to selftest -> tests change
3
import bzrlib.tests
0.1.29 by Michael Ellerman
Add basic tests for shelve --all, and unshelve.
4
514 by Aaron Bentley
Fix imports
5
from bzrlib.plugins.bzrtools.hunk_selector import (
6
    ShelveHunkSelector,
7
    UnshelveHunkSelector,
8
    )
9
from bzrlib.plugins.bzrtools.errors import NoColor
531.1.1 by Aaron Bentley
Add test for shelf help, since it's custom
10
from bzrlib.plugins.bzrtools import cmd_shelf
447 by Aaron Bentley
Fix up test and selftest, make robust against missing PyBaz
11
423.1.4 by Aaron Bentley
Add --no-color option to shelf
12
0.2.26 by Michael Ellerman
Update tests for BzrDir changes, use test helpers to save future pain.
13
class ShelfTests(bzrlib.tests.TestCaseWithTransport):
0.1.29 by Michael Ellerman
Add basic tests for shelve --all, and unshelve.
14
    ORIGINAL = '\n\nhello test world\n\n'
15
    MODIFIED = '\n\ngoodbye test world\n\n'
0.1.108 by Michael Ellerman
Update tests for -p0 format diffs by default.
16
    DIFF_HEADER = "=== modified file '%(filename)s'\n"
0.1.119 by Aaron Bentley
Fix tests for patches with dates
17
    DIFF_1 = """--- %(filename)s\t%(old_date)s
18
+++ %(filename)s\t%(new_date)s
0.1.29 by Michael Ellerman
Add basic tests for shelve --all, and unshelve.
19
@@ -1,4 +1,4 @@
531.2.2 by Charlie Shepherd
Remove all trailing whitespace
20
21
0.1.29 by Michael Ellerman
Add basic tests for shelve --all, and unshelve.
22
-hello test world
23
+goodbye test world
531.2.2 by Charlie Shepherd
Remove all trailing whitespace
24
0.1.29 by Michael Ellerman
Add basic tests for shelve --all, and unshelve.
25
"""
0.1.119 by Aaron Bentley
Fix tests for patches with dates
26
    DIFF_2 = """--- test_file\t%(old_date)s
27
+++ test_file\t%(new_date)s
0.1.40 by Michael Ellerman
Update test with revision to actually test the shelf worked properly.
28
@@ -1,4 +1,4 @@
531.2.2 by Charlie Shepherd
Remove all trailing whitespace
29
30
0.1.40 by Michael Ellerman
Update test with revision to actually test the shelf worked properly.
31
-goodbye test world
32
+hello test world
531.2.2 by Charlie Shepherd
Remove all trailing whitespace
33
0.1.40 by Michael Ellerman
Update test with revision to actually test the shelf worked properly.
34
"""
0.1.103 by Michael Ellerman
Add test machinery to cope with subdirectories.
35
    def _check_diff(self, diff=DIFF_1, filename='test_file'):
0.1.119 by Aaron Bentley
Fix tests for patches with dates
36
        old_tree = self.tree.basis_tree()
515.1.2 by Aaron Bentley
Fix all test suite bugs w/ dirstate
37
        old_tree.lock_read()
38
        self.tree.lock_read()
39
        try:
40
            old_date = _patch_header_date(old_tree,
41
                                          old_tree.inventory.path2id(filename),
42
                                          filename)
43
            new_date = _patch_header_date(self.tree,
44
                self.tree.inventory.path2id(filename), filename)
45
        finally:
524 by Aaron Bentley
Fix lock handling in shelf tests
46
            self.tree.unlock()
515.1.2 by Aaron Bentley
Fix all test suite bugs w/ dirstate
47
            old_tree.unlock()
531.2.2 by Charlie Shepherd
Remove all trailing whitespace
48
        keys = { 'filename' : filename , 'old_date': old_date,
0.1.119 by Aaron Bentley
Fix tests for patches with dates
49
                 'new_date': new_date}
0.1.103 by Michael Ellerman
Add test machinery to cope with subdirectories.
50
        hdr  = self.DIFF_HEADER % keys
51
        diff = diff % keys
52
        self.assertEqual(self.capture('diff', retcode=1), hdr + diff + '\n')
0.1.102 by Michael Ellerman
Factor out some common test functionality.
53
0.1.119 by Aaron Bentley
Fix tests for patches with dates
54
    def _check_shelf(self, idx, diff=DIFF_1, filename='test_file',
55
                     new_date=None):
56
        old_tree = self.tree.basis_tree()
515.1.2 by Aaron Bentley
Fix all test suite bugs w/ dirstate
57
        old_tree.lock_read()
58
        try:
59
            old_date = _patch_header_date(old_tree,
60
                                          old_tree.inventory.path2id(filename),
61
                                          filename)
62
        finally:
63
            old_tree.unlock()
0.1.119 by Aaron Bentley
Fix tests for patches with dates
64
        diff = diff % { 'filename' : filename, 'old_date': old_date,
65
                        'new_date': new_date}
66
        shelf = open(os.path.join(self.tree.basedir,
0.1.102 by Michael Ellerman
Factor out some common test functionality.
67
                '.shelf/shelves/default/' + idx)).read()
68
        shelf = shelf[shelf.index('\n') + 1:] # skip the message
69
        self.assertEqual(shelf, diff)
70
0.1.29 by Michael Ellerman
Add basic tests for shelve --all, and unshelve.
71
    def test_shelf(self):
0.1.42 by Michael Ellerman
Add tests for new shelf layout.
72
        self.__test_loop(1)
73
74
    def test_shelf_multi(self):
75
        self.__test_loop(10)
76
77
    def __test_loop(self, count):
0.1.102 by Michael Ellerman
Factor out some common test functionality.
78
        self.tree = self.make_branch_and_tree('.')
79
        self.__create_and_add_test_file()
517.1.7 by Aaron Bentley
Fix shelf tests, release 0.15.4
80
        for counter in range(count):
81
            # Modify the test file
82
            # write in binary mode because on win32 line-endings should be
83
            # LF
84
            f = file('test_file', 'wb')
85
            f.write(self.MODIFIED)
86
            f.close()
87
88
            self._check_diff()
89
90
            self.tree.lock_write()
91
            try:
524 by Aaron Bentley
Fix lock handling in shelf tests
92
                new_date = _patch_header_date(self.tree,
93
                    self.tree.inventory.path2id('test_file'), 'test_file')
517.1.7 by Aaron Bentley
Fix shelf tests, release 0.15.4
94
            finally:
95
                self.tree.unlock()
96
97
            # Shelve the changes
98
            self.run_bzr('shelve', '--all', retcode=0)
99
100
            # Make sure there is no diff anymore
101
            self.assertEqual(self.capture('diff', retcode=0), '')
102
103
            # Make sure the file is actually back the way it was
104
            self.assertEqual(file('test_file').read(), self.ORIGINAL)
105
106
            self._check_shelf('00', new_date=new_date)
107
108
            # Unshelve
109
            self.run_bzr('unshelve', '--all', retcode=0)
110
111
            self._check_diff()
112
113
            # Check the shelved patch was backed up
114
            self._check_shelf('00~', new_date=new_date)
115
116
            # Make sure the file is back the way it should be
117
            self.assertEqual(file('test_file').read(), self.MODIFIED)
0.1.29 by Michael Ellerman
Add basic tests for shelve --all, and unshelve.
118
119
    def test_shelf_nothing_to_shelve(self):
120
        import os.path
0.1.102 by Michael Ellerman
Factor out some common test functionality.
121
        self.tree = self.make_branch_and_tree('.')
122
        self.__create_and_add_test_file()
0.1.29 by Michael Ellerman
Add basic tests for shelve --all, and unshelve.
123
124
        # Shelve the changes
0.1.80 by Michael Ellerman
After extensive user testing, ie. me using it, I've decided --pick should be
125
        self.run_bzr('shelve', '--all', retcode=3)
0.1.29 by Michael Ellerman
Add basic tests for shelve --all, and unshelve.
126
0.1.102 by Michael Ellerman
Factor out some common test functionality.
127
        if os.path.exists(os.path.join(self.tree.branch.base,
0.2.26 by Michael Ellerman
Update tests for BzrDir changes, use test helpers to save future pain.
128
                '.shelf/shelves/default/00')):
0.1.29 by Michael Ellerman
Add basic tests for shelve --all, and unshelve.
129
            self.fail("Shelf exists, but it shouldn't")
0.1.37 by Michael Ellerman
Add (failing) tests of revision argument for shelve.
130
0.1.102 by Michael Ellerman
Factor out some common test functionality.
131
    def __create_and_add_test_file(self, filename='test_file'):
0.6.2 by Alexander Belchenko
Shelf selftest win32-fixes: write all files in binary mode,
132
        # write in binary mode because on win32 line-endings should be LF
133
        f = open(filename, 'wb')
0.1.98 by Michael Ellerman
Add a test for shelving in a subdirectory
134
        f.write(self.ORIGINAL)
135
        f.close()
0.1.102 by Michael Ellerman
Factor out some common test functionality.
136
        self.tree.add(self.tree.relpath(os.path.join(os.getcwd(), filename)))
137
        self.tree.commit(message='add %s' % filename)
0.2.26 by Michael Ellerman
Update tests for BzrDir changes, use test helpers to save future pain.
138
0.1.37 by Michael Ellerman
Add (failing) tests of revision argument for shelve.
139
    def test_shelf_with_revision(self):
0.1.102 by Michael Ellerman
Factor out some common test functionality.
140
        self.tree = self.make_branch_and_tree('.')
0.1.37 by Michael Ellerman
Add (failing) tests of revision argument for shelve.
141
0.1.102 by Michael Ellerman
Factor out some common test functionality.
142
        self.__create_and_add_test_file()
0.1.37 by Michael Ellerman
Add (failing) tests of revision argument for shelve.
143
144
        # Modify the test file and commit it
0.2.26 by Michael Ellerman
Update tests for BzrDir changes, use test helpers to save future pain.
145
        self.build_tree_contents([('test_file', self.MODIFIED)])
0.1.102 by Michael Ellerman
Factor out some common test functionality.
146
        self.tree.commit(message='update test_file')
0.1.37 by Michael Ellerman
Add (failing) tests of revision argument for shelve.
147
148
        # Shelve the changes
0.1.80 by Michael Ellerman
After extensive user testing, ie. me using it, I've decided --pick should be
149
        self.run_bzr('shelve', '--all', '-r', '1', retcode=0)
0.1.37 by Michael Ellerman
Add (failing) tests of revision argument for shelve.
150
0.1.102 by Michael Ellerman
Factor out some common test functionality.
151
        self._check_diff(self.DIFF_2)
0.1.40 by Michael Ellerman
Update test with revision to actually test the shelf worked properly.
152
153
        # Make sure the file is the way it should be
154
        self.assertEqual(file('test_file').read(), self.ORIGINAL)
155
156
        # Unshelve
0.1.80 by Michael Ellerman
After extensive user testing, ie. me using it, I've decided --pick should be
157
        self.run_bzr('unshelve', '--all', retcode=0)
0.1.40 by Michael Ellerman
Update test with revision to actually test the shelf worked properly.
158
159
        # Make sure the file is back the way it should be
160
        self.assertEqual(file('test_file').read(), self.MODIFIED)
161
0.1.37 by Michael Ellerman
Add (failing) tests of revision argument for shelve.
162
    def test_shelf_with_two_revisions(self):
0.1.102 by Michael Ellerman
Factor out some common test functionality.
163
        self.tree = self.make_branch_and_tree('.')
0.1.37 by Michael Ellerman
Add (failing) tests of revision argument for shelve.
164
0.1.80 by Michael Ellerman
After extensive user testing, ie. me using it, I've decided --pick should be
165
        cmd = 'shelve --all -r 1..2'
0.1.37 by Michael Ellerman
Add (failing) tests of revision argument for shelve.
166
        (stdout, stderr) = self.run_bzr_captured(cmd.split(), retcode=None)
167
168
        self.assertEqual(stderr.split('\n')[0],
295 by Aaron Bentley
Fixed test case failure
169
            'bzr: ERROR: shelve only accepts a single revision parameter.')
281 by Aaron Bentley
Handled whitespace branch names better
170
0.2.27 by Michael Ellerman
Add basic test for shelf show
171
    def test_shelf_show_basic(self):
0.1.102 by Michael Ellerman
Factor out some common test functionality.
172
        self.tree = self.make_branch_and_tree('.')
173
        self.__create_and_add_test_file()
174
        self.__test_show(self.tree, '00')
0.2.27 by Michael Ellerman
Add basic test for shelf show
175
0.2.28 by Michael Ellerman
More tests of shelf show
176
    def __test_show(self, tree, patch):
0.2.27 by Michael Ellerman
Add basic test for shelf show
177
        # Modify the test file
0.2.28 by Michael Ellerman
More tests of shelf show
178
        self.build_tree_contents([('test_file', 'patch %s\n' % patch)])
0.2.27 by Michael Ellerman
Add basic test for shelf show
179
180
        # Shelve the changes
0.1.80 by Michael Ellerman
After extensive user testing, ie. me using it, I've decided --pick should be
181
        self.run_bzr('shelve', '--all', retcode=0)
0.2.27 by Michael Ellerman
Add basic test for shelf show
182
183
        # Make sure there is no diff anymore
184
        self.assertEqual(self.capture('diff', retcode=0), '')
185
186
        # Check the shelf is right
0.1.119 by Aaron Bentley
Fix tests for patches with dates
187
        shelf = open(os.path.join(self.tree.basedir,
0.2.28 by Michael Ellerman
More tests of shelf show
188
                    '.shelf/shelves/default', patch)).read()
189
        self.assertTrue('patch %s' % patch in shelf)
190
191
        # Check the shown output is right
192
        shown = self.capture('shelf show %s' % patch, retcode=0)
193
        self.assertEqual(shown, shelf)
194
195
    def test_shelf_show_multi(self):
0.1.102 by Michael Ellerman
Factor out some common test functionality.
196
        self.tree = self.make_branch_and_tree('.')
197
        self.__create_and_add_test_file()
198
        self.__test_show(self.tree, '00')
199
        self.__test_show(self.tree, '01')
200
        self.__test_show(self.tree, '02')
0.2.28 by Michael Ellerman
More tests of shelf show
201
202
        # Now check we can show a previously shelved patch
0.1.119 by Aaron Bentley
Fix tests for patches with dates
203
        shelf = open(os.path.join(self.tree.basedir,
0.2.27 by Michael Ellerman
Add basic test for shelf show
204
                    '.shelf/shelves/default/00')).read()
0.2.28 by Michael Ellerman
More tests of shelf show
205
        self.assertTrue('patch 00' in shelf)
0.2.27 by Michael Ellerman
Add basic test for shelf show
206
207
        # Check the shown output is right
208
        shown = self.capture('shelf show 00', retcode=0)
209
        self.assertEqual(shown, shelf)
0.2.30 by Michael Ellerman
Test for shelf show with no patch to show.
210
0.1.110 by Michael Ellerman
Make the patch argument to 'shelf show' optional.
211
    def test_shelf_show_unspecified(self):
212
        self.tree = self.make_branch_and_tree('.')
213
        self.__create_and_add_test_file()
214
        self.__test_show(self.tree, '00')
215
        self.__test_show(self.tree, '01')
216
        self.__test_show(self.tree, '02')
217
218
        # Check that not specifying at patch gets us the most recent
0.1.119 by Aaron Bentley
Fix tests for patches with dates
219
        shelf = open(os.path.join(self.tree.basedir,
0.1.110 by Michael Ellerman
Make the patch argument to 'shelf show' optional.
220
                    '.shelf/shelves/default/02')).read()
221
        self.assertTrue('patch 02' in shelf)
222
223
        # Check the shown output is right
224
        shown = self.capture('shelf show', retcode=0)
225
        self.assertEqual(shown, shelf)
226
0.2.30 by Michael Ellerman
Test for shelf show with no patch to show.
227
    def test_shelf_show_with_no_patch(self):
0.1.102 by Michael Ellerman
Factor out some common test functionality.
228
        self.tree = self.make_branch_and_tree('.')
0.2.30 by Michael Ellerman
Test for shelf show with no patch to show.
229
        stderr = self.run_bzr_captured(['shelf', 'show', '00'], retcode=None)[1]
230
        self.assertTrue("Patch '00' doesn't exist on shelf default!" in stderr)
231
0.1.76 by Michael Ellerman
Add a test to make sure we don't delete the shelved patch if unshelving
232
    def test_shelf_unshelve_failure(self):
0.1.102 by Michael Ellerman
Factor out some common test functionality.
233
        self.tree = self.make_branch_and_tree('.')
0.1.76 by Michael Ellerman
Add a test to make sure we don't delete the shelved patch if unshelving
234
0.1.102 by Michael Ellerman
Factor out some common test functionality.
235
        self.__create_and_add_test_file()
0.1.76 by Michael Ellerman
Add a test to make sure we don't delete the shelved patch if unshelving
236
237
        # Modify the test file
238
        file('test_file', 'w').write(self.MODIFIED)
239
240
        # Shelve the changes
0.1.80 by Michael Ellerman
After extensive user testing, ie. me using it, I've decided --pick should be
241
        self.run_bzr('shelve', '--all', retcode=0)
0.1.76 by Michael Ellerman
Add a test to make sure we don't delete the shelved patch if unshelving
242
243
        # Write an unapplyable patch into the shelf
0.1.119 by Aaron Bentley
Fix tests for patches with dates
244
        shelf = open(os.path.join(self.tree.basedir,
0.1.76 by Michael Ellerman
Add a test to make sure we don't delete the shelved patch if unshelving
245
                    '.shelf/shelves/default/00'), 'w')
246
        shelf.write(self.DIFF_2)
247
        shelf.close()
248
249
        # Unshelve, should fail
0.1.80 by Michael Ellerman
After extensive user testing, ie. me using it, I've decided --pick should be
250
        self.run_bzr('unshelve', '--all', retcode=3)
0.1.76 by Michael Ellerman
Add a test to make sure we don't delete the shelved patch if unshelving
251
252
        # Make sure the patch is still there, eventhough it's broken
0.1.119 by Aaron Bentley
Fix tests for patches with dates
253
        shelf = open(os.path.join(self.tree.basedir,
0.1.76 by Michael Ellerman
Add a test to make sure we don't delete the shelved patch if unshelving
254
                    '.shelf/shelves/default/00')).read()
255
        self.assertEqual(shelf, self.DIFF_2)
0.1.77 by Michael Ellerman
When unshelving, try to patch with --dry-run first, if that fails bail out.
256
257
        # Working tree should be unchanged
258
        diff = self.capture('diff', retcode=0)
259
        self.assertEqual(diff, '')
260
261
    def test_shelf_unshelve_failure_two_hunks(self):
0.1.102 by Michael Ellerman
Factor out some common test functionality.
262
        self.tree = self.make_branch_and_tree('.')
0.1.77 by Michael Ellerman
When unshelving, try to patch with --dry-run first, if that fails bail out.
263
0.1.102 by Michael Ellerman
Factor out some common test functionality.
264
        self.__create_and_add_test_file()
265
        self.__create_and_add_test_file(filename='test_file2')
0.1.77 by Michael Ellerman
When unshelving, try to patch with --dry-run first, if that fails bail out.
266
267
        # Modify the test files
268
        file('test_file', 'w').write(self.MODIFIED)
269
        file('test_file2', 'w').write(self.MODIFIED)
270
271
        # Shelve the changes
0.1.80 by Michael Ellerman
After extensive user testing, ie. me using it, I've decided --pick should be
272
        self.run_bzr('shelve', '--all', retcode=0)
0.1.77 by Michael Ellerman
When unshelving, try to patch with --dry-run first, if that fails bail out.
273
274
        # Put the changes to test_file back, the shelved patch won't apply now
275
        file('test_file', 'w').write(self.MODIFIED)
0.1.102 by Michael Ellerman
Factor out some common test functionality.
276
        self.tree.commit(message='screw up test_file')
0.1.77 by Michael Ellerman
When unshelving, try to patch with --dry-run first, if that fails bail out.
277
278
        # Unshelve, should fail
0.1.80 by Michael Ellerman
After extensive user testing, ie. me using it, I've decided --pick should be
279
        self.run_bzr('unshelve', '--all', retcode=3)
0.1.77 by Michael Ellerman
When unshelving, try to patch with --dry-run first, if that fails bail out.
280
281
        # Working tree should be unchanged
282
        diff = self.capture('diff', retcode=0)
283
        self.assertEqual(diff, '')
0.1.84 by Michael Ellerman
Backup the patch when we unshelve. Suggested by Christian Reis.
284
0.1.91 by Michael Ellerman
Add --force option to unshelve, which runs the shelved changes through
285
        # Force should succeed and modify test_file2, but leave shelf
286
        self.run_bzr('unshelve', '--force', '--all', retcode=0)
287
        self.assertEqual(open('test_file2').read(), self.MODIFIED)
288
        self.assertTrue(os.path.exists('.shelf/shelves/default/00'))
289
0.1.84 by Michael Ellerman
Backup the patch when we unshelve. Suggested by Christian Reis.
290
    def test_shelf_after_unshelve(self):
0.1.102 by Michael Ellerman
Factor out some common test functionality.
291
        self.tree = self.make_branch_and_tree('.')
0.1.84 by Michael Ellerman
Backup the patch when we unshelve. Suggested by Christian Reis.
292
0.1.102 by Michael Ellerman
Factor out some common test functionality.
293
        self.__create_and_add_test_file()
294
        self.__create_and_add_test_file(filename='test_file2')
0.1.84 by Michael Ellerman
Backup the patch when we unshelve. Suggested by Christian Reis.
295
296
        # Modify the test files
297
        file('test_file', 'w').write(self.MODIFIED)
298
        file('test_file2', 'w').write(self.MODIFIED)
299
300
        # Shelve the changes
301
        self.run_bzr('shelve', '--all', 'test_file', retcode=0)
302
        self.run_bzr('shelve', '--all', 'test_file2', retcode=0)
303
304
        # Unshelve
305
        self.run_bzr('unshelve', '--all', retcode=0)
306
307
        # We should now have 00 and 01~
308
        self.assertTrue(os.path.exists('.shelf/shelves/default/00'))
309
        self.assertTrue(os.path.exists('.shelf/shelves/default/01~'))
310
311
        # Check ls works
0.1.114 by Michael Ellerman
Internals, don't shadow list in tests.py
312
        lines = self.capture('shelf ls', retcode=0).split('\n')
313
        for line in lines:
0.1.84 by Michael Ellerman
Backup the patch when we unshelve. Suggested by Christian Reis.
314
            self.assertFalse(line.startswith(' 01'))
315
316
        # Unshelve, if unshelve is confused by the backup it will fail
317
        self.run_bzr('unshelve', '--all', retcode=0)
0.1.85 by Michael Ellerman
Add a test for delete
318
319
    def test_shelf_delete(self):
0.1.102 by Michael Ellerman
Factor out some common test functionality.
320
        self.tree = self.make_branch_and_tree('.')
515.1.2 by Aaron Bentley
Fix all test suite bugs w/ dirstate
321
        self.tree.lock_write()
322
        try:
323
            self.__create_and_add_test_file()
324
            self.__create_and_add_test_file(filename='test_file2')
325
326
            # Modify the test files
327
            # write in binary mode because on win32 line-endings should be LF
328
            f = file('test_file', 'wb')
329
            f.write(self.MODIFIED)
330
            f.close()
331
            f = file('test_file2', 'wb')
332
            f.write(self.MODIFIED)
333
            f.close()
334
            new_date = _patch_header_date(self.tree,
335
                self.tree.inventory.path2id('test_file'), 'test_file')
336
        finally:
337
            self.tree.unlock()
0.1.85 by Michael Ellerman
Add a test for delete
338
339
        # Shelve the changes
340
        self.run_bzr('shelve', '--all', 'test_file', retcode=0)
341
        self.run_bzr('shelve', '--all', 'test_file2', retcode=0)
342
0.1.119 by Aaron Bentley
Fix tests for patches with dates
343
        self._check_shelf('00', new_date=new_date)
0.1.100 by Michael Ellerman
Backup the shelved patch when we delete in "shelf delete".
344
0.1.85 by Michael Ellerman
Add a test for delete
345
        # Delete 00
346
        self.run_bzr('shelf', 'delete', '00', retcode=0)
347
0.1.100 by Michael Ellerman
Backup the shelved patch when we delete in "shelf delete".
348
        # We should now have 01 but not 00, but we should have 00~
0.1.85 by Michael Ellerman
Add a test for delete
349
        self.assertFalse(os.path.exists('.shelf/shelves/default/00'))
0.1.100 by Michael Ellerman
Backup the shelved patch when we delete in "shelf delete".
350
        self.assertTrue(os.path.exists('.shelf/shelves/default/00~'))
0.1.85 by Michael Ellerman
Add a test for delete
351
        self.assertTrue(os.path.exists('.shelf/shelves/default/01'))
352
0.1.100 by Michael Ellerman
Backup the shelved patch when we delete in "shelf delete".
353
        # Check the backup is right
0.1.119 by Aaron Bentley
Fix tests for patches with dates
354
        self._check_shelf('00~', new_date=new_date)
0.1.100 by Michael Ellerman
Backup the shelved patch when we delete in "shelf delete".
355
0.1.85 by Michael Ellerman
Add a test for delete
356
        # Check ls works
0.1.114 by Michael Ellerman
Internals, don't shadow list in tests.py
357
        lines = self.capture('shelf ls', retcode=0).split('\n')
358
        for line in lines:
0.1.85 by Michael Ellerman
Add a test for delete
359
            self.assertFalse(line.startswith(' 00'))
360
361
        # Unshelve should unshelve 01
362
        self.run_bzr('unshelve', '--all', retcode=0)
363
        self.assertEqual(file('test_file2').read(), self.MODIFIED)
0.1.86 by Michael Ellerman
Add a test to make sure we can cope with gaps in the patch numbering.
364
365
    def test_shelf_gaps(self):
0.1.102 by Michael Ellerman
Factor out some common test functionality.
366
        self.tree = self.make_branch_and_tree('.')
367
        self.__create_and_add_test_file()
0.1.86 by Michael Ellerman
Add a test to make sure we can cope with gaps in the patch numbering.
368
        file('test_file', 'w').write(self.MODIFIED)
369
        self.run_bzr('shelve', '--all', 'test_file', retcode=0)
370
        file('test_file', 'w').write(self.MODIFIED)
371
        self.run_bzr('shelve', '--all', 'test_file', retcode=0)
372
373
        # Now delete 00, leaving 01, next shelve should go into 02
374
        self.run_bzr('shelf', 'delete', '0', retcode=0)
375
        self.assertFalse(os.path.exists('.shelf/shelves/default/00'))
376
        self.assertFalse(os.path.exists('.shelf/shelves/default/02'))
377
        file('test_file', 'w').write(self.MODIFIED)
378
        self.run_bzr('shelve', '--all', 'test_file', retcode=0)
379
        self.assertFalse(os.path.exists('.shelf/shelves/default/00'))
380
        self.assertTrue(os.path.exists('.shelf/shelves/default/02'))
0.1.87 by Michael Ellerman
Add support for detecting and upgrading from old format shelves.
381
382
    def test_shelf_upgrade(self):
0.1.102 by Michael Ellerman
Factor out some common test functionality.
383
        self.tree = self.make_branch_and_tree('.')
0.1.87 by Michael Ellerman
Add support for detecting and upgrading from old format shelves.
384
0.1.102 by Michael Ellerman
Factor out some common test functionality.
385
        self.__create_and_add_test_file()
0.1.87 by Michael Ellerman
Add support for detecting and upgrading from old format shelves.
386
387
        # Modify then shelve, so we're not upgrading to 00, just for kicks
388
        file('test_file', 'w').write(self.MODIFIED)
389
        self.run_bzr('shelve', '--all', 'test_file', retcode=0)
390
391
        open('.bzr-shelf', 'w').write('First old shelf')
392
        open('.bzr-shelf-1', 'w').write('Second old shelf')
393
        open('.bzr-shelf-3', 'w').write('Fourth old shelf')
394
395
        # shelve and unshelve should bitch and do nothing
396
        file('test_file', 'w').write('blah blah blah')
397
        self.run_bzr('shelve', '--all', retcode=3)
398
        self.assertFalse(os.path.exists('.shelf/shelves/default/01'))
399
        self.assertEqual(file('test_file').read(), 'blah blah blah')
400
        self.run_bzr('unshelve', '--all', retcode=3)
401
        self.assertTrue(os.path.exists('.shelf/shelves/default/00'))
402
403
        # Upgrade, make sure it worked
404
        self.run_bzr('shelf', 'upgrade', retcode=0)
405
        self.assertEqual(open('.shelf/shelves/default/01').read(),
406
                'First old shelf')
407
        self.assertEqual(open('.shelf/shelves/default/02').read(),
408
                'Second old shelf')
409
        self.assertEqual(open('.shelf/shelves/default/03').read(),
410
                'Fourth old shelf')
411
412
        # Check the old shelves got backed up right
413
        self.assertTrue(os.path.exists('.bzr-shelf~'))
414
        self.assertTrue(os.path.exists('.bzr-shelf-1~'))
415
        self.assertTrue(os.path.exists('.bzr-shelf-3~'))
416
        self.assertFalse(os.path.exists('.bzr-shelf'))
417
        self.assertFalse(os.path.exists('.bzr-shelf-1'))
418
        self.assertFalse(os.path.exists('.bzr-shelf-3'))
419
420
        # Shelve should work now
421
        self.run_bzr('shelve', '--all', retcode=0)
0.1.89 by Michael Ellerman
Add support for unshelving -p0 patches, for backward compatibility.
422
0.1.108 by Michael Ellerman
Update tests for -p0 format diffs by default.
423
    def test_shelf_p1_patch(self):
0.1.102 by Michael Ellerman
Factor out some common test functionality.
424
        self.tree = self.make_branch_and_tree('.')
0.1.89 by Michael Ellerman
Add support for unshelving -p0 patches, for backward compatibility.
425
0.1.102 by Michael Ellerman
Factor out some common test functionality.
426
        self.__create_and_add_test_file()
0.1.89 by Michael Ellerman
Add support for unshelving -p0 patches, for backward compatibility.
427
428
        # Run a benign shelf command to setup .shelf for us
429
        self.run_bzr('shelf', 'ls', retcode=0)
430
0.1.119 by Aaron Bentley
Fix tests for patches with dates
431
        old_tree = self.tree.basis_tree()
515.1.2 by Aaron Bentley
Fix all test suite bugs w/ dirstate
432
        old_tree.lock_read()
433
        self.tree.lock_read()
434
        try:
435
            old_date = _patch_header_date(old_tree,
436
                old_tree.inventory.path2id('test_file'),
437
                                          'test_file')
438
            new_date = _patch_header_date(self.tree,
439
                self.tree.inventory.path2id('test_file'), 'test_file')
440
        finally:
441
            old_tree.unlock()
442
            self.tree.unlock()
0.1.89 by Michael Ellerman
Add support for unshelving -p0 patches, for backward compatibility.
443
        # Fake a -p0 shelved patch
0.1.119 by Aaron Bentley
Fix tests for patches with dates
444
        diff = self.DIFF_1 % { 'filename' : 'test_file', 'old_date': old_date,
445
                               'new_date' : new_date}
0.1.108 by Michael Ellerman
Update tests for -p0 format diffs by default.
446
        diff = diff.replace('--- ', '--- a/')
447
        diff = diff.replace('+++ ', '+++ b/')
0.1.89 by Michael Ellerman
Add support for unshelving -p0 patches, for backward compatibility.
448
        open('.shelf/shelves/default/00', 'w').write(diff)
449
450
        # This should work
451
        self.run_bzr('unshelve', '--all', retcode=0)
452
0.1.102 by Michael Ellerman
Factor out some common test functionality.
453
        self._check_diff()
0.1.98 by Michael Ellerman
Add a test for shelving in a subdirectory
454
455
    def test_shelf_shelve_in_subdir(self):
0.1.102 by Michael Ellerman
Factor out some common test functionality.
456
        self.tree = self.make_branch_and_tree('.')
0.1.103 by Michael Ellerman
Add test machinery to cope with subdirectories.
457
458
        subdir = 'subdir'
459
        os.mkdir(subdir)
460
        self.tree.add(subdir)
461
        os.chdir(subdir)
462
0.1.102 by Michael Ellerman
Factor out some common test functionality.
463
        self.__create_and_add_test_file()
0.1.98 by Michael Ellerman
Add a test for shelving in a subdirectory
464
465
        # Modify the test file
0.6.2 by Alexander Belchenko
Shelf selftest win32-fixes: write all files in binary mode,
466
        # write in binary mode because on win32 line-endings should be LF
467
        f = file('test_file', 'wb')
468
        f.write(self.MODIFIED)
469
        f.close()
0.1.98 by Michael Ellerman
Add a test for shelving in a subdirectory
470
471
        # Shelve the changes
472
        self.run_bzr('shelve', '--all', retcode=0)
473
474
        # Working tree should be unchanged
475
        diff = self.capture('diff', retcode=0)
476
        self.assertEqual(diff, '')
477
478
        # Unshelve, should succeed
479
        self.run_bzr('unshelve', '--all', retcode=0)
480
0.1.103 by Michael Ellerman
Add test machinery to cope with subdirectories.
481
        self._check_diff(filename='subdir/test_file')
0.1.104 by Michael Ellerman
Use show_diff_trees() in BzrPatchSource, reduce the size of the v0.7
482
483
        # Make sure relative filenames work ok
484
        self.run_bzr('shelve', 'test_file', '--all', retcode=0)
0.1.105 by Michael Ellerman
Error properly if we get an invalid subcommand for 'shelf'.
485
486
    def test_shelf_shelf_bogus_subcommand(self):
487
        self.tree = self.make_branch_and_tree('.')
488
        self.run_bzr('shelf', 'foo', retcode=3) # <- retcode == 3
0.1.113 by Michael Ellerman
Support for unshelving in arbitrary order.
489
490
    def test_shelf_OOO_unshelve(self):
491
        self.tree = self.make_branch_and_tree('.')
492
493
        for i in range(1, 5):
494
            self.__create_and_add_test_file(filename='test_file%d' % i)
495
496
        # Modify the test files
497
        for i in range(1, 5):
498
            file('test_file%d' % i, 'w').write(self.MODIFIED)
499
500
        # Shelve the changes
501
        for i in range(1, 5):
502
            self.run_bzr('shelve', '--all', 'test_file%d' % i, retcode=0)
503
504
        # Check shelving worked
505
        for i in range(1, 5):
506
            self.assertEqual(file('test_file%d' % i).read(), self.ORIGINAL)
507
508
        # We should now have 00-03
509
        for i in range(0, 4):
510
            self.assertTrue(os.path.exists('.shelf/shelves/default/0%d' % i))
511
512
        # Unshelve 00
513
        self.run_bzr('unshelve', '--all', '00', retcode=0)
514
        self.assertEqual(file('test_file1').read(), self.MODIFIED)
515
516
        # Check ls works
0.1.114 by Michael Ellerman
Internals, don't shadow list in tests.py
517
        lines = self.capture('shelf ls', retcode=0).split('\n')
518
        for line in lines:
0.1.113 by Michael Ellerman
Support for unshelving in arbitrary order.
519
            self.assertFalse(line.startswith(' 00'))
520
521
        # Check we can reshelve once we've unshelved out of order, should be 04
522
        self.assertFalse(os.path.exists('.shelf/shelves/default/04'))
523
        self.run_bzr('shelve', '--all')
524
        self.assertTrue(os.path.exists('.shelf/shelves/default/04'))
525
526
        # Check ls works
527
        text = self.capture('shelf ls', retcode=0)
528
        for line in text.split('\n'):
529
            self.assertFalse(line.startswith(' 00'))
530
531
        # We now have 01,02,03,04
532
        # Unshelve 02
533
        self.run_bzr('unshelve', '--all', '02', retcode=0)
534
        self.assertEqual(file('test_file3').read(), self.MODIFIED)
535
536
        # Unshelve the default, this is the reshelved 00, hence modifies file 1
537
        self.run_bzr('unshelve', '--all', retcode=0)
538
        self.assertEqual(file('test_file1').read(), self.MODIFIED)
0.1.118 by Michael Ellerman
Add a test for basic switch functionality.
539
540
    def test_shelf_switch_basic(self):
541
        self.tree = self.make_branch_and_tree('.')
542
        self.__create_and_add_test_file()
543
544
        # This should go to "default"
545
        file('test_file', 'w').write(self.MODIFIED)
546
        self.run_bzr('shelve', '--all', 'test_file', retcode=0)
547
548
        # Switch to "other"
549
        self.run_bzr('shelf', 'switch', 'other', retcode=0)
550
        file('test_file', 'w').write(self.MODIFIED)
551
        self.run_bzr('shelve', '--all', 'test_file', retcode=0)
552
553
        # Check it worked
554
        self.assertTrue(os.path.exists('.shelf/shelves/default/00'))
555
        self.assertFalse(os.path.exists('.shelf/shelves/default/01'))
556
        self.assertTrue(os.path.exists('.shelf/shelves/other/00'))
557
558
        # Switch back
559
        self.run_bzr('shelf', 'switch', 'default', retcode=0)
560
        file('test_file', 'w').write(self.MODIFIED)
561
        self.run_bzr('shelve', '--all', 'test_file', retcode=0)
562
563
        # Check that worked
564
        self.assertTrue(os.path.exists('.shelf/shelves/default/01'))
565
        self.assertFalse(os.path.exists('.shelf/shelves/other/01'))
0.3.2 by Michael Ellerman
Fix error handling for non-int patch arguments
566
567
    def test_shelf_bad_patch_arg(self):
568
        self.tree = self.make_branch_and_tree('.')
569
570
        # Check the bad arg handling
571
        stdout, error = self.run_bzr_captured(['unshelve', '01'], retcode=3)
572
        self.assertTrue("Patch '01' doesn't exist on shelf" in error)
573
574
        stdout, error = self.run_bzr_captured(['unshelve', 'foo'], retcode=3)
575
        self.assertTrue("Invalid patch name 'foo'" in error)
576
577
        # Hex and is cracky, so it shouldn't work
578
        stdout, error = self.run_bzr_captured(['unshelve', '0x00'], retcode=3)
579
        self.assertTrue("Invalid patch name '0x00'" in error)
423.1.4 by Aaron Bentley
Add --no-color option to shelf
580
581
    def test_color_hunk_selector(self):
582
        """Make sure NoColor is raised iff terminal.has_ansi_colors is False"""
583
        hs = ShelveHunkSelector([])
584
        hs = UnshelveHunkSelector([])
447 by Aaron Bentley
Fix up test and selftest, make robust against missing PyBaz
585
        try:
586
            from bzrlib.plugins.bzrtools import terminal
587
        except ImportError:
588
            from bzrtools import terminal
423.1.4 by Aaron Bentley
Add --no-color option to shelf
589
        old_has_ansi_colors = terminal.has_ansi_colors
590
        terminal.has_ansi_colors = lambda: False
591
        try:
592
            self.assertRaises(NoColor, ShelveHunkSelector, [], True)
593
            self.assertRaises(NoColor, UnshelveHunkSelector, [], True)
594
            terminal.has_ansi_colors = lambda: True
595
            hs = ShelveHunkSelector([], color=True)
596
            hs = UnshelveHunkSelector([], color=True)
597
        finally:
598
            terminal.has_ansi_colors = old_has_ansi_colors
599
600
    def test_no_color(self):
601
        """Ensure --no-color switch can be passed"""
602
        self.tree = self.make_branch_and_tree('.')
603
604
        subdir = 'subdir'
605
        os.mkdir(subdir)
606
        self.tree.add(subdir)
607
        os.chdir(subdir)
608
609
        self.__create_and_add_test_file()
610
611
        # Modify the test file
612
        # write in binary mode because on win32 line-endings should be LF
613
        f = file('test_file', 'wb')
614
        f.write(self.MODIFIED)
615
        f.close()
531.2.2 by Charlie Shepherd
Remove all trailing whitespace
616
        stdout, error = self.run_bzr_captured(['shelve', '--all',
423.1.4 by Aaron Bentley
Add --no-color option to shelf
617
                                               '--no-color'])
531.2.2 by Charlie Shepherd
Remove all trailing whitespace
618
        stdout, error = self.run_bzr_captured(['unshelve', '--all',
423.1.4 by Aaron Bentley
Add --no-color option to shelf
619
                                               '--no-color'])
531.1.1 by Aaron Bentley
Add test for shelf help, since it's custom
620
621
    def test_shelf_help(self):
622
        self.assertContainsRe(cmd_shelf().help(),
623
                              'list\n.*List the patches on the current shelf')