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