~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_shelf_ui.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2009-08-24 21:25:26 UTC
  • mfrom: (4641.1.1 integration)
  • Revision ID: pqm@pqm.ubuntu.com-20090824212526-5j41jw5zsciji66e
(robertc) Back out draft documentation change landed as 'selftest
        improvements'. (Robert Collins)

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 (
 
23
    errors,
 
24
    shelf_ui,
 
25
    revision,
 
26
    tests,
 
27
)
 
28
 
 
29
 
 
30
class ExpectShelver(shelf_ui.Shelver):
 
31
    """A variant of Shelver that intercepts console activity, for testing."""
 
32
 
 
33
    def __init__(self, work_tree, target_tree, diff_writer=None,
 
34
                 auto=False, auto_apply=False, file_list=None, message=None,
 
35
                 destroy=False, reporter=None):
 
36
        shelf_ui.Shelver.__init__(self, work_tree, target_tree, diff_writer,
 
37
                                  auto, auto_apply, file_list, message,
 
38
                                  destroy, reporter=reporter)
 
39
        self.expected = []
 
40
        self.diff_writer = StringIO()
 
41
 
 
42
    def expect(self, prompt, response):
 
43
        self.expected.append((prompt, response))
 
44
 
 
45
    def prompt(self, message):
 
46
        try:
 
47
            prompt, response = self.expected.pop(0)
 
48
        except IndexError:
 
49
            raise AssertionError('Unexpected prompt: %s' % message)
 
50
        if prompt != message:
 
51
            raise AssertionError('Wrong prompt: %s' % message)
 
52
        return response
 
53
 
 
54
 
 
55
LINES_AJ = 'a\nb\nc\nd\ne\nf\ng\nh\ni\nj\n'
 
56
 
 
57
 
 
58
LINES_ZY = 'z\nb\nc\nd\ne\nf\ng\nh\ni\ny\n'
 
59
 
 
60
 
 
61
LINES_AY = 'a\nb\nc\nd\ne\nf\ng\nh\ni\ny\n'
 
62
 
 
63
 
 
64
class TestShelver(tests.TestCaseWithTransport):
 
65
 
 
66
    def create_shelvable_tree(self):
 
67
        tree = self.make_branch_and_tree('tree')
 
68
        self.build_tree_contents([('tree/foo', LINES_AJ)])
 
69
        tree.add('foo', 'foo-id')
 
70
        tree.commit('added foo')
 
71
        self.build_tree_contents([('tree/foo', LINES_ZY)])
 
72
        return tree
 
73
 
 
74
    def test_unexpected_prompt_failure(self):
 
75
        self.thisFailsStrictLockCheck()
 
76
        tree = self.create_shelvable_tree()
 
77
        shelver = ExpectShelver(tree, tree.basis_tree())
 
78
        e = self.assertRaises(AssertionError, shelver.run)
 
79
        self.assertEqual('Unexpected prompt: Shelve? [yNfq?]', str(e))
 
80
 
 
81
    def test_wrong_prompt_failure(self):
 
82
        self.thisFailsStrictLockCheck()
 
83
        tree = self.create_shelvable_tree()
 
84
        shelver = ExpectShelver(tree, tree.basis_tree())
 
85
        shelver.expect('foo', 'y')
 
86
        e = self.assertRaises(AssertionError, shelver.run)
 
87
        self.assertEqual('Wrong prompt: Shelve? [yNfq?]', str(e))
 
88
 
 
89
    def test_shelve_not_diff(self):
 
90
        self.thisFailsStrictLockCheck()
 
91
        tree = self.create_shelvable_tree()
 
92
        shelver = ExpectShelver(tree, tree.basis_tree())
 
93
        shelver.expect('Shelve? [yNfq?]', 'n')
 
94
        shelver.expect('Shelve? [yNfq?]', 'n')
 
95
        # No final shelving prompt because no changes were selected
 
96
        shelver.run()
 
97
        self.assertFileEqual(LINES_ZY, 'tree/foo')
 
98
 
 
99
    def test_shelve_diff_no(self):
 
100
        self.thisFailsStrictLockCheck()
 
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?]', 'n')
 
106
        shelver.run()
 
107
        self.assertFileEqual(LINES_ZY, 'tree/foo')
 
108
 
 
109
    def test_shelve_diff(self):
 
110
        self.thisFailsStrictLockCheck()
 
111
        tree = self.create_shelvable_tree()
 
112
        shelver = ExpectShelver(tree, tree.basis_tree())
 
113
        shelver.expect('Shelve? [yNfq?]', 'y')
 
114
        shelver.expect('Shelve? [yNfq?]', 'y')
 
115
        shelver.expect('Shelve 2 change(s)? [yNfq?]', 'y')
 
116
        shelver.run()
 
117
        self.assertFileEqual(LINES_AJ, 'tree/foo')
 
118
 
 
119
    def test_shelve_one_diff(self):
 
120
        self.thisFailsStrictLockCheck()
 
121
        tree = self.create_shelvable_tree()
 
122
        shelver = ExpectShelver(tree, tree.basis_tree())
 
123
        shelver.expect('Shelve? [yNfq?]', 'y')
 
124
        shelver.expect('Shelve? [yNfq?]', 'n')
 
125
        shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
 
126
        shelver.run()
 
127
        self.assertFileEqual(LINES_AY, 'tree/foo')
 
128
 
 
129
    def test_shelve_binary_change(self):
 
130
        self.thisFailsStrictLockCheck()
 
131
        tree = self.create_shelvable_tree()
 
132
        self.build_tree_contents([('tree/foo', '\x00')])
 
133
        shelver = ExpectShelver(tree, tree.basis_tree())
 
134
        shelver.expect('Shelve binary changes? [yNfq?]', 'y')
 
135
        shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
 
136
        shelver.run()
 
137
        self.assertFileEqual(LINES_AJ, 'tree/foo')
 
138
 
 
139
    def test_shelve_rename(self):
 
140
        self.thisFailsStrictLockCheck()
 
141
        tree = self.create_shelvable_tree()
 
142
        tree.rename_one('foo', 'bar')
 
143
        shelver = ExpectShelver(tree, tree.basis_tree())
 
144
        shelver.expect('Shelve renaming "foo" => "bar"? [yNfq?]', 'y')
 
145
        shelver.expect('Shelve? [yNfq?]', 'y')
 
146
        shelver.expect('Shelve? [yNfq?]', 'y')
 
147
        shelver.expect('Shelve 3 change(s)? [yNfq?]', 'y')
 
148
        shelver.run()
 
149
        self.assertFileEqual(LINES_AJ, 'tree/foo')
 
150
 
 
151
    def test_shelve_deletion(self):
 
152
        self.thisFailsStrictLockCheck()
 
153
        tree = self.create_shelvable_tree()
 
154
        os.unlink('tree/foo')
 
155
        shelver = ExpectShelver(tree, tree.basis_tree())
 
156
        shelver.expect('Shelve removing file "foo"? [yNfq?]', 'y')
 
157
        shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
 
158
        shelver.run()
 
159
        self.assertFileEqual(LINES_AJ, 'tree/foo')
 
160
 
 
161
    def test_shelve_creation(self):
 
162
        self.thisFailsStrictLockCheck()
 
163
        tree = self.make_branch_and_tree('tree')
 
164
        tree.commit('add tree root')
 
165
        self.build_tree(['tree/foo'])
 
166
        tree.add('foo')
 
167
        shelver = ExpectShelver(tree, tree.basis_tree())
 
168
        shelver.expect('Shelve adding file "foo"? [yNfq?]', 'y')
 
169
        shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
 
170
        shelver.run()
 
171
        self.failIfExists('tree/foo')
 
172
 
 
173
    def test_shelve_kind_change(self):
 
174
        self.thisFailsStrictLockCheck()
 
175
        tree = self.create_shelvable_tree()
 
176
        os.unlink('tree/foo')
 
177
        os.mkdir('tree/foo')
 
178
        shelver = ExpectShelver(tree, tree.basis_tree())
 
179
        shelver.expect('Shelve changing "foo" from file to directory? [yNfq?]',
 
180
                       'y')
 
181
        shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
 
182
 
 
183
    def test_shelve_modify_target(self):
 
184
        self.thisFailsStrictLockCheck()
 
185
        self.requireFeature(tests.SymlinkFeature)
 
186
        tree = self.create_shelvable_tree()
 
187
        os.symlink('bar', 'tree/baz')
 
188
        tree.add('baz', 'baz-id')
 
189
        tree.commit("Add symlink")
 
190
        os.unlink('tree/baz')
 
191
        os.symlink('vax', 'tree/baz')
 
192
        shelver = ExpectShelver(tree, tree.basis_tree())
 
193
        shelver.expect('Shelve changing target of "baz" from "bar" to '
 
194
                '"vax"? [yNfq?]', 'y')
 
195
        shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
 
196
        shelver.run()
 
197
        self.assertEqual('bar', os.readlink('tree/baz'))
 
198
 
 
199
    def test_shelve_finish(self):
 
200
        self.thisFailsStrictLockCheck()
 
201
        tree = self.create_shelvable_tree()
 
202
        shelver = ExpectShelver(tree, tree.basis_tree())
 
203
        shelver.expect('Shelve? [yNfq?]', 'f')
 
204
        shelver.expect('Shelve 2 change(s)? [yNfq?]', 'y')
 
205
        shelver.run()
 
206
        self.assertFileEqual(LINES_AJ, 'tree/foo')
 
207
 
 
208
    def test_shelve_quit(self):
 
209
        self.thisFailsStrictLockCheck()
 
210
        tree = self.create_shelvable_tree()
 
211
        shelver = ExpectShelver(tree, tree.basis_tree())
 
212
        shelver.expect('Shelve? [yNfq?]', 'q')
 
213
        self.assertRaises(errors.UserAbort, shelver.run)
 
214
        self.assertFileEqual(LINES_ZY, 'tree/foo')
 
215
 
 
216
    def test_shelve_all(self):
 
217
        self.thisFailsStrictLockCheck()
 
218
        tree = self.create_shelvable_tree()
 
219
        ExpectShelver.from_args(sys.stdout, all=True, directory='tree').run()
 
220
        self.assertFileEqual(LINES_AJ, 'tree/foo')
 
221
 
 
222
    def test_shelve_filename(self):
 
223
        self.thisFailsStrictLockCheck()
 
224
        tree = self.create_shelvable_tree()
 
225
        self.build_tree(['tree/bar'])
 
226
        tree.add('bar')
 
227
        shelver = ExpectShelver(tree, tree.basis_tree(), file_list=['bar'])
 
228
        shelver.expect('Shelve adding file "bar"? [yNfq?]', 'y')
 
229
        shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
 
230
        shelver.run()
 
231
 
 
232
    def test_shelve_help(self):
 
233
        self.thisFailsStrictLockCheck()
 
234
        tree = self.create_shelvable_tree()
 
235
        shelver = ExpectShelver(tree, tree.basis_tree())
 
236
        shelver.expect('Shelve? [yNfq?]', '?')
 
237
        shelver.expect('Shelve? [(y)es, (N)o, (f)inish, or (q)uit]', 'f')
 
238
        shelver.expect('Shelve 2 change(s)? [yNfq?]', 'y')
 
239
        shelver.run()
 
240
 
 
241
    def test_shelve_distroy(self):
 
242
        self.thisFailsStrictLockCheck()
 
243
        tree = self.create_shelvable_tree()
 
244
        shelver = shelf_ui.Shelver.from_args(sys.stdout, all=True,
 
245
                                             directory='tree', destroy=True)
 
246
        shelver.run()
 
247
        self.assertIs(None, tree.get_shelf_manager().last_shelf())
 
248
        self.assertFileEqual(LINES_AJ, 'tree/foo')
 
249
 
 
250
    @staticmethod
 
251
    def shelve_all(tree, target_revision_id):
 
252
        tree.lock_write()
 
253
        try:
 
254
            target = tree.branch.repository.revision_tree(target_revision_id)
 
255
            shelver = shelf_ui.Shelver(tree, target, auto=True,
 
256
                                       auto_apply=True)
 
257
            shelver.run()
 
258
        finally:
 
259
            tree.unlock()
 
260
 
 
261
    def test_shelve_old_root_deleted(self):
 
262
        tree1 = self.make_branch_and_tree('tree1')
 
263
        tree1.commit('add root')
 
264
        tree2 = self.make_branch_and_tree('tree2')
 
265
        rev2 = tree2.commit('add root')
 
266
        tree1.merge_from_branch(tree2.branch,
 
267
                                from_revision=revision.NULL_REVISION)
 
268
        tree1.commit('Replaced root entry')
 
269
        # This is essentially assertNotRaises(InconsistentDelta)
 
270
        self.expectFailure('Cannot shelve replacing a root entry',
 
271
                           self.assertRaises, AssertionError,
 
272
                           self.assertRaises, errors.InconsistentDelta,
 
273
                           self.shelve_all, tree1, rev2)
 
274
 
 
275
    def test_shelve_split(self):
 
276
        outer_tree = self.make_branch_and_tree('outer')
 
277
        outer_tree.commit('Add root')
 
278
        inner_tree = self.make_branch_and_tree('outer/inner')
 
279
        rev2 = inner_tree.commit('Add root')
 
280
        outer_tree.subsume(inner_tree)
 
281
        # This is essentially assertNotRaises(ValueError).
 
282
        # The ValueError is 'None is not a valid file id'.
 
283
        self.expectFailure('Cannot shelve a join back to the inner tree.',
 
284
                           self.assertRaises, AssertionError,
 
285
                           self.assertRaises, ValueError, self.shelve_all,
 
286
                           outer_tree, rev2)
 
287
 
 
288
 
 
289
class TestApplyReporter(TestShelver):
 
290
 
 
291
    def test_shelve_not_diff(self):
 
292
        self.thisFailsStrictLockCheck()
 
293
        tree = self.create_shelvable_tree()
 
294
        shelver = ExpectShelver(tree, tree.basis_tree(),
 
295
                                reporter=shelf_ui.ApplyReporter())
 
296
        shelver.expect('Apply change? [yNfq?]', 'n')
 
297
        shelver.expect('Apply change? [yNfq?]', 'n')
 
298
        # No final shelving prompt because no changes were selected
 
299
        shelver.run()
 
300
        self.assertFileEqual(LINES_ZY, 'tree/foo')
 
301
 
 
302
    def test_shelve_diff_no(self):
 
303
        self.thisFailsStrictLockCheck()
 
304
        tree = self.create_shelvable_tree()
 
305
        shelver = ExpectShelver(tree, tree.basis_tree(),
 
306
                                reporter=shelf_ui.ApplyReporter())
 
307
        shelver.expect('Apply change? [yNfq?]', 'y')
 
308
        shelver.expect('Apply change? [yNfq?]', 'y')
 
309
        shelver.expect('Apply 2 change(s)? [yNfq?]', 'n')
 
310
        shelver.run()
 
311
        self.assertFileEqual(LINES_ZY, 'tree/foo')
 
312
 
 
313
    def test_shelve_diff(self):
 
314
        self.thisFailsStrictLockCheck()
 
315
        tree = self.create_shelvable_tree()
 
316
        shelver = ExpectShelver(tree, tree.basis_tree(),
 
317
                                reporter=shelf_ui.ApplyReporter())
 
318
        shelver.expect('Apply change? [yNfq?]', 'y')
 
319
        shelver.expect('Apply change? [yNfq?]', 'y')
 
320
        shelver.expect('Apply 2 change(s)? [yNfq?]', 'y')
 
321
        shelver.run()
 
322
        self.assertFileEqual(LINES_AJ, 'tree/foo')
 
323
 
 
324
    def test_shelve_binary_change(self):
 
325
        self.thisFailsStrictLockCheck()
 
326
        tree = self.create_shelvable_tree()
 
327
        self.build_tree_contents([('tree/foo', '\x00')])
 
328
        shelver = ExpectShelver(tree, tree.basis_tree(),
 
329
                                reporter=shelf_ui.ApplyReporter())
 
330
        shelver.expect('Apply binary changes? [yNfq?]', 'y')
 
331
        shelver.expect('Apply 1 change(s)? [yNfq?]', 'y')
 
332
        shelver.run()
 
333
        self.assertFileEqual(LINES_AJ, 'tree/foo')
 
334
 
 
335
    def test_shelve_rename(self):
 
336
        self.thisFailsStrictLockCheck()
 
337
        tree = self.create_shelvable_tree()
 
338
        tree.rename_one('foo', 'bar')
 
339
        shelver = ExpectShelver(tree, tree.basis_tree(),
 
340
                                reporter=shelf_ui.ApplyReporter())
 
341
        shelver.expect('Rename "bar" => "foo"? [yNfq?]', 'y')
 
342
        shelver.expect('Apply change? [yNfq?]', 'y')
 
343
        shelver.expect('Apply change? [yNfq?]', 'y')
 
344
        shelver.expect('Apply 3 change(s)? [yNfq?]', 'y')
 
345
        shelver.run()
 
346
        self.assertFileEqual(LINES_AJ, 'tree/foo')
 
347
 
 
348
    def test_shelve_deletion(self):
 
349
        self.thisFailsStrictLockCheck()
 
350
        tree = self.create_shelvable_tree()
 
351
        os.unlink('tree/foo')
 
352
        shelver = ExpectShelver(tree, tree.basis_tree(),
 
353
                                reporter=shelf_ui.ApplyReporter())
 
354
        shelver.expect('Add file "foo"? [yNfq?]', 'y')
 
355
        shelver.expect('Apply 1 change(s)? [yNfq?]', 'y')
 
356
        shelver.run()
 
357
        self.assertFileEqual(LINES_AJ, 'tree/foo')
 
358
 
 
359
    def test_shelve_creation(self):
 
360
        self.thisFailsStrictLockCheck()
 
361
        tree = self.make_branch_and_tree('tree')
 
362
        tree.commit('add tree root')
 
363
        self.build_tree(['tree/foo'])
 
364
        tree.add('foo')
 
365
        shelver = ExpectShelver(tree, tree.basis_tree(),
 
366
                                reporter=shelf_ui.ApplyReporter())
 
367
        shelver.expect('Delete file "foo"? [yNfq?]', 'y')
 
368
        shelver.expect('Apply 1 change(s)? [yNfq?]', 'y')
 
369
        shelver.run()
 
370
        self.failIfExists('tree/foo')
 
371
 
 
372
    def test_shelve_kind_change(self):
 
373
        self.thisFailsStrictLockCheck()
 
374
        tree = self.create_shelvable_tree()
 
375
        os.unlink('tree/foo')
 
376
        os.mkdir('tree/foo')
 
377
        shelver = ExpectShelver(tree, tree.basis_tree(),
 
378
                               reporter=shelf_ui.ApplyReporter())
 
379
        shelver.expect('Change "foo" from directory to a file? [yNfq?]', 'y')
 
380
        shelver.expect('Apply 1 change(s)? [yNfq?]', 'y')
 
381
 
 
382
    def test_shelve_modify_target(self):
 
383
        self.thisFailsStrictLockCheck()
 
384
        self.requireFeature(tests.SymlinkFeature)
 
385
        tree = self.create_shelvable_tree()
 
386
        os.symlink('bar', 'tree/baz')
 
387
        tree.add('baz', 'baz-id')
 
388
        tree.commit("Add symlink")
 
389
        os.unlink('tree/baz')
 
390
        os.symlink('vax', 'tree/baz')
 
391
        shelver = ExpectShelver(tree, tree.basis_tree(),
 
392
                                reporter=shelf_ui.ApplyReporter())
 
393
        shelver.expect('Change target of "baz" from "vax" to "bar"? [yNfq?]',
 
394
                       'y')
 
395
        shelver.expect('Apply 1 change(s)? [yNfq?]', 'y')
 
396
        shelver.run()
 
397
        self.assertEqual('bar', os.readlink('tree/baz'))
 
398
 
 
399
 
 
400
class TestUnshelver(tests.TestCaseWithTransport):
 
401
 
 
402
    def create_tree_with_shelf(self):
 
403
        tree = self.make_branch_and_tree('tree')
 
404
        self.build_tree_contents([('tree/foo', LINES_AJ)])
 
405
        tree.add('foo', 'foo-id')
 
406
        tree.commit('added foo')
 
407
        self.build_tree_contents([('tree/foo', LINES_ZY)])
 
408
        shelf_ui.Shelver(tree, tree.basis_tree(), auto_apply=True,
 
409
                         auto=True).run()
 
410
        return tree
 
411
 
 
412
    def test_unshelve(self):
 
413
        self.thisFailsStrictLockCheck()
 
414
        tree = self.create_tree_with_shelf()
 
415
        tree.lock_write()
 
416
        self.addCleanup(tree.unlock)
 
417
        manager = tree.get_shelf_manager()
 
418
        shelf_ui.Unshelver(tree, manager, 1, True, True, True).run()
 
419
        self.assertFileEqual(LINES_ZY, 'tree/foo')
 
420
 
 
421
    def test_unshelve_args(self):
 
422
        self.thisFailsStrictLockCheck()
 
423
        tree = self.create_tree_with_shelf()
 
424
        shelf_ui.Unshelver.from_args(directory='tree').run()
 
425
        self.assertFileEqual(LINES_ZY, 'tree/foo')
 
426
        self.assertIs(None, tree.get_shelf_manager().last_shelf())
 
427
 
 
428
    def test_unshelve_args_dry_run(self):
 
429
        self.thisFailsStrictLockCheck()
 
430
        tree = self.create_tree_with_shelf()
 
431
        shelf_ui.Unshelver.from_args(directory='tree', action='dry-run').run()
 
432
        self.assertFileEqual(LINES_AJ, 'tree/foo')
 
433
        self.assertEqual(1, tree.get_shelf_manager().last_shelf())
 
434
 
 
435
    def test_unshelve_args_delete_only(self):
 
436
        self.thisFailsStrictLockCheck()
 
437
        tree = self.make_branch_and_tree('tree')
 
438
        manager = tree.get_shelf_manager()
 
439
        shelf_file = manager.new_shelf()[1]
 
440
        try:
 
441
            shelf_file.write('garbage')
 
442
        finally:
 
443
            shelf_file.close()
 
444
        unshelver = shelf_ui.Unshelver.from_args(directory='tree',
 
445
                                                 action='delete-only')
 
446
        unshelver.run()
 
447
        self.assertIs(None, manager.last_shelf())
 
448
 
 
449
    def test_unshelve_args_invalid_shelf_id(self):
 
450
        self.thisFailsStrictLockCheck()
 
451
        tree = self.make_branch_and_tree('tree')
 
452
        manager = tree.get_shelf_manager()
 
453
        shelf_file = manager.new_shelf()[1]
 
454
        try:
 
455
            shelf_file.write('garbage')
 
456
        finally:
 
457
            shelf_file.close()
 
458
        self.assertRaises(errors.InvalidShelfId,
 
459
            shelf_ui.Unshelver.from_args, directory='tree',
 
460
            action='delete-only', shelf_id='foo')