~abentley/bzrtools/bzrtools.dev

0.1.29 by Michael Ellerman
Add basic tests for shelve --all, and unshelve.
1
#!/usr/bin/python
2
291 by Aaron Bentley
Adjusted to selftest -> tests change
3
import bzrlib.tests
0.2.6 by Michael Ellerman
Make tests work with new bzr API, we can't use _transport anymore.
4
import os.path
0.1.29 by Michael Ellerman
Add basic tests for shelve --all, and unshelve.
5
0.2.26 by Michael Ellerman
Update tests for BzrDir changes, use test helpers to save future pain.
6
class ShelfTests(bzrlib.tests.TestCaseWithTransport):
0.1.29 by Michael Ellerman
Add basic tests for shelve --all, and unshelve.
7
    ORIGINAL = '\n\nhello test world\n\n'
8
    MODIFIED = '\n\ngoodbye test world\n\n'
320 by Aaron Bentley
Updated to match new bzr diff behaviour
9
    DIFF_HEADER = "=== modified file 'a/test_file'\n"
10
    DIFF_1 = """--- a/test_file\t
11
+++ b/test_file\t
0.1.29 by Michael Ellerman
Add basic tests for shelve --all, and unshelve.
12
@@ -1,4 +1,4 @@
13
 
14
 
15
-hello test world
16
+goodbye test world
17
 
18
"""
320 by Aaron Bentley
Updated to match new bzr diff behaviour
19
    DIFF_2 = """--- a/test_file\t
20
+++ b/test_file\t
0.1.40 by Michael Ellerman
Update test with revision to actually test the shelf worked properly.
21
@@ -1,4 +1,4 @@
22
 
23
 
24
-goodbye test world
25
+hello test world
26
 
27
"""
0.1.29 by Michael Ellerman
Add basic tests for shelve --all, and unshelve.
28
    def test_shelf(self):
0.1.42 by Michael Ellerman
Add tests for new shelf layout.
29
        self.__test_loop(1)
30
31
    def test_shelf_multi(self):
32
        self.__test_loop(10)
33
34
    def __test_loop(self, count):
0.2.26 by Michael Ellerman
Update tests for BzrDir changes, use test helpers to save future pain.
35
        tree = self.make_branch_and_tree('.')
36
        self.__create_and_add_test_file(tree)
0.1.29 by Michael Ellerman
Add basic tests for shelve --all, and unshelve.
37
0.1.42 by Michael Ellerman
Add tests for new shelf layout.
38
        while count > 0:
39
            count -= 1
40
41
            # Modify the test file
42
            file('test_file', 'w').write(self.MODIFIED)
43
44
            # Check the diff is right
45
            self.assertEqual(self.capture('diff', retcode=1),
46
                self.DIFF_HEADER + self.DIFF_1 + '\n')
47
48
            # Shelve the changes
0.1.80 by Michael Ellerman
After extensive user testing, ie. me using it, I've decided --pick should be
49
            self.run_bzr('shelve', '--all', retcode=0)
0.1.42 by Michael Ellerman
Add tests for new shelf layout.
50
51
            # Make sure there is no diff anymore
52
            self.assertEqual(self.capture('diff', retcode=0), '')
53
54
            # Make sure the file is actually back the way it was
55
            self.assertEqual(file('test_file').read(), self.ORIGINAL)
56
57
            # Check the shelf is right
0.2.26 by Michael Ellerman
Update tests for BzrDir changes, use test helpers to save future pain.
58
            shelf = open(os.path.join(tree.branch.base,
59
                        '.shelf/shelves/default/00')).read()
0.2.11 by Michael Ellerman
Fix test now that we have a message always.
60
            shelf = shelf[shelf.index('\n') + 1:] # skip the message
0.1.42 by Michael Ellerman
Add tests for new shelf layout.
61
            self.assertEqual(shelf, self.DIFF_1)
62
63
            # Unshelve
0.1.80 by Michael Ellerman
After extensive user testing, ie. me using it, I've decided --pick should be
64
            self.run_bzr('unshelve', '--all', retcode=0)
0.1.42 by Michael Ellerman
Add tests for new shelf layout.
65
66
            # Check the diff is right again
67
            self.assertEqual(self.capture('diff', retcode=1),
68
                self.DIFF_HEADER + self.DIFF_1 + '\n')
69
0.1.84 by Michael Ellerman
Backup the patch when we unshelve. Suggested by Christian Reis.
70
            # Check the shelved patch was backed up
71
            shelf = open(os.path.join(tree.branch.base,
72
                        '.shelf/shelves/default/00~')).read()
73
            shelf = shelf[shelf.index('\n') + 1:] # skip the message
74
            self.assertEqual(shelf, self.DIFF_1)
75
0.1.42 by Michael Ellerman
Add tests for new shelf layout.
76
            # Make sure the file is back the way it should be
77
            self.assertEqual(file('test_file').read(), self.MODIFIED)
0.1.29 by Michael Ellerman
Add basic tests for shelve --all, and unshelve.
78
79
    def test_shelf_nothing_to_shelve(self):
80
        import os.path
0.2.26 by Michael Ellerman
Update tests for BzrDir changes, use test helpers to save future pain.
81
        tree = self.make_branch_and_tree('.')
82
        self.__create_and_add_test_file(tree)
0.1.29 by Michael Ellerman
Add basic tests for shelve --all, and unshelve.
83
84
        # Shelve the changes
0.1.80 by Michael Ellerman
After extensive user testing, ie. me using it, I've decided --pick should be
85
        self.run_bzr('shelve', '--all', retcode=3)
0.1.29 by Michael Ellerman
Add basic tests for shelve --all, and unshelve.
86
0.2.26 by Michael Ellerman
Update tests for BzrDir changes, use test helpers to save future pain.
87
        if os.path.exists(os.path.join(tree.branch.base,
88
                '.shelf/shelves/default/00')):
0.1.29 by Michael Ellerman
Add basic tests for shelve --all, and unshelve.
89
            self.fail("Shelf exists, but it shouldn't")
0.1.37 by Michael Ellerman
Add (failing) tests of revision argument for shelve.
90
0.1.77 by Michael Ellerman
When unshelving, try to patch with --dry-run first, if that fails bail out.
91
    def __create_and_add_test_file(self, tree, filename='test_file'):
0.1.98 by Michael Ellerman
Add a test for shelving in a subdirectory
92
        f = open(filename, 'w')
93
        f.write(self.ORIGINAL)
94
        f.close()
95
        tree.add(tree.relpath(os.path.join(os.getcwd(), filename)))
0.1.77 by Michael Ellerman
When unshelving, try to patch with --dry-run first, if that fails bail out.
96
        tree.commit(message='add %s' % filename)
0.2.26 by Michael Ellerman
Update tests for BzrDir changes, use test helpers to save future pain.
97
0.1.37 by Michael Ellerman
Add (failing) tests of revision argument for shelve.
98
    def test_shelf_with_revision(self):
0.2.26 by Michael Ellerman
Update tests for BzrDir changes, use test helpers to save future pain.
99
        tree = self.make_branch_and_tree('.')
0.1.37 by Michael Ellerman
Add (failing) tests of revision argument for shelve.
100
0.2.26 by Michael Ellerman
Update tests for BzrDir changes, use test helpers to save future pain.
101
        self.__create_and_add_test_file(tree)
0.1.37 by Michael Ellerman
Add (failing) tests of revision argument for shelve.
102
103
        # 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.
104
        self.build_tree_contents([('test_file', self.MODIFIED)])
313 by Aaron Bentley
Updated to match API changes
105
        tree.commit(message='update test_file')
0.1.37 by Michael Ellerman
Add (failing) tests of revision argument for shelve.
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', '-r', '1', retcode=0)
0.1.37 by Michael Ellerman
Add (failing) tests of revision argument for shelve.
109
0.1.40 by Michael Ellerman
Update test with revision to actually test the shelf worked properly.
110
        # Check the diff is right
271 by Aaron Bentley
Cherry-picked Robert's diff and push fixes
111
        self.assertEqual(self.capture('diff', retcode=1),
0.1.40 by Michael Ellerman
Update test with revision to actually test the shelf worked properly.
112
            self.DIFF_HEADER + self.DIFF_2 + '\n')
113
114
        # Make sure the file is the way it should be
115
        self.assertEqual(file('test_file').read(), self.ORIGINAL)
116
117
        # Unshelve
0.1.80 by Michael Ellerman
After extensive user testing, ie. me using it, I've decided --pick should be
118
        self.run_bzr('unshelve', '--all', retcode=0)
0.1.40 by Michael Ellerman
Update test with revision to actually test the shelf worked properly.
119
120
        # Make sure the file is back the way it should be
121
        self.assertEqual(file('test_file').read(), self.MODIFIED)
122
0.1.37 by Michael Ellerman
Add (failing) tests of revision argument for shelve.
123
    def test_shelf_with_two_revisions(self):
0.2.26 by Michael Ellerman
Update tests for BzrDir changes, use test helpers to save future pain.
124
        tree = self.make_branch_and_tree('.')
0.1.37 by Michael Ellerman
Add (failing) tests of revision argument for shelve.
125
0.1.80 by Michael Ellerman
After extensive user testing, ie. me using it, I've decided --pick should be
126
        cmd = 'shelve --all -r 1..2'
0.1.37 by Michael Ellerman
Add (failing) tests of revision argument for shelve.
127
        (stdout, stderr) = self.run_bzr_captured(cmd.split(), retcode=None)
128
129
        self.assertEqual(stderr.split('\n')[0],
295 by Aaron Bentley
Fixed test case failure
130
            'bzr: ERROR: shelve only accepts a single revision parameter.')
281 by Aaron Bentley
Handled whitespace branch names better
131
0.2.27 by Michael Ellerman
Add basic test for shelf show
132
    def test_shelf_show_basic(self):
133
        tree = self.make_branch_and_tree('.')
134
        self.__create_and_add_test_file(tree)
0.2.28 by Michael Ellerman
More tests of shelf show
135
        self.__test_show(tree, '00')
0.2.27 by Michael Ellerman
Add basic test for shelf show
136
0.2.28 by Michael Ellerman
More tests of shelf show
137
    def __test_show(self, tree, patch):
0.2.27 by Michael Ellerman
Add basic test for shelf show
138
        # Modify the test file
0.2.28 by Michael Ellerman
More tests of shelf show
139
        self.build_tree_contents([('test_file', 'patch %s\n' % patch)])
0.2.27 by Michael Ellerman
Add basic test for shelf show
140
141
        # Shelve the changes
0.1.80 by Michael Ellerman
After extensive user testing, ie. me using it, I've decided --pick should be
142
        self.run_bzr('shelve', '--all', retcode=0)
0.2.27 by Michael Ellerman
Add basic test for shelf show
143
144
        # Make sure there is no diff anymore
145
        self.assertEqual(self.capture('diff', retcode=0), '')
146
147
        # Check the shelf is right
148
        shelf = open(os.path.join(tree.branch.base,
0.2.28 by Michael Ellerman
More tests of shelf show
149
                    '.shelf/shelves/default', patch)).read()
150
        self.assertTrue('patch %s' % patch in shelf)
151
152
        # Check the shown output is right
153
        shown = self.capture('shelf show %s' % patch, retcode=0)
154
        self.assertEqual(shown, shelf)
155
156
    def test_shelf_show_multi(self):
157
        tree = self.make_branch_and_tree('.')
158
        self.__create_and_add_test_file(tree)
159
        self.__test_show(tree, '00')
160
        self.__test_show(tree, '01')
161
        self.__test_show(tree, '02')
162
163
        # Now check we can show a previously shelved patch
164
        shelf = open(os.path.join(tree.branch.base,
0.2.27 by Michael Ellerman
Add basic test for shelf show
165
                    '.shelf/shelves/default/00')).read()
0.2.28 by Michael Ellerman
More tests of shelf show
166
        self.assertTrue('patch 00' in shelf)
0.2.27 by Michael Ellerman
Add basic test for shelf show
167
168
        # Check the shown output is right
169
        shown = self.capture('shelf show 00', retcode=0)
170
        self.assertEqual(shown, shelf)
0.2.30 by Michael Ellerman
Test for shelf show with no patch to show.
171
172
    def test_shelf_show_with_no_patch(self):
173
        tree = self.make_branch_and_tree('.')
174
        stderr = self.run_bzr_captured(['shelf', 'show', '00'], retcode=None)[1]
175
        self.assertTrue("Patch '00' doesn't exist on shelf default!" in stderr)
176
0.1.76 by Michael Ellerman
Add a test to make sure we don't delete the shelved patch if unshelving
177
    def test_shelf_unshelve_failure(self):
178
        tree = self.make_branch_and_tree('.')
179
180
        self.__create_and_add_test_file(tree)
181
182
        # Modify the test file
183
        file('test_file', 'w').write(self.MODIFIED)
184
185
        # Shelve the changes
0.1.80 by Michael Ellerman
After extensive user testing, ie. me using it, I've decided --pick should be
186
        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
187
188
        # Write an unapplyable patch into the shelf
189
        shelf = open(os.path.join(tree.branch.base,
190
                    '.shelf/shelves/default/00'), 'w')
191
        shelf.write(self.DIFF_2)
192
        shelf.close()
193
194
        # Unshelve, should fail
0.1.80 by Michael Ellerman
After extensive user testing, ie. me using it, I've decided --pick should be
195
        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
196
197
        # Make sure the patch is still there, eventhough it's broken
198
        shelf = open(os.path.join(tree.branch.base,
199
                    '.shelf/shelves/default/00')).read()
200
        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.
201
202
        # Working tree should be unchanged
203
        diff = self.capture('diff', retcode=0)
204
        self.assertEqual(diff, '')
205
206
    def test_shelf_unshelve_failure_two_hunks(self):
207
        tree = self.make_branch_and_tree('.')
208
209
        self.__create_and_add_test_file(tree)
210
        self.__create_and_add_test_file(tree, filename='test_file2')
211
212
        # Modify the test files
213
        file('test_file', 'w').write(self.MODIFIED)
214
        file('test_file2', 'w').write(self.MODIFIED)
215
216
        # Shelve the changes
0.1.80 by Michael Ellerman
After extensive user testing, ie. me using it, I've decided --pick should be
217
        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.
218
219
        # Put the changes to test_file back, the shelved patch won't apply now
220
        file('test_file', 'w').write(self.MODIFIED)
221
        tree.commit(message='screw up test_file')
222
223
        # Unshelve, should fail
0.1.80 by Michael Ellerman
After extensive user testing, ie. me using it, I've decided --pick should be
224
        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.
225
226
        # Working tree should be unchanged
227
        diff = self.capture('diff', retcode=0)
228
        self.assertEqual(diff, '')
0.1.84 by Michael Ellerman
Backup the patch when we unshelve. Suggested by Christian Reis.
229
0.1.91 by Michael Ellerman
Add --force option to unshelve, which runs the shelved changes through
230
        # Force should succeed and modify test_file2, but leave shelf
231
        self.run_bzr('unshelve', '--force', '--all', retcode=0)
232
        self.assertEqual(open('test_file2').read(), self.MODIFIED)
233
        self.assertTrue(os.path.exists('.shelf/shelves/default/00'))
234
0.1.84 by Michael Ellerman
Backup the patch when we unshelve. Suggested by Christian Reis.
235
    def test_shelf_after_unshelve(self):
236
        tree = self.make_branch_and_tree('.')
237
238
        self.__create_and_add_test_file(tree)
239
        self.__create_and_add_test_file(tree, filename='test_file2')
240
241
        # Modify the test files
242
        file('test_file', 'w').write(self.MODIFIED)
243
        file('test_file2', 'w').write(self.MODIFIED)
244
245
        # Shelve the changes
246
        self.run_bzr('shelve', '--all', 'test_file', retcode=0)
247
        self.run_bzr('shelve', '--all', 'test_file2', retcode=0)
248
249
        # Unshelve
250
        self.run_bzr('unshelve', '--all', retcode=0)
251
252
        # We should now have 00 and 01~
253
        self.assertTrue(os.path.exists('.shelf/shelves/default/00'))
254
        self.assertTrue(os.path.exists('.shelf/shelves/default/01~'))
255
256
        # Check ls works
257
        list = self.capture('shelf ls', retcode=0).split('\n')
258
        for line in list:
259
            self.assertFalse(line.startswith(' 01'))
260
261
        # Unshelve, if unshelve is confused by the backup it will fail
262
        self.run_bzr('unshelve', '--all', retcode=0)
0.1.85 by Michael Ellerman
Add a test for delete
263
264
    def test_shelf_delete(self):
265
        tree = self.make_branch_and_tree('.')
266
267
        self.__create_and_add_test_file(tree)
268
        self.__create_and_add_test_file(tree, filename='test_file2')
269
270
        # Modify the test files
271
        file('test_file', 'w').write(self.MODIFIED)
272
        file('test_file2', 'w').write(self.MODIFIED)
273
274
        # Shelve the changes
275
        self.run_bzr('shelve', '--all', 'test_file', retcode=0)
276
        self.run_bzr('shelve', '--all', 'test_file2', retcode=0)
277
278
        # Delete 00
279
        self.run_bzr('shelf', 'delete', '00', retcode=0)
280
281
        # We should now have 01 but not 00
282
        self.assertFalse(os.path.exists('.shelf/shelves/default/00'))
283
        self.assertTrue(os.path.exists('.shelf/shelves/default/01'))
284
285
        # Check ls works
286
        list = self.capture('shelf ls', retcode=0).split('\n')
287
        for line in list:
288
            self.assertFalse(line.startswith(' 00'))
289
290
        # Unshelve should unshelve 01
291
        self.run_bzr('unshelve', '--all', retcode=0)
292
        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.
293
294
    def test_shelf_gaps(self):
295
        tree = self.make_branch_and_tree('.')
296
        self.__create_and_add_test_file(tree)
297
        file('test_file', 'w').write(self.MODIFIED)
298
        self.run_bzr('shelve', '--all', 'test_file', retcode=0)
299
        file('test_file', 'w').write(self.MODIFIED)
300
        self.run_bzr('shelve', '--all', 'test_file', retcode=0)
301
302
        # Now delete 00, leaving 01, next shelve should go into 02
303
        self.run_bzr('shelf', 'delete', '0', retcode=0)
304
        self.assertFalse(os.path.exists('.shelf/shelves/default/00'))
305
        self.assertFalse(os.path.exists('.shelf/shelves/default/02'))
306
        file('test_file', 'w').write(self.MODIFIED)
307
        self.run_bzr('shelve', '--all', 'test_file', retcode=0)
308
        self.assertFalse(os.path.exists('.shelf/shelves/default/00'))
309
        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.
310
311
    def test_shelf_upgrade(self):
312
        tree = self.make_branch_and_tree('.')
313
314
        self.__create_and_add_test_file(tree)
315
316
        # Modify then shelve, so we're not upgrading to 00, just for kicks
317
        file('test_file', 'w').write(self.MODIFIED)
318
        self.run_bzr('shelve', '--all', 'test_file', retcode=0)
319
320
        open('.bzr-shelf', 'w').write('First old shelf')
321
        open('.bzr-shelf-1', 'w').write('Second old shelf')
322
        open('.bzr-shelf-3', 'w').write('Fourth old shelf')
323
324
        # shelve and unshelve should bitch and do nothing
325
        file('test_file', 'w').write('blah blah blah')
326
        self.run_bzr('shelve', '--all', retcode=3)
327
        self.assertFalse(os.path.exists('.shelf/shelves/default/01'))
328
        self.assertEqual(file('test_file').read(), 'blah blah blah')
329
        self.run_bzr('unshelve', '--all', retcode=3)
330
        self.assertTrue(os.path.exists('.shelf/shelves/default/00'))
331
332
        # Upgrade, make sure it worked
333
        self.run_bzr('shelf', 'upgrade', retcode=0)
334
        self.assertEqual(open('.shelf/shelves/default/01').read(),
335
                'First old shelf')
336
        self.assertEqual(open('.shelf/shelves/default/02').read(),
337
                'Second old shelf')
338
        self.assertEqual(open('.shelf/shelves/default/03').read(),
339
                'Fourth old shelf')
340
341
        # Check the old shelves got backed up right
342
        self.assertTrue(os.path.exists('.bzr-shelf~'))
343
        self.assertTrue(os.path.exists('.bzr-shelf-1~'))
344
        self.assertTrue(os.path.exists('.bzr-shelf-3~'))
345
        self.assertFalse(os.path.exists('.bzr-shelf'))
346
        self.assertFalse(os.path.exists('.bzr-shelf-1'))
347
        self.assertFalse(os.path.exists('.bzr-shelf-3'))
348
349
        # Shelve should work now
350
        self.run_bzr('shelve', '--all', retcode=0)
0.1.89 by Michael Ellerman
Add support for unshelving -p0 patches, for backward compatibility.
351
352
    def test_shelf_p0_patch(self):
353
        tree = self.make_branch_and_tree('.')
354
355
        self.__create_and_add_test_file(tree)
356
357
        # Run a benign shelf command to setup .shelf for us
358
        self.run_bzr('shelf', 'ls', retcode=0)
359
360
        # Fake a -p0 shelved patch
361
        diff = self.DIFF_1
362
        diff = diff.replace('a/', '')
363
        diff = diff.replace('b/', '')
364
        open('.shelf/shelves/default/00', 'w').write(diff)
365
366
        # This should work
367
        self.run_bzr('unshelve', '--all', retcode=0)
368
369
        # Check the diff is right
370
        self.assertEqual(self.capture('diff', retcode=1),
371
            self.DIFF_HEADER + self.DIFF_1 + '\n')
0.1.98 by Michael Ellerman
Add a test for shelving in a subdirectory
372
373
    def test_shelf_shelve_in_subdir(self):
374
        tree = self.make_branch_and_tree('.')
375
376
        self.__create_and_add_test_file(tree)
377
378
        # Modify the test file
379
        file('test_file', 'w').write(self.MODIFIED)
380
381
        # Shelve the changes
382
        self.run_bzr('shelve', '--all', retcode=0)
383
384
        # Working tree should be unchanged
385
        diff = self.capture('diff', retcode=0)
386
        self.assertEqual(diff, '')
387
388
        # Unshelve, should succeed
389
        self.run_bzr('unshelve', '--all', retcode=0)
390
391
        # Check the diff is right
392
        self.assertEqual(self.capture('diff', retcode=1),
393
            self.DIFF_HEADER + self.DIFF_1 + '\n')