~bzr-pqm/bzr/bzr.dev

4763.2.4 by John Arbash Meinel
merge bzr.2.1 in preparation for NEWS entry.
1
# Copyright (C) 2008, 2009, 2010 Canonical Ltd
0.16.89 by Aaron Bentley
Add tests for Shelver
2
#
0.16.101 by Aaron Bentley
Update GPL formatting and copyright
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
#
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
#
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
0.16.89 by Aaron Bentley
Add tests for Shelver
16
17
18
from cStringIO import StringIO
0.16.91 by Aaron Bentley
Test finish and quit
19
import os
0.16.108 by Aaron Bentley
Shelf supports multiple diff writers.
20
import sys
4902.1.6 by Guilherme Salgado
Tweak the text to check to assert the meat of the diff is what we expect, instead of just checking it matches some regexps
21
from textwrap import dedent
0.16.89 by Aaron Bentley
Add tests for Shelver
22
4595.9.5 by Aaron Bentley
Add expected failures for shelving root changes.
23
from bzrlib import (
24
    errors,
25
    shelf_ui,
26
    revision,
27
    tests,
28
)
4899.2.1 by Neil Martinsen-Burrell
add beter feedback from the unshelve command
29
from bzrlib.tests import script
0.16.89 by Aaron Bentley
Add tests for Shelver
30
31
32
class ExpectShelver(shelf_ui.Shelver):
33
    """A variant of Shelver that intercepts console activity, for testing."""
34
4100.3.4 by Aaron Bentley
Clean up signatures
35
    def __init__(self, work_tree, target_tree, diff_writer=None,
36
                 auto=False, auto_apply=False, file_list=None, message=None,
4526.7.1 by Aaron Bentley
Make vocabulary part of reporter.
37
                 destroy=False, reporter=None):
0.16.108 by Aaron Bentley
Shelf supports multiple diff writers.
38
        shelf_ui.Shelver.__init__(self, work_tree, target_tree, diff_writer,
4100.3.4 by Aaron Bentley
Clean up signatures
39
                                  auto, auto_apply, file_list, message,
4526.7.1 by Aaron Bentley
Make vocabulary part of reporter.
40
                                  destroy, reporter=reporter)
0.16.89 by Aaron Bentley
Add tests for Shelver
41
        self.expected = []
42
        self.diff_writer = StringIO()
43
44
    def expect(self, prompt, response):
45
        self.expected.append((prompt, response))
46
47
    def prompt(self, message):
48
        try:
49
            prompt, response = self.expected.pop(0)
50
        except IndexError:
51
            raise AssertionError('Unexpected prompt: %s' % message)
52
        if prompt != message:
53
            raise AssertionError('Wrong prompt: %s' % message)
54
        return response
55
56
0.16.90 by Aaron Bentley
Handle shelving multiple diff hunks
57
LINES_AJ = 'a\nb\nc\nd\ne\nf\ng\nh\ni\nj\n'
58
59
60
LINES_ZY = 'z\nb\nc\nd\ne\nf\ng\nh\ni\ny\n'
61
62
63
LINES_AY = 'a\nb\nc\nd\ne\nf\ng\nh\ni\ny\n'
64
65
0.16.89 by Aaron Bentley
Add tests for Shelver
66
class TestShelver(tests.TestCaseWithTransport):
67
68
    def create_shelvable_tree(self):
69
        tree = self.make_branch_and_tree('tree')
0.16.90 by Aaron Bentley
Handle shelving multiple diff hunks
70
        self.build_tree_contents([('tree/foo', LINES_AJ)])
0.16.89 by Aaron Bentley
Add tests for Shelver
71
        tree.add('foo', 'foo-id')
72
        tree.commit('added foo')
0.16.90 by Aaron Bentley
Handle shelving multiple diff hunks
73
        self.build_tree_contents([('tree/foo', LINES_ZY)])
0.16.89 by Aaron Bentley
Add tests for Shelver
74
        return tree
75
76
    def test_unexpected_prompt_failure(self):
77
        tree = self.create_shelvable_tree()
4595.13.2 by Alexander Belchenko
[cherrypick revno 4650 from bzr.dev] Fix shelve on windows. (Robert Collins, #305006)
78
        tree.lock_tree_write()
79
        self.addCleanup(tree.unlock)
0.16.89 by Aaron Bentley
Add tests for Shelver
80
        shelver = ExpectShelver(tree, tree.basis_tree())
4603.1.17 by Aaron Bentley
Fix shelf_ui tests to finalize.
81
        self.addCleanup(shelver.finalize)
0.16.89 by Aaron Bentley
Add tests for Shelver
82
        e = self.assertRaises(AssertionError, shelver.run)
3990.4.1 by Daniel Watkins
Changed all shelve tests to expect a '?'.
83
        self.assertEqual('Unexpected prompt: Shelve? [yNfq?]', str(e))
0.16.89 by Aaron Bentley
Add tests for Shelver
84
85
    def test_wrong_prompt_failure(self):
86
        tree = self.create_shelvable_tree()
4595.13.2 by Alexander Belchenko
[cherrypick revno 4650 from bzr.dev] Fix shelve on windows. (Robert Collins, #305006)
87
        tree.lock_tree_write()
88
        self.addCleanup(tree.unlock)
0.16.89 by Aaron Bentley
Add tests for Shelver
89
        shelver = ExpectShelver(tree, tree.basis_tree())
4603.1.17 by Aaron Bentley
Fix shelf_ui tests to finalize.
90
        self.addCleanup(shelver.finalize)
0.16.89 by Aaron Bentley
Add tests for Shelver
91
        shelver.expect('foo', 'y')
92
        e = self.assertRaises(AssertionError, shelver.run)
3990.4.1 by Daniel Watkins
Changed all shelve tests to expect a '?'.
93
        self.assertEqual('Wrong prompt: Shelve? [yNfq?]', str(e))
0.16.89 by Aaron Bentley
Add tests for Shelver
94
95
    def test_shelve_not_diff(self):
96
        tree = self.create_shelvable_tree()
4595.13.2 by Alexander Belchenko
[cherrypick revno 4650 from bzr.dev] Fix shelve on windows. (Robert Collins, #305006)
97
        tree.lock_tree_write()
98
        self.addCleanup(tree.unlock)
0.16.89 by Aaron Bentley
Add tests for Shelver
99
        shelver = ExpectShelver(tree, tree.basis_tree())
4603.1.17 by Aaron Bentley
Fix shelf_ui tests to finalize.
100
        self.addCleanup(shelver.finalize)
3990.4.1 by Daniel Watkins
Changed all shelve tests to expect a '?'.
101
        shelver.expect('Shelve? [yNfq?]', 'n')
102
        shelver.expect('Shelve? [yNfq?]', 'n')
0.16.89 by Aaron Bentley
Add tests for Shelver
103
        # No final shelving prompt because no changes were selected
104
        shelver.run()
0.16.90 by Aaron Bentley
Handle shelving multiple diff hunks
105
        self.assertFileEqual(LINES_ZY, 'tree/foo')
0.16.89 by Aaron Bentley
Add tests for Shelver
106
107
    def test_shelve_diff_no(self):
108
        tree = self.create_shelvable_tree()
4595.13.2 by Alexander Belchenko
[cherrypick revno 4650 from bzr.dev] Fix shelve on windows. (Robert Collins, #305006)
109
        tree.lock_tree_write()
110
        self.addCleanup(tree.unlock)
0.16.89 by Aaron Bentley
Add tests for Shelver
111
        shelver = ExpectShelver(tree, tree.basis_tree())
4603.1.17 by Aaron Bentley
Fix shelf_ui tests to finalize.
112
        self.addCleanup(shelver.finalize)
3990.4.1 by Daniel Watkins
Changed all shelve tests to expect a '?'.
113
        shelver.expect('Shelve? [yNfq?]', 'y')
114
        shelver.expect('Shelve? [yNfq?]', 'y')
115
        shelver.expect('Shelve 2 change(s)? [yNfq?]', 'n')
0.16.89 by Aaron Bentley
Add tests for Shelver
116
        shelver.run()
0.16.90 by Aaron Bentley
Handle shelving multiple diff hunks
117
        self.assertFileEqual(LINES_ZY, 'tree/foo')
0.16.89 by Aaron Bentley
Add tests for Shelver
118
119
    def test_shelve_diff(self):
120
        tree = self.create_shelvable_tree()
4595.13.2 by Alexander Belchenko
[cherrypick revno 4650 from bzr.dev] Fix shelve on windows. (Robert Collins, #305006)
121
        tree.lock_tree_write()
122
        self.addCleanup(tree.unlock)
0.16.89 by Aaron Bentley
Add tests for Shelver
123
        shelver = ExpectShelver(tree, tree.basis_tree())
4603.1.17 by Aaron Bentley
Fix shelf_ui tests to finalize.
124
        self.addCleanup(shelver.finalize)
3990.4.1 by Daniel Watkins
Changed all shelve tests to expect a '?'.
125
        shelver.expect('Shelve? [yNfq?]', 'y')
126
        shelver.expect('Shelve? [yNfq?]', 'y')
127
        shelver.expect('Shelve 2 change(s)? [yNfq?]', 'y')
0.16.90 by Aaron Bentley
Handle shelving multiple diff hunks
128
        shelver.run()
129
        self.assertFileEqual(LINES_AJ, 'tree/foo')
130
131
    def test_shelve_one_diff(self):
132
        tree = self.create_shelvable_tree()
4595.13.2 by Alexander Belchenko
[cherrypick revno 4650 from bzr.dev] Fix shelve on windows. (Robert Collins, #305006)
133
        tree.lock_tree_write()
134
        self.addCleanup(tree.unlock)
0.16.90 by Aaron Bentley
Handle shelving multiple diff hunks
135
        shelver = ExpectShelver(tree, tree.basis_tree())
4603.1.17 by Aaron Bentley
Fix shelf_ui tests to finalize.
136
        self.addCleanup(shelver.finalize)
3990.4.1 by Daniel Watkins
Changed all shelve tests to expect a '?'.
137
        shelver.expect('Shelve? [yNfq?]', 'y')
138
        shelver.expect('Shelve? [yNfq?]', 'n')
139
        shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
0.16.89 by Aaron Bentley
Add tests for Shelver
140
        shelver.run()
0.16.90 by Aaron Bentley
Handle shelving multiple diff hunks
141
        self.assertFileEqual(LINES_AY, 'tree/foo')
0.16.89 by Aaron Bentley
Add tests for Shelver
142
0.16.92 by Aaron Bentley
Test kind, add, binary, --all
143
    def test_shelve_binary_change(self):
144
        tree = self.create_shelvable_tree()
145
        self.build_tree_contents([('tree/foo', '\x00')])
4595.13.2 by Alexander Belchenko
[cherrypick revno 4650 from bzr.dev] Fix shelve on windows. (Robert Collins, #305006)
146
        tree.lock_tree_write()
147
        self.addCleanup(tree.unlock)
0.16.92 by Aaron Bentley
Test kind, add, binary, --all
148
        shelver = ExpectShelver(tree, tree.basis_tree())
4603.1.17 by Aaron Bentley
Fix shelf_ui tests to finalize.
149
        self.addCleanup(shelver.finalize)
3990.4.1 by Daniel Watkins
Changed all shelve tests to expect a '?'.
150
        shelver.expect('Shelve binary changes? [yNfq?]', 'y')
151
        shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
0.16.92 by Aaron Bentley
Test kind, add, binary, --all
152
        shelver.run()
153
        self.assertFileEqual(LINES_AJ, 'tree/foo')
154
0.16.89 by Aaron Bentley
Add tests for Shelver
155
    def test_shelve_rename(self):
156
        tree = self.create_shelvable_tree()
157
        tree.rename_one('foo', 'bar')
4595.13.2 by Alexander Belchenko
[cherrypick revno 4650 from bzr.dev] Fix shelve on windows. (Robert Collins, #305006)
158
        tree.lock_tree_write()
159
        self.addCleanup(tree.unlock)
0.16.89 by Aaron Bentley
Add tests for Shelver
160
        shelver = ExpectShelver(tree, tree.basis_tree())
4603.1.17 by Aaron Bentley
Fix shelf_ui tests to finalize.
161
        self.addCleanup(shelver.finalize)
3990.4.1 by Daniel Watkins
Changed all shelve tests to expect a '?'.
162
        shelver.expect('Shelve renaming "foo" => "bar"? [yNfq?]', 'y')
163
        shelver.expect('Shelve? [yNfq?]', 'y')
164
        shelver.expect('Shelve? [yNfq?]', 'y')
165
        shelver.expect('Shelve 3 change(s)? [yNfq?]', 'y')
0.16.89 by Aaron Bentley
Add tests for Shelver
166
        shelver.run()
0.16.90 by Aaron Bentley
Handle shelving multiple diff hunks
167
        self.assertFileEqual(LINES_AJ, 'tree/foo')
0.16.91 by Aaron Bentley
Test finish and quit
168
169
    def test_shelve_deletion(self):
170
        tree = self.create_shelvable_tree()
171
        os.unlink('tree/foo')
4595.13.2 by Alexander Belchenko
[cherrypick revno 4650 from bzr.dev] Fix shelve on windows. (Robert Collins, #305006)
172
        tree.lock_tree_write()
173
        self.addCleanup(tree.unlock)
0.16.91 by Aaron Bentley
Test finish and quit
174
        shelver = ExpectShelver(tree, tree.basis_tree())
4603.1.17 by Aaron Bentley
Fix shelf_ui tests to finalize.
175
        self.addCleanup(shelver.finalize)
3990.4.1 by Daniel Watkins
Changed all shelve tests to expect a '?'.
176
        shelver.expect('Shelve removing file "foo"? [yNfq?]', 'y')
177
        shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
0.16.91 by Aaron Bentley
Test finish and quit
178
        shelver.run()
179
        self.assertFileEqual(LINES_AJ, 'tree/foo')
180
0.16.92 by Aaron Bentley
Test kind, add, binary, --all
181
    def test_shelve_creation(self):
182
        tree = self.make_branch_and_tree('tree')
183
        tree.commit('add tree root')
184
        self.build_tree(['tree/foo'])
185
        tree.add('foo')
4595.13.2 by Alexander Belchenko
[cherrypick revno 4650 from bzr.dev] Fix shelve on windows. (Robert Collins, #305006)
186
        tree.lock_tree_write()
187
        self.addCleanup(tree.unlock)
0.16.92 by Aaron Bentley
Test kind, add, binary, --all
188
        shelver = ExpectShelver(tree, tree.basis_tree())
4603.1.17 by Aaron Bentley
Fix shelf_ui tests to finalize.
189
        self.addCleanup(shelver.finalize)
3990.4.1 by Daniel Watkins
Changed all shelve tests to expect a '?'.
190
        shelver.expect('Shelve adding file "foo"? [yNfq?]', 'y')
191
        shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
0.16.92 by Aaron Bentley
Test kind, add, binary, --all
192
        shelver.run()
193
        self.failIfExists('tree/foo')
194
195
    def test_shelve_kind_change(self):
196
        tree = self.create_shelvable_tree()
197
        os.unlink('tree/foo')
198
        os.mkdir('tree/foo')
4595.13.2 by Alexander Belchenko
[cherrypick revno 4650 from bzr.dev] Fix shelve on windows. (Robert Collins, #305006)
199
        tree.lock_tree_write()
200
        self.addCleanup(tree.unlock)
0.16.92 by Aaron Bentley
Test kind, add, binary, --all
201
        shelver = ExpectShelver(tree, tree.basis_tree())
4603.1.17 by Aaron Bentley
Fix shelf_ui tests to finalize.
202
        self.addCleanup(shelver.finalize)
3990.4.1 by Daniel Watkins
Changed all shelve tests to expect a '?'.
203
        shelver.expect('Shelve changing "foo" from file to directory? [yNfq?]',
0.16.92 by Aaron Bentley
Test kind, add, binary, --all
204
                       'y')
3990.4.1 by Daniel Watkins
Changed all shelve tests to expect a '?'.
205
        shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
0.16.92 by Aaron Bentley
Test kind, add, binary, --all
206
4119.5.1 by James Westby
Shelve can now shelve changes to a symlink's target.
207
    def test_shelve_modify_target(self):
4526.6.12 by Aaron Bentley
Updates from review.
208
        self.requireFeature(tests.SymlinkFeature)
4119.5.1 by James Westby
Shelve can now shelve changes to a symlink's target.
209
        tree = self.create_shelvable_tree()
210
        os.symlink('bar', 'tree/baz')
211
        tree.add('baz', 'baz-id')
212
        tree.commit("Add symlink")
213
        os.unlink('tree/baz')
214
        os.symlink('vax', 'tree/baz')
4595.13.2 by Alexander Belchenko
[cherrypick revno 4650 from bzr.dev] Fix shelve on windows. (Robert Collins, #305006)
215
        tree.lock_tree_write()
216
        self.addCleanup(tree.unlock)
4119.5.1 by James Westby
Shelve can now shelve changes to a symlink's target.
217
        shelver = ExpectShelver(tree, tree.basis_tree())
4603.1.17 by Aaron Bentley
Fix shelf_ui tests to finalize.
218
        self.addCleanup(shelver.finalize)
4119.5.1 by James Westby
Shelve can now shelve changes to a symlink's target.
219
        shelver.expect('Shelve changing target of "baz" from "bar" to '
220
                '"vax"? [yNfq?]', 'y')
221
        shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
222
        shelver.run()
223
        self.assertEqual('bar', os.readlink('tree/baz'))
224
0.16.91 by Aaron Bentley
Test finish and quit
225
    def test_shelve_finish(self):
226
        tree = self.create_shelvable_tree()
4595.13.2 by Alexander Belchenko
[cherrypick revno 4650 from bzr.dev] Fix shelve on windows. (Robert Collins, #305006)
227
        tree.lock_tree_write()
228
        self.addCleanup(tree.unlock)
0.16.91 by Aaron Bentley
Test finish and quit
229
        shelver = ExpectShelver(tree, tree.basis_tree())
4603.1.17 by Aaron Bentley
Fix shelf_ui tests to finalize.
230
        self.addCleanup(shelver.finalize)
3990.4.1 by Daniel Watkins
Changed all shelve tests to expect a '?'.
231
        shelver.expect('Shelve? [yNfq?]', 'f')
232
        shelver.expect('Shelve 2 change(s)? [yNfq?]', 'y')
0.16.91 by Aaron Bentley
Test finish and quit
233
        shelver.run()
234
        self.assertFileEqual(LINES_AJ, 'tree/foo')
235
236
    def test_shelve_quit(self):
237
        tree = self.create_shelvable_tree()
4595.13.2 by Alexander Belchenko
[cherrypick revno 4650 from bzr.dev] Fix shelve on windows. (Robert Collins, #305006)
238
        tree.lock_tree_write()
239
        self.addCleanup(tree.unlock)
0.16.91 by Aaron Bentley
Test finish and quit
240
        shelver = ExpectShelver(tree, tree.basis_tree())
4603.1.17 by Aaron Bentley
Fix shelf_ui tests to finalize.
241
        self.addCleanup(shelver.finalize)
3990.4.1 by Daniel Watkins
Changed all shelve tests to expect a '?'.
242
        shelver.expect('Shelve? [yNfq?]', 'q')
0.16.103 by Aaron Bentley
raise UserAbort instead of doing sys.exit
243
        self.assertRaises(errors.UserAbort, shelver.run)
0.16.91 by Aaron Bentley
Test finish and quit
244
        self.assertFileEqual(LINES_ZY, 'tree/foo')
0.16.92 by Aaron Bentley
Test kind, add, binary, --all
245
246
    def test_shelve_all(self):
247
        tree = self.create_shelvable_tree()
4595.13.2 by Alexander Belchenko
[cherrypick revno 4650 from bzr.dev] Fix shelve on windows. (Robert Collins, #305006)
248
        shelver = ExpectShelver.from_args(sys.stdout, all=True,
249
            directory='tree')
250
        try:
251
            shelver.run()
252
        finally:
4603.1.17 by Aaron Bentley
Fix shelf_ui tests to finalize.
253
            shelver.finalize()
0.16.92 by Aaron Bentley
Test kind, add, binary, --all
254
        self.assertFileEqual(LINES_AJ, 'tree/foo')
0.16.93 by Aaron Bentley
Test shelving one file
255
256
    def test_shelve_filename(self):
257
        tree = self.create_shelvable_tree()
258
        self.build_tree(['tree/bar'])
259
        tree.add('bar')
4595.13.2 by Alexander Belchenko
[cherrypick revno 4650 from bzr.dev] Fix shelve on windows. (Robert Collins, #305006)
260
        tree.lock_tree_write()
261
        self.addCleanup(tree.unlock)
0.16.93 by Aaron Bentley
Test shelving one file
262
        shelver = ExpectShelver(tree, tree.basis_tree(), file_list=['bar'])
4603.1.17 by Aaron Bentley
Fix shelf_ui tests to finalize.
263
        self.addCleanup(shelver.finalize)
3990.4.1 by Daniel Watkins
Changed all shelve tests to expect a '?'.
264
        shelver.expect('Shelve adding file "bar"? [yNfq?]', 'y')
265
        shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
0.16.93 by Aaron Bentley
Test shelving one file
266
        shelver.run()
0.16.94 by Aaron Bentley
Add unshelve tests
267
3990.4.2 by Daniel Watkins
Added test for help option.
268
    def test_shelve_help(self):
269
        tree = self.create_shelvable_tree()
4595.13.2 by Alexander Belchenko
[cherrypick revno 4650 from bzr.dev] Fix shelve on windows. (Robert Collins, #305006)
270
        tree.lock_tree_write()
271
        self.addCleanup(tree.unlock)
3990.4.2 by Daniel Watkins
Added test for help option.
272
        shelver = ExpectShelver(tree, tree.basis_tree())
4603.1.17 by Aaron Bentley
Fix shelf_ui tests to finalize.
273
        self.addCleanup(shelver.finalize)
3990.4.2 by Daniel Watkins
Added test for help option.
274
        shelver.expect('Shelve? [yNfq?]', '?')
275
        shelver.expect('Shelve? [(y)es, (N)o, (f)inish, or (q)uit]', 'f')
276
        shelver.expect('Shelve 2 change(s)? [yNfq?]', 'y')
277
        shelver.run()
278
4603.1.17 by Aaron Bentley
Fix shelf_ui tests to finalize.
279
    def test_shelve_destroy(self):
4100.3.1 by Aaron Bentley
Implement shelve --destroy
280
        tree = self.create_shelvable_tree()
281
        shelver = shelf_ui.Shelver.from_args(sys.stdout, all=True,
282
                                             directory='tree', destroy=True)
4603.1.17 by Aaron Bentley
Fix shelf_ui tests to finalize.
283
        self.addCleanup(shelver.finalize)
284
        shelver.run()
4100.3.1 by Aaron Bentley
Implement shelve --destroy
285
        self.assertIs(None, tree.get_shelf_manager().last_shelf())
286
        self.assertFileEqual(LINES_AJ, 'tree/foo')
287
4595.9.5 by Aaron Bentley
Add expected failures for shelving root changes.
288
    @staticmethod
289
    def shelve_all(tree, target_revision_id):
290
        tree.lock_write()
291
        try:
292
            target = tree.branch.repository.revision_tree(target_revision_id)
293
            shelver = shelf_ui.Shelver(tree, target, auto=True,
294
                                       auto_apply=True)
4603.1.17 by Aaron Bentley
Fix shelf_ui tests to finalize.
295
            try:
296
                shelver.run()
297
            finally:
298
                shelver.finalize()
4595.9.5 by Aaron Bentley
Add expected failures for shelving root changes.
299
        finally:
300
            tree.unlock()
301
302
    def test_shelve_old_root_deleted(self):
303
        tree1 = self.make_branch_and_tree('tree1')
304
        tree1.commit('add root')
305
        tree2 = self.make_branch_and_tree('tree2')
306
        rev2 = tree2.commit('add root')
307
        tree1.merge_from_branch(tree2.branch,
308
                                from_revision=revision.NULL_REVISION)
309
        tree1.commit('Replaced root entry')
310
        # This is essentially assertNotRaises(InconsistentDelta)
311
        self.expectFailure('Cannot shelve replacing a root entry',
312
                           self.assertRaises, AssertionError,
313
                           self.assertRaises, errors.InconsistentDelta,
314
                           self.shelve_all, tree1, rev2)
315
316
    def test_shelve_split(self):
317
        outer_tree = self.make_branch_and_tree('outer')
318
        outer_tree.commit('Add root')
319
        inner_tree = self.make_branch_and_tree('outer/inner')
320
        rev2 = inner_tree.commit('Add root')
321
        outer_tree.subsume(inner_tree)
4595.9.6 by Aaron Bentley
Update from review.
322
        # This is essentially assertNotRaises(ValueError).
323
        # The ValueError is 'None is not a valid file id'.
4595.9.5 by Aaron Bentley
Add expected failures for shelving root changes.
324
        self.expectFailure('Cannot shelve a join back to the inner tree.',
325
                           self.assertRaises, AssertionError,
326
                           self.assertRaises, ValueError, self.shelve_all,
327
                           outer_tree, rev2)
328
0.16.94 by Aaron Bentley
Add unshelve tests
329
4526.7.1 by Aaron Bentley
Make vocabulary part of reporter.
330
class TestApplyReporter(TestShelver):
4526.6.1 by Aaron Bentley
Reverse the way changes are described by Shelver.
331
332
    def test_shelve_not_diff(self):
333
        tree = self.create_shelvable_tree()
4595.13.2 by Alexander Belchenko
[cherrypick revno 4650 from bzr.dev] Fix shelve on windows. (Robert Collins, #305006)
334
        tree.lock_tree_write()
335
        self.addCleanup(tree.unlock)
4526.7.1 by Aaron Bentley
Make vocabulary part of reporter.
336
        shelver = ExpectShelver(tree, tree.basis_tree(),
337
                                reporter=shelf_ui.ApplyReporter())
4603.1.17 by Aaron Bentley
Fix shelf_ui tests to finalize.
338
        self.addCleanup(shelver.finalize)
4526.6.1 by Aaron Bentley
Reverse the way changes are described by Shelver.
339
        shelver.expect('Apply change? [yNfq?]', 'n')
340
        shelver.expect('Apply change? [yNfq?]', 'n')
341
        # No final shelving prompt because no changes were selected
342
        shelver.run()
343
        self.assertFileEqual(LINES_ZY, 'tree/foo')
344
345
    def test_shelve_diff_no(self):
346
        tree = self.create_shelvable_tree()
4595.13.2 by Alexander Belchenko
[cherrypick revno 4650 from bzr.dev] Fix shelve on windows. (Robert Collins, #305006)
347
        tree.lock_tree_write()
348
        self.addCleanup(tree.unlock)
4526.7.1 by Aaron Bentley
Make vocabulary part of reporter.
349
        shelver = ExpectShelver(tree, tree.basis_tree(),
350
                                reporter=shelf_ui.ApplyReporter())
4603.1.17 by Aaron Bentley
Fix shelf_ui tests to finalize.
351
        self.addCleanup(shelver.finalize)
4526.6.1 by Aaron Bentley
Reverse the way changes are described by Shelver.
352
        shelver.expect('Apply change? [yNfq?]', 'y')
353
        shelver.expect('Apply change? [yNfq?]', 'y')
354
        shelver.expect('Apply 2 change(s)? [yNfq?]', 'n')
355
        shelver.run()
356
        self.assertFileEqual(LINES_ZY, 'tree/foo')
357
358
    def test_shelve_diff(self):
359
        tree = self.create_shelvable_tree()
4595.13.2 by Alexander Belchenko
[cherrypick revno 4650 from bzr.dev] Fix shelve on windows. (Robert Collins, #305006)
360
        tree.lock_tree_write()
361
        self.addCleanup(tree.unlock)
4526.7.1 by Aaron Bentley
Make vocabulary part of reporter.
362
        shelver = ExpectShelver(tree, tree.basis_tree(),
363
                                reporter=shelf_ui.ApplyReporter())
4603.1.17 by Aaron Bentley
Fix shelf_ui tests to finalize.
364
        self.addCleanup(shelver.finalize)
4526.6.1 by Aaron Bentley
Reverse the way changes are described by Shelver.
365
        shelver.expect('Apply change? [yNfq?]', 'y')
366
        shelver.expect('Apply change? [yNfq?]', 'y')
367
        shelver.expect('Apply 2 change(s)? [yNfq?]', 'y')
368
        shelver.run()
369
        self.assertFileEqual(LINES_AJ, 'tree/foo')
370
371
    def test_shelve_binary_change(self):
372
        tree = self.create_shelvable_tree()
373
        self.build_tree_contents([('tree/foo', '\x00')])
4595.13.2 by Alexander Belchenko
[cherrypick revno 4650 from bzr.dev] Fix shelve on windows. (Robert Collins, #305006)
374
        tree.lock_tree_write()
375
        self.addCleanup(tree.unlock)
4526.7.1 by Aaron Bentley
Make vocabulary part of reporter.
376
        shelver = ExpectShelver(tree, tree.basis_tree(),
377
                                reporter=shelf_ui.ApplyReporter())
4603.1.17 by Aaron Bentley
Fix shelf_ui tests to finalize.
378
        self.addCleanup(shelver.finalize)
4526.6.1 by Aaron Bentley
Reverse the way changes are described by Shelver.
379
        shelver.expect('Apply binary changes? [yNfq?]', 'y')
380
        shelver.expect('Apply 1 change(s)? [yNfq?]', 'y')
381
        shelver.run()
382
        self.assertFileEqual(LINES_AJ, 'tree/foo')
383
384
    def test_shelve_rename(self):
385
        tree = self.create_shelvable_tree()
386
        tree.rename_one('foo', 'bar')
4595.13.2 by Alexander Belchenko
[cherrypick revno 4650 from bzr.dev] Fix shelve on windows. (Robert Collins, #305006)
387
        tree.lock_tree_write()
388
        self.addCleanup(tree.unlock)
4526.7.1 by Aaron Bentley
Make vocabulary part of reporter.
389
        shelver = ExpectShelver(tree, tree.basis_tree(),
390
                                reporter=shelf_ui.ApplyReporter())
4603.1.17 by Aaron Bentley
Fix shelf_ui tests to finalize.
391
        self.addCleanup(shelver.finalize)
4526.6.1 by Aaron Bentley
Reverse the way changes are described by Shelver.
392
        shelver.expect('Rename "bar" => "foo"? [yNfq?]', 'y')
393
        shelver.expect('Apply change? [yNfq?]', 'y')
394
        shelver.expect('Apply change? [yNfq?]', 'y')
395
        shelver.expect('Apply 3 change(s)? [yNfq?]', 'y')
396
        shelver.run()
397
        self.assertFileEqual(LINES_AJ, 'tree/foo')
398
399
    def test_shelve_deletion(self):
400
        tree = self.create_shelvable_tree()
401
        os.unlink('tree/foo')
4595.13.2 by Alexander Belchenko
[cherrypick revno 4650 from bzr.dev] Fix shelve on windows. (Robert Collins, #305006)
402
        tree.lock_tree_write()
403
        self.addCleanup(tree.unlock)
4526.7.1 by Aaron Bentley
Make vocabulary part of reporter.
404
        shelver = ExpectShelver(tree, tree.basis_tree(),
405
                                reporter=shelf_ui.ApplyReporter())
4603.1.17 by Aaron Bentley
Fix shelf_ui tests to finalize.
406
        self.addCleanup(shelver.finalize)
4526.6.1 by Aaron Bentley
Reverse the way changes are described by Shelver.
407
        shelver.expect('Add file "foo"? [yNfq?]', 'y')
408
        shelver.expect('Apply 1 change(s)? [yNfq?]', 'y')
409
        shelver.run()
410
        self.assertFileEqual(LINES_AJ, 'tree/foo')
411
412
    def test_shelve_creation(self):
413
        tree = self.make_branch_and_tree('tree')
414
        tree.commit('add tree root')
415
        self.build_tree(['tree/foo'])
416
        tree.add('foo')
4595.13.2 by Alexander Belchenko
[cherrypick revno 4650 from bzr.dev] Fix shelve on windows. (Robert Collins, #305006)
417
        tree.lock_tree_write()
418
        self.addCleanup(tree.unlock)
4526.7.1 by Aaron Bentley
Make vocabulary part of reporter.
419
        shelver = ExpectShelver(tree, tree.basis_tree(),
420
                                reporter=shelf_ui.ApplyReporter())
4603.1.17 by Aaron Bentley
Fix shelf_ui tests to finalize.
421
        self.addCleanup(shelver.finalize)
4526.6.1 by Aaron Bentley
Reverse the way changes are described by Shelver.
422
        shelver.expect('Delete file "foo"? [yNfq?]', 'y')
423
        shelver.expect('Apply 1 change(s)? [yNfq?]', 'y')
424
        shelver.run()
425
        self.failIfExists('tree/foo')
426
427
    def test_shelve_kind_change(self):
428
        tree = self.create_shelvable_tree()
429
        os.unlink('tree/foo')
430
        os.mkdir('tree/foo')
4595.13.2 by Alexander Belchenko
[cherrypick revno 4650 from bzr.dev] Fix shelve on windows. (Robert Collins, #305006)
431
        tree.lock_tree_write()
432
        self.addCleanup(tree.unlock)
4526.7.1 by Aaron Bentley
Make vocabulary part of reporter.
433
        shelver = ExpectShelver(tree, tree.basis_tree(),
434
                               reporter=shelf_ui.ApplyReporter())
4603.1.17 by Aaron Bentley
Fix shelf_ui tests to finalize.
435
        self.addCleanup(shelver.finalize)
4526.6.1 by Aaron Bentley
Reverse the way changes are described by Shelver.
436
        shelver.expect('Change "foo" from directory to a file? [yNfq?]', 'y')
437
        shelver.expect('Apply 1 change(s)? [yNfq?]', 'y')
438
439
    def test_shelve_modify_target(self):
4526.6.12 by Aaron Bentley
Updates from review.
440
        self.requireFeature(tests.SymlinkFeature)
4526.6.1 by Aaron Bentley
Reverse the way changes are described by Shelver.
441
        tree = self.create_shelvable_tree()
442
        os.symlink('bar', 'tree/baz')
443
        tree.add('baz', 'baz-id')
444
        tree.commit("Add symlink")
445
        os.unlink('tree/baz')
446
        os.symlink('vax', 'tree/baz')
4595.13.2 by Alexander Belchenko
[cherrypick revno 4650 from bzr.dev] Fix shelve on windows. (Robert Collins, #305006)
447
        tree.lock_tree_write()
448
        self.addCleanup(tree.unlock)
4526.7.1 by Aaron Bentley
Make vocabulary part of reporter.
449
        shelver = ExpectShelver(tree, tree.basis_tree(),
450
                                reporter=shelf_ui.ApplyReporter())
4603.1.17 by Aaron Bentley
Fix shelf_ui tests to finalize.
451
        self.addCleanup(shelver.finalize)
4526.6.1 by Aaron Bentley
Reverse the way changes are described by Shelver.
452
        shelver.expect('Change target of "baz" from "vax" to "bar"? [yNfq?]',
453
                       'y')
454
        shelver.expect('Apply 1 change(s)? [yNfq?]', 'y')
455
        shelver.run()
456
        self.assertEqual('bar', os.readlink('tree/baz'))
457
458
0.16.94 by Aaron Bentley
Add unshelve tests
459
class TestUnshelver(tests.TestCaseWithTransport):
460
461
    def create_tree_with_shelf(self):
462
        tree = self.make_branch_and_tree('tree')
4595.13.2 by Alexander Belchenko
[cherrypick revno 4650 from bzr.dev] Fix shelve on windows. (Robert Collins, #305006)
463
        tree.lock_write()
464
        try:
465
            self.build_tree_contents([('tree/foo', LINES_AJ)])
466
            tree.add('foo', 'foo-id')
467
            tree.commit('added foo')
468
            self.build_tree_contents([('tree/foo', LINES_ZY)])
4603.1.17 by Aaron Bentley
Fix shelf_ui tests to finalize.
469
            shelver = shelf_ui.Shelver(tree, tree.basis_tree(),
470
                                       auto_apply=True, auto=True)
471
            try:
472
                shelver.run()
473
            finally:
474
                shelver.finalize()
4595.13.2 by Alexander Belchenko
[cherrypick revno 4650 from bzr.dev] Fix shelve on windows. (Robert Collins, #305006)
475
        finally:
476
            tree.unlock()
0.16.94 by Aaron Bentley
Add unshelve tests
477
        return tree
478
479
    def test_unshelve(self):
480
        tree = self.create_tree_with_shelf()
481
        tree.lock_write()
482
        self.addCleanup(tree.unlock)
483
        manager = tree.get_shelf_manager()
484
        shelf_ui.Unshelver(tree, manager, 1, True, True, True).run()
485
        self.assertFileEqual(LINES_ZY, 'tree/foo')
486
487
    def test_unshelve_args(self):
488
        tree = self.create_tree_with_shelf()
4595.13.2 by Alexander Belchenko
[cherrypick revno 4650 from bzr.dev] Fix shelve on windows. (Robert Collins, #305006)
489
        unshelver = shelf_ui.Unshelver.from_args(directory='tree')
490
        try:
491
            unshelver.run()
492
        finally:
493
            unshelver.tree.unlock()
0.16.94 by Aaron Bentley
Add unshelve tests
494
        self.assertFileEqual(LINES_ZY, 'tree/foo')
495
        self.assertIs(None, tree.get_shelf_manager().last_shelf())
496
4902.1.5 by Guilherme Salgado
Re-add the test for Unshelver.dry_run and twek the test for preview
497
    def test_unshelve_args_dry_run(self):
498
        tree = self.create_tree_with_shelf()
499
        unshelver = shelf_ui.Unshelver.from_args(directory='tree',
500
            action='dry-run')
501
        try:
502
            unshelver.run()
503
        finally:
504
            unshelver.tree.unlock()
505
        self.assertFileEqual(LINES_AJ, 'tree/foo')
506
        self.assertEqual(1, tree.get_shelf_manager().last_shelf())
507
4902.1.3 by Guilherme Salgado
A few tweaks as per John's review
508
    def test_unshelve_args_preview(self):
0.16.94 by Aaron Bentley
Add unshelve tests
509
        tree = self.create_tree_with_shelf()
4902.1.3 by Guilherme Salgado
A few tweaks as per John's review
510
        write_diff_to = StringIO()
511
        unshelver = shelf_ui.Unshelver.from_args(
512
            directory='tree', action='preview', write_diff_to=write_diff_to)
4595.13.2 by Alexander Belchenko
[cherrypick revno 4650 from bzr.dev] Fix shelve on windows. (Robert Collins, #305006)
513
        try:
514
            unshelver.run()
515
        finally:
516
            unshelver.tree.unlock()
4902.1.3 by Guilherme Salgado
A few tweaks as per John's review
517
        # The changes were not unshelved.
0.16.94 by Aaron Bentley
Add unshelve tests
518
        self.assertFileEqual(LINES_AJ, 'tree/foo')
519
        self.assertEqual(1, tree.get_shelf_manager().last_shelf())
4902.1.3 by Guilherme Salgado
A few tweaks as per John's review
520
521
        # But the diff was written to write_diff_to.
4902.1.5 by Guilherme Salgado
Re-add the test for Unshelver.dry_run and twek the test for preview
522
        diff = write_diff_to.getvalue()
4902.1.6 by Guilherme Salgado
Tweak the text to check to assert the meat of the diff is what we expect, instead of just checking it matches some regexps
523
        expected = dedent("""\
524
            @@ -1,4 +1,4 @@
525
            -a
526
            +z
527
             b
528
             c
529
             d
530
            @@ -7,4 +7,4 @@
531
             g
532
             h
533
             i
534
            -j
535
            +y
536
537
            """)
538
        self.assertEqualDiff(expected, diff[-len(expected):])
0.16.94 by Aaron Bentley
Add unshelve tests
539
540
    def test_unshelve_args_delete_only(self):
541
        tree = self.make_branch_and_tree('tree')
542
        manager = tree.get_shelf_manager()
543
        shelf_file = manager.new_shelf()[1]
544
        try:
545
            shelf_file.write('garbage')
546
        finally:
547
            shelf_file.close()
548
        unshelver = shelf_ui.Unshelver.from_args(directory='tree',
549
                                                 action='delete-only')
4595.13.2 by Alexander Belchenko
[cherrypick revno 4650 from bzr.dev] Fix shelve on windows. (Robert Collins, #305006)
550
        try:
551
            unshelver.run()
552
        finally:
553
            unshelver.tree.unlock()
0.16.94 by Aaron Bentley
Add unshelve tests
554
        self.assertIs(None, manager.last_shelf())
3990.2.1 by Daniel Watkins
Added test for unshelve being passed an invalid shelf_id.
555
556
    def test_unshelve_args_invalid_shelf_id(self):
557
        tree = self.make_branch_and_tree('tree')
558
        manager = tree.get_shelf_manager()
559
        shelf_file = manager.new_shelf()[1]
560
        try:
561
            shelf_file.write('garbage')
562
        finally:
563
            shelf_file.close()
564
        self.assertRaises(errors.InvalidShelfId,
3999.1.1 by Ian Clatworthy
Improve shelf documentation & fix backtrace (Daniel Watkins)
565
            shelf_ui.Unshelver.from_args, directory='tree',
566
            action='delete-only', shelf_id='foo')
4899.2.1 by Neil Martinsen-Burrell
add beter feedback from the unshelve command
567
4899.2.2 by Neil Martinsen-Burrell
tests updated to use the appropriate TestCase... class for scripts
568
569
class TestUnshelveScripts(TestUnshelver, 
570
                          script.TestCaseWithTransportAndScript): 
571
4899.2.1 by Neil Martinsen-Burrell
add beter feedback from the unshelve command
572
    def test_unshelve_messages_keep(self):
573
        self.create_tree_with_shelf()
4899.2.2 by Neil Martinsen-Burrell
tests updated to use the appropriate TestCase... class for scripts
574
        self.run_script("""
4899.2.1 by Neil Martinsen-Burrell
add beter feedback from the unshelve command
575
$ cd tree
576
$ bzr unshelve --keep
577
2>Using changes with id "1".
578
2> M  foo
579
2>All changes applied successfully.
580
""")
581
582
    def test_unshelve_messages_delete(self):
583
        self.create_tree_with_shelf()
4899.2.2 by Neil Martinsen-Burrell
tests updated to use the appropriate TestCase... class for scripts
584
        self.run_script("""
4899.2.1 by Neil Martinsen-Burrell
add beter feedback from the unshelve command
585
$ cd tree
586
$ bzr unshelve --delete-only
587
2>Deleted changes with id "1".
588
""")
589
590
    def test_unshelve_messages_apply(self):
591
        self.create_tree_with_shelf()
4899.2.2 by Neil Martinsen-Burrell
tests updated to use the appropriate TestCase... class for scripts
592
        self.run_script("""
4899.2.1 by Neil Martinsen-Burrell
add beter feedback from the unshelve command
593
$ cd tree
594
$ bzr unshelve --apply
595
2>Using changes with id "1".
596
2> M  foo
597
2>All changes applied successfully.
598
2>Deleted changes with id "1".
599
""")
600
601
    def test_unshelve_messages_dry_run(self):
602
        self.create_tree_with_shelf()
4899.2.2 by Neil Martinsen-Burrell
tests updated to use the appropriate TestCase... class for scripts
603
        self.run_script("""
4899.2.1 by Neil Martinsen-Burrell
add beter feedback from the unshelve command
604
$ cd tree
605
$ bzr unshelve --dry-run
606
2>Using changes with id "1".
607
2> M  foo
608
""")