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
20
from bzrlib import shelf_ui, tests
23
class ExpectShelver(shelf_ui.Shelver):
24
"""A variant of Shelver that intercepts console activity, for testing."""
26
def __init__(self, tree, target_tree):
27
shelf_ui.Shelver.__init__(self, tree, target_tree, None)
29
self.diff_writer = StringIO()
31
def expect(self, prompt, response):
32
self.expected.append((prompt, response))
34
def prompt(self, message):
36
prompt, response = self.expected.pop(0)
38
raise AssertionError('Unexpected prompt: %s' % message)
40
raise AssertionError('Wrong prompt: %s' % message)
44
class TestShelver(tests.TestCaseWithTransport):
46
def create_shelvable_tree(self):
47
tree = self.make_branch_and_tree('tree')
48
self.build_tree_contents([('tree/foo', 'a\nb\nc\nd\ne\nf\n')])
49
tree.add('foo', 'foo-id')
50
tree.commit('added foo')
51
self.build_tree_contents([('tree/foo', 'z\nb\nc\nd\ne\nf\n')])
54
def test_unexpected_prompt_failure(self):
55
tree = self.create_shelvable_tree()
56
shelver = ExpectShelver(tree, tree.basis_tree())
57
e = self.assertRaises(AssertionError, shelver.run)
58
self.assertEqual('Unexpected prompt: Shelve? [yNfq]', str(e))
60
def test_wrong_prompt_failure(self):
61
tree = self.create_shelvable_tree()
62
shelver = ExpectShelver(tree, tree.basis_tree())
63
shelver.expect('foo', 'y')
64
e = self.assertRaises(AssertionError, shelver.run)
65
self.assertEqual('Wrong prompt: Shelve? [yNfq]', str(e))
67
def test_shelve_not_diff(self):
68
tree = self.create_shelvable_tree()
69
shelver = ExpectShelver(tree, tree.basis_tree())
70
shelver.expect('Shelve? [yNfq]', 'n')
71
# No final shelving prompt because no changes were selected
73
self.assertFileEqual('z\nb\nc\nd\ne\nf\n', 'tree/foo')
75
def test_shelve_diff_no(self):
76
tree = self.create_shelvable_tree()
77
shelver = ExpectShelver(tree, tree.basis_tree())
78
shelver.expect('Shelve? [yNfq]', 'y')
79
shelver.expect('Shelve 1 change(s)? [yNfq]', 'n')
81
self.assertFileEqual('z\nb\nc\nd\ne\nf\n', 'tree/foo')
83
def test_shelve_diff(self):
84
tree = self.create_shelvable_tree()
85
shelver = ExpectShelver(tree, tree.basis_tree())
86
shelver.expect('Shelve? [yNfq]', 'y')
87
shelver.expect('Shelve 1 change(s)? [yNfq]', 'y')
89
self.assertFileEqual('a\nb\nc\nd\ne\nf\n', 'tree/foo')
91
def test_shelve_rename(self):
92
tree = self.create_shelvable_tree()
94
tree.rename_one('foo', 'bar')
95
shelver = ExpectShelver(tree, tree.basis_tree())
96
shelver.expect('Shelve renaming foo => bar? [yNfq]', 'y')
97
shelver.expect('Shelve 1 change(s)? [yNfq]', 'y')
99
self.assertFileEqual('a\nb\nc\nd\ne\nf\n', 'tree/foo')