~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_shelf_ui.py

  • Committer: Alexander Belchenko
  • Date: 2009-08-04 10:06:30 UTC
  • mto: This revision was merged to the branch mainline in revision 4589.
  • Revision ID: bialix@ukr.net-20090804100630-w8qiper6rucyglri
Bundle imageformats plugins for PyQt4 into bzr.exe installer to enable support of many image formats in QBzr.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2008 Canonical Ltd
 
2
#
 
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
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
16
 
 
17
 
 
18
from cStringIO import StringIO
 
19
import os
 
20
import sys
 
21
 
 
22
from bzrlib import errors, shelf_ui, tests
 
23
 
 
24
 
 
25
class ExpectShelver(shelf_ui.Shelver):
 
26
    """A variant of Shelver that intercepts console activity, for testing."""
 
27
 
 
28
    def __init__(self, work_tree, target_tree, diff_writer=None,
 
29
                 auto=False, auto_apply=False, file_list=None, message=None,
 
30
                 destroy=False, reporter=None):
 
31
        shelf_ui.Shelver.__init__(self, work_tree, target_tree, diff_writer,
 
32
                                  auto, auto_apply, file_list, message,
 
33
                                  destroy, reporter=reporter)
 
34
        self.expected = []
 
35
        self.diff_writer = StringIO()
 
36
 
 
37
    def expect(self, prompt, response):
 
38
        self.expected.append((prompt, response))
 
39
 
 
40
    def prompt(self, message):
 
41
        try:
 
42
            prompt, response = self.expected.pop(0)
 
43
        except IndexError:
 
44
            raise AssertionError('Unexpected prompt: %s' % message)
 
45
        if prompt != message:
 
46
            raise AssertionError('Wrong prompt: %s' % message)
 
47
        return response
 
48
 
 
49
 
 
50
LINES_AJ = 'a\nb\nc\nd\ne\nf\ng\nh\ni\nj\n'
 
51
 
 
52
 
 
53
LINES_ZY = 'z\nb\nc\nd\ne\nf\ng\nh\ni\ny\n'
 
54
 
 
55
 
 
56
LINES_AY = 'a\nb\nc\nd\ne\nf\ng\nh\ni\ny\n'
 
57
 
 
58
 
 
59
class TestShelver(tests.TestCaseWithTransport):
 
60
 
 
61
    def create_shelvable_tree(self):
 
62
        tree = self.make_branch_and_tree('tree')
 
63
        self.build_tree_contents([('tree/foo', LINES_AJ)])
 
64
        tree.add('foo', 'foo-id')
 
65
        tree.commit('added foo')
 
66
        self.build_tree_contents([('tree/foo', LINES_ZY)])
 
67
        return tree
 
68
 
 
69
    def test_unexpected_prompt_failure(self):
 
70
        tree = self.create_shelvable_tree()
 
71
        shelver = ExpectShelver(tree, tree.basis_tree())
 
72
        e = self.assertRaises(AssertionError, shelver.run)
 
73
        self.assertEqual('Unexpected prompt: Shelve? [yNfq?]', str(e))
 
74
 
 
75
    def test_wrong_prompt_failure(self):
 
76
        tree = self.create_shelvable_tree()
 
77
        shelver = ExpectShelver(tree, tree.basis_tree())
 
78
        shelver.expect('foo', 'y')
 
79
        e = self.assertRaises(AssertionError, shelver.run)
 
80
        self.assertEqual('Wrong prompt: Shelve? [yNfq?]', str(e))
 
81
 
 
82
    def test_shelve_not_diff(self):
 
83
        tree = self.create_shelvable_tree()
 
84
        shelver = ExpectShelver(tree, tree.basis_tree())
 
85
        shelver.expect('Shelve? [yNfq?]', 'n')
 
86
        shelver.expect('Shelve? [yNfq?]', 'n')
 
87
        # No final shelving prompt because no changes were selected
 
88
        shelver.run()
 
89
        self.assertFileEqual(LINES_ZY, 'tree/foo')
 
90
 
 
91
    def test_shelve_diff_no(self):
 
92
        tree = self.create_shelvable_tree()
 
93
        shelver = ExpectShelver(tree, tree.basis_tree())
 
94
        shelver.expect('Shelve? [yNfq?]', 'y')
 
95
        shelver.expect('Shelve? [yNfq?]', 'y')
 
96
        shelver.expect('Shelve 2 change(s)? [yNfq?]', 'n')
 
97
        shelver.run()
 
98
        self.assertFileEqual(LINES_ZY, 'tree/foo')
 
99
 
 
100
    def test_shelve_diff(self):
 
101
        tree = self.create_shelvable_tree()
 
102
        shelver = ExpectShelver(tree, tree.basis_tree())
 
103
        shelver.expect('Shelve? [yNfq?]', 'y')
 
104
        shelver.expect('Shelve? [yNfq?]', 'y')
 
105
        shelver.expect('Shelve 2 change(s)? [yNfq?]', 'y')
 
106
        shelver.run()
 
107
        self.assertFileEqual(LINES_AJ, 'tree/foo')
 
108
 
 
109
    def test_shelve_one_diff(self):
 
110
        tree = self.create_shelvable_tree()
 
111
        shelver = ExpectShelver(tree, tree.basis_tree())
 
112
        shelver.expect('Shelve? [yNfq?]', 'y')
 
113
        shelver.expect('Shelve? [yNfq?]', 'n')
 
114
        shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
 
115
        shelver.run()
 
116
        self.assertFileEqual(LINES_AY, 'tree/foo')
 
117
 
 
118
    def test_shelve_binary_change(self):
 
119
        tree = self.create_shelvable_tree()
 
120
        self.build_tree_contents([('tree/foo', '\x00')])
 
121
        shelver = ExpectShelver(tree, tree.basis_tree())
 
122
        shelver.expect('Shelve binary changes? [yNfq?]', 'y')
 
123
        shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
 
124
        shelver.run()
 
125
        self.assertFileEqual(LINES_AJ, 'tree/foo')
 
126
 
 
127
    def test_shelve_rename(self):
 
128
        tree = self.create_shelvable_tree()
 
129
        tree.rename_one('foo', 'bar')
 
130
        shelver = ExpectShelver(tree, tree.basis_tree())
 
131
        shelver.expect('Shelve renaming "foo" => "bar"? [yNfq?]', 'y')
 
132
        shelver.expect('Shelve? [yNfq?]', 'y')
 
133
        shelver.expect('Shelve? [yNfq?]', 'y')
 
134
        shelver.expect('Shelve 3 change(s)? [yNfq?]', 'y')
 
135
        shelver.run()
 
136
        self.assertFileEqual(LINES_AJ, 'tree/foo')
 
137
 
 
138
    def test_shelve_deletion(self):
 
139
        tree = self.create_shelvable_tree()
 
140
        os.unlink('tree/foo')
 
141
        shelver = ExpectShelver(tree, tree.basis_tree())
 
142
        shelver.expect('Shelve removing file "foo"? [yNfq?]', 'y')
 
143
        shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
 
144
        shelver.run()
 
145
        self.assertFileEqual(LINES_AJ, 'tree/foo')
 
146
 
 
147
    def test_shelve_creation(self):
 
148
        tree = self.make_branch_and_tree('tree')
 
149
        tree.commit('add tree root')
 
150
        self.build_tree(['tree/foo'])
 
151
        tree.add('foo')
 
152
        shelver = ExpectShelver(tree, tree.basis_tree())
 
153
        shelver.expect('Shelve adding file "foo"? [yNfq?]', 'y')
 
154
        shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
 
155
        shelver.run()
 
156
        self.failIfExists('tree/foo')
 
157
 
 
158
    def test_shelve_kind_change(self):
 
159
        tree = self.create_shelvable_tree()
 
160
        os.unlink('tree/foo')
 
161
        os.mkdir('tree/foo')
 
162
        shelver = ExpectShelver(tree, tree.basis_tree())
 
163
        shelver.expect('Shelve changing "foo" from file to directory? [yNfq?]',
 
164
                       'y')
 
165
        shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
 
166
 
 
167
    def test_shelve_modify_target(self):
 
168
        self.requireFeature(tests.SymlinkFeature)
 
169
        tree = self.create_shelvable_tree()
 
170
        os.symlink('bar', 'tree/baz')
 
171
        tree.add('baz', 'baz-id')
 
172
        tree.commit("Add symlink")
 
173
        os.unlink('tree/baz')
 
174
        os.symlink('vax', 'tree/baz')
 
175
        shelver = ExpectShelver(tree, tree.basis_tree())
 
176
        shelver.expect('Shelve changing target of "baz" from "bar" to '
 
177
                '"vax"? [yNfq?]', 'y')
 
178
        shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
 
179
        shelver.run()
 
180
        self.assertEqual('bar', os.readlink('tree/baz'))
 
181
 
 
182
    def test_shelve_finish(self):
 
183
        tree = self.create_shelvable_tree()
 
184
        shelver = ExpectShelver(tree, tree.basis_tree())
 
185
        shelver.expect('Shelve? [yNfq?]', 'f')
 
186
        shelver.expect('Shelve 2 change(s)? [yNfq?]', 'y')
 
187
        shelver.run()
 
188
        self.assertFileEqual(LINES_AJ, 'tree/foo')
 
189
 
 
190
    def test_shelve_quit(self):
 
191
        tree = self.create_shelvable_tree()
 
192
        shelver = ExpectShelver(tree, tree.basis_tree())
 
193
        shelver.expect('Shelve? [yNfq?]', 'q')
 
194
        self.assertRaises(errors.UserAbort, shelver.run)
 
195
        self.assertFileEqual(LINES_ZY, 'tree/foo')
 
196
 
 
197
    def test_shelve_all(self):
 
198
        tree = self.create_shelvable_tree()
 
199
        ExpectShelver.from_args(sys.stdout, all=True, directory='tree').run()
 
200
        self.assertFileEqual(LINES_AJ, 'tree/foo')
 
201
 
 
202
    def test_shelve_filename(self):
 
203
        tree = self.create_shelvable_tree()
 
204
        self.build_tree(['tree/bar'])
 
205
        tree.add('bar')
 
206
        shelver = ExpectShelver(tree, tree.basis_tree(), file_list=['bar'])
 
207
        shelver.expect('Shelve adding file "bar"? [yNfq?]', 'y')
 
208
        shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
 
209
        shelver.run()
 
210
 
 
211
    def test_shelve_help(self):
 
212
        tree = self.create_shelvable_tree()
 
213
        shelver = ExpectShelver(tree, tree.basis_tree())
 
214
        shelver.expect('Shelve? [yNfq?]', '?')
 
215
        shelver.expect('Shelve? [(y)es, (N)o, (f)inish, or (q)uit]', 'f')
 
216
        shelver.expect('Shelve 2 change(s)? [yNfq?]', 'y')
 
217
        shelver.run()
 
218
 
 
219
    def test_shelve_distroy(self):
 
220
        tree = self.create_shelvable_tree()
 
221
        shelver = shelf_ui.Shelver.from_args(sys.stdout, all=True,
 
222
                                             directory='tree', destroy=True)
 
223
        shelver.run()
 
224
        self.assertIs(None, tree.get_shelf_manager().last_shelf())
 
225
        self.assertFileEqual(LINES_AJ, 'tree/foo')
 
226
 
 
227
 
 
228
class TestApplyReporter(TestShelver):
 
229
 
 
230
    def test_shelve_not_diff(self):
 
231
        tree = self.create_shelvable_tree()
 
232
        shelver = ExpectShelver(tree, tree.basis_tree(),
 
233
                                reporter=shelf_ui.ApplyReporter())
 
234
        shelver.expect('Apply change? [yNfq?]', 'n')
 
235
        shelver.expect('Apply change? [yNfq?]', 'n')
 
236
        # No final shelving prompt because no changes were selected
 
237
        shelver.run()
 
238
        self.assertFileEqual(LINES_ZY, 'tree/foo')
 
239
 
 
240
    def test_shelve_diff_no(self):
 
241
        tree = self.create_shelvable_tree()
 
242
        shelver = ExpectShelver(tree, tree.basis_tree(),
 
243
                                reporter=shelf_ui.ApplyReporter())
 
244
        shelver.expect('Apply change? [yNfq?]', 'y')
 
245
        shelver.expect('Apply change? [yNfq?]', 'y')
 
246
        shelver.expect('Apply 2 change(s)? [yNfq?]', 'n')
 
247
        shelver.run()
 
248
        self.assertFileEqual(LINES_ZY, 'tree/foo')
 
249
 
 
250
    def test_shelve_diff(self):
 
251
        tree = self.create_shelvable_tree()
 
252
        shelver = ExpectShelver(tree, tree.basis_tree(),
 
253
                                reporter=shelf_ui.ApplyReporter())
 
254
        shelver.expect('Apply change? [yNfq?]', 'y')
 
255
        shelver.expect('Apply change? [yNfq?]', 'y')
 
256
        shelver.expect('Apply 2 change(s)? [yNfq?]', 'y')
 
257
        shelver.run()
 
258
        self.assertFileEqual(LINES_AJ, 'tree/foo')
 
259
 
 
260
    def test_shelve_binary_change(self):
 
261
        tree = self.create_shelvable_tree()
 
262
        self.build_tree_contents([('tree/foo', '\x00')])
 
263
        shelver = ExpectShelver(tree, tree.basis_tree(),
 
264
                                reporter=shelf_ui.ApplyReporter())
 
265
        shelver.expect('Apply binary changes? [yNfq?]', 'y')
 
266
        shelver.expect('Apply 1 change(s)? [yNfq?]', 'y')
 
267
        shelver.run()
 
268
        self.assertFileEqual(LINES_AJ, 'tree/foo')
 
269
 
 
270
    def test_shelve_rename(self):
 
271
        tree = self.create_shelvable_tree()
 
272
        tree.rename_one('foo', 'bar')
 
273
        shelver = ExpectShelver(tree, tree.basis_tree(),
 
274
                                reporter=shelf_ui.ApplyReporter())
 
275
        shelver.expect('Rename "bar" => "foo"? [yNfq?]', 'y')
 
276
        shelver.expect('Apply change? [yNfq?]', 'y')
 
277
        shelver.expect('Apply change? [yNfq?]', 'y')
 
278
        shelver.expect('Apply 3 change(s)? [yNfq?]', 'y')
 
279
        shelver.run()
 
280
        self.assertFileEqual(LINES_AJ, 'tree/foo')
 
281
 
 
282
    def test_shelve_deletion(self):
 
283
        tree = self.create_shelvable_tree()
 
284
        os.unlink('tree/foo')
 
285
        shelver = ExpectShelver(tree, tree.basis_tree(),
 
286
                                reporter=shelf_ui.ApplyReporter())
 
287
        shelver.expect('Add file "foo"? [yNfq?]', 'y')
 
288
        shelver.expect('Apply 1 change(s)? [yNfq?]', 'y')
 
289
        shelver.run()
 
290
        self.assertFileEqual(LINES_AJ, 'tree/foo')
 
291
 
 
292
    def test_shelve_creation(self):
 
293
        tree = self.make_branch_and_tree('tree')
 
294
        tree.commit('add tree root')
 
295
        self.build_tree(['tree/foo'])
 
296
        tree.add('foo')
 
297
        shelver = ExpectShelver(tree, tree.basis_tree(),
 
298
                                reporter=shelf_ui.ApplyReporter())
 
299
        shelver.expect('Delete file "foo"? [yNfq?]', 'y')
 
300
        shelver.expect('Apply 1 change(s)? [yNfq?]', 'y')
 
301
        shelver.run()
 
302
        self.failIfExists('tree/foo')
 
303
 
 
304
    def test_shelve_kind_change(self):
 
305
        tree = self.create_shelvable_tree()
 
306
        os.unlink('tree/foo')
 
307
        os.mkdir('tree/foo')
 
308
        shelver = ExpectShelver(tree, tree.basis_tree(),
 
309
                               reporter=shelf_ui.ApplyReporter())
 
310
        shelver.expect('Change "foo" from directory to a file? [yNfq?]', 'y')
 
311
        shelver.expect('Apply 1 change(s)? [yNfq?]', 'y')
 
312
 
 
313
    def test_shelve_modify_target(self):
 
314
        self.requireFeature(tests.SymlinkFeature)
 
315
        tree = self.create_shelvable_tree()
 
316
        os.symlink('bar', 'tree/baz')
 
317
        tree.add('baz', 'baz-id')
 
318
        tree.commit("Add symlink")
 
319
        os.unlink('tree/baz')
 
320
        os.symlink('vax', 'tree/baz')
 
321
        shelver = ExpectShelver(tree, tree.basis_tree(),
 
322
                                reporter=shelf_ui.ApplyReporter())
 
323
        shelver.expect('Change target of "baz" from "vax" to "bar"? [yNfq?]',
 
324
                       'y')
 
325
        shelver.expect('Apply 1 change(s)? [yNfq?]', 'y')
 
326
        shelver.run()
 
327
        self.assertEqual('bar', os.readlink('tree/baz'))
 
328
 
 
329
 
 
330
class TestUnshelver(tests.TestCaseWithTransport):
 
331
 
 
332
    def create_tree_with_shelf(self):
 
333
        tree = self.make_branch_and_tree('tree')
 
334
        self.build_tree_contents([('tree/foo', LINES_AJ)])
 
335
        tree.add('foo', 'foo-id')
 
336
        tree.commit('added foo')
 
337
        self.build_tree_contents([('tree/foo', LINES_ZY)])
 
338
        shelf_ui.Shelver(tree, tree.basis_tree(), auto_apply=True,
 
339
                         auto=True).run()
 
340
        return tree
 
341
 
 
342
    def test_unshelve(self):
 
343
        tree = self.create_tree_with_shelf()
 
344
        tree.lock_write()
 
345
        self.addCleanup(tree.unlock)
 
346
        manager = tree.get_shelf_manager()
 
347
        shelf_ui.Unshelver(tree, manager, 1, True, True, True).run()
 
348
        self.assertFileEqual(LINES_ZY, 'tree/foo')
 
349
 
 
350
    def test_unshelve_args(self):
 
351
        tree = self.create_tree_with_shelf()
 
352
        shelf_ui.Unshelver.from_args(directory='tree').run()
 
353
        self.assertFileEqual(LINES_ZY, 'tree/foo')
 
354
        self.assertIs(None, tree.get_shelf_manager().last_shelf())
 
355
 
 
356
    def test_unshelve_args_dry_run(self):
 
357
        tree = self.create_tree_with_shelf()
 
358
        shelf_ui.Unshelver.from_args(directory='tree', action='dry-run').run()
 
359
        self.assertFileEqual(LINES_AJ, 'tree/foo')
 
360
        self.assertEqual(1, tree.get_shelf_manager().last_shelf())
 
361
 
 
362
    def test_unshelve_args_delete_only(self):
 
363
        tree = self.make_branch_and_tree('tree')
 
364
        manager = tree.get_shelf_manager()
 
365
        shelf_file = manager.new_shelf()[1]
 
366
        try:
 
367
            shelf_file.write('garbage')
 
368
        finally:
 
369
            shelf_file.close()
 
370
        unshelver = shelf_ui.Unshelver.from_args(directory='tree',
 
371
                                                 action='delete-only')
 
372
        unshelver.run()
 
373
        self.assertIs(None, manager.last_shelf())
 
374
 
 
375
    def test_unshelve_args_invalid_shelf_id(self):
 
376
        tree = self.make_branch_and_tree('tree')
 
377
        manager = tree.get_shelf_manager()
 
378
        shelf_file = manager.new_shelf()[1]
 
379
        try:
 
380
            shelf_file.write('garbage')
 
381
        finally:
 
382
            shelf_file.close()
 
383
        self.assertRaises(errors.InvalidShelfId,
 
384
            shelf_ui.Unshelver.from_args, directory='tree',
 
385
            action='delete-only', shelf_id='foo')