~abentley/bzrtools/bzrtools.dev

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