/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
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
5954.2.3 by Aaron Bentley
Fix test duplication in TestApplyReporter
66
class ShelfTestCase(tests.TestCaseWithTransport):
0.16.89 by Aaron Bentley
Add tests for Shelver
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
5954.2.3 by Aaron Bentley
Fix test duplication in TestApplyReporter
76
77
class TestShelver(ShelfTestCase):
78
0.16.89 by Aaron Bentley
Add tests for Shelver
79
    def test_unexpected_prompt_failure(self):
80
        tree = self.create_shelvable_tree()
4595.13.2 by Alexander Belchenko
[cherrypick revno 4650 from bzr.dev] Fix shelve on windows. (Robert Collins, #305006)
81
        tree.lock_tree_write()
82
        self.addCleanup(tree.unlock)
0.16.89 by Aaron Bentley
Add tests for Shelver
83
        shelver = ExpectShelver(tree, tree.basis_tree())
4603.1.17 by Aaron Bentley
Fix shelf_ui tests to finalize.
84
        self.addCleanup(shelver.finalize)
0.16.89 by Aaron Bentley
Add tests for Shelver
85
        e = self.assertRaises(AssertionError, shelver.run)
3990.4.1 by Daniel Watkins
Changed all shelve tests to expect a '?'.
86
        self.assertEqual('Unexpected prompt: Shelve? [yNfq?]', str(e))
0.16.89 by Aaron Bentley
Add tests for Shelver
87
88
    def test_wrong_prompt_failure(self):
89
        tree = self.create_shelvable_tree()
4595.13.2 by Alexander Belchenko
[cherrypick revno 4650 from bzr.dev] Fix shelve on windows. (Robert Collins, #305006)
90
        tree.lock_tree_write()
91
        self.addCleanup(tree.unlock)
0.16.89 by Aaron Bentley
Add tests for Shelver
92
        shelver = ExpectShelver(tree, tree.basis_tree())
4603.1.17 by Aaron Bentley
Fix shelf_ui tests to finalize.
93
        self.addCleanup(shelver.finalize)
0.16.89 by Aaron Bentley
Add tests for Shelver
94
        shelver.expect('foo', 'y')
95
        e = self.assertRaises(AssertionError, shelver.run)
3990.4.1 by Daniel Watkins
Changed all shelve tests to expect a '?'.
96
        self.assertEqual('Wrong prompt: Shelve? [yNfq?]', str(e))
0.16.89 by Aaron Bentley
Add tests for Shelver
97
98
    def test_shelve_not_diff(self):
99
        tree = self.create_shelvable_tree()
4595.13.2 by Alexander Belchenko
[cherrypick revno 4650 from bzr.dev] Fix shelve on windows. (Robert Collins, #305006)
100
        tree.lock_tree_write()
101
        self.addCleanup(tree.unlock)
0.16.89 by Aaron Bentley
Add tests for Shelver
102
        shelver = ExpectShelver(tree, tree.basis_tree())
4603.1.17 by Aaron Bentley
Fix shelf_ui tests to finalize.
103
        self.addCleanup(shelver.finalize)
3990.4.1 by Daniel Watkins
Changed all shelve tests to expect a '?'.
104
        shelver.expect('Shelve? [yNfq?]', 'n')
105
        shelver.expect('Shelve? [yNfq?]', 'n')
0.16.89 by Aaron Bentley
Add tests for Shelver
106
        # No final shelving prompt because no changes were selected
107
        shelver.run()
0.16.90 by Aaron Bentley
Handle shelving multiple diff hunks
108
        self.assertFileEqual(LINES_ZY, 'tree/foo')
0.16.89 by Aaron Bentley
Add tests for Shelver
109
110
    def test_shelve_diff_no(self):
111
        tree = self.create_shelvable_tree()
4595.13.2 by Alexander Belchenko
[cherrypick revno 4650 from bzr.dev] Fix shelve on windows. (Robert Collins, #305006)
112
        tree.lock_tree_write()
113
        self.addCleanup(tree.unlock)
0.16.89 by Aaron Bentley
Add tests for Shelver
114
        shelver = ExpectShelver(tree, tree.basis_tree())
4603.1.17 by Aaron Bentley
Fix shelf_ui tests to finalize.
115
        self.addCleanup(shelver.finalize)
3990.4.1 by Daniel Watkins
Changed all shelve tests to expect a '?'.
116
        shelver.expect('Shelve? [yNfq?]', 'y')
117
        shelver.expect('Shelve? [yNfq?]', 'y')
118
        shelver.expect('Shelve 2 change(s)? [yNfq?]', 'n')
0.16.89 by Aaron Bentley
Add tests for Shelver
119
        shelver.run()
0.16.90 by Aaron Bentley
Handle shelving multiple diff hunks
120
        self.assertFileEqual(LINES_ZY, 'tree/foo')
0.16.89 by Aaron Bentley
Add tests for Shelver
121
122
    def test_shelve_diff(self):
123
        tree = self.create_shelvable_tree()
4595.13.2 by Alexander Belchenko
[cherrypick revno 4650 from bzr.dev] Fix shelve on windows. (Robert Collins, #305006)
124
        tree.lock_tree_write()
125
        self.addCleanup(tree.unlock)
0.16.89 by Aaron Bentley
Add tests for Shelver
126
        shelver = ExpectShelver(tree, tree.basis_tree())
4603.1.17 by Aaron Bentley
Fix shelf_ui tests to finalize.
127
        self.addCleanup(shelver.finalize)
3990.4.1 by Daniel Watkins
Changed all shelve tests to expect a '?'.
128
        shelver.expect('Shelve? [yNfq?]', 'y')
129
        shelver.expect('Shelve? [yNfq?]', 'y')
130
        shelver.expect('Shelve 2 change(s)? [yNfq?]', 'y')
0.16.90 by Aaron Bentley
Handle shelving multiple diff hunks
131
        shelver.run()
132
        self.assertFileEqual(LINES_AJ, 'tree/foo')
133
134
    def test_shelve_one_diff(self):
135
        tree = self.create_shelvable_tree()
4595.13.2 by Alexander Belchenko
[cherrypick revno 4650 from bzr.dev] Fix shelve on windows. (Robert Collins, #305006)
136
        tree.lock_tree_write()
137
        self.addCleanup(tree.unlock)
0.16.90 by Aaron Bentley
Handle shelving multiple diff hunks
138
        shelver = ExpectShelver(tree, tree.basis_tree())
4603.1.17 by Aaron Bentley
Fix shelf_ui tests to finalize.
139
        self.addCleanup(shelver.finalize)
3990.4.1 by Daniel Watkins
Changed all shelve tests to expect a '?'.
140
        shelver.expect('Shelve? [yNfq?]', 'y')
141
        shelver.expect('Shelve? [yNfq?]', 'n')
142
        shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
0.16.89 by Aaron Bentley
Add tests for Shelver
143
        shelver.run()
0.16.90 by Aaron Bentley
Handle shelving multiple diff hunks
144
        self.assertFileEqual(LINES_AY, 'tree/foo')
0.16.89 by Aaron Bentley
Add tests for Shelver
145
0.16.92 by Aaron Bentley
Test kind, add, binary, --all
146
    def test_shelve_binary_change(self):
147
        tree = self.create_shelvable_tree()
148
        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)
149
        tree.lock_tree_write()
150
        self.addCleanup(tree.unlock)
0.16.92 by Aaron Bentley
Test kind, add, binary, --all
151
        shelver = ExpectShelver(tree, tree.basis_tree())
4603.1.17 by Aaron Bentley
Fix shelf_ui tests to finalize.
152
        self.addCleanup(shelver.finalize)
3990.4.1 by Daniel Watkins
Changed all shelve tests to expect a '?'.
153
        shelver.expect('Shelve binary changes? [yNfq?]', 'y')
154
        shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
0.16.92 by Aaron Bentley
Test kind, add, binary, --all
155
        shelver.run()
156
        self.assertFileEqual(LINES_AJ, 'tree/foo')
157
0.16.89 by Aaron Bentley
Add tests for Shelver
158
    def test_shelve_rename(self):
159
        tree = self.create_shelvable_tree()
160
        tree.rename_one('foo', 'bar')
4595.13.2 by Alexander Belchenko
[cherrypick revno 4650 from bzr.dev] Fix shelve on windows. (Robert Collins, #305006)
161
        tree.lock_tree_write()
162
        self.addCleanup(tree.unlock)
0.16.89 by Aaron Bentley
Add tests for Shelver
163
        shelver = ExpectShelver(tree, tree.basis_tree())
4603.1.17 by Aaron Bentley
Fix shelf_ui tests to finalize.
164
        self.addCleanup(shelver.finalize)
3990.4.1 by Daniel Watkins
Changed all shelve tests to expect a '?'.
165
        shelver.expect('Shelve renaming "foo" => "bar"? [yNfq?]', 'y')
166
        shelver.expect('Shelve? [yNfq?]', 'y')
167
        shelver.expect('Shelve? [yNfq?]', 'y')
168
        shelver.expect('Shelve 3 change(s)? [yNfq?]', 'y')
0.16.89 by Aaron Bentley
Add tests for Shelver
169
        shelver.run()
0.16.90 by Aaron Bentley
Handle shelving multiple diff hunks
170
        self.assertFileEqual(LINES_AJ, 'tree/foo')
0.16.91 by Aaron Bentley
Test finish and quit
171
172
    def test_shelve_deletion(self):
173
        tree = self.create_shelvable_tree()
174
        os.unlink('tree/foo')
4595.13.2 by Alexander Belchenko
[cherrypick revno 4650 from bzr.dev] Fix shelve on windows. (Robert Collins, #305006)
175
        tree.lock_tree_write()
176
        self.addCleanup(tree.unlock)
0.16.91 by Aaron Bentley
Test finish and quit
177
        shelver = ExpectShelver(tree, tree.basis_tree())
4603.1.17 by Aaron Bentley
Fix shelf_ui tests to finalize.
178
        self.addCleanup(shelver.finalize)
3990.4.1 by Daniel Watkins
Changed all shelve tests to expect a '?'.
179
        shelver.expect('Shelve removing file "foo"? [yNfq?]', 'y')
180
        shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
0.16.91 by Aaron Bentley
Test finish and quit
181
        shelver.run()
182
        self.assertFileEqual(LINES_AJ, 'tree/foo')
183
0.16.92 by Aaron Bentley
Test kind, add, binary, --all
184
    def test_shelve_creation(self):
185
        tree = self.make_branch_and_tree('tree')
186
        tree.commit('add tree root')
187
        self.build_tree(['tree/foo'])
188
        tree.add('foo')
4595.13.2 by Alexander Belchenko
[cherrypick revno 4650 from bzr.dev] Fix shelve on windows. (Robert Collins, #305006)
189
        tree.lock_tree_write()
190
        self.addCleanup(tree.unlock)
0.16.92 by Aaron Bentley
Test kind, add, binary, --all
191
        shelver = ExpectShelver(tree, tree.basis_tree())
4603.1.17 by Aaron Bentley
Fix shelf_ui tests to finalize.
192
        self.addCleanup(shelver.finalize)
3990.4.1 by Daniel Watkins
Changed all shelve tests to expect a '?'.
193
        shelver.expect('Shelve adding file "foo"? [yNfq?]', 'y')
194
        shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
0.16.92 by Aaron Bentley
Test kind, add, binary, --all
195
        shelver.run()
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
196
        self.assertPathDoesNotExist('tree/foo')
0.16.92 by Aaron Bentley
Test kind, add, binary, --all
197
198
    def test_shelve_kind_change(self):
199
        tree = self.create_shelvable_tree()
200
        os.unlink('tree/foo')
201
        os.mkdir('tree/foo')
4595.13.2 by Alexander Belchenko
[cherrypick revno 4650 from bzr.dev] Fix shelve on windows. (Robert Collins, #305006)
202
        tree.lock_tree_write()
203
        self.addCleanup(tree.unlock)
0.16.92 by Aaron Bentley
Test kind, add, binary, --all
204
        shelver = ExpectShelver(tree, tree.basis_tree())
4603.1.17 by Aaron Bentley
Fix shelf_ui tests to finalize.
205
        self.addCleanup(shelver.finalize)
3990.4.1 by Daniel Watkins
Changed all shelve tests to expect a '?'.
206
        shelver.expect('Shelve changing "foo" from file to directory? [yNfq?]',
0.16.92 by Aaron Bentley
Test kind, add, binary, --all
207
                       'y')
3990.4.1 by Daniel Watkins
Changed all shelve tests to expect a '?'.
208
        shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
0.16.92 by Aaron Bentley
Test kind, add, binary, --all
209
4119.5.1 by James Westby
Shelve can now shelve changes to a symlink's target.
210
    def test_shelve_modify_target(self):
4526.6.12 by Aaron Bentley
Updates from review.
211
        self.requireFeature(tests.SymlinkFeature)
4119.5.1 by James Westby
Shelve can now shelve changes to a symlink's target.
212
        tree = self.create_shelvable_tree()
213
        os.symlink('bar', 'tree/baz')
214
        tree.add('baz', 'baz-id')
215
        tree.commit("Add symlink")
216
        os.unlink('tree/baz')
217
        os.symlink('vax', 'tree/baz')
4595.13.2 by Alexander Belchenko
[cherrypick revno 4650 from bzr.dev] Fix shelve on windows. (Robert Collins, #305006)
218
        tree.lock_tree_write()
219
        self.addCleanup(tree.unlock)
4119.5.1 by James Westby
Shelve can now shelve changes to a symlink's target.
220
        shelver = ExpectShelver(tree, tree.basis_tree())
4603.1.17 by Aaron Bentley
Fix shelf_ui tests to finalize.
221
        self.addCleanup(shelver.finalize)
4119.5.1 by James Westby
Shelve can now shelve changes to a symlink's target.
222
        shelver.expect('Shelve changing target of "baz" from "bar" to '
223
                '"vax"? [yNfq?]', 'y')
224
        shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
225
        shelver.run()
226
        self.assertEqual('bar', os.readlink('tree/baz'))
227
0.16.91 by Aaron Bentley
Test finish and quit
228
    def test_shelve_finish(self):
229
        tree = self.create_shelvable_tree()
4595.13.2 by Alexander Belchenko
[cherrypick revno 4650 from bzr.dev] Fix shelve on windows. (Robert Collins, #305006)
230
        tree.lock_tree_write()
231
        self.addCleanup(tree.unlock)
0.16.91 by Aaron Bentley
Test finish and quit
232
        shelver = ExpectShelver(tree, tree.basis_tree())
4603.1.17 by Aaron Bentley
Fix shelf_ui tests to finalize.
233
        self.addCleanup(shelver.finalize)
3990.4.1 by Daniel Watkins
Changed all shelve tests to expect a '?'.
234
        shelver.expect('Shelve? [yNfq?]', 'f')
235
        shelver.expect('Shelve 2 change(s)? [yNfq?]', 'y')
0.16.91 by Aaron Bentley
Test finish and quit
236
        shelver.run()
237
        self.assertFileEqual(LINES_AJ, 'tree/foo')
238
239
    def test_shelve_quit(self):
240
        tree = self.create_shelvable_tree()
4595.13.2 by Alexander Belchenko
[cherrypick revno 4650 from bzr.dev] Fix shelve on windows. (Robert Collins, #305006)
241
        tree.lock_tree_write()
242
        self.addCleanup(tree.unlock)
0.16.91 by Aaron Bentley
Test finish and quit
243
        shelver = ExpectShelver(tree, tree.basis_tree())
4603.1.17 by Aaron Bentley
Fix shelf_ui tests to finalize.
244
        self.addCleanup(shelver.finalize)
3990.4.1 by Daniel Watkins
Changed all shelve tests to expect a '?'.
245
        shelver.expect('Shelve? [yNfq?]', 'q')
0.16.103 by Aaron Bentley
raise UserAbort instead of doing sys.exit
246
        self.assertRaises(errors.UserAbort, shelver.run)
0.16.91 by Aaron Bentley
Test finish and quit
247
        self.assertFileEqual(LINES_ZY, 'tree/foo')
0.16.92 by Aaron Bentley
Test kind, add, binary, --all
248
249
    def test_shelve_all(self):
250
        tree = self.create_shelvable_tree()
4595.13.2 by Alexander Belchenko
[cherrypick revno 4650 from bzr.dev] Fix shelve on windows. (Robert Collins, #305006)
251
        shelver = ExpectShelver.from_args(sys.stdout, all=True,
252
            directory='tree')
253
        try:
254
            shelver.run()
255
        finally:
4603.1.17 by Aaron Bentley
Fix shelf_ui tests to finalize.
256
            shelver.finalize()
0.16.92 by Aaron Bentley
Test kind, add, binary, --all
257
        self.assertFileEqual(LINES_AJ, 'tree/foo')
0.16.93 by Aaron Bentley
Test shelving one file
258
259
    def test_shelve_filename(self):
260
        tree = self.create_shelvable_tree()
261
        self.build_tree(['tree/bar'])
262
        tree.add('bar')
4595.13.2 by Alexander Belchenko
[cherrypick revno 4650 from bzr.dev] Fix shelve on windows. (Robert Collins, #305006)
263
        tree.lock_tree_write()
264
        self.addCleanup(tree.unlock)
0.16.93 by Aaron Bentley
Test shelving one file
265
        shelver = ExpectShelver(tree, tree.basis_tree(), file_list=['bar'])
4603.1.17 by Aaron Bentley
Fix shelf_ui tests to finalize.
266
        self.addCleanup(shelver.finalize)
3990.4.1 by Daniel Watkins
Changed all shelve tests to expect a '?'.
267
        shelver.expect('Shelve adding file "bar"? [yNfq?]', 'y')
268
        shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
0.16.93 by Aaron Bentley
Test shelving one file
269
        shelver.run()
0.16.94 by Aaron Bentley
Add unshelve tests
270
3990.4.2 by Daniel Watkins
Added test for help option.
271
    def test_shelve_help(self):
272
        tree = self.create_shelvable_tree()
4595.13.2 by Alexander Belchenko
[cherrypick revno 4650 from bzr.dev] Fix shelve on windows. (Robert Collins, #305006)
273
        tree.lock_tree_write()
274
        self.addCleanup(tree.unlock)
3990.4.2 by Daniel Watkins
Added test for help option.
275
        shelver = ExpectShelver(tree, tree.basis_tree())
4603.1.17 by Aaron Bentley
Fix shelf_ui tests to finalize.
276
        self.addCleanup(shelver.finalize)
3990.4.2 by Daniel Watkins
Added test for help option.
277
        shelver.expect('Shelve? [yNfq?]', '?')
278
        shelver.expect('Shelve? [(y)es, (N)o, (f)inish, or (q)uit]', 'f')
279
        shelver.expect('Shelve 2 change(s)? [yNfq?]', 'y')
280
        shelver.run()
281
4603.1.17 by Aaron Bentley
Fix shelf_ui tests to finalize.
282
    def test_shelve_destroy(self):
4100.3.1 by Aaron Bentley
Implement shelve --destroy
283
        tree = self.create_shelvable_tree()
284
        shelver = shelf_ui.Shelver.from_args(sys.stdout, all=True,
285
                                             directory='tree', destroy=True)
4603.1.17 by Aaron Bentley
Fix shelf_ui tests to finalize.
286
        self.addCleanup(shelver.finalize)
287
        shelver.run()
4100.3.1 by Aaron Bentley
Implement shelve --destroy
288
        self.assertIs(None, tree.get_shelf_manager().last_shelf())
289
        self.assertFileEqual(LINES_AJ, 'tree/foo')
290
4595.9.5 by Aaron Bentley
Add expected failures for shelving root changes.
291
    @staticmethod
292
    def shelve_all(tree, target_revision_id):
293
        tree.lock_write()
294
        try:
295
            target = tree.branch.repository.revision_tree(target_revision_id)
296
            shelver = shelf_ui.Shelver(tree, target, auto=True,
297
                                       auto_apply=True)
4603.1.17 by Aaron Bentley
Fix shelf_ui tests to finalize.
298
            try:
299
                shelver.run()
300
            finally:
301
                shelver.finalize()
4595.9.5 by Aaron Bentley
Add expected failures for shelving root changes.
302
        finally:
303
            tree.unlock()
304
305
    def test_shelve_old_root_deleted(self):
306
        tree1 = self.make_branch_and_tree('tree1')
307
        tree1.commit('add root')
308
        tree2 = self.make_branch_and_tree('tree2')
309
        rev2 = tree2.commit('add root')
310
        tree1.merge_from_branch(tree2.branch,
311
                                from_revision=revision.NULL_REVISION)
312
        tree1.commit('Replaced root entry')
313
        # This is essentially assertNotRaises(InconsistentDelta)
314
        self.expectFailure('Cannot shelve replacing a root entry',
315
                           self.assertRaises, AssertionError,
316
                           self.assertRaises, errors.InconsistentDelta,
317
                           self.shelve_all, tree1, rev2)
318
319
    def test_shelve_split(self):
320
        outer_tree = self.make_branch_and_tree('outer')
321
        outer_tree.commit('Add root')
322
        inner_tree = self.make_branch_and_tree('outer/inner')
323
        rev2 = inner_tree.commit('Add root')
324
        outer_tree.subsume(inner_tree)
4595.9.6 by Aaron Bentley
Update from review.
325
        # This is essentially assertNotRaises(ValueError).
326
        # The ValueError is 'None is not a valid file id'.
4595.9.5 by Aaron Bentley
Add expected failures for shelving root changes.
327
        self.expectFailure('Cannot shelve a join back to the inner tree.',
328
                           self.assertRaises, AssertionError,
329
                           self.assertRaises, ValueError, self.shelve_all,
330
                           outer_tree, rev2)
331
0.16.94 by Aaron Bentley
Add unshelve tests
332
5954.2.3 by Aaron Bentley
Fix test duplication in TestApplyReporter
333
class TestApplyReporter(ShelfTestCase):
4526.6.1 by Aaron Bentley
Reverse the way changes are described by Shelver.
334
335
    def test_shelve_not_diff(self):
336
        tree = self.create_shelvable_tree()
4595.13.2 by Alexander Belchenko
[cherrypick revno 4650 from bzr.dev] Fix shelve on windows. (Robert Collins, #305006)
337
        tree.lock_tree_write()
338
        self.addCleanup(tree.unlock)
4526.7.1 by Aaron Bentley
Make vocabulary part of reporter.
339
        shelver = ExpectShelver(tree, tree.basis_tree(),
340
                                reporter=shelf_ui.ApplyReporter())
4603.1.17 by Aaron Bentley
Fix shelf_ui tests to finalize.
341
        self.addCleanup(shelver.finalize)
4526.6.1 by Aaron Bentley
Reverse the way changes are described by Shelver.
342
        shelver.expect('Apply change? [yNfq?]', 'n')
343
        shelver.expect('Apply change? [yNfq?]', 'n')
344
        # No final shelving prompt because no changes were selected
345
        shelver.run()
346
        self.assertFileEqual(LINES_ZY, 'tree/foo')
347
348
    def test_shelve_diff_no(self):
349
        tree = self.create_shelvable_tree()
4595.13.2 by Alexander Belchenko
[cherrypick revno 4650 from bzr.dev] Fix shelve on windows. (Robert Collins, #305006)
350
        tree.lock_tree_write()
351
        self.addCleanup(tree.unlock)
4526.7.1 by Aaron Bentley
Make vocabulary part of reporter.
352
        shelver = ExpectShelver(tree, tree.basis_tree(),
353
                                reporter=shelf_ui.ApplyReporter())
4603.1.17 by Aaron Bentley
Fix shelf_ui tests to finalize.
354
        self.addCleanup(shelver.finalize)
4526.6.1 by Aaron Bentley
Reverse the way changes are described by Shelver.
355
        shelver.expect('Apply change? [yNfq?]', 'y')
356
        shelver.expect('Apply change? [yNfq?]', 'y')
357
        shelver.expect('Apply 2 change(s)? [yNfq?]', 'n')
358
        shelver.run()
359
        self.assertFileEqual(LINES_ZY, 'tree/foo')
360
361
    def test_shelve_diff(self):
362
        tree = self.create_shelvable_tree()
4595.13.2 by Alexander Belchenko
[cherrypick revno 4650 from bzr.dev] Fix shelve on windows. (Robert Collins, #305006)
363
        tree.lock_tree_write()
364
        self.addCleanup(tree.unlock)
4526.7.1 by Aaron Bentley
Make vocabulary part of reporter.
365
        shelver = ExpectShelver(tree, tree.basis_tree(),
366
                                reporter=shelf_ui.ApplyReporter())
4603.1.17 by Aaron Bentley
Fix shelf_ui tests to finalize.
367
        self.addCleanup(shelver.finalize)
4526.6.1 by Aaron Bentley
Reverse the way changes are described by Shelver.
368
        shelver.expect('Apply change? [yNfq?]', 'y')
369
        shelver.expect('Apply change? [yNfq?]', 'y')
370
        shelver.expect('Apply 2 change(s)? [yNfq?]', 'y')
371
        shelver.run()
372
        self.assertFileEqual(LINES_AJ, 'tree/foo')
373
374
    def test_shelve_binary_change(self):
375
        tree = self.create_shelvable_tree()
376
        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)
377
        tree.lock_tree_write()
378
        self.addCleanup(tree.unlock)
4526.7.1 by Aaron Bentley
Make vocabulary part of reporter.
379
        shelver = ExpectShelver(tree, tree.basis_tree(),
380
                                reporter=shelf_ui.ApplyReporter())
4603.1.17 by Aaron Bentley
Fix shelf_ui tests to finalize.
381
        self.addCleanup(shelver.finalize)
4526.6.1 by Aaron Bentley
Reverse the way changes are described by Shelver.
382
        shelver.expect('Apply binary changes? [yNfq?]', 'y')
383
        shelver.expect('Apply 1 change(s)? [yNfq?]', 'y')
384
        shelver.run()
385
        self.assertFileEqual(LINES_AJ, 'tree/foo')
386
387
    def test_shelve_rename(self):
388
        tree = self.create_shelvable_tree()
389
        tree.rename_one('foo', 'bar')
4595.13.2 by Alexander Belchenko
[cherrypick revno 4650 from bzr.dev] Fix shelve on windows. (Robert Collins, #305006)
390
        tree.lock_tree_write()
391
        self.addCleanup(tree.unlock)
4526.7.1 by Aaron Bentley
Make vocabulary part of reporter.
392
        shelver = ExpectShelver(tree, tree.basis_tree(),
393
                                reporter=shelf_ui.ApplyReporter())
4603.1.17 by Aaron Bentley
Fix shelf_ui tests to finalize.
394
        self.addCleanup(shelver.finalize)
4526.6.1 by Aaron Bentley
Reverse the way changes are described by Shelver.
395
        shelver.expect('Rename "bar" => "foo"? [yNfq?]', 'y')
396
        shelver.expect('Apply change? [yNfq?]', 'y')
397
        shelver.expect('Apply change? [yNfq?]', 'y')
398
        shelver.expect('Apply 3 change(s)? [yNfq?]', 'y')
399
        shelver.run()
400
        self.assertFileEqual(LINES_AJ, 'tree/foo')
401
402
    def test_shelve_deletion(self):
403
        tree = self.create_shelvable_tree()
404
        os.unlink('tree/foo')
4595.13.2 by Alexander Belchenko
[cherrypick revno 4650 from bzr.dev] Fix shelve on windows. (Robert Collins, #305006)
405
        tree.lock_tree_write()
406
        self.addCleanup(tree.unlock)
4526.7.1 by Aaron Bentley
Make vocabulary part of reporter.
407
        shelver = ExpectShelver(tree, tree.basis_tree(),
408
                                reporter=shelf_ui.ApplyReporter())
4603.1.17 by Aaron Bentley
Fix shelf_ui tests to finalize.
409
        self.addCleanup(shelver.finalize)
4526.6.1 by Aaron Bentley
Reverse the way changes are described by Shelver.
410
        shelver.expect('Add file "foo"? [yNfq?]', 'y')
411
        shelver.expect('Apply 1 change(s)? [yNfq?]', 'y')
412
        shelver.run()
413
        self.assertFileEqual(LINES_AJ, 'tree/foo')
414
415
    def test_shelve_creation(self):
416
        tree = self.make_branch_and_tree('tree')
417
        tree.commit('add tree root')
418
        self.build_tree(['tree/foo'])
419
        tree.add('foo')
4595.13.2 by Alexander Belchenko
[cherrypick revno 4650 from bzr.dev] Fix shelve on windows. (Robert Collins, #305006)
420
        tree.lock_tree_write()
421
        self.addCleanup(tree.unlock)
4526.7.1 by Aaron Bentley
Make vocabulary part of reporter.
422
        shelver = ExpectShelver(tree, tree.basis_tree(),
423
                                reporter=shelf_ui.ApplyReporter())
4603.1.17 by Aaron Bentley
Fix shelf_ui tests to finalize.
424
        self.addCleanup(shelver.finalize)
4526.6.1 by Aaron Bentley
Reverse the way changes are described by Shelver.
425
        shelver.expect('Delete file "foo"? [yNfq?]', 'y')
426
        shelver.expect('Apply 1 change(s)? [yNfq?]', 'y')
427
        shelver.run()
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
428
        self.assertPathDoesNotExist('tree/foo')
4526.6.1 by Aaron Bentley
Reverse the way changes are described by Shelver.
429
430
    def test_shelve_kind_change(self):
431
        tree = self.create_shelvable_tree()
432
        os.unlink('tree/foo')
433
        os.mkdir('tree/foo')
4595.13.2 by Alexander Belchenko
[cherrypick revno 4650 from bzr.dev] Fix shelve on windows. (Robert Collins, #305006)
434
        tree.lock_tree_write()
435
        self.addCleanup(tree.unlock)
4526.7.1 by Aaron Bentley
Make vocabulary part of reporter.
436
        shelver = ExpectShelver(tree, tree.basis_tree(),
437
                               reporter=shelf_ui.ApplyReporter())
4603.1.17 by Aaron Bentley
Fix shelf_ui tests to finalize.
438
        self.addCleanup(shelver.finalize)
4526.6.1 by Aaron Bentley
Reverse the way changes are described by Shelver.
439
        shelver.expect('Change "foo" from directory to a file? [yNfq?]', 'y')
440
        shelver.expect('Apply 1 change(s)? [yNfq?]', 'y')
441
442
    def test_shelve_modify_target(self):
4526.6.12 by Aaron Bentley
Updates from review.
443
        self.requireFeature(tests.SymlinkFeature)
4526.6.1 by Aaron Bentley
Reverse the way changes are described by Shelver.
444
        tree = self.create_shelvable_tree()
445
        os.symlink('bar', 'tree/baz')
446
        tree.add('baz', 'baz-id')
447
        tree.commit("Add symlink")
448
        os.unlink('tree/baz')
449
        os.symlink('vax', 'tree/baz')
4595.13.2 by Alexander Belchenko
[cherrypick revno 4650 from bzr.dev] Fix shelve on windows. (Robert Collins, #305006)
450
        tree.lock_tree_write()
451
        self.addCleanup(tree.unlock)
4526.7.1 by Aaron Bentley
Make vocabulary part of reporter.
452
        shelver = ExpectShelver(tree, tree.basis_tree(),
453
                                reporter=shelf_ui.ApplyReporter())
4603.1.17 by Aaron Bentley
Fix shelf_ui tests to finalize.
454
        self.addCleanup(shelver.finalize)
4526.6.1 by Aaron Bentley
Reverse the way changes are described by Shelver.
455
        shelver.expect('Change target of "baz" from "vax" to "bar"? [yNfq?]',
456
                       'y')
457
        shelver.expect('Apply 1 change(s)? [yNfq?]', 'y')
458
        shelver.run()
459
        self.assertEqual('bar', os.readlink('tree/baz'))
460
461
0.16.94 by Aaron Bentley
Add unshelve tests
462
class TestUnshelver(tests.TestCaseWithTransport):
463
464
    def create_tree_with_shelf(self):
465
        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)
466
        tree.lock_write()
467
        try:
468
            self.build_tree_contents([('tree/foo', LINES_AJ)])
469
            tree.add('foo', 'foo-id')
470
            tree.commit('added foo')
471
            self.build_tree_contents([('tree/foo', LINES_ZY)])
4603.1.17 by Aaron Bentley
Fix shelf_ui tests to finalize.
472
            shelver = shelf_ui.Shelver(tree, tree.basis_tree(),
473
                                       auto_apply=True, auto=True)
474
            try:
475
                shelver.run()
476
            finally:
477
                shelver.finalize()
4595.13.2 by Alexander Belchenko
[cherrypick revno 4650 from bzr.dev] Fix shelve on windows. (Robert Collins, #305006)
478
        finally:
479
            tree.unlock()
0.16.94 by Aaron Bentley
Add unshelve tests
480
        return tree
481
482
    def test_unshelve(self):
483
        tree = self.create_tree_with_shelf()
484
        tree.lock_write()
485
        self.addCleanup(tree.unlock)
486
        manager = tree.get_shelf_manager()
487
        shelf_ui.Unshelver(tree, manager, 1, True, True, True).run()
488
        self.assertFileEqual(LINES_ZY, 'tree/foo')
489
490
    def test_unshelve_args(self):
491
        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)
492
        unshelver = shelf_ui.Unshelver.from_args(directory='tree')
493
        try:
494
            unshelver.run()
495
        finally:
496
            unshelver.tree.unlock()
0.16.94 by Aaron Bentley
Add unshelve tests
497
        self.assertFileEqual(LINES_ZY, 'tree/foo')
498
        self.assertIs(None, tree.get_shelf_manager().last_shelf())
499
4902.1.5 by Guilherme Salgado
Re-add the test for Unshelver.dry_run and twek the test for preview
500
    def test_unshelve_args_dry_run(self):
501
        tree = self.create_tree_with_shelf()
502
        unshelver = shelf_ui.Unshelver.from_args(directory='tree',
503
            action='dry-run')
504
        try:
505
            unshelver.run()
506
        finally:
507
            unshelver.tree.unlock()
508
        self.assertFileEqual(LINES_AJ, 'tree/foo')
509
        self.assertEqual(1, tree.get_shelf_manager().last_shelf())
510
4902.1.3 by Guilherme Salgado
A few tweaks as per John's review
511
    def test_unshelve_args_preview(self):
0.16.94 by Aaron Bentley
Add unshelve tests
512
        tree = self.create_tree_with_shelf()
4902.1.3 by Guilherme Salgado
A few tweaks as per John's review
513
        write_diff_to = StringIO()
514
        unshelver = shelf_ui.Unshelver.from_args(
515
            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)
516
        try:
517
            unshelver.run()
518
        finally:
519
            unshelver.tree.unlock()
4902.1.3 by Guilherme Salgado
A few tweaks as per John's review
520
        # The changes were not unshelved.
0.16.94 by Aaron Bentley
Add unshelve tests
521
        self.assertFileEqual(LINES_AJ, 'tree/foo')
522
        self.assertEqual(1, tree.get_shelf_manager().last_shelf())
4902.1.3 by Guilherme Salgado
A few tweaks as per John's review
523
524
        # 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
525
        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
526
        expected = dedent("""\
527
            @@ -1,4 +1,4 @@
528
            -a
529
            +z
530
             b
531
             c
532
             d
533
            @@ -7,4 +7,4 @@
534
             g
535
             h
536
             i
537
            -j
538
            +y
539
540
            """)
541
        self.assertEqualDiff(expected, diff[-len(expected):])
0.16.94 by Aaron Bentley
Add unshelve tests
542
543
    def test_unshelve_args_delete_only(self):
544
        tree = self.make_branch_and_tree('tree')
545
        manager = tree.get_shelf_manager()
546
        shelf_file = manager.new_shelf()[1]
547
        try:
548
            shelf_file.write('garbage')
549
        finally:
550
            shelf_file.close()
551
        unshelver = shelf_ui.Unshelver.from_args(directory='tree',
552
                                                 action='delete-only')
4595.13.2 by Alexander Belchenko
[cherrypick revno 4650 from bzr.dev] Fix shelve on windows. (Robert Collins, #305006)
553
        try:
554
            unshelver.run()
555
        finally:
556
            unshelver.tree.unlock()
0.16.94 by Aaron Bentley
Add unshelve tests
557
        self.assertIs(None, manager.last_shelf())
3990.2.1 by Daniel Watkins
Added test for unshelve being passed an invalid shelf_id.
558
559
    def test_unshelve_args_invalid_shelf_id(self):
560
        tree = self.make_branch_and_tree('tree')
561
        manager = tree.get_shelf_manager()
562
        shelf_file = manager.new_shelf()[1]
563
        try:
564
            shelf_file.write('garbage')
565
        finally:
566
            shelf_file.close()
567
        self.assertRaises(errors.InvalidShelfId,
3999.1.1 by Ian Clatworthy
Improve shelf documentation & fix backtrace (Daniel Watkins)
568
            shelf_ui.Unshelver.from_args, directory='tree',
569
            action='delete-only', shelf_id='foo')
4899.2.1 by Neil Martinsen-Burrell
add beter feedback from the unshelve command
570
4899.2.2 by Neil Martinsen-Burrell
tests updated to use the appropriate TestCase... class for scripts
571
572
class TestUnshelveScripts(TestUnshelver, 
573
                          script.TestCaseWithTransportAndScript): 
574
4899.2.1 by Neil Martinsen-Burrell
add beter feedback from the unshelve command
575
    def test_unshelve_messages_keep(self):
576
        self.create_tree_with_shelf()
4899.2.2 by Neil Martinsen-Burrell
tests updated to use the appropriate TestCase... class for scripts
577
        self.run_script("""
4899.2.1 by Neil Martinsen-Burrell
add beter feedback from the unshelve command
578
$ cd tree
579
$ bzr unshelve --keep
580
2>Using changes with id "1".
581
2> M  foo
582
2>All changes applied successfully.
583
""")
584
585
    def test_unshelve_messages_delete(self):
586
        self.create_tree_with_shelf()
4899.2.2 by Neil Martinsen-Burrell
tests updated to use the appropriate TestCase... class for scripts
587
        self.run_script("""
4899.2.1 by Neil Martinsen-Burrell
add beter feedback from the unshelve command
588
$ cd tree
589
$ bzr unshelve --delete-only
590
2>Deleted changes with id "1".
591
""")
592
593
    def test_unshelve_messages_apply(self):
594
        self.create_tree_with_shelf()
4899.2.2 by Neil Martinsen-Burrell
tests updated to use the appropriate TestCase... class for scripts
595
        self.run_script("""
4899.2.1 by Neil Martinsen-Burrell
add beter feedback from the unshelve command
596
$ cd tree
597
$ bzr unshelve --apply
598
2>Using changes with id "1".
599
2> M  foo
600
2>All changes applied successfully.
601
2>Deleted changes with id "1".
602
""")
603
604
    def test_unshelve_messages_dry_run(self):
605
        self.create_tree_with_shelf()
4899.2.2 by Neil Martinsen-Burrell
tests updated to use the appropriate TestCase... class for scripts
606
        self.run_script("""
4899.2.1 by Neil Martinsen-Burrell
add beter feedback from the unshelve command
607
$ cd tree
608
$ bzr unshelve --dry-run
609
2>Using changes with id "1".
610
2> M  foo
611
""")