/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
4763.2.4 by John Arbash Meinel
merge bzr.2.1 in preparation for NEWS entry.
1
# Copyright (C) 2009, 2010 Canonical Ltd
4665.5.1 by Vincent Ladeuil
Start some shell-like capability to write tests.
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 (
5417.1.1 by Martin Pool
ScriptRunner can now cope with commands that prompt for input.
19
    commands,
4665.5.5 by Vincent Ladeuil
Implement 'cd' and 'mkdir'.
20
    osutils,
4665.5.1 by Vincent Ladeuil
Start some shell-like capability to write tests.
21
    tests,
5463.1.2 by Martin Pool
Make run_argv_aliases reset the global verbosity after running
22
    trace,
5417.1.1 by Martin Pool
ScriptRunner can now cope with commands that prompt for input.
23
    ui,
4665.5.1 by Vincent Ladeuil
Start some shell-like capability to write tests.
24
    )
25
from bzrlib.tests import script
26
27
4665.5.19 by Vincent Ladeuil
Implement globbing and enhance cat to accept multiple files.
28
class TestSyntax(tests.TestCase):
4665.5.1 by Vincent Ladeuil
Start some shell-like capability to write tests.
29
30
    def test_comment_is_ignored(self):
31
        self.assertEquals([], script._script_to_commands('#comment\n'))
32
5417.1.6 by Martin Pool
Add passing test for multi-line comment
33
    def test_comment_multiple_lines(self):
34
        self.assertEquals([
35
            (['bar'], None, None, None),
36
            ],
37
            script._script_to_commands("""
38
            # this comment is ignored
39
            # so is this
40
            # no we run bar
41
            $ bar
42
            """))
43
5417.1.5 by Martin Pool
Better handling of blank lines in test scripts
44
    def test_trim_blank_lines(self):
45
        """Blank lines are respected, but trimmed at the start and end.
46
47
        Python triple-quoted syntax is going to give stubby/empty blank lines 
48
        right at the start and the end.  These are cut off so that callers don't 
49
        need special syntax to avoid them.
50
51
        However we do want to be able to match commands that emit blank lines.
52
        """
53
        self.assertEquals([
54
            (['bar'], None, '\n', None),
55
            ],
56
            script._script_to_commands("""
57
            $bar
58
59
            """))
4665.5.1 by Vincent Ladeuil
Start some shell-like capability to write tests.
60
61
    def test_simple_command(self):
4665.5.3 by Vincent Ladeuil
Separate error from normal output.
62
        self.assertEquals([(['cd', 'trunk'], None, None, None)],
4665.5.20 by Vincent Ladeuil
Fixed as per Martin's review.
63
                           script._script_to_commands('$ cd trunk'))
4665.5.1 by Vincent Ladeuil
Start some shell-like capability to write tests.
64
4665.5.2 by Vincent Ladeuil
Handle simple, double and back quotes.
65
    def test_command_with_single_quoted_param(self):
4665.5.20 by Vincent Ladeuil
Fixed as per Martin's review.
66
        story = """$ bzr commit -m 'two words'"""
4665.5.19 by Vincent Ladeuil
Implement globbing and enhance cat to accept multiple files.
67
        self.assertEquals([(['bzr', 'commit', '-m', "'two words'"],
4665.5.3 by Vincent Ladeuil
Separate error from normal output.
68
                            None, None, None)],
4665.5.2 by Vincent Ladeuil
Handle simple, double and back quotes.
69
                           script._script_to_commands(story))
70
71
    def test_command_with_double_quoted_param(self):
4665.5.20 by Vincent Ladeuil
Fixed as per Martin's review.
72
        story = """$ bzr commit -m "two words" """
4665.5.19 by Vincent Ladeuil
Implement globbing and enhance cat to accept multiple files.
73
        self.assertEquals([(['bzr', 'commit', '-m', '"two words"'],
4665.5.3 by Vincent Ladeuil
Separate error from normal output.
74
                            None, None, None)],
4665.5.2 by Vincent Ladeuil
Handle simple, double and back quotes.
75
                           script._script_to_commands(story))
76
4665.5.1 by Vincent Ladeuil
Start some shell-like capability to write tests.
77
    def test_command_with_input(self):
4665.5.20 by Vincent Ladeuil
Fixed as per Martin's review.
78
        self.assertEquals(
79
            [(['cat', '>file'], 'content\n', None, None)],
80
            script._script_to_commands('$ cat >file\n<content\n'))
4665.5.1 by Vincent Ladeuil
Start some shell-like capability to write tests.
81
5283.1.2 by Martin Pool
ScriptRunner strips consistent leading indentation from scripts
82
    def test_indented(self):
83
        # scripts are commonly given indented within the test source code, and
84
        # common indentation is stripped off
85
        story = """
86
            $ bzr add
87
            adding file
88
            adding file2
89
            """
90
        self.assertEquals([(['bzr', 'add'], None,
91
                            'adding file\nadding file2\n', None)],
92
                          script._script_to_commands(story))
93
4665.5.1 by Vincent Ladeuil
Start some shell-like capability to write tests.
94
    def test_command_with_output(self):
95
        story = """
4665.5.20 by Vincent Ladeuil
Fixed as per Martin's review.
96
$ bzr add
97
adding file
98
adding file2
4665.5.1 by Vincent Ladeuil
Start some shell-like capability to write tests.
99
"""
4665.5.7 by Vincent Ladeuil
Simplify output/errors handling.
100
        self.assertEquals([(['bzr', 'add'], None,
101
                            'adding file\nadding file2\n', None)],
4665.5.1 by Vincent Ladeuil
Start some shell-like capability to write tests.
102
                          script._script_to_commands(story))
103
4665.5.3 by Vincent Ladeuil
Separate error from normal output.
104
    def test_command_with_error(self):
105
        story = """
4665.5.20 by Vincent Ladeuil
Fixed as per Martin's review.
106
$ bzr branch foo
4665.5.3 by Vincent Ladeuil
Separate error from normal output.
107
2>bzr: ERROR: Not a branch: "foo"
108
"""
109
        self.assertEquals([(['bzr', 'branch', 'foo'],
4665.5.7 by Vincent Ladeuil
Simplify output/errors handling.
110
                            None, None, 'bzr: ERROR: Not a branch: "foo"\n')],
4665.5.3 by Vincent Ladeuil
Separate error from normal output.
111
                          script._script_to_commands(story))
4665.5.15 by Vincent Ladeuil
Catch the retcode for all commands.
112
4665.5.1 by Vincent Ladeuil
Start some shell-like capability to write tests.
113
    def test_input_without_command(self):
114
        self.assertRaises(SyntaxError, script._script_to_commands, '<input')
115
116
    def test_output_without_command(self):
117
        self.assertRaises(SyntaxError, script._script_to_commands, '>input')
118
4665.5.2 by Vincent Ladeuil
Handle simple, double and back quotes.
119
    def test_command_with_backquotes(self):
4665.5.1 by Vincent Ladeuil
Start some shell-like capability to write tests.
120
        story = """
4665.5.20 by Vincent Ladeuil
Fixed as per Martin's review.
121
$ foo = `bzr file-id toto`
4665.5.1 by Vincent Ladeuil
Start some shell-like capability to write tests.
122
"""
4665.5.3 by Vincent Ladeuil
Separate error from normal output.
123
        self.assertEquals([(['foo', '=', '`bzr file-id toto`'],
124
                            None, None, None)],
4665.5.1 by Vincent Ladeuil
Start some shell-like capability to write tests.
125
                          script._script_to_commands(story))
126
127
4665.5.18 by Vincent Ladeuil
Better redirection handling.
128
class TestRedirections(tests.TestCase):
129
130
    def _check(self, in_name, out_name, out_mode, remaining, args):
131
        self.assertEqual(script._scan_redirection_options(args),
132
                         (in_name, out_name, out_mode, remaining))
133
134
    def test_no_redirection(self):
135
        self._check(None, None, None, [], [])
136
        self._check(None, None, None, ['foo', 'bar'], ['foo', 'bar'])
137
138
    def test_input_redirection(self):
139
        self._check('foo', None, None, [], ['<foo'])
140
        self._check('foo', None, None, ['bar'], ['bar', '<foo'])
141
        self._check('foo', None, None, ['bar'], ['bar', '<', 'foo'])
142
        self._check('foo', None, None, ['bar'], ['<foo', 'bar'])
143
        self._check('foo', None, None, ['bar', 'baz'], ['bar', '<foo', 'baz'])
144
145
    def test_output_redirection(self):
146
        self._check(None, 'foo', 'wb+', [], ['>foo'])
147
        self._check(None, 'foo', 'wb+', ['bar'], ['bar', '>foo'])
148
        self._check(None, 'foo', 'wb+', ['bar'], ['bar', '>', 'foo'])
149
        self._check(None, 'foo', 'ab+', [], ['>>foo'])
150
        self._check(None, 'foo', 'ab+', ['bar'], ['bar', '>>foo'])
151
        self._check(None, 'foo', 'ab+', ['bar'], ['bar', '>>', 'foo'])
152
153
    def test_redirection_syntax_errors(self):
154
        self._check('', None, None, [], ['<'])
155
        self._check(None, '', 'wb+', [], ['>'])
156
        self._check(None, '', 'ab+', [], ['>>'])
157
        self._check('>', '', 'ab+', [], ['<', '>', '>>'])
158
159
160
4665.5.19 by Vincent Ladeuil
Implement globbing and enhance cat to accept multiple files.
161
class TestExecution(script.TestCaseWithTransportAndScript):
4665.5.4 by Vincent Ladeuil
Implement a 'cat' command.
162
163
    def test_unknown_command(self):
164
        self.assertRaises(SyntaxError, self.run_script, 'foo')
165
4665.5.13 by Vincent Ladeuil
Script execution must stop on unexpected errors.
166
    def test_stops_on_unexpected_output(self):
4665.5.6 by Vincent Ladeuil
Implement 'bzr' command.
167
        story = """
4665.5.20 by Vincent Ladeuil
Fixed as per Martin's review.
168
$ mkdir dir
169
$ cd dir
170
The cd command ouputs nothing
4665.5.6 by Vincent Ladeuil
Implement 'bzr' command.
171
"""
172
        self.assertRaises(AssertionError, self.run_script, story)
173
4665.5.4 by Vincent Ladeuil
Implement a 'cat' command.
174
4665.5.13 by Vincent Ladeuil
Script execution must stop on unexpected errors.
175
    def test_stops_on_unexpected_error(self):
176
        story = """
4665.5.20 by Vincent Ladeuil
Fixed as per Martin's review.
177
$ cat
4665.5.13 by Vincent Ladeuil
Script execution must stop on unexpected errors.
178
<Hello
4665.5.20 by Vincent Ladeuil
Fixed as per Martin's review.
179
$ bzr not-a-command
4665.5.13 by Vincent Ladeuil
Script execution must stop on unexpected errors.
180
"""
181
        self.assertRaises(AssertionError, self.run_script, story)
182
183
    def test_continue_on_expected_error(self):
184
        story = """
4665.5.20 by Vincent Ladeuil
Fixed as per Martin's review.
185
$ bzr not-a-command
4665.5.13 by Vincent Ladeuil
Script execution must stop on unexpected errors.
186
2>..."not-a-command"
187
"""
188
        self.run_script(story)
189
4665.5.16 by Vincent Ladeuil
Continue script execution based on status not error output.
190
    def test_continue_on_error_output(self):
191
        # The status matters, not the output
192
        story = """
4665.5.20 by Vincent Ladeuil
Fixed as per Martin's review.
193
$ bzr init
194
$ cat >file
4665.5.16 by Vincent Ladeuil
Continue script execution based on status not error output.
195
<Hello
4665.5.20 by Vincent Ladeuil
Fixed as per Martin's review.
196
$ bzr add file
197
$ bzr commit -m 'adding file'
4665.5.16 by Vincent Ladeuil
Continue script execution based on status not error output.
198
"""
199
        self.run_script(story)
200
4665.5.12 by Vincent Ladeuil
Support '...' in expected strings.
201
    def test_ellipsis_output(self):
202
        story = """
4665.5.20 by Vincent Ladeuil
Fixed as per Martin's review.
203
$ cat
4665.5.12 by Vincent Ladeuil
Support '...' in expected strings.
204
<first line
205
<second line
206
<last line
4665.5.20 by Vincent Ladeuil
Fixed as per Martin's review.
207
first line
208
...
209
last line
4665.5.12 by Vincent Ladeuil
Support '...' in expected strings.
210
"""
211
        self.run_script(story)
212
        story = """
4665.5.20 by Vincent Ladeuil
Fixed as per Martin's review.
213
$ bzr not-a-command
4665.5.12 by Vincent Ladeuil
Support '...' in expected strings.
214
2>..."not-a-command"
215
"""
216
        self.run_script(story)
217
218
        story = """
4665.5.20 by Vincent Ladeuil
Fixed as per Martin's review.
219
$ bzr branch not-a-branch
4665.5.12 by Vincent Ladeuil
Support '...' in expected strings.
220
2>bzr: ERROR: Not a branch...not-a-branch/".
221
"""
222
        self.run_script(story)
223
224
4665.5.19 by Vincent Ladeuil
Implement globbing and enhance cat to accept multiple files.
225
class TestArgumentProcessing(script.TestCaseWithTransportAndScript):
226
227
    def test_globing(self):
228
        self.run_script("""
4665.5.20 by Vincent Ladeuil
Fixed as per Martin's review.
229
$ echo cat >cat
230
$ echo dog >dog
231
$ cat *
232
cat
233
dog
4665.5.19 by Vincent Ladeuil
Implement globbing and enhance cat to accept multiple files.
234
""")
235
236
    def test_quoted_globbing(self):
237
        self.run_script("""
4665.5.20 by Vincent Ladeuil
Fixed as per Martin's review.
238
$ echo cat >cat
239
$ cat '*'
4665.5.19 by Vincent Ladeuil
Implement globbing and enhance cat to accept multiple files.
240
2>*: No such file or directory
241
""")
242
243
    def test_quotes_removal(self):
244
        self.run_script("""
4665.5.20 by Vincent Ladeuil
Fixed as per Martin's review.
245
$ echo 'cat' "dog" '"chicken"' "'dragon'"
246
cat dog "chicken" 'dragon'
4665.5.19 by Vincent Ladeuil
Implement globbing and enhance cat to accept multiple files.
247
""")
248
5463.1.2 by Martin Pool
Make run_argv_aliases reset the global verbosity after running
249
    def test_verbosity_isolated(self):
250
        """Global verbosity is isolated from commands run in scripts.
251
        """
252
        # see also 656694; we should get rid of global verbosity
253
        self.run_script("""
254
        $ bzr init --quiet a
255
        """)
256
        self.assertEquals(trace.is_quiet(), False)
257
4665.5.19 by Vincent Ladeuil
Implement globbing and enhance cat to accept multiple files.
258
4665.5.11 by Vincent Ladeuil
Create a new test case based on TestCaseWithMemoryTransport.
259
class TestCat(script.TestCaseWithTransportAndScript):
4665.5.4 by Vincent Ladeuil
Implement a 'cat' command.
260
261
    def test_cat_usage(self):
4665.5.8 by Vincent Ladeuil
Implement 'echo' command.
262
        self.assertRaises(SyntaxError, self.run_script, 'cat foo <bar')
4665.5.4 by Vincent Ladeuil
Implement a 'cat' command.
263
264
    def test_cat_input_to_output(self):
4665.5.15 by Vincent Ladeuil
Catch the retcode for all commands.
265
        retcode, out, err = self.run_command(['cat'],
266
                                             'content\n', 'content\n', None)
4665.5.8 by Vincent Ladeuil
Implement 'echo' command.
267
        self.assertEquals('content\n', out)
268
        self.assertEquals(None, err)
4665.5.4 by Vincent Ladeuil
Implement a 'cat' command.
269
270
    def test_cat_file_to_output(self):
271
        self.build_tree_contents([('file', 'content\n')])
4665.5.15 by Vincent Ladeuil
Catch the retcode for all commands.
272
        retcode, out, err = self.run_command(['cat', 'file'],
273
                                             None, 'content\n', None)
4665.5.8 by Vincent Ladeuil
Implement 'echo' command.
274
        self.assertEquals('content\n', out)
275
        self.assertEquals(None, err)
4665.5.4 by Vincent Ladeuil
Implement a 'cat' command.
276
277
    def test_cat_input_to_file(self):
4665.5.15 by Vincent Ladeuil
Catch the retcode for all commands.
278
        retcode, out, err = self.run_command(['cat', '>file'],
279
                                             'content\n', None, None)
4665.5.4 by Vincent Ladeuil
Implement a 'cat' command.
280
        self.assertFileEqual('content\n', 'file')
4665.5.8 by Vincent Ladeuil
Implement 'echo' command.
281
        self.assertEquals(None, out)
282
        self.assertEquals(None, err)
4665.5.15 by Vincent Ladeuil
Catch the retcode for all commands.
283
        retcode, out, err = self.run_command(['cat', '>>file'],
284
                                             'more\n', None, None)
4665.5.8 by Vincent Ladeuil
Implement 'echo' command.
285
        self.assertFileEqual('content\nmore\n', 'file')
286
        self.assertEquals(None, out)
287
        self.assertEquals(None, err)
4665.5.4 by Vincent Ladeuil
Implement a 'cat' command.
288
289
    def test_cat_file_to_file(self):
290
        self.build_tree_contents([('file', 'content\n')])
4665.5.15 by Vincent Ladeuil
Catch the retcode for all commands.
291
        retcode, out, err = self.run_command(['cat', 'file', '>file2'],
292
                                             None, None, None)
4665.5.4 by Vincent Ladeuil
Implement a 'cat' command.
293
        self.assertFileEqual('content\n', 'file2')
4665.5.5 by Vincent Ladeuil
Implement 'cd' and 'mkdir'.
294
4665.5.19 by Vincent Ladeuil
Implement globbing and enhance cat to accept multiple files.
295
    def test_cat_files_to_file(self):
296
        self.build_tree_contents([('cat', 'cat\n')])
297
        self.build_tree_contents([('dog', 'dog\n')])
298
        retcode, out, err = self.run_command(['cat', 'cat', 'dog', '>file'],
299
                                             None, None, None)
300
        self.assertFileEqual('cat\ndog\n', 'file')
301
4665.5.18 by Vincent Ladeuil
Better redirection handling.
302
    def test_cat_bogus_input_file(self):
303
        self.run_script("""
4665.5.20 by Vincent Ladeuil
Fixed as per Martin's review.
304
$ cat <file
4665.5.18 by Vincent Ladeuil
Better redirection handling.
305
2>file: No such file or directory
306
""")
307
308
    def test_cat_bogus_output_file(self):
309
        self.run_script("""
4665.5.20 by Vincent Ladeuil
Fixed as per Martin's review.
310
$ cat >
4665.5.18 by Vincent Ladeuil
Better redirection handling.
311
2>: No such file or directory
312
""")
313
314
    def test_echo_bogus_output_file(self):
315
        # We need a backing file sysytem for that test so it can't be in
316
        # TestEcho
317
        self.run_script("""
4665.5.20 by Vincent Ladeuil
Fixed as per Martin's review.
318
$ echo >
4665.5.18 by Vincent Ladeuil
Better redirection handling.
319
2>: No such file or directory
320
""")
321
4665.5.5 by Vincent Ladeuil
Implement 'cd' and 'mkdir'.
322
4665.5.11 by Vincent Ladeuil
Create a new test case based on TestCaseWithMemoryTransport.
323
class TestMkdir(script.TestCaseWithTransportAndScript):
4665.5.5 by Vincent Ladeuil
Implement 'cd' and 'mkdir'.
324
325
    def test_mkdir_usage(self):
4665.5.20 by Vincent Ladeuil
Fixed as per Martin's review.
326
        self.assertRaises(SyntaxError, self.run_script, '$ mkdir')
327
        self.assertRaises(SyntaxError, self.run_script, '$ mkdir foo bar')
4665.5.5 by Vincent Ladeuil
Implement 'cd' and 'mkdir'.
328
329
    def test_mkdir_jailed(self):
4665.5.20 by Vincent Ladeuil
Fixed as per Martin's review.
330
        self.assertRaises(ValueError, self.run_script, '$ mkdir /out-of-jail')
331
        self.assertRaises(ValueError, self.run_script, '$ mkdir ../out-of-jail')
4665.5.5 by Vincent Ladeuil
Implement 'cd' and 'mkdir'.
332
333
    def test_mkdir_in_jail(self):
334
        self.run_script("""
4665.5.20 by Vincent Ladeuil
Fixed as per Martin's review.
335
$ mkdir dir
336
$ cd dir
337
$ mkdir ../dir2
338
$ cd ..
4665.5.5 by Vincent Ladeuil
Implement 'cd' and 'mkdir'.
339
""")
340
        self.failUnlessExists('dir')
341
        self.failUnlessExists('dir2')
342
343
4665.5.11 by Vincent Ladeuil
Create a new test case based on TestCaseWithMemoryTransport.
344
class TestCd(script.TestCaseWithTransportAndScript):
4665.5.5 by Vincent Ladeuil
Implement 'cd' and 'mkdir'.
345
346
    def test_cd_usage(self):
4665.5.20 by Vincent Ladeuil
Fixed as per Martin's review.
347
        self.assertRaises(SyntaxError, self.run_script, '$ cd foo bar')
4665.5.5 by Vincent Ladeuil
Implement 'cd' and 'mkdir'.
348
349
    def test_cd_out_of_jail(self):
4665.5.20 by Vincent Ladeuil
Fixed as per Martin's review.
350
        self.assertRaises(ValueError, self.run_script, '$ cd /out-of-jail')
351
        self.assertRaises(ValueError, self.run_script, '$ cd ..')
4665.5.5 by Vincent Ladeuil
Implement 'cd' and 'mkdir'.
352
353
    def test_cd_dir_and_back_home(self):
354
        self.assertEquals(self.test_dir, osutils.getcwd())
355
        self.run_script("""
4665.5.20 by Vincent Ladeuil
Fixed as per Martin's review.
356
$ mkdir dir
357
$ cd dir
4665.5.5 by Vincent Ladeuil
Implement 'cd' and 'mkdir'.
358
""")
359
        self.assertEquals(osutils.pathjoin(self.test_dir, 'dir'),
360
                          osutils.getcwd())
361
4665.5.20 by Vincent Ladeuil
Fixed as per Martin's review.
362
        self.run_script('$ cd')
4665.5.5 by Vincent Ladeuil
Implement 'cd' and 'mkdir'.
363
        self.assertEquals(self.test_dir, osutils.getcwd())
4665.5.6 by Vincent Ladeuil
Implement 'bzr' command.
364
365
4665.5.11 by Vincent Ladeuil
Create a new test case based on TestCaseWithMemoryTransport.
366
class TestBzr(script.TestCaseWithTransportAndScript):
4665.5.6 by Vincent Ladeuil
Implement 'bzr' command.
367
368
    def test_bzr_smoke(self):
4665.5.20 by Vincent Ladeuil
Fixed as per Martin's review.
369
        self.run_script('$ bzr init branch')
4665.5.6 by Vincent Ladeuil
Implement 'bzr' command.
370
        self.failUnlessExists('branch')
4665.5.8 by Vincent Ladeuil
Implement 'echo' command.
371
372
4665.5.11 by Vincent Ladeuil
Create a new test case based on TestCaseWithMemoryTransport.
373
class TestEcho(script.TestCaseWithMemoryTransportAndScript):
4665.5.8 by Vincent Ladeuil
Implement 'echo' command.
374
375
    def test_echo_usage(self):
376
        story = """
4665.5.20 by Vincent Ladeuil
Fixed as per Martin's review.
377
$ echo foo
4665.5.8 by Vincent Ladeuil
Implement 'echo' command.
378
<bar
379
"""
380
        self.assertRaises(SyntaxError, self.run_script, story)
381
4789.18.1 by John Arbash Meinel
Rework test_script a little bit.
382
    def test_echo_input(self):
383
        self.assertRaises(SyntaxError, self.run_script, """
384
            $ echo <foo
385
            """)
386
4665.5.8 by Vincent Ladeuil
Implement 'echo' command.
387
    def test_echo_to_output(self):
4665.5.15 by Vincent Ladeuil
Catch the retcode for all commands.
388
        retcode, out, err = self.run_command(['echo'], None, '\n', None)
4665.5.8 by Vincent Ladeuil
Implement 'echo' command.
389
        self.assertEquals('\n', out)
390
        self.assertEquals(None, err)
391
392
    def test_echo_some_to_output(self):
4665.5.15 by Vincent Ladeuil
Catch the retcode for all commands.
393
        retcode, out, err = self.run_command(['echo', 'hello'],
394
                                             None, 'hello\n', None)
4665.5.8 by Vincent Ladeuil
Implement 'echo' command.
395
        self.assertEquals('hello\n', out)
396
        self.assertEquals(None, err)
397
398
    def test_echo_more_output(self):
4665.5.15 by Vincent Ladeuil
Catch the retcode for all commands.
399
        retcode, out, err = self.run_command(
400
            ['echo', 'hello', 'happy', 'world'],
4665.5.20 by Vincent Ladeuil
Fixed as per Martin's review.
401
            None, 'hello happy world\n', None)
402
        self.assertEquals('hello happy world\n', out)
4665.5.8 by Vincent Ladeuil
Implement 'echo' command.
403
        self.assertEquals(None, err)
404
405
    def test_echo_appended(self):
4665.5.15 by Vincent Ladeuil
Catch the retcode for all commands.
406
        retcode, out, err = self.run_command(['echo', 'hello', '>file'],
407
                                             None, None, None)
4665.5.8 by Vincent Ladeuil
Implement 'echo' command.
408
        self.assertEquals(None, out)
409
        self.assertEquals(None, err)
410
        self.assertFileEqual('hello\n', 'file')
4665.5.15 by Vincent Ladeuil
Catch the retcode for all commands.
411
        retcode, out, err = self.run_command(['echo', 'happy', '>>file'],
412
                                             None, None, None)
4665.5.8 by Vincent Ladeuil
Implement 'echo' command.
413
        self.assertEquals(None, out)
414
        self.assertEquals(None, err)
415
        self.assertFileEqual('hello\nhappy\n', 'file')
4665.5.15 by Vincent Ladeuil
Catch the retcode for all commands.
416
5417.1.5 by Martin Pool
Better handling of blank lines in test scripts
417
    def test_empty_line_in_output_is_respected(self):
418
        self.run_script("""
419
            $ echo
420
421
            $ echo bar
422
            bar
423
            """)
424
4665.5.17 by Vincent Ladeuil
Implement 'rm' command.
425
426
class TestRm(script.TestCaseWithTransportAndScript):
427
428
    def test_rm_usage(self):
4665.5.20 by Vincent Ladeuil
Fixed as per Martin's review.
429
        self.assertRaises(SyntaxError, self.run_script, '$ rm')
430
        self.assertRaises(SyntaxError, self.run_script, '$ rm -ff foo')
4665.5.17 by Vincent Ladeuil
Implement 'rm' command.
431
432
    def test_rm_file(self):
4665.5.20 by Vincent Ladeuil
Fixed as per Martin's review.
433
        self.run_script('$ echo content >file')
4665.5.17 by Vincent Ladeuil
Implement 'rm' command.
434
        self.failUnlessExists('file')
4665.5.20 by Vincent Ladeuil
Fixed as per Martin's review.
435
        self.run_script('$ rm file')
4665.5.17 by Vincent Ladeuil
Implement 'rm' command.
436
        self.failIfExists('file')
437
438
    def test_rm_file_force(self):
439
        self.failIfExists('file')
4665.5.20 by Vincent Ladeuil
Fixed as per Martin's review.
440
        self.run_script('$ rm -f file')
4665.5.17 by Vincent Ladeuil
Implement 'rm' command.
441
        self.failIfExists('file')
442
443
    def test_rm_files(self):
444
        self.run_script("""
4665.5.20 by Vincent Ladeuil
Fixed as per Martin's review.
445
$ echo content >file
446
$ echo content >file2
4665.5.17 by Vincent Ladeuil
Implement 'rm' command.
447
""")
448
        self.failUnlessExists('file2')
4665.5.20 by Vincent Ladeuil
Fixed as per Martin's review.
449
        self.run_script('$ rm file file2')
4665.5.17 by Vincent Ladeuil
Implement 'rm' command.
450
        self.failIfExists('file2')
451
452
    def test_rm_dir(self):
4665.5.20 by Vincent Ladeuil
Fixed as per Martin's review.
453
        self.run_script('$ mkdir dir')
4665.5.17 by Vincent Ladeuil
Implement 'rm' command.
454
        self.failUnlessExists('dir')
455
        self.run_script("""
4665.5.20 by Vincent Ladeuil
Fixed as per Martin's review.
456
$ rm dir
4665.5.17 by Vincent Ladeuil
Implement 'rm' command.
457
2>rm: cannot remove 'dir': Is a directory
458
""")
459
        self.failUnlessExists('dir')
460
461
    def test_rm_dir_recursive(self):
462
        self.run_script("""
4665.5.20 by Vincent Ladeuil
Fixed as per Martin's review.
463
$ mkdir dir
464
$ rm -r dir
4665.5.17 by Vincent Ladeuil
Implement 'rm' command.
465
""")
466
        self.failIfExists('dir')
4953.2.1 by Neil Martinsen-Burrell
add an mv command to shell-like tests
467
468
469
class TestMv(script.TestCaseWithTransportAndScript):
470
471
    def test_usage(self):
472
        self.assertRaises(SyntaxError, self.run_script, '$ mv')
473
        self.assertRaises(SyntaxError, self.run_script, '$ mv f')
474
        self.assertRaises(SyntaxError, self.run_script, '$ mv f1 f2 f3')
475
476
    def test_move_file(self):
477
        self.run_script('$ echo content >file')
478
        self.failUnlessExists('file')
479
        self.run_script('$ mv file new_name')
480
        self.failIfExists('file')
481
        self.failUnlessExists('new_name')
482
4953.2.3 by Vincent Ladeuil
Use os.rename() and fix some typos.
483
    def test_move_unknown_file(self):
484
        self.assertRaises(AssertionError,
485
                          self.run_script, '$ mv unknown does-not-exist')
486
4953.2.1 by Neil Martinsen-Burrell
add an mv command to shell-like tests
487
    def test_move_dir(self):
488
        self.run_script("""
489
$ mkdir dir
490
$ echo content >dir/file
491
""")
492
        self.run_script('$ mv dir new_name')
493
        self.failIfExists('dir')
494
        self.failUnlessExists('new_name')
495
        self.failUnlessExists('new_name/file')
4953.2.3 by Vincent Ladeuil
Use os.rename() and fix some typos.
496
4953.2.2 by Neil Martinsen-Burrell
added a test for "mv file dir" and a NEWS entry
497
    def test_move_file_into_dir(self):
498
        self.run_script("""
499
$ mkdir dir
500
$ echo content > file
501
""")
502
        self.run_script('$ mv file dir')
503
        self.failUnlessExists('dir')
504
        self.failIfExists('file')
505
        self.failUnlessExists('dir/file')
506
5417.1.1 by Martin Pool
ScriptRunner can now cope with commands that prompt for input.
507
508
class cmd_test_confirm(commands.Command):
509
510
    def run(self):
511
        if ui.ui_factory.get_boolean(
512
            'Really do it',
513
            # 'bzrlib.tests.test_script.confirm',
514
            # {}
515
            ):
5417.1.3 by Martin Pool
Additional script tests
516
            self.outf.write('Do it!\n')
5417.1.1 by Martin Pool
ScriptRunner can now cope with commands that prompt for input.
517
        else:
5417.1.3 by Martin Pool
Additional script tests
518
            print 'ok, no'
5417.1.1 by Martin Pool
ScriptRunner can now cope with commands that prompt for input.
519
520
521
class TestUserInteraction(script.TestCaseWithMemoryTransportAndScript):
522
523
    def test_confirm_action(self):
5417.1.2 by Martin Pool
Additional docs; yay testdoc
524
        """You can write tests that demonstrate user confirmation.
525
        
526
        Specifically, ScriptRunner does't care if the output line for the prompt
527
        isn't terminated by a newline from the program; it's implicitly terminated 
528
        by the input.
529
        """
5417.1.1 by Martin Pool
ScriptRunner can now cope with commands that prompt for input.
530
        commands.builtin_command_registry.register(cmd_test_confirm)
531
        self.addCleanup(commands.builtin_command_registry.remove, 'test-confirm')
532
        self.run_script("""
533
            $ bzr test-confirm
534
            2>Really do it? [y/n]: 
535
            <yes
5417.1.3 by Martin Pool
Additional script tests
536
            Do it!
537
            $ bzr test-confirm
538
            2>Really do it? [y/n]: 
539
            <no
540
            ok, no
5417.1.1 by Martin Pool
ScriptRunner can now cope with commands that prompt for input.
541
            """)
542