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