~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_shelf_ui.py

  • Committer: Aaron Bentley
  • Date: 2008-10-27 15:52:24 UTC
  • mto: This revision was merged to the branch mainline in revision 3823.
  • Revision ID: aaron@aaronbentley.com-20081027155224-08puqycohzr2tgzw
Add tests for Shelver

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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
16
 
 
17
 
 
18
from cStringIO import StringIO
 
19
 
 
20
from bzrlib import shelf_ui, tests
 
21
 
 
22
 
 
23
class ExpectShelver(shelf_ui.Shelver):
 
24
    """A variant of Shelver that intercepts console activity, for testing."""
 
25
 
 
26
    def __init__(self, tree, target_tree):
 
27
        shelf_ui.Shelver.__init__(self, tree, target_tree, None)
 
28
        self.expected = []
 
29
        self.diff_writer = StringIO()
 
30
 
 
31
    def expect(self, prompt, response):
 
32
        self.expected.append((prompt, response))
 
33
 
 
34
    def prompt(self, message):
 
35
        try:
 
36
            prompt, response = self.expected.pop(0)
 
37
        except IndexError:
 
38
            raise AssertionError('Unexpected prompt: %s' % message)
 
39
        if prompt != message:
 
40
            raise AssertionError('Wrong prompt: %s' % message)
 
41
        return response
 
42
 
 
43
 
 
44
class TestShelver(tests.TestCaseWithTransport):
 
45
 
 
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')])
 
52
        return tree
 
53
 
 
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))
 
59
 
 
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))
 
66
 
 
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
 
72
        shelver.run()
 
73
        self.assertFileEqual('z\nb\nc\nd\ne\nf\n', 'tree/foo')
 
74
 
 
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')
 
80
        shelver.run()
 
81
        self.assertFileEqual('z\nb\nc\nd\ne\nf\n', 'tree/foo')
 
82
 
 
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')
 
88
        shelver.run()
 
89
        self.assertFileEqual('a\nb\nc\nd\ne\nf\n', 'tree/foo')
 
90
 
 
91
    def test_shelve_rename(self):
 
92
        tree = self.create_shelvable_tree()
 
93
        tree.revert()
 
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')
 
98
        shelver.run()
 
99
        self.assertFileEqual('a\nb\nc\nd\ne\nf\n', 'tree/foo')