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