1
# Copyright (C) 2008 Canonical Ltd
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.
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.
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
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
from cStringIO import StringIO
21
from bzrlib import errors, shelf_ui, tests
24
class ExpectShelver(shelf_ui.Shelver):
25
"""A variant of Shelver that intercepts console activity, for testing."""
27
def __init__(self, work_tree, target_tree, path=None, auto=False,
28
auto_apply=False, file_list=None, message=None):
29
shelf_ui.Shelver.__init__(self, work_tree, target_tree, auto,
30
auto_apply, file_list, message)
32
self.diff_writer = StringIO()
34
def expect(self, prompt, response):
35
self.expected.append((prompt, response))
37
def prompt(self, message):
39
prompt, response = self.expected.pop(0)
41
raise AssertionError('Unexpected prompt: %s' % message)
43
raise AssertionError('Wrong prompt: %s' % message)
47
LINES_AJ = 'a\nb\nc\nd\ne\nf\ng\nh\ni\nj\n'
50
LINES_ZY = 'z\nb\nc\nd\ne\nf\ng\nh\ni\ny\n'
53
LINES_AY = 'a\nb\nc\nd\ne\nf\ng\nh\ni\ny\n'
56
class TestShelver(tests.TestCaseWithTransport):
58
def create_shelvable_tree(self):
59
tree = self.make_branch_and_tree('tree')
60
self.build_tree_contents([('tree/foo', LINES_AJ)])
61
tree.add('foo', 'foo-id')
62
tree.commit('added foo')
63
self.build_tree_contents([('tree/foo', LINES_ZY)])
66
def test_unexpected_prompt_failure(self):
67
tree = self.create_shelvable_tree()
68
shelver = ExpectShelver(tree, tree.basis_tree())
69
e = self.assertRaises(AssertionError, shelver.run)
70
self.assertEqual('Unexpected prompt: Shelve? [yNfq]', str(e))
72
def test_wrong_prompt_failure(self):
73
tree = self.create_shelvable_tree()
74
shelver = ExpectShelver(tree, tree.basis_tree())
75
shelver.expect('foo', 'y')
76
e = self.assertRaises(AssertionError, shelver.run)
77
self.assertEqual('Wrong prompt: Shelve? [yNfq]', str(e))
79
def test_shelve_not_diff(self):
80
tree = self.create_shelvable_tree()
81
shelver = ExpectShelver(tree, tree.basis_tree())
82
shelver.expect('Shelve? [yNfq]', 'n')
83
shelver.expect('Shelve? [yNfq]', 'n')
84
# No final shelving prompt because no changes were selected
86
self.assertFileEqual(LINES_ZY, 'tree/foo')
88
def test_shelve_diff_no(self):
89
tree = self.create_shelvable_tree()
90
shelver = ExpectShelver(tree, tree.basis_tree())
91
shelver.expect('Shelve? [yNfq]', 'y')
92
shelver.expect('Shelve? [yNfq]', 'y')
93
shelver.expect('Shelve 2 change(s)? [yNfq]', 'n')
95
self.assertFileEqual(LINES_ZY, 'tree/foo')
97
def test_shelve_diff(self):
98
tree = self.create_shelvable_tree()
99
shelver = ExpectShelver(tree, tree.basis_tree())
100
shelver.expect('Shelve? [yNfq]', 'y')
101
shelver.expect('Shelve? [yNfq]', 'y')
102
shelver.expect('Shelve 2 change(s)? [yNfq]', 'y')
104
self.assertFileEqual(LINES_AJ, 'tree/foo')
106
def test_shelve_one_diff(self):
107
tree = self.create_shelvable_tree()
108
shelver = ExpectShelver(tree, tree.basis_tree())
109
shelver.expect('Shelve? [yNfq]', 'y')
110
shelver.expect('Shelve? [yNfq]', 'n')
111
shelver.expect('Shelve 1 change(s)? [yNfq]', 'y')
113
self.assertFileEqual(LINES_AY, 'tree/foo')
115
def test_shelve_binary_change(self):
116
tree = self.create_shelvable_tree()
117
self.build_tree_contents([('tree/foo', '\x00')])
118
shelver = ExpectShelver(tree, tree.basis_tree())
119
shelver.expect('Shelve binary changes? [yNfq]', 'y')
120
shelver.expect('Shelve 1 change(s)? [yNfq]', 'y')
122
self.assertFileEqual(LINES_AJ, 'tree/foo')
124
def test_shelve_rename(self):
125
tree = self.create_shelvable_tree()
126
tree.rename_one('foo', 'bar')
127
shelver = ExpectShelver(tree, tree.basis_tree())
128
shelver.expect('Shelve renaming "foo" => "bar"? [yNfq]', 'y')
129
shelver.expect('Shelve? [yNfq]', 'y')
130
shelver.expect('Shelve? [yNfq]', 'y')
131
shelver.expect('Shelve 3 change(s)? [yNfq]', 'y')
133
self.assertFileEqual(LINES_AJ, 'tree/foo')
135
def test_shelve_deletion(self):
136
tree = self.create_shelvable_tree()
137
os.unlink('tree/foo')
138
shelver = ExpectShelver(tree, tree.basis_tree())
139
shelver.expect('Shelve removing file "foo"? [yNfq]', 'y')
140
shelver.expect('Shelve 1 change(s)? [yNfq]', 'y')
142
self.assertFileEqual(LINES_AJ, 'tree/foo')
144
def test_shelve_creation(self):
145
tree = self.make_branch_and_tree('tree')
146
tree.commit('add tree root')
147
self.build_tree(['tree/foo'])
149
shelver = ExpectShelver(tree, tree.basis_tree())
150
shelver.expect('Shelve adding file "foo"? [yNfq]', 'y')
151
shelver.expect('Shelve 1 change(s)? [yNfq]', 'y')
153
self.failIfExists('tree/foo')
155
def test_shelve_kind_change(self):
156
tree = self.create_shelvable_tree()
157
os.unlink('tree/foo')
159
shelver = ExpectShelver(tree, tree.basis_tree())
160
shelver.expect('Shelve changing "foo" from file to directory? [yNfq]',
162
shelver.expect('Shelve 1 change(s)? [yNfq]', 'y')
164
def test_shelve_finish(self):
165
tree = self.create_shelvable_tree()
166
shelver = ExpectShelver(tree, tree.basis_tree())
167
shelver.expect('Shelve? [yNfq]', 'f')
168
shelver.expect('Shelve 2 change(s)? [yNfq]', 'y')
170
self.assertFileEqual(LINES_AJ, 'tree/foo')
172
def test_shelve_quit(self):
173
tree = self.create_shelvable_tree()
174
shelver = ExpectShelver(tree, tree.basis_tree())
175
shelver.expect('Shelve? [yNfq]', 'q')
176
self.assertRaises(errors.UserAbort, shelver.run)
177
self.assertFileEqual(LINES_ZY, 'tree/foo')
179
def test_shelve_all(self):
180
tree = self.create_shelvable_tree()
181
ExpectShelver.from_args(all=True, directory='tree').run()
182
self.assertFileEqual(LINES_AJ, 'tree/foo')
184
def test_shelve_filename(self):
185
tree = self.create_shelvable_tree()
186
self.build_tree(['tree/bar'])
188
shelver = ExpectShelver(tree, tree.basis_tree(), file_list=['bar'])
189
shelver.expect('Shelve adding file "bar"? [yNfq]', 'y')
190
shelver.expect('Shelve 1 change(s)? [yNfq]', 'y')
194
class TestUnshelver(tests.TestCaseWithTransport):
196
def create_tree_with_shelf(self):
197
tree = self.make_branch_and_tree('tree')
198
self.build_tree_contents([('tree/foo', LINES_AJ)])
199
tree.add('foo', 'foo-id')
200
tree.commit('added foo')
201
self.build_tree_contents([('tree/foo', LINES_ZY)])
202
shelf_ui.Shelver(tree, tree.basis_tree(), auto_apply=True,
206
def test_unshelve(self):
207
tree = self.create_tree_with_shelf()
209
self.addCleanup(tree.unlock)
210
manager = tree.get_shelf_manager()
211
shelf_ui.Unshelver(tree, manager, 1, True, True, True).run()
212
self.assertFileEqual(LINES_ZY, 'tree/foo')
214
def test_unshelve_args(self):
215
tree = self.create_tree_with_shelf()
216
shelf_ui.Unshelver.from_args(directory='tree').run()
217
self.assertFileEqual(LINES_ZY, 'tree/foo')
218
self.assertIs(None, tree.get_shelf_manager().last_shelf())
220
def test_unshelve_args_dry_run(self):
221
tree = self.create_tree_with_shelf()
222
shelf_ui.Unshelver.from_args(directory='tree', action='dry-run').run()
223
self.assertFileEqual(LINES_AJ, 'tree/foo')
224
self.assertEqual(1, tree.get_shelf_manager().last_shelf())
226
def test_unshelve_args_delete_only(self):
227
tree = self.make_branch_and_tree('tree')
228
manager = tree.get_shelf_manager()
229
shelf_file = manager.new_shelf()[1]
231
shelf_file.write('garbage')
234
unshelver = shelf_ui.Unshelver.from_args(directory='tree',
235
action='delete-only')
237
self.assertIs(None, manager.last_shelf())