/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
0.16.89 by Aaron Bentley
Add tests for Shelver
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
0.16.91 by Aaron Bentley
Test finish and quit
19
import os
0.16.89 by Aaron Bentley
Add tests for Shelver
20
21
from bzrlib import shelf_ui, tests
22
23
24
class ExpectShelver(shelf_ui.Shelver):
25
    """A variant of Shelver that intercepts console activity, for testing."""
26
0.16.92 by Aaron Bentley
Test kind, add, binary, --all
27
    def __init__(self, work_tree, target_tree, path=None, auto=False,
28
                 auto_apply=False, file_list=None, message=None):
0.16.97 by Aaron Bentley
Turn diff_file and text_differ into instance variables.
29
        shelf_ui.Shelver.__init__(self, work_tree, target_tree, auto,
30
                                  auto_apply, file_list, message)
0.16.89 by Aaron Bentley
Add tests for Shelver
31
        self.expected = []
32
        self.diff_writer = StringIO()
33
34
    def expect(self, prompt, response):
35
        self.expected.append((prompt, response))
36
37
    def prompt(self, message):
38
        try:
39
            prompt, response = self.expected.pop(0)
40
        except IndexError:
41
            raise AssertionError('Unexpected prompt: %s' % message)
42
        if prompt != message:
43
            raise AssertionError('Wrong prompt: %s' % message)
44
        return response
45
46
0.16.90 by Aaron Bentley
Handle shelving multiple diff hunks
47
LINES_AJ = 'a\nb\nc\nd\ne\nf\ng\nh\ni\nj\n'
48
49
50
LINES_ZY = 'z\nb\nc\nd\ne\nf\ng\nh\ni\ny\n'
51
52
53
LINES_AY = 'a\nb\nc\nd\ne\nf\ng\nh\ni\ny\n'
54
55
0.16.89 by Aaron Bentley
Add tests for Shelver
56
class TestShelver(tests.TestCaseWithTransport):
57
58
    def create_shelvable_tree(self):
59
        tree = self.make_branch_and_tree('tree')
0.16.90 by Aaron Bentley
Handle shelving multiple diff hunks
60
        self.build_tree_contents([('tree/foo', LINES_AJ)])
0.16.89 by Aaron Bentley
Add tests for Shelver
61
        tree.add('foo', 'foo-id')
62
        tree.commit('added foo')
0.16.90 by Aaron Bentley
Handle shelving multiple diff hunks
63
        self.build_tree_contents([('tree/foo', LINES_ZY)])
0.16.89 by Aaron Bentley
Add tests for Shelver
64
        return tree
65
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))
71
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))
78
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')
0.16.90 by Aaron Bentley
Handle shelving multiple diff hunks
83
        shelver.expect('Shelve? [yNfq]', 'n')
0.16.89 by Aaron Bentley
Add tests for Shelver
84
        # No final shelving prompt because no changes were selected
85
        shelver.run()
0.16.90 by Aaron Bentley
Handle shelving multiple diff hunks
86
        self.assertFileEqual(LINES_ZY, 'tree/foo')
0.16.89 by Aaron Bentley
Add tests for Shelver
87
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')
0.16.90 by Aaron Bentley
Handle shelving multiple diff hunks
92
        shelver.expect('Shelve? [yNfq]', 'y')
93
        shelver.expect('Shelve 2 change(s)? [yNfq]', 'n')
0.16.89 by Aaron Bentley
Add tests for Shelver
94
        shelver.run()
0.16.90 by Aaron Bentley
Handle shelving multiple diff hunks
95
        self.assertFileEqual(LINES_ZY, 'tree/foo')
0.16.89 by Aaron Bentley
Add tests for Shelver
96
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')
0.16.90 by Aaron Bentley
Handle shelving multiple diff hunks
101
        shelver.expect('Shelve? [yNfq]', 'y')
102
        shelver.expect('Shelve 2 change(s)? [yNfq]', 'y')
103
        shelver.run()
104
        self.assertFileEqual(LINES_AJ, 'tree/foo')
105
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')
0.16.89 by Aaron Bentley
Add tests for Shelver
111
        shelver.expect('Shelve 1 change(s)? [yNfq]', 'y')
112
        shelver.run()
0.16.90 by Aaron Bentley
Handle shelving multiple diff hunks
113
        self.assertFileEqual(LINES_AY, 'tree/foo')
0.16.89 by Aaron Bentley
Add tests for Shelver
114
0.16.92 by Aaron Bentley
Test kind, add, binary, --all
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')
121
        shelver.run()
122
        self.assertFileEqual(LINES_AJ, 'tree/foo')
123
124
0.16.89 by Aaron Bentley
Add tests for Shelver
125
    def test_shelve_rename(self):
126
        tree = self.create_shelvable_tree()
127
        tree.rename_one('foo', 'bar')
128
        shelver = ExpectShelver(tree, tree.basis_tree())
129
        shelver.expect('Shelve renaming foo => bar? [yNfq]', 'y')
0.16.96 by Aaron Bentley
Fix shelving with renames
130
        shelver.expect('Shelve? [yNfq]', 'y')
131
        shelver.expect('Shelve? [yNfq]', 'y')
132
        shelver.expect('Shelve 3 change(s)? [yNfq]', 'y')
0.16.89 by Aaron Bentley
Add tests for Shelver
133
        shelver.run()
0.16.90 by Aaron Bentley
Handle shelving multiple diff hunks
134
        self.assertFileEqual(LINES_AJ, 'tree/foo')
0.16.91 by Aaron Bentley
Test finish and quit
135
136
    def test_shelve_deletion(self):
137
        tree = self.create_shelvable_tree()
138
        os.unlink('tree/foo')
139
        shelver = ExpectShelver(tree, tree.basis_tree())
140
        shelver.expect('Shelve removing file "foo"?  [yNfq]', 'y')
141
        shelver.expect('Shelve 1 change(s)? [yNfq]', 'y')
142
        shelver.run()
143
        self.assertFileEqual(LINES_AJ, 'tree/foo')
144
0.16.92 by Aaron Bentley
Test kind, add, binary, --all
145
    def test_shelve_creation(self):
146
        tree = self.make_branch_and_tree('tree')
147
        tree.commit('add tree root')
148
        self.build_tree(['tree/foo'])
149
        tree.add('foo')
150
        shelver = ExpectShelver(tree, tree.basis_tree())
151
        shelver.expect('Shelve adding file "foo"? [yNfq]', 'y')
152
        shelver.expect('Shelve 1 change(s)? [yNfq]', 'y')
153
        shelver.run()
154
        self.failIfExists('tree/foo')
155
156
    def test_shelve_kind_change(self):
157
        tree = self.create_shelvable_tree()
158
        os.unlink('tree/foo')
159
        os.mkdir('tree/foo')
160
        shelver = ExpectShelver(tree, tree.basis_tree())
161
        shelver.expect('Shelve changing "foo" from file to directory? [yNfq]',
162
                       'y')
163
        shelver.expect('Shelve 1 change(s)? [yNfq]', 'y')
164
0.16.91 by Aaron Bentley
Test finish and quit
165
    def test_shelve_finish(self):
166
        tree = self.create_shelvable_tree()
167
        shelver = ExpectShelver(tree, tree.basis_tree())
168
        shelver.expect('Shelve? [yNfq]', 'f')
169
        shelver.expect('Shelve 2 change(s)? [yNfq]', 'y')
170
        shelver.run()
171
        self.assertFileEqual(LINES_AJ, 'tree/foo')
172
173
    def test_shelve_quit(self):
174
        tree = self.create_shelvable_tree()
175
        shelver = ExpectShelver(tree, tree.basis_tree())
176
        shelver.expect('Shelve? [yNfq]', 'q')
177
        self.assertRaises(SystemExit, shelver.run)
178
        self.assertFileEqual(LINES_ZY, 'tree/foo')
0.16.92 by Aaron Bentley
Test kind, add, binary, --all
179
180
    def test_shelve_all(self):
181
        tree = self.create_shelvable_tree()
0.16.94 by Aaron Bentley
Add unshelve tests
182
        ExpectShelver.from_args(all=True, directory='tree').run()
0.16.92 by Aaron Bentley
Test kind, add, binary, --all
183
        self.assertFileEqual(LINES_AJ, 'tree/foo')
0.16.93 by Aaron Bentley
Test shelving one file
184
185
    def test_shelve_filename(self):
186
        tree = self.create_shelvable_tree()
187
        self.build_tree(['tree/bar'])
188
        tree.add('bar')
189
        shelver = ExpectShelver(tree, tree.basis_tree(), file_list=['bar'])
190
        shelver.expect('Shelve adding file "bar"? [yNfq]', 'y')
191
        shelver.expect('Shelve 1 change(s)? [yNfq]', 'y')
192
        shelver.run()
0.16.94 by Aaron Bentley
Add unshelve tests
193
194
195
class TestUnshelver(tests.TestCaseWithTransport):
196
197
    def create_tree_with_shelf(self):
198
        tree = self.make_branch_and_tree('tree')
199
        self.build_tree_contents([('tree/foo', LINES_AJ)])
200
        tree.add('foo', 'foo-id')
201
        tree.commit('added foo')
202
        self.build_tree_contents([('tree/foo', LINES_ZY)])
0.16.97 by Aaron Bentley
Turn diff_file and text_differ into instance variables.
203
        shelf_ui.Shelver(tree, tree.basis_tree(), auto_apply=True,
0.16.94 by Aaron Bentley
Add unshelve tests
204
                         auto=True).run()
205
        return tree
206
207
    def test_unshelve(self):
208
        tree = self.create_tree_with_shelf()
209
        tree.lock_write()
210
        self.addCleanup(tree.unlock)
211
        manager = tree.get_shelf_manager()
212
        shelf_ui.Unshelver(tree, manager, 1, True, True, True).run()
213
        self.assertFileEqual(LINES_ZY, 'tree/foo')
214
215
    def test_unshelve_args(self):
216
        tree = self.create_tree_with_shelf()
217
        shelf_ui.Unshelver.from_args(directory='tree').run()
218
        self.assertFileEqual(LINES_ZY, 'tree/foo')
219
        self.assertIs(None, tree.get_shelf_manager().last_shelf())
220
221
    def test_unshelve_args_dry_run(self):
222
        tree = self.create_tree_with_shelf()
223
        shelf_ui.Unshelver.from_args(directory='tree', action='dry-run').run()
224
        self.assertFileEqual(LINES_AJ, 'tree/foo')
225
        self.assertEqual(1, tree.get_shelf_manager().last_shelf())
226
227
    def test_unshelve_args_delete_only(self):
228
        tree = self.make_branch_and_tree('tree')
229
        manager = tree.get_shelf_manager()
230
        shelf_file = manager.new_shelf()[1]
231
        try:
232
            shelf_file.write('garbage')
233
        finally:
234
            shelf_file.close()
235
        unshelver = shelf_ui.Unshelver.from_args(directory='tree',
236
                                                 action='delete-only')
237
        unshelver.run()
238
        self.assertIs(None, manager.last_shelf())