/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
5422.3.1 by Martin Pool
Blank scriptrunner output only matches blank
166
    def test_blank_output_mismatches_output(self):
167
        """If you give output, the output must actually be blank.
168
        
169
        See <https://bugs.launchpad.net/bzr/+bug/637830>: previously blank
170
        output was a wildcard.  Now you must say ... if you want that.
171
        """
172
        self.assertRaises(AssertionError,
173
            self.run_script,
174
            """
175
            $ echo foo
176
            """)
177
178
    def test_ellipsis_everything(self):
179
        """A simple ellipsis matches everything."""
180
        self.run_script("""
181
        $ echo foo
182
        ...
183
        """)
184
185
    def test_ellipsis_matches_empty(self):
186
        self.run_script("""
187
        $ cd .
188
        ...
189
        """)
190
4665.5.13 by Vincent Ladeuil
Script execution must stop on unexpected errors.
191
    def test_stops_on_unexpected_output(self):
4665.5.6 by Vincent Ladeuil
Implement 'bzr' command.
192
        story = """
4665.5.20 by Vincent Ladeuil
Fixed as per Martin's review.
193
$ mkdir dir
194
$ cd dir
195
The cd command ouputs nothing
4665.5.6 by Vincent Ladeuil
Implement 'bzr' command.
196
"""
197
        self.assertRaises(AssertionError, self.run_script, story)
198
4665.5.13 by Vincent Ladeuil
Script execution must stop on unexpected errors.
199
    def test_stops_on_unexpected_error(self):
200
        story = """
4665.5.20 by Vincent Ladeuil
Fixed as per Martin's review.
201
$ cat
4665.5.13 by Vincent Ladeuil
Script execution must stop on unexpected errors.
202
<Hello
4665.5.20 by Vincent Ladeuil
Fixed as per Martin's review.
203
$ bzr not-a-command
4665.5.13 by Vincent Ladeuil
Script execution must stop on unexpected errors.
204
"""
205
        self.assertRaises(AssertionError, self.run_script, story)
206
207
    def test_continue_on_expected_error(self):
208
        story = """
4665.5.20 by Vincent Ladeuil
Fixed as per Martin's review.
209
$ bzr not-a-command
4665.5.13 by Vincent Ladeuil
Script execution must stop on unexpected errors.
210
2>..."not-a-command"
211
"""
212
        self.run_script(story)
213
4665.5.16 by Vincent Ladeuil
Continue script execution based on status not error output.
214
    def test_continue_on_error_output(self):
215
        # The status matters, not the output
216
        story = """
4665.5.20 by Vincent Ladeuil
Fixed as per Martin's review.
217
$ bzr init
5422.3.1 by Martin Pool
Blank scriptrunner output only matches blank
218
...
4665.5.20 by Vincent Ladeuil
Fixed as per Martin's review.
219
$ cat >file
4665.5.16 by Vincent Ladeuil
Continue script execution based on status not error output.
220
<Hello
4665.5.20 by Vincent Ladeuil
Fixed as per Martin's review.
221
$ bzr add file
5422.3.1 by Martin Pool
Blank scriptrunner output only matches blank
222
...
4665.5.20 by Vincent Ladeuil
Fixed as per Martin's review.
223
$ bzr commit -m 'adding file'
5422.3.1 by Martin Pool
Blank scriptrunner output only matches blank
224
2>...
4665.5.16 by Vincent Ladeuil
Continue script execution based on status not error output.
225
"""
226
        self.run_script(story)
227
4665.5.12 by Vincent Ladeuil
Support '...' in expected strings.
228
    def test_ellipsis_output(self):
229
        story = """
4665.5.20 by Vincent Ladeuil
Fixed as per Martin's review.
230
$ cat
4665.5.12 by Vincent Ladeuil
Support '...' in expected strings.
231
<first line
232
<second line
233
<last line
4665.5.20 by Vincent Ladeuil
Fixed as per Martin's review.
234
first line
235
...
236
last line
4665.5.12 by Vincent Ladeuil
Support '...' in expected strings.
237
"""
238
        self.run_script(story)
239
        story = """
4665.5.20 by Vincent Ladeuil
Fixed as per Martin's review.
240
$ bzr not-a-command
4665.5.12 by Vincent Ladeuil
Support '...' in expected strings.
241
2>..."not-a-command"
242
"""
243
        self.run_script(story)
244
245
        story = """
4665.5.20 by Vincent Ladeuil
Fixed as per Martin's review.
246
$ bzr branch not-a-branch
4665.5.12 by Vincent Ladeuil
Support '...' in expected strings.
247
2>bzr: ERROR: Not a branch...not-a-branch/".
248
"""
249
        self.run_script(story)
250
251
4665.5.19 by Vincent Ladeuil
Implement globbing and enhance cat to accept multiple files.
252
class TestArgumentProcessing(script.TestCaseWithTransportAndScript):
253
254
    def test_globing(self):
255
        self.run_script("""
4665.5.20 by Vincent Ladeuil
Fixed as per Martin's review.
256
$ echo cat >cat
257
$ echo dog >dog
258
$ cat *
259
cat
260
dog
4665.5.19 by Vincent Ladeuil
Implement globbing and enhance cat to accept multiple files.
261
""")
262
263
    def test_quoted_globbing(self):
264
        self.run_script("""
4665.5.20 by Vincent Ladeuil
Fixed as per Martin's review.
265
$ echo cat >cat
266
$ cat '*'
4665.5.19 by Vincent Ladeuil
Implement globbing and enhance cat to accept multiple files.
267
2>*: No such file or directory
268
""")
269
270
    def test_quotes_removal(self):
271
        self.run_script("""
4665.5.20 by Vincent Ladeuil
Fixed as per Martin's review.
272
$ echo 'cat' "dog" '"chicken"' "'dragon'"
273
cat dog "chicken" 'dragon'
4665.5.19 by Vincent Ladeuil
Implement globbing and enhance cat to accept multiple files.
274
""")
275
5463.1.2 by Martin Pool
Make run_argv_aliases reset the global verbosity after running
276
    def test_verbosity_isolated(self):
277
        """Global verbosity is isolated from commands run in scripts.
278
        """
279
        # see also 656694; we should get rid of global verbosity
280
        self.run_script("""
281
        $ bzr init --quiet a
282
        """)
283
        self.assertEquals(trace.is_quiet(), False)
284
4665.5.19 by Vincent Ladeuil
Implement globbing and enhance cat to accept multiple files.
285
4665.5.11 by Vincent Ladeuil
Create a new test case based on TestCaseWithMemoryTransport.
286
class TestCat(script.TestCaseWithTransportAndScript):
4665.5.4 by Vincent Ladeuil
Implement a 'cat' command.
287
288
    def test_cat_usage(self):
4665.5.8 by Vincent Ladeuil
Implement 'echo' command.
289
        self.assertRaises(SyntaxError, self.run_script, 'cat foo <bar')
4665.5.4 by Vincent Ladeuil
Implement a 'cat' command.
290
291
    def test_cat_input_to_output(self):
4665.5.15 by Vincent Ladeuil
Catch the retcode for all commands.
292
        retcode, out, err = self.run_command(['cat'],
293
                                             'content\n', 'content\n', None)
4665.5.8 by Vincent Ladeuil
Implement 'echo' command.
294
        self.assertEquals('content\n', out)
295
        self.assertEquals(None, err)
4665.5.4 by Vincent Ladeuil
Implement a 'cat' command.
296
297
    def test_cat_file_to_output(self):
298
        self.build_tree_contents([('file', 'content\n')])
4665.5.15 by Vincent Ladeuil
Catch the retcode for all commands.
299
        retcode, out, err = self.run_command(['cat', 'file'],
300
                                             None, 'content\n', None)
4665.5.8 by Vincent Ladeuil
Implement 'echo' command.
301
        self.assertEquals('content\n', out)
302
        self.assertEquals(None, err)
4665.5.4 by Vincent Ladeuil
Implement a 'cat' command.
303
304
    def test_cat_input_to_file(self):
4665.5.15 by Vincent Ladeuil
Catch the retcode for all commands.
305
        retcode, out, err = self.run_command(['cat', '>file'],
306
                                             'content\n', None, None)
4665.5.4 by Vincent Ladeuil
Implement a 'cat' command.
307
        self.assertFileEqual('content\n', 'file')
4665.5.8 by Vincent Ladeuil
Implement 'echo' command.
308
        self.assertEquals(None, out)
309
        self.assertEquals(None, err)
4665.5.15 by Vincent Ladeuil
Catch the retcode for all commands.
310
        retcode, out, err = self.run_command(['cat', '>>file'],
311
                                             'more\n', None, None)
4665.5.8 by Vincent Ladeuil
Implement 'echo' command.
312
        self.assertFileEqual('content\nmore\n', 'file')
313
        self.assertEquals(None, out)
314
        self.assertEquals(None, err)
4665.5.4 by Vincent Ladeuil
Implement a 'cat' command.
315
316
    def test_cat_file_to_file(self):
317
        self.build_tree_contents([('file', 'content\n')])
4665.5.15 by Vincent Ladeuil
Catch the retcode for all commands.
318
        retcode, out, err = self.run_command(['cat', 'file', '>file2'],
319
                                             None, None, None)
4665.5.4 by Vincent Ladeuil
Implement a 'cat' command.
320
        self.assertFileEqual('content\n', 'file2')
4665.5.5 by Vincent Ladeuil
Implement 'cd' and 'mkdir'.
321
4665.5.19 by Vincent Ladeuil
Implement globbing and enhance cat to accept multiple files.
322
    def test_cat_files_to_file(self):
323
        self.build_tree_contents([('cat', 'cat\n')])
324
        self.build_tree_contents([('dog', 'dog\n')])
325
        retcode, out, err = self.run_command(['cat', 'cat', 'dog', '>file'],
326
                                             None, None, None)
327
        self.assertFileEqual('cat\ndog\n', 'file')
328
4665.5.18 by Vincent Ladeuil
Better redirection handling.
329
    def test_cat_bogus_input_file(self):
330
        self.run_script("""
4665.5.20 by Vincent Ladeuil
Fixed as per Martin's review.
331
$ cat <file
4665.5.18 by Vincent Ladeuil
Better redirection handling.
332
2>file: No such file or directory
333
""")
334
335
    def test_cat_bogus_output_file(self):
336
        self.run_script("""
4665.5.20 by Vincent Ladeuil
Fixed as per Martin's review.
337
$ cat >
4665.5.18 by Vincent Ladeuil
Better redirection handling.
338
2>: No such file or directory
339
""")
340
341
    def test_echo_bogus_output_file(self):
342
        # We need a backing file sysytem for that test so it can't be in
343
        # TestEcho
344
        self.run_script("""
4665.5.20 by Vincent Ladeuil
Fixed as per Martin's review.
345
$ echo >
4665.5.18 by Vincent Ladeuil
Better redirection handling.
346
2>: No such file or directory
347
""")
348
4665.5.5 by Vincent Ladeuil
Implement 'cd' and 'mkdir'.
349
4665.5.11 by Vincent Ladeuil
Create a new test case based on TestCaseWithMemoryTransport.
350
class TestMkdir(script.TestCaseWithTransportAndScript):
4665.5.5 by Vincent Ladeuil
Implement 'cd' and 'mkdir'.
351
352
    def test_mkdir_usage(self):
4665.5.20 by Vincent Ladeuil
Fixed as per Martin's review.
353
        self.assertRaises(SyntaxError, self.run_script, '$ mkdir')
354
        self.assertRaises(SyntaxError, self.run_script, '$ mkdir foo bar')
4665.5.5 by Vincent Ladeuil
Implement 'cd' and 'mkdir'.
355
356
    def test_mkdir_jailed(self):
4665.5.20 by Vincent Ladeuil
Fixed as per Martin's review.
357
        self.assertRaises(ValueError, self.run_script, '$ mkdir /out-of-jail')
358
        self.assertRaises(ValueError, self.run_script, '$ mkdir ../out-of-jail')
4665.5.5 by Vincent Ladeuil
Implement 'cd' and 'mkdir'.
359
360
    def test_mkdir_in_jail(self):
361
        self.run_script("""
4665.5.20 by Vincent Ladeuil
Fixed as per Martin's review.
362
$ mkdir dir
363
$ cd dir
364
$ mkdir ../dir2
365
$ cd ..
4665.5.5 by Vincent Ladeuil
Implement 'cd' and 'mkdir'.
366
""")
367
        self.failUnlessExists('dir')
368
        self.failUnlessExists('dir2')
369
370
4665.5.11 by Vincent Ladeuil
Create a new test case based on TestCaseWithMemoryTransport.
371
class TestCd(script.TestCaseWithTransportAndScript):
4665.5.5 by Vincent Ladeuil
Implement 'cd' and 'mkdir'.
372
373
    def test_cd_usage(self):
4665.5.20 by Vincent Ladeuil
Fixed as per Martin's review.
374
        self.assertRaises(SyntaxError, self.run_script, '$ cd foo bar')
4665.5.5 by Vincent Ladeuil
Implement 'cd' and 'mkdir'.
375
376
    def test_cd_out_of_jail(self):
4665.5.20 by Vincent Ladeuil
Fixed as per Martin's review.
377
        self.assertRaises(ValueError, self.run_script, '$ cd /out-of-jail')
378
        self.assertRaises(ValueError, self.run_script, '$ cd ..')
4665.5.5 by Vincent Ladeuil
Implement 'cd' and 'mkdir'.
379
380
    def test_cd_dir_and_back_home(self):
381
        self.assertEquals(self.test_dir, osutils.getcwd())
382
        self.run_script("""
4665.5.20 by Vincent Ladeuil
Fixed as per Martin's review.
383
$ mkdir dir
384
$ cd dir
4665.5.5 by Vincent Ladeuil
Implement 'cd' and 'mkdir'.
385
""")
386
        self.assertEquals(osutils.pathjoin(self.test_dir, 'dir'),
387
                          osutils.getcwd())
388
4665.5.20 by Vincent Ladeuil
Fixed as per Martin's review.
389
        self.run_script('$ cd')
4665.5.5 by Vincent Ladeuil
Implement 'cd' and 'mkdir'.
390
        self.assertEquals(self.test_dir, osutils.getcwd())
4665.5.6 by Vincent Ladeuil
Implement 'bzr' command.
391
392
4665.5.11 by Vincent Ladeuil
Create a new test case based on TestCaseWithMemoryTransport.
393
class TestBzr(script.TestCaseWithTransportAndScript):
4665.5.6 by Vincent Ladeuil
Implement 'bzr' command.
394
395
    def test_bzr_smoke(self):
5422.3.1 by Martin Pool
Blank scriptrunner output only matches blank
396
        self.run_script("""
397
            $ bzr init branch
398
            Created a standalone tree (format: ...)
399
            """)
4665.5.6 by Vincent Ladeuil
Implement 'bzr' command.
400
        self.failUnlessExists('branch')
4665.5.8 by Vincent Ladeuil
Implement 'echo' command.
401
402
4665.5.11 by Vincent Ladeuil
Create a new test case based on TestCaseWithMemoryTransport.
403
class TestEcho(script.TestCaseWithMemoryTransportAndScript):
4665.5.8 by Vincent Ladeuil
Implement 'echo' command.
404
405
    def test_echo_usage(self):
406
        story = """
4665.5.20 by Vincent Ladeuil
Fixed as per Martin's review.
407
$ echo foo
4665.5.8 by Vincent Ladeuil
Implement 'echo' command.
408
<bar
409
"""
410
        self.assertRaises(SyntaxError, self.run_script, story)
411
4789.18.1 by John Arbash Meinel
Rework test_script a little bit.
412
    def test_echo_input(self):
413
        self.assertRaises(SyntaxError, self.run_script, """
414
            $ echo <foo
415
            """)
416
4665.5.8 by Vincent Ladeuil
Implement 'echo' command.
417
    def test_echo_to_output(self):
4665.5.15 by Vincent Ladeuil
Catch the retcode for all commands.
418
        retcode, out, err = self.run_command(['echo'], None, '\n', None)
4665.5.8 by Vincent Ladeuil
Implement 'echo' command.
419
        self.assertEquals('\n', out)
420
        self.assertEquals(None, err)
421
422
    def test_echo_some_to_output(self):
4665.5.15 by Vincent Ladeuil
Catch the retcode for all commands.
423
        retcode, out, err = self.run_command(['echo', 'hello'],
424
                                             None, 'hello\n', None)
4665.5.8 by Vincent Ladeuil
Implement 'echo' command.
425
        self.assertEquals('hello\n', out)
426
        self.assertEquals(None, err)
427
428
    def test_echo_more_output(self):
4665.5.15 by Vincent Ladeuil
Catch the retcode for all commands.
429
        retcode, out, err = self.run_command(
430
            ['echo', 'hello', 'happy', 'world'],
4665.5.20 by Vincent Ladeuil
Fixed as per Martin's review.
431
            None, 'hello happy world\n', None)
432
        self.assertEquals('hello happy world\n', out)
4665.5.8 by Vincent Ladeuil
Implement 'echo' command.
433
        self.assertEquals(None, err)
434
435
    def test_echo_appended(self):
4665.5.15 by Vincent Ladeuil
Catch the retcode for all commands.
436
        retcode, out, err = self.run_command(['echo', 'hello', '>file'],
437
                                             None, None, None)
4665.5.8 by Vincent Ladeuil
Implement 'echo' command.
438
        self.assertEquals(None, out)
439
        self.assertEquals(None, err)
440
        self.assertFileEqual('hello\n', 'file')
4665.5.15 by Vincent Ladeuil
Catch the retcode for all commands.
441
        retcode, out, err = self.run_command(['echo', 'happy', '>>file'],
442
                                             None, None, None)
4665.5.8 by Vincent Ladeuil
Implement 'echo' command.
443
        self.assertEquals(None, out)
444
        self.assertEquals(None, err)
445
        self.assertFileEqual('hello\nhappy\n', 'file')
4665.5.15 by Vincent Ladeuil
Catch the retcode for all commands.
446
5417.1.5 by Martin Pool
Better handling of blank lines in test scripts
447
    def test_empty_line_in_output_is_respected(self):
448
        self.run_script("""
449
            $ echo
450
451
            $ echo bar
452
            bar
453
            """)
454
4665.5.17 by Vincent Ladeuil
Implement 'rm' command.
455
456
class TestRm(script.TestCaseWithTransportAndScript):
457
458
    def test_rm_usage(self):
4665.5.20 by Vincent Ladeuil
Fixed as per Martin's review.
459
        self.assertRaises(SyntaxError, self.run_script, '$ rm')
460
        self.assertRaises(SyntaxError, self.run_script, '$ rm -ff foo')
4665.5.17 by Vincent Ladeuil
Implement 'rm' command.
461
462
    def test_rm_file(self):
4665.5.20 by Vincent Ladeuil
Fixed as per Martin's review.
463
        self.run_script('$ echo content >file')
4665.5.17 by Vincent Ladeuil
Implement 'rm' command.
464
        self.failUnlessExists('file')
4665.5.20 by Vincent Ladeuil
Fixed as per Martin's review.
465
        self.run_script('$ rm file')
4665.5.17 by Vincent Ladeuil
Implement 'rm' command.
466
        self.failIfExists('file')
467
468
    def test_rm_file_force(self):
469
        self.failIfExists('file')
4665.5.20 by Vincent Ladeuil
Fixed as per Martin's review.
470
        self.run_script('$ rm -f file')
4665.5.17 by Vincent Ladeuil
Implement 'rm' command.
471
        self.failIfExists('file')
472
473
    def test_rm_files(self):
474
        self.run_script("""
4665.5.20 by Vincent Ladeuil
Fixed as per Martin's review.
475
$ echo content >file
476
$ echo content >file2
4665.5.17 by Vincent Ladeuil
Implement 'rm' command.
477
""")
478
        self.failUnlessExists('file2')
4665.5.20 by Vincent Ladeuil
Fixed as per Martin's review.
479
        self.run_script('$ rm file file2')
4665.5.17 by Vincent Ladeuil
Implement 'rm' command.
480
        self.failIfExists('file2')
481
482
    def test_rm_dir(self):
4665.5.20 by Vincent Ladeuil
Fixed as per Martin's review.
483
        self.run_script('$ mkdir dir')
4665.5.17 by Vincent Ladeuil
Implement 'rm' command.
484
        self.failUnlessExists('dir')
485
        self.run_script("""
4665.5.20 by Vincent Ladeuil
Fixed as per Martin's review.
486
$ rm dir
4665.5.17 by Vincent Ladeuil
Implement 'rm' command.
487
2>rm: cannot remove 'dir': Is a directory
488
""")
489
        self.failUnlessExists('dir')
490
491
    def test_rm_dir_recursive(self):
492
        self.run_script("""
4665.5.20 by Vincent Ladeuil
Fixed as per Martin's review.
493
$ mkdir dir
494
$ rm -r dir
4665.5.17 by Vincent Ladeuil
Implement 'rm' command.
495
""")
496
        self.failIfExists('dir')
4953.2.1 by Neil Martinsen-Burrell
add an mv command to shell-like tests
497
498
499
class TestMv(script.TestCaseWithTransportAndScript):
500
501
    def test_usage(self):
502
        self.assertRaises(SyntaxError, self.run_script, '$ mv')
503
        self.assertRaises(SyntaxError, self.run_script, '$ mv f')
504
        self.assertRaises(SyntaxError, self.run_script, '$ mv f1 f2 f3')
505
506
    def test_move_file(self):
507
        self.run_script('$ echo content >file')
508
        self.failUnlessExists('file')
509
        self.run_script('$ mv file new_name')
510
        self.failIfExists('file')
511
        self.failUnlessExists('new_name')
512
4953.2.3 by Vincent Ladeuil
Use os.rename() and fix some typos.
513
    def test_move_unknown_file(self):
514
        self.assertRaises(AssertionError,
515
                          self.run_script, '$ mv unknown does-not-exist')
516
4953.2.1 by Neil Martinsen-Burrell
add an mv command to shell-like tests
517
    def test_move_dir(self):
518
        self.run_script("""
519
$ mkdir dir
520
$ echo content >dir/file
521
""")
522
        self.run_script('$ mv dir new_name')
523
        self.failIfExists('dir')
524
        self.failUnlessExists('new_name')
525
        self.failUnlessExists('new_name/file')
4953.2.3 by Vincent Ladeuil
Use os.rename() and fix some typos.
526
4953.2.2 by Neil Martinsen-Burrell
added a test for "mv file dir" and a NEWS entry
527
    def test_move_file_into_dir(self):
528
        self.run_script("""
529
$ mkdir dir
530
$ echo content > file
531
""")
532
        self.run_script('$ mv file dir')
533
        self.failUnlessExists('dir')
534
        self.failIfExists('file')
535
        self.failUnlessExists('dir/file')
536
5417.1.1 by Martin Pool
ScriptRunner can now cope with commands that prompt for input.
537
538
class cmd_test_confirm(commands.Command):
539
540
    def run(self):
541
        if ui.ui_factory.get_boolean(
542
            'Really do it',
543
            # 'bzrlib.tests.test_script.confirm',
544
            # {}
545
            ):
5417.1.3 by Martin Pool
Additional script tests
546
            self.outf.write('Do it!\n')
5417.1.1 by Martin Pool
ScriptRunner can now cope with commands that prompt for input.
547
        else:
5417.1.3 by Martin Pool
Additional script tests
548
            print 'ok, no'
5417.1.1 by Martin Pool
ScriptRunner can now cope with commands that prompt for input.
549
550
551
class TestUserInteraction(script.TestCaseWithMemoryTransportAndScript):
552
553
    def test_confirm_action(self):
5417.1.2 by Martin Pool
Additional docs; yay testdoc
554
        """You can write tests that demonstrate user confirmation.
555
        
556
        Specifically, ScriptRunner does't care if the output line for the prompt
557
        isn't terminated by a newline from the program; it's implicitly terminated 
558
        by the input.
559
        """
5417.1.1 by Martin Pool
ScriptRunner can now cope with commands that prompt for input.
560
        commands.builtin_command_registry.register(cmd_test_confirm)
561
        self.addCleanup(commands.builtin_command_registry.remove, 'test-confirm')
562
        self.run_script("""
563
            $ bzr test-confirm
564
            2>Really do it? [y/n]: 
565
            <yes
5417.1.3 by Martin Pool
Additional script tests
566
            Do it!
567
            $ bzr test-confirm
568
            2>Really do it? [y/n]: 
569
            <no
570
            ok, no
5417.1.1 by Martin Pool
ScriptRunner can now cope with commands that prompt for input.
571
            """)
572