/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
4665.5.1 by Vincent Ladeuil
Start some shell-like capability to write tests.
1
# Copyright (C) 2009 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
4665.5.5 by Vincent Ladeuil
Implement 'cd' and 'mkdir'.
17
4665.5.1 by Vincent Ladeuil
Start some shell-like capability to write tests.
18
from bzrlib import (
4665.5.5 by Vincent Ladeuil
Implement 'cd' and 'mkdir'.
19
    osutils,
4665.5.1 by Vincent Ladeuil
Start some shell-like capability to write tests.
20
    tests,
21
    )
22
from bzrlib.tests import script
23
24
4665.5.2 by Vincent Ladeuil
Handle simple, double and back quotes.
25
class TestScriptSyntax(tests.TestCase):
4665.5.1 by Vincent Ladeuil
Start some shell-like capability to write tests.
26
27
    def test_comment_is_ignored(self):
28
        self.assertEquals([], script._script_to_commands('#comment\n'))
29
30
    def test_empty_line_is_ignored(self):
31
        self.assertEquals([], script._script_to_commands('\n'))
32
33
    def test_simple_command(self):
4665.5.3 by Vincent Ladeuil
Separate error from normal output.
34
        self.assertEquals([(['cd', 'trunk'], None, None, None)],
4665.5.1 by Vincent Ladeuil
Start some shell-like capability to write tests.
35
                           script._script_to_commands('cd trunk'))
36
4665.5.2 by Vincent Ladeuil
Handle simple, double and back quotes.
37
    def test_command_with_single_quoted_param(self):
38
        story = """bzr commit -m 'two words'"""
4665.5.3 by Vincent Ladeuil
Separate error from normal output.
39
        self.assertEquals([(['bzr', 'commit', '-m', 'two words'],
40
                            None, None, None)],
4665.5.2 by Vincent Ladeuil
Handle simple, double and back quotes.
41
                           script._script_to_commands(story))
42
43
    def test_command_with_double_quoted_param(self):
44
        story = """bzr commit -m "two words" """
4665.5.3 by Vincent Ladeuil
Separate error from normal output.
45
        self.assertEquals([(['bzr', 'commit', '-m', 'two words'],
46
                            None, None, None)],
4665.5.2 by Vincent Ladeuil
Handle simple, double and back quotes.
47
                           script._script_to_commands(story))
48
4665.5.1 by Vincent Ladeuil
Start some shell-like capability to write tests.
49
    def test_command_with_input(self):
4665.5.7 by Vincent Ladeuil
Simplify output/errors handling.
50
        self.assertEquals([(['cat', '>file'], 'content\n', None, None)],
4665.5.1 by Vincent Ladeuil
Start some shell-like capability to write tests.
51
                           script._script_to_commands('cat >file\n<content\n'))
52
53
    def test_command_with_output(self):
54
        story = """
55
bzr add
56
>adding file
4665.5.7 by Vincent Ladeuil
Simplify output/errors handling.
57
>adding file2
4665.5.1 by Vincent Ladeuil
Start some shell-like capability to write tests.
58
"""
4665.5.7 by Vincent Ladeuil
Simplify output/errors handling.
59
        self.assertEquals([(['bzr', 'add'], None,
60
                            'adding file\nadding file2\n', None)],
4665.5.1 by Vincent Ladeuil
Start some shell-like capability to write tests.
61
                          script._script_to_commands(story))
62
4665.5.3 by Vincent Ladeuil
Separate error from normal output.
63
    def test_command_with_error(self):
64
        story = """
65
bzr branch foo
66
2>bzr: ERROR: Not a branch: "foo"
67
"""
68
        self.assertEquals([(['bzr', 'branch', 'foo'],
4665.5.7 by Vincent Ladeuil
Simplify output/errors handling.
69
                            None, None, 'bzr: ERROR: Not a branch: "foo"\n')],
4665.5.3 by Vincent Ladeuil
Separate error from normal output.
70
                          script._script_to_commands(story))
4597.4.5 by Vincent Ladeuil
Merge shell-like tests patch to define errors on status
71
4665.5.1 by Vincent Ladeuil
Start some shell-like capability to write tests.
72
    def test_input_without_command(self):
73
        self.assertRaises(SyntaxError, script._script_to_commands, '<input')
74
75
    def test_output_without_command(self):
76
        self.assertRaises(SyntaxError, script._script_to_commands, '>input')
77
4665.5.2 by Vincent Ladeuil
Handle simple, double and back quotes.
78
    def test_command_with_backquotes(self):
4665.5.1 by Vincent Ladeuil
Start some shell-like capability to write tests.
79
        story = """
80
foo = `bzr file-id toto`
81
"""
4665.5.3 by Vincent Ladeuil
Separate error from normal output.
82
        self.assertEquals([(['foo', '=', '`bzr file-id toto`'],
83
                            None, None, None)],
4665.5.1 by Vincent Ladeuil
Start some shell-like capability to write tests.
84
                          script._script_to_commands(story))
85
86
4665.5.11 by Vincent Ladeuil
Create a new test case based on TestCaseWithMemoryTransport.
87
class TestScriptExecution(script.TestCaseWithTransportAndScript):
4665.5.4 by Vincent Ladeuil
Implement a 'cat' command.
88
89
    def test_unknown_command(self):
90
        self.assertRaises(SyntaxError, self.run_script, 'foo')
91
4665.5.13 by Vincent Ladeuil
Script execution must stop on unexpected errors.
92
    def test_stops_on_unexpected_output(self):
4665.5.6 by Vincent Ladeuil
Implement 'bzr' command.
93
        story = """
94
mkdir dir
95
cd dir
4597.4.5 by Vincent Ladeuil
Merge shell-like tests patch to define errors on status
96
>The cd command ouputs nothing
4665.5.6 by Vincent Ladeuil
Implement 'bzr' command.
97
"""
98
        self.assertRaises(AssertionError, self.run_script, story)
99
4665.5.4 by Vincent Ladeuil
Implement a 'cat' command.
100
4665.5.13 by Vincent Ladeuil
Script execution must stop on unexpected errors.
101
    def test_stops_on_unexpected_error(self):
102
        story = """
103
cat
104
<Hello
105
bzr not-a-command
106
"""
107
        self.assertRaises(AssertionError, self.run_script, story)
108
109
    def test_continue_on_expected_error(self):
110
        story = """
111
bzr not-a-command
112
2>..."not-a-command"
113
"""
114
        self.run_script(story)
115
4597.4.5 by Vincent Ladeuil
Merge shell-like tests patch to define errors on status
116
    def test_continue_on_error_output(self):
117
        # The status matters, not the output
118
        story = """
119
bzr init
120
cat >file
121
<Hello
122
bzr add file
123
bzr commit -m 'adding file'
124
"""
125
        self.run_script(story)
126
4665.5.12 by Vincent Ladeuil
Support '...' in expected strings.
127
    def test_ellipsis_output(self):
128
        story = """
129
cat
130
<first line
131
<second line
132
<last line
133
>first line
134
>...
135
>last line
136
"""
137
        self.run_script(story)
138
        story = """
139
bzr not-a-command
140
2>..."not-a-command"
141
"""
142
        self.run_script(story)
143
144
        story = """
145
bzr branch not-a-branch
146
2>bzr: ERROR: Not a branch...not-a-branch/".
147
"""
148
        self.run_script(story)
149
150
4665.5.11 by Vincent Ladeuil
Create a new test case based on TestCaseWithMemoryTransport.
151
class TestCat(script.TestCaseWithTransportAndScript):
4665.5.4 by Vincent Ladeuil
Implement a 'cat' command.
152
153
    def test_cat_usage(self):
154
        self.assertRaises(SyntaxError, self.run_script, 'cat foo bar baz')
4665.5.8 by Vincent Ladeuil
Implement 'echo' command.
155
        self.assertRaises(SyntaxError, self.run_script, 'cat foo <bar')
4665.5.4 by Vincent Ladeuil
Implement a 'cat' command.
156
157
    def test_cat_input_to_output(self):
4597.4.5 by Vincent Ladeuil
Merge shell-like tests patch to define errors on status
158
        retcode, out, err = self.run_command(['cat'],
159
                                             'content\n', 'content\n', None)
4665.5.8 by Vincent Ladeuil
Implement 'echo' command.
160
        self.assertEquals('content\n', out)
161
        self.assertEquals(None, err)
4665.5.4 by Vincent Ladeuil
Implement a 'cat' command.
162
163
    def test_cat_file_to_output(self):
164
        self.build_tree_contents([('file', 'content\n')])
4597.4.5 by Vincent Ladeuil
Merge shell-like tests patch to define errors on status
165
        retcode, out, err = self.run_command(['cat', 'file'],
166
                                             None, 'content\n', None)
4665.5.8 by Vincent Ladeuil
Implement 'echo' command.
167
        self.assertEquals('content\n', out)
168
        self.assertEquals(None, err)
4665.5.4 by Vincent Ladeuil
Implement a 'cat' command.
169
170
    def test_cat_input_to_file(self):
4597.4.5 by Vincent Ladeuil
Merge shell-like tests patch to define errors on status
171
        retcode, out, err = self.run_command(['cat', '>file'],
172
                                             'content\n', None, None)
4665.5.4 by Vincent Ladeuil
Implement a 'cat' command.
173
        self.assertFileEqual('content\n', 'file')
4665.5.8 by Vincent Ladeuil
Implement 'echo' command.
174
        self.assertEquals(None, out)
175
        self.assertEquals(None, err)
4597.4.5 by Vincent Ladeuil
Merge shell-like tests patch to define errors on status
176
        retcode, out, err = self.run_command(['cat', '>>file'],
177
                                             'more\n', None, None)
4665.5.8 by Vincent Ladeuil
Implement 'echo' command.
178
        self.assertFileEqual('content\nmore\n', 'file')
179
        self.assertEquals(None, out)
180
        self.assertEquals(None, err)
4665.5.4 by Vincent Ladeuil
Implement a 'cat' command.
181
182
    def test_cat_file_to_file(self):
183
        self.build_tree_contents([('file', 'content\n')])
4597.4.5 by Vincent Ladeuil
Merge shell-like tests patch to define errors on status
184
        retcode, out, err = self.run_command(['cat', 'file', '>file2'],
185
                                             None, None, None)
4665.5.4 by Vincent Ladeuil
Implement a 'cat' command.
186
        self.assertFileEqual('content\n', 'file2')
4665.5.5 by Vincent Ladeuil
Implement 'cd' and 'mkdir'.
187
188
4665.5.11 by Vincent Ladeuil
Create a new test case based on TestCaseWithMemoryTransport.
189
class TestMkdir(script.TestCaseWithTransportAndScript):
4665.5.5 by Vincent Ladeuil
Implement 'cd' and 'mkdir'.
190
191
    def test_mkdir_usage(self):
192
        self.assertRaises(SyntaxError, self.run_script, 'mkdir')
193
        self.assertRaises(SyntaxError, self.run_script, 'mkdir foo bar')
194
195
    def test_mkdir_jailed(self):
196
        self.assertRaises(ValueError, self.run_script, 'mkdir /out-of-jail')
197
        self.assertRaises(ValueError, self.run_script, 'mkdir ../out-of-jail')
198
199
    def test_mkdir_in_jail(self):
200
        self.run_script("""
201
mkdir dir
202
cd dir
203
mkdir ../dir2
204
cd ..
205
""")
206
        self.failUnlessExists('dir')
207
        self.failUnlessExists('dir2')
208
209
4665.5.11 by Vincent Ladeuil
Create a new test case based on TestCaseWithMemoryTransport.
210
class TestCd(script.TestCaseWithTransportAndScript):
4665.5.5 by Vincent Ladeuil
Implement 'cd' and 'mkdir'.
211
212
    def test_cd_usage(self):
213
        self.assertRaises(SyntaxError, self.run_script, 'cd foo bar')
214
215
    def test_cd_out_of_jail(self):
216
        self.assertRaises(ValueError, self.run_script, 'cd /out-of-jail')
217
        self.assertRaises(ValueError, self.run_script, 'cd ..')
218
219
    def test_cd_dir_and_back_home(self):
220
        self.assertEquals(self.test_dir, osutils.getcwd())
221
        self.run_script("""
222
mkdir dir
223
cd dir
224
""")
225
        self.assertEquals(osutils.pathjoin(self.test_dir, 'dir'),
226
                          osutils.getcwd())
227
228
        self.run_script('cd')
229
        self.assertEquals(self.test_dir, osutils.getcwd())
4665.5.6 by Vincent Ladeuil
Implement 'bzr' command.
230
231
4665.5.11 by Vincent Ladeuil
Create a new test case based on TestCaseWithMemoryTransport.
232
class TestBzr(script.TestCaseWithTransportAndScript):
4665.5.6 by Vincent Ladeuil
Implement 'bzr' command.
233
234
    def test_bzr_smoke(self):
235
        self.run_script('bzr init branch')
236
        self.failUnlessExists('branch')
4665.5.8 by Vincent Ladeuil
Implement 'echo' command.
237
238
4665.5.11 by Vincent Ladeuil
Create a new test case based on TestCaseWithMemoryTransport.
239
class TestEcho(script.TestCaseWithMemoryTransportAndScript):
4665.5.8 by Vincent Ladeuil
Implement 'echo' command.
240
241
    def test_echo_usage(self):
242
        story = """
243
echo foo
244
<bar
245
"""
246
        self.assertRaises(SyntaxError, self.run_script, story)
247
248
    def test_echo_to_output(self):
4597.4.5 by Vincent Ladeuil
Merge shell-like tests patch to define errors on status
249
        retcode, out, err = self.run_command(['echo'], None, '\n', None)
4665.5.8 by Vincent Ladeuil
Implement 'echo' command.
250
        self.assertEquals('\n', out)
251
        self.assertEquals(None, err)
252
253
    def test_echo_some_to_output(self):
4597.4.5 by Vincent Ladeuil
Merge shell-like tests patch to define errors on status
254
        retcode, out, err = self.run_command(['echo', 'hello'],
255
                                             None, 'hello\n', None)
4665.5.8 by Vincent Ladeuil
Implement 'echo' command.
256
        self.assertEquals('hello\n', out)
257
        self.assertEquals(None, err)
258
259
    def test_echo_more_output(self):
4597.4.5 by Vincent Ladeuil
Merge shell-like tests patch to define errors on status
260
        retcode, out, err = self.run_command(
261
            ['echo', 'hello', 'happy', 'world'],
262
            None, 'hellohappyworld\n', None)
4665.5.8 by Vincent Ladeuil
Implement 'echo' command.
263
        self.assertEquals('hellohappyworld\n', out)
264
        self.assertEquals(None, err)
265
266
    def test_echo_appended(self):
4597.4.5 by Vincent Ladeuil
Merge shell-like tests patch to define errors on status
267
        retcode, out, err = self.run_command(['echo', 'hello', '>file'],
268
                                             None, None, None)
4665.5.8 by Vincent Ladeuil
Implement 'echo' command.
269
        self.assertEquals(None, out)
270
        self.assertEquals(None, err)
271
        self.assertFileEqual('hello\n', 'file')
4597.4.5 by Vincent Ladeuil
Merge shell-like tests patch to define errors on status
272
        retcode, out, err = self.run_command(['echo', 'happy', '>>file'],
273
                                             None, None, None)
4665.5.8 by Vincent Ladeuil
Implement 'echo' command.
274
        self.assertEquals(None, out)
275
        self.assertEquals(None, err)
276
        self.assertFileEqual('hello\nhappy\n', 'file')
4597.4.5 by Vincent Ladeuil
Merge shell-like tests patch to define errors on status
277
4597.4.6 by Vincent Ladeuil
Merge shell-like rm implementation
278
279
class TestRm(script.TestCaseWithTransportAndScript):
280
281
    def test_rm_usage(self):
282
        self.assertRaises(SyntaxError, self.run_script, 'rm')
283
        self.assertRaises(SyntaxError, self.run_script, 'rm -ff foo')
284
285
    def test_rm_file(self):
286
        self.run_script('echo content >file')
287
        self.failUnlessExists('file')
288
        self.run_script('rm file')
289
        self.failIfExists('file')
290
291
    def test_rm_file_force(self):
292
        self.failIfExists('file')
293
        self.run_script('rm -f file')
294
        self.failIfExists('file')
295
296
    def test_rm_files(self):
297
        self.run_script("""
298
echo content >file
299
echo content >file2
300
""")
301
        self.failUnlessExists('file2')
302
        self.run_script('rm file file2')
303
        self.failIfExists('file2')
304
305
    def test_rm_dir(self):
306
        self.run_script('mkdir dir')
307
        self.failUnlessExists('dir')
308
        self.run_script("""
309
rm dir
310
2>rm: cannot remove 'dir': Is a directory
311
""")
312
        self.failUnlessExists('dir')
313
314
    def test_rm_dir_recursive(self):
315
        self.run_script("""
316
mkdir dir
317
rm -r dir
318
""")
319
        self.failIfExists('dir')