/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
#
0.16.101 by Aaron Bentley
Update GPL formatting and copyright
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
0.16.89 by Aaron Bentley
Add tests for Shelver
16
17
18
from cStringIO import StringIO
0.16.91 by Aaron Bentley
Test finish and quit
19
import os
0.16.108 by Aaron Bentley
Shelf supports multiple diff writers.
20
import sys
0.16.89 by Aaron Bentley
Add tests for Shelver
21
0.16.103 by Aaron Bentley
raise UserAbort instead of doing sys.exit
22
from bzrlib import errors, shelf_ui, tests
0.16.89 by Aaron Bentley
Add tests for Shelver
23
24
25
class ExpectShelver(shelf_ui.Shelver):
26
    """A variant of Shelver that intercepts console activity, for testing."""
27
0.16.108 by Aaron Bentley
Shelf supports multiple diff writers.
28
    def __init__(self, work_tree, target_tree, diff_writer=None, path=None,
29
                 auto=False, auto_apply=False, file_list=None, message=None):
30
        shelf_ui.Shelver.__init__(self, work_tree, target_tree, diff_writer,
31
                                  auto, auto_apply, file_list, message)
0.16.89 by Aaron Bentley
Add tests for Shelver
32
        self.expected = []
33
        self.diff_writer = StringIO()
34
35
    def expect(self, prompt, response):
36
        self.expected.append((prompt, response))
37
38
    def prompt(self, message):
39
        try:
40
            prompt, response = self.expected.pop(0)
41
        except IndexError:
42
            raise AssertionError('Unexpected prompt: %s' % message)
43
        if prompt != message:
44
            raise AssertionError('Wrong prompt: %s' % message)
45
        return response
46
47
0.16.90 by Aaron Bentley
Handle shelving multiple diff hunks
48
LINES_AJ = 'a\nb\nc\nd\ne\nf\ng\nh\ni\nj\n'
49
50
51
LINES_ZY = 'z\nb\nc\nd\ne\nf\ng\nh\ni\ny\n'
52
53
54
LINES_AY = 'a\nb\nc\nd\ne\nf\ng\nh\ni\ny\n'
55
56
0.16.89 by Aaron Bentley
Add tests for Shelver
57
class TestShelver(tests.TestCaseWithTransport):
58
59
    def create_shelvable_tree(self):
60
        tree = self.make_branch_and_tree('tree')
0.16.90 by Aaron Bentley
Handle shelving multiple diff hunks
61
        self.build_tree_contents([('tree/foo', LINES_AJ)])
0.16.89 by Aaron Bentley
Add tests for Shelver
62
        tree.add('foo', 'foo-id')
63
        tree.commit('added foo')
0.16.90 by Aaron Bentley
Handle shelving multiple diff hunks
64
        self.build_tree_contents([('tree/foo', LINES_ZY)])
0.16.89 by Aaron Bentley
Add tests for Shelver
65
        return tree
66
67
    def test_unexpected_prompt_failure(self):
68
        tree = self.create_shelvable_tree()
69
        shelver = ExpectShelver(tree, tree.basis_tree())
70
        e = self.assertRaises(AssertionError, shelver.run)
3990.4.1 by Daniel Watkins
Changed all shelve tests to expect a '?'.
71
        self.assertEqual('Unexpected prompt: Shelve? [yNfq?]', str(e))
0.16.89 by Aaron Bentley
Add tests for Shelver
72
73
    def test_wrong_prompt_failure(self):
74
        tree = self.create_shelvable_tree()
75
        shelver = ExpectShelver(tree, tree.basis_tree())
76
        shelver.expect('foo', 'y')
77
        e = self.assertRaises(AssertionError, shelver.run)
3990.4.1 by Daniel Watkins
Changed all shelve tests to expect a '?'.
78
        self.assertEqual('Wrong prompt: Shelve? [yNfq?]', str(e))
0.16.89 by Aaron Bentley
Add tests for Shelver
79
80
    def test_shelve_not_diff(self):
81
        tree = self.create_shelvable_tree()
82
        shelver = ExpectShelver(tree, tree.basis_tree())
3990.4.1 by Daniel Watkins
Changed all shelve tests to expect a '?'.
83
        shelver.expect('Shelve? [yNfq?]', 'n')
84
        shelver.expect('Shelve? [yNfq?]', 'n')
0.16.89 by Aaron Bentley
Add tests for Shelver
85
        # No final shelving prompt because no changes were selected
86
        shelver.run()
0.16.90 by Aaron Bentley
Handle shelving multiple diff hunks
87
        self.assertFileEqual(LINES_ZY, 'tree/foo')
0.16.89 by Aaron Bentley
Add tests for Shelver
88
89
    def test_shelve_diff_no(self):
90
        tree = self.create_shelvable_tree()
91
        shelver = ExpectShelver(tree, tree.basis_tree())
3990.4.1 by Daniel Watkins
Changed all shelve tests to expect a '?'.
92
        shelver.expect('Shelve? [yNfq?]', 'y')
93
        shelver.expect('Shelve? [yNfq?]', 'y')
94
        shelver.expect('Shelve 2 change(s)? [yNfq?]', 'n')
0.16.89 by Aaron Bentley
Add tests for Shelver
95
        shelver.run()
0.16.90 by Aaron Bentley
Handle shelving multiple diff hunks
96
        self.assertFileEqual(LINES_ZY, 'tree/foo')
0.16.89 by Aaron Bentley
Add tests for Shelver
97
98
    def test_shelve_diff(self):
99
        tree = self.create_shelvable_tree()
100
        shelver = ExpectShelver(tree, tree.basis_tree())
3990.4.1 by Daniel Watkins
Changed all shelve tests to expect a '?'.
101
        shelver.expect('Shelve? [yNfq?]', 'y')
102
        shelver.expect('Shelve? [yNfq?]', 'y')
103
        shelver.expect('Shelve 2 change(s)? [yNfq?]', 'y')
0.16.90 by Aaron Bentley
Handle shelving multiple diff hunks
104
        shelver.run()
105
        self.assertFileEqual(LINES_AJ, 'tree/foo')
106
107
    def test_shelve_one_diff(self):
108
        tree = self.create_shelvable_tree()
109
        shelver = ExpectShelver(tree, tree.basis_tree())
3990.4.1 by Daniel Watkins
Changed all shelve tests to expect a '?'.
110
        shelver.expect('Shelve? [yNfq?]', 'y')
111
        shelver.expect('Shelve? [yNfq?]', 'n')
112
        shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
0.16.89 by Aaron Bentley
Add tests for Shelver
113
        shelver.run()
0.16.90 by Aaron Bentley
Handle shelving multiple diff hunks
114
        self.assertFileEqual(LINES_AY, 'tree/foo')
0.16.89 by Aaron Bentley
Add tests for Shelver
115
0.16.92 by Aaron Bentley
Test kind, add, binary, --all
116
    def test_shelve_binary_change(self):
117
        tree = self.create_shelvable_tree()
118
        self.build_tree_contents([('tree/foo', '\x00')])
119
        shelver = ExpectShelver(tree, tree.basis_tree())
3990.4.1 by Daniel Watkins
Changed all shelve tests to expect a '?'.
120
        shelver.expect('Shelve binary changes? [yNfq?]', 'y')
121
        shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
0.16.92 by Aaron Bentley
Test kind, add, binary, --all
122
        shelver.run()
123
        self.assertFileEqual(LINES_AJ, 'tree/foo')
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())
3990.4.1 by Daniel Watkins
Changed all shelve tests to expect a '?'.
129
        shelver.expect('Shelve renaming "foo" => "bar"? [yNfq?]', 'y')
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())
3990.4.1 by Daniel Watkins
Changed all shelve tests to expect a '?'.
140
        shelver.expect('Shelve removing file "foo"? [yNfq?]', 'y')
141
        shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
0.16.91 by Aaron Bentley
Test finish and quit
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())
3990.4.1 by Daniel Watkins
Changed all shelve tests to expect a '?'.
151
        shelver.expect('Shelve adding file "foo"? [yNfq?]', 'y')
152
        shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
0.16.92 by Aaron Bentley
Test kind, add, binary, --all
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())
3990.4.1 by Daniel Watkins
Changed all shelve tests to expect a '?'.
161
        shelver.expect('Shelve changing "foo" from file to directory? [yNfq?]',
0.16.92 by Aaron Bentley
Test kind, add, binary, --all
162
                       'y')
3990.4.1 by Daniel Watkins
Changed all shelve tests to expect a '?'.
163
        shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
0.16.92 by Aaron Bentley
Test kind, add, binary, --all
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())
3990.4.1 by Daniel Watkins
Changed all shelve tests to expect a '?'.
168
        shelver.expect('Shelve? [yNfq?]', 'f')
169
        shelver.expect('Shelve 2 change(s)? [yNfq?]', 'y')
0.16.91 by Aaron Bentley
Test finish and quit
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())
3990.4.1 by Daniel Watkins
Changed all shelve tests to expect a '?'.
176
        shelver.expect('Shelve? [yNfq?]', 'q')
0.16.103 by Aaron Bentley
raise UserAbort instead of doing sys.exit
177
        self.assertRaises(errors.UserAbort, shelver.run)
0.16.91 by Aaron Bentley
Test finish and quit
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.108 by Aaron Bentley
Shelf supports multiple diff writers.
182
        ExpectShelver.from_args(sys.stdout, 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'])
3990.4.1 by Daniel Watkins
Changed all shelve tests to expect a '?'.
190
        shelver.expect('Shelve adding file "bar"? [yNfq?]', 'y')
191
        shelver.expect('Shelve 1 change(s)? [yNfq?]', 'y')
0.16.93 by Aaron Bentley
Test shelving one file
192
        shelver.run()
0.16.94 by Aaron Bentley
Add unshelve tests
193
3990.4.2 by Daniel Watkins
Added test for help option.
194
    def test_shelve_help(self):
195
        tree = self.create_shelvable_tree()
196
        shelver = ExpectShelver(tree, tree.basis_tree())
197
        shelver.expect('Shelve? [yNfq?]', '?')
198
        shelver.expect('Shelve? [(y)es, (N)o, (f)inish, or (q)uit]', 'f')
199
        shelver.expect('Shelve 2 change(s)? [yNfq?]', 'y')
200
        shelver.run()
201
4100.3.1 by Aaron Bentley
Implement shelve --destroy
202
    def test_shelve_distroy(self):
203
        tree = self.create_shelvable_tree()
204
        shelver = shelf_ui.Shelver.from_args(sys.stdout, all=True,
205
                                             directory='tree', destroy=True)
206
        shelver.run()
207
        self.assertIs(None, tree.get_shelf_manager().last_shelf())
208
        self.assertFileEqual(LINES_AJ, 'tree/foo')
209
0.16.94 by Aaron Bentley
Add unshelve tests
210
211
class TestUnshelver(tests.TestCaseWithTransport):
212
213
    def create_tree_with_shelf(self):
214
        tree = self.make_branch_and_tree('tree')
215
        self.build_tree_contents([('tree/foo', LINES_AJ)])
216
        tree.add('foo', 'foo-id')
217
        tree.commit('added foo')
218
        self.build_tree_contents([('tree/foo', LINES_ZY)])
0.16.97 by Aaron Bentley
Turn diff_file and text_differ into instance variables.
219
        shelf_ui.Shelver(tree, tree.basis_tree(), auto_apply=True,
0.16.94 by Aaron Bentley
Add unshelve tests
220
                         auto=True).run()
221
        return tree
222
223
    def test_unshelve(self):
224
        tree = self.create_tree_with_shelf()
225
        tree.lock_write()
226
        self.addCleanup(tree.unlock)
227
        manager = tree.get_shelf_manager()
228
        shelf_ui.Unshelver(tree, manager, 1, True, True, True).run()
229
        self.assertFileEqual(LINES_ZY, 'tree/foo')
230
231
    def test_unshelve_args(self):
232
        tree = self.create_tree_with_shelf()
233
        shelf_ui.Unshelver.from_args(directory='tree').run()
234
        self.assertFileEqual(LINES_ZY, 'tree/foo')
235
        self.assertIs(None, tree.get_shelf_manager().last_shelf())
236
237
    def test_unshelve_args_dry_run(self):
238
        tree = self.create_tree_with_shelf()
239
        shelf_ui.Unshelver.from_args(directory='tree', action='dry-run').run()
240
        self.assertFileEqual(LINES_AJ, 'tree/foo')
241
        self.assertEqual(1, tree.get_shelf_manager().last_shelf())
242
243
    def test_unshelve_args_delete_only(self):
244
        tree = self.make_branch_and_tree('tree')
245
        manager = tree.get_shelf_manager()
246
        shelf_file = manager.new_shelf()[1]
247
        try:
248
            shelf_file.write('garbage')
249
        finally:
250
            shelf_file.close()
251
        unshelver = shelf_ui.Unshelver.from_args(directory='tree',
252
                                                 action='delete-only')
253
        unshelver.run()
254
        self.assertIs(None, manager.last_shelf())
3990.2.1 by Daniel Watkins
Added test for unshelve being passed an invalid shelf_id.
255
256
    def test_unshelve_args_invalid_shelf_id(self):
257
        tree = self.make_branch_and_tree('tree')
258
        manager = tree.get_shelf_manager()
259
        shelf_file = manager.new_shelf()[1]
260
        try:
261
            shelf_file.write('garbage')
262
        finally:
263
            shelf_file.close()
264
        self.assertRaises(errors.InvalidShelfId,
3999.1.1 by Ian Clatworthy
Improve shelf documentation & fix backtrace (Daniel Watkins)
265
            shelf_ui.Unshelver.from_args, directory='tree',
266
            action='delete-only', shelf_id='foo')