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