~abentley/bzrtools/bzrtools.dev

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