~abentley/bzrtools/bzrtools.dev

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