~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_script.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2010-09-01 08:02:42 UTC
  • mfrom: (5390.3.3 faster-revert-593560)
  • Revision ID: pqm@pqm.ubuntu.com-20100901080242-esg62ody4frwmy66
(spiv) Avoid repeatedly calling self.target.all_file_ids() in
 InterTree.iter_changes. (Andrew Bennetts)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2009, 2010 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
 
 
17
 
 
18
from bzrlib import (
 
19
    osutils,
 
20
    tests,
 
21
    )
 
22
from bzrlib.tests import script
 
23
 
 
24
 
 
25
class TestSyntax(tests.TestCase):
 
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):
 
34
        self.assertEquals([(['cd', 'trunk'], None, None, None)],
 
35
                           script._script_to_commands('$ cd trunk'))
 
36
 
 
37
    def test_command_with_single_quoted_param(self):
 
38
        story = """$ bzr commit -m 'two words'"""
 
39
        self.assertEquals([(['bzr', 'commit', '-m', "'two words'"],
 
40
                            None, None, None)],
 
41
                           script._script_to_commands(story))
 
42
 
 
43
    def test_command_with_double_quoted_param(self):
 
44
        story = """$ bzr commit -m "two words" """
 
45
        self.assertEquals([(['bzr', 'commit', '-m', '"two words"'],
 
46
                            None, None, None)],
 
47
                           script._script_to_commands(story))
 
48
 
 
49
    def test_command_with_input(self):
 
50
        self.assertEquals(
 
51
            [(['cat', '>file'], 'content\n', None, None)],
 
52
            script._script_to_commands('$ cat >file\n<content\n'))
 
53
 
 
54
    def test_indented(self):
 
55
        # scripts are commonly given indented within the test source code, and
 
56
        # common indentation is stripped off
 
57
        story = """
 
58
            $ bzr add
 
59
            adding file
 
60
            adding file2
 
61
            """
 
62
        self.assertEquals([(['bzr', 'add'], None,
 
63
                            'adding file\nadding file2\n', None)],
 
64
                          script._script_to_commands(story))
 
65
 
 
66
    def test_command_with_output(self):
 
67
        story = """
 
68
$ bzr add
 
69
adding file
 
70
adding file2
 
71
"""
 
72
        self.assertEquals([(['bzr', 'add'], None,
 
73
                            'adding file\nadding file2\n', None)],
 
74
                          script._script_to_commands(story))
 
75
 
 
76
    def test_command_with_error(self):
 
77
        story = """
 
78
$ bzr branch foo
 
79
2>bzr: ERROR: Not a branch: "foo"
 
80
"""
 
81
        self.assertEquals([(['bzr', 'branch', 'foo'],
 
82
                            None, None, 'bzr: ERROR: Not a branch: "foo"\n')],
 
83
                          script._script_to_commands(story))
 
84
 
 
85
    def test_input_without_command(self):
 
86
        self.assertRaises(SyntaxError, script._script_to_commands, '<input')
 
87
 
 
88
    def test_output_without_command(self):
 
89
        self.assertRaises(SyntaxError, script._script_to_commands, '>input')
 
90
 
 
91
    def test_command_with_backquotes(self):
 
92
        story = """
 
93
$ foo = `bzr file-id toto`
 
94
"""
 
95
        self.assertEquals([(['foo', '=', '`bzr file-id toto`'],
 
96
                            None, None, None)],
 
97
                          script._script_to_commands(story))
 
98
 
 
99
 
 
100
class TestRedirections(tests.TestCase):
 
101
 
 
102
    def _check(self, in_name, out_name, out_mode, remaining, args):
 
103
        self.assertEqual(script._scan_redirection_options(args),
 
104
                         (in_name, out_name, out_mode, remaining))
 
105
 
 
106
    def test_no_redirection(self):
 
107
        self._check(None, None, None, [], [])
 
108
        self._check(None, None, None, ['foo', 'bar'], ['foo', 'bar'])
 
109
 
 
110
    def test_input_redirection(self):
 
111
        self._check('foo', None, None, [], ['<foo'])
 
112
        self._check('foo', None, None, ['bar'], ['bar', '<foo'])
 
113
        self._check('foo', None, None, ['bar'], ['bar', '<', 'foo'])
 
114
        self._check('foo', None, None, ['bar'], ['<foo', 'bar'])
 
115
        self._check('foo', None, None, ['bar', 'baz'], ['bar', '<foo', 'baz'])
 
116
 
 
117
    def test_output_redirection(self):
 
118
        self._check(None, 'foo', 'wb+', [], ['>foo'])
 
119
        self._check(None, 'foo', 'wb+', ['bar'], ['bar', '>foo'])
 
120
        self._check(None, 'foo', 'wb+', ['bar'], ['bar', '>', 'foo'])
 
121
        self._check(None, 'foo', 'ab+', [], ['>>foo'])
 
122
        self._check(None, 'foo', 'ab+', ['bar'], ['bar', '>>foo'])
 
123
        self._check(None, 'foo', 'ab+', ['bar'], ['bar', '>>', 'foo'])
 
124
 
 
125
    def test_redirection_syntax_errors(self):
 
126
        self._check('', None, None, [], ['<'])
 
127
        self._check(None, '', 'wb+', [], ['>'])
 
128
        self._check(None, '', 'ab+', [], ['>>'])
 
129
        self._check('>', '', 'ab+', [], ['<', '>', '>>'])
 
130
 
 
131
 
 
132
 
 
133
class TestExecution(script.TestCaseWithTransportAndScript):
 
134
 
 
135
    def test_unknown_command(self):
 
136
        self.assertRaises(SyntaxError, self.run_script, 'foo')
 
137
 
 
138
    def test_stops_on_unexpected_output(self):
 
139
        story = """
 
140
$ mkdir dir
 
141
$ cd dir
 
142
The cd command ouputs nothing
 
143
"""
 
144
        self.assertRaises(AssertionError, self.run_script, story)
 
145
 
 
146
 
 
147
    def test_stops_on_unexpected_error(self):
 
148
        story = """
 
149
$ cat
 
150
<Hello
 
151
$ bzr not-a-command
 
152
"""
 
153
        self.assertRaises(AssertionError, self.run_script, story)
 
154
 
 
155
    def test_continue_on_expected_error(self):
 
156
        story = """
 
157
$ bzr not-a-command
 
158
2>..."not-a-command"
 
159
"""
 
160
        self.run_script(story)
 
161
 
 
162
    def test_continue_on_error_output(self):
 
163
        # The status matters, not the output
 
164
        story = """
 
165
$ bzr init
 
166
$ cat >file
 
167
<Hello
 
168
$ bzr add file
 
169
$ bzr commit -m 'adding file'
 
170
"""
 
171
        self.run_script(story)
 
172
 
 
173
    def test_ellipsis_output(self):
 
174
        story = """
 
175
$ cat
 
176
<first line
 
177
<second line
 
178
<last line
 
179
first line
 
180
...
 
181
last line
 
182
"""
 
183
        self.run_script(story)
 
184
        story = """
 
185
$ bzr not-a-command
 
186
2>..."not-a-command"
 
187
"""
 
188
        self.run_script(story)
 
189
 
 
190
        story = """
 
191
$ bzr branch not-a-branch
 
192
2>bzr: ERROR: Not a branch...not-a-branch/".
 
193
"""
 
194
        self.run_script(story)
 
195
 
 
196
 
 
197
class TestArgumentProcessing(script.TestCaseWithTransportAndScript):
 
198
 
 
199
    def test_globing(self):
 
200
        self.run_script("""
 
201
$ echo cat >cat
 
202
$ echo dog >dog
 
203
$ cat *
 
204
cat
 
205
dog
 
206
""")
 
207
 
 
208
    def test_quoted_globbing(self):
 
209
        self.run_script("""
 
210
$ echo cat >cat
 
211
$ cat '*'
 
212
2>*: No such file or directory
 
213
""")
 
214
 
 
215
    def test_quotes_removal(self):
 
216
        self.run_script("""
 
217
$ echo 'cat' "dog" '"chicken"' "'dragon'"
 
218
cat dog "chicken" 'dragon'
 
219
""")
 
220
 
 
221
 
 
222
class TestCat(script.TestCaseWithTransportAndScript):
 
223
 
 
224
    def test_cat_usage(self):
 
225
        self.assertRaises(SyntaxError, self.run_script, 'cat foo <bar')
 
226
 
 
227
    def test_cat_input_to_output(self):
 
228
        retcode, out, err = self.run_command(['cat'],
 
229
                                             'content\n', 'content\n', None)
 
230
        self.assertEquals('content\n', out)
 
231
        self.assertEquals(None, err)
 
232
 
 
233
    def test_cat_file_to_output(self):
 
234
        self.build_tree_contents([('file', 'content\n')])
 
235
        retcode, out, err = self.run_command(['cat', 'file'],
 
236
                                             None, 'content\n', None)
 
237
        self.assertEquals('content\n', out)
 
238
        self.assertEquals(None, err)
 
239
 
 
240
    def test_cat_input_to_file(self):
 
241
        retcode, out, err = self.run_command(['cat', '>file'],
 
242
                                             'content\n', None, None)
 
243
        self.assertFileEqual('content\n', 'file')
 
244
        self.assertEquals(None, out)
 
245
        self.assertEquals(None, err)
 
246
        retcode, out, err = self.run_command(['cat', '>>file'],
 
247
                                             'more\n', None, None)
 
248
        self.assertFileEqual('content\nmore\n', 'file')
 
249
        self.assertEquals(None, out)
 
250
        self.assertEquals(None, err)
 
251
 
 
252
    def test_cat_file_to_file(self):
 
253
        self.build_tree_contents([('file', 'content\n')])
 
254
        retcode, out, err = self.run_command(['cat', 'file', '>file2'],
 
255
                                             None, None, None)
 
256
        self.assertFileEqual('content\n', 'file2')
 
257
 
 
258
    def test_cat_files_to_file(self):
 
259
        self.build_tree_contents([('cat', 'cat\n')])
 
260
        self.build_tree_contents([('dog', 'dog\n')])
 
261
        retcode, out, err = self.run_command(['cat', 'cat', 'dog', '>file'],
 
262
                                             None, None, None)
 
263
        self.assertFileEqual('cat\ndog\n', 'file')
 
264
 
 
265
    def test_cat_bogus_input_file(self):
 
266
        self.run_script("""
 
267
$ cat <file
 
268
2>file: No such file or directory
 
269
""")
 
270
 
 
271
    def test_cat_bogus_output_file(self):
 
272
        self.run_script("""
 
273
$ cat >
 
274
2>: No such file or directory
 
275
""")
 
276
 
 
277
    def test_echo_bogus_output_file(self):
 
278
        # We need a backing file sysytem for that test so it can't be in
 
279
        # TestEcho
 
280
        self.run_script("""
 
281
$ echo >
 
282
2>: No such file or directory
 
283
""")
 
284
 
 
285
 
 
286
class TestMkdir(script.TestCaseWithTransportAndScript):
 
287
 
 
288
    def test_mkdir_usage(self):
 
289
        self.assertRaises(SyntaxError, self.run_script, '$ mkdir')
 
290
        self.assertRaises(SyntaxError, self.run_script, '$ mkdir foo bar')
 
291
 
 
292
    def test_mkdir_jailed(self):
 
293
        self.assertRaises(ValueError, self.run_script, '$ mkdir /out-of-jail')
 
294
        self.assertRaises(ValueError, self.run_script, '$ mkdir ../out-of-jail')
 
295
 
 
296
    def test_mkdir_in_jail(self):
 
297
        self.run_script("""
 
298
$ mkdir dir
 
299
$ cd dir
 
300
$ mkdir ../dir2
 
301
$ cd ..
 
302
""")
 
303
        self.failUnlessExists('dir')
 
304
        self.failUnlessExists('dir2')
 
305
 
 
306
 
 
307
class TestCd(script.TestCaseWithTransportAndScript):
 
308
 
 
309
    def test_cd_usage(self):
 
310
        self.assertRaises(SyntaxError, self.run_script, '$ cd foo bar')
 
311
 
 
312
    def test_cd_out_of_jail(self):
 
313
        self.assertRaises(ValueError, self.run_script, '$ cd /out-of-jail')
 
314
        self.assertRaises(ValueError, self.run_script, '$ cd ..')
 
315
 
 
316
    def test_cd_dir_and_back_home(self):
 
317
        self.assertEquals(self.test_dir, osutils.getcwd())
 
318
        self.run_script("""
 
319
$ mkdir dir
 
320
$ cd dir
 
321
""")
 
322
        self.assertEquals(osutils.pathjoin(self.test_dir, 'dir'),
 
323
                          osutils.getcwd())
 
324
 
 
325
        self.run_script('$ cd')
 
326
        self.assertEquals(self.test_dir, osutils.getcwd())
 
327
 
 
328
 
 
329
class TestBzr(script.TestCaseWithTransportAndScript):
 
330
 
 
331
    def test_bzr_smoke(self):
 
332
        self.run_script('$ bzr init branch')
 
333
        self.failUnlessExists('branch')
 
334
 
 
335
 
 
336
class TestEcho(script.TestCaseWithMemoryTransportAndScript):
 
337
 
 
338
    def test_echo_usage(self):
 
339
        story = """
 
340
$ echo foo
 
341
<bar
 
342
"""
 
343
        self.assertRaises(SyntaxError, self.run_script, story)
 
344
 
 
345
    def test_echo_input(self):
 
346
        self.assertRaises(SyntaxError, self.run_script, """
 
347
            $ echo <foo
 
348
            """)
 
349
 
 
350
    def test_echo_to_output(self):
 
351
        retcode, out, err = self.run_command(['echo'], None, '\n', None)
 
352
        self.assertEquals('\n', out)
 
353
        self.assertEquals(None, err)
 
354
 
 
355
    def test_echo_some_to_output(self):
 
356
        retcode, out, err = self.run_command(['echo', 'hello'],
 
357
                                             None, 'hello\n', None)
 
358
        self.assertEquals('hello\n', out)
 
359
        self.assertEquals(None, err)
 
360
 
 
361
    def test_echo_more_output(self):
 
362
        retcode, out, err = self.run_command(
 
363
            ['echo', 'hello', 'happy', 'world'],
 
364
            None, 'hello happy world\n', None)
 
365
        self.assertEquals('hello happy world\n', out)
 
366
        self.assertEquals(None, err)
 
367
 
 
368
    def test_echo_appended(self):
 
369
        retcode, out, err = self.run_command(['echo', 'hello', '>file'],
 
370
                                             None, None, None)
 
371
        self.assertEquals(None, out)
 
372
        self.assertEquals(None, err)
 
373
        self.assertFileEqual('hello\n', 'file')
 
374
        retcode, out, err = self.run_command(['echo', 'happy', '>>file'],
 
375
                                             None, None, None)
 
376
        self.assertEquals(None, out)
 
377
        self.assertEquals(None, err)
 
378
        self.assertFileEqual('hello\nhappy\n', 'file')
 
379
 
 
380
 
 
381
class TestRm(script.TestCaseWithTransportAndScript):
 
382
 
 
383
    def test_rm_usage(self):
 
384
        self.assertRaises(SyntaxError, self.run_script, '$ rm')
 
385
        self.assertRaises(SyntaxError, self.run_script, '$ rm -ff foo')
 
386
 
 
387
    def test_rm_file(self):
 
388
        self.run_script('$ echo content >file')
 
389
        self.failUnlessExists('file')
 
390
        self.run_script('$ rm file')
 
391
        self.failIfExists('file')
 
392
 
 
393
    def test_rm_file_force(self):
 
394
        self.failIfExists('file')
 
395
        self.run_script('$ rm -f file')
 
396
        self.failIfExists('file')
 
397
 
 
398
    def test_rm_files(self):
 
399
        self.run_script("""
 
400
$ echo content >file
 
401
$ echo content >file2
 
402
""")
 
403
        self.failUnlessExists('file2')
 
404
        self.run_script('$ rm file file2')
 
405
        self.failIfExists('file2')
 
406
 
 
407
    def test_rm_dir(self):
 
408
        self.run_script('$ mkdir dir')
 
409
        self.failUnlessExists('dir')
 
410
        self.run_script("""
 
411
$ rm dir
 
412
2>rm: cannot remove 'dir': Is a directory
 
413
""")
 
414
        self.failUnlessExists('dir')
 
415
 
 
416
    def test_rm_dir_recursive(self):
 
417
        self.run_script("""
 
418
$ mkdir dir
 
419
$ rm -r dir
 
420
""")
 
421
        self.failIfExists('dir')
 
422
 
 
423
 
 
424
class TestMv(script.TestCaseWithTransportAndScript):
 
425
 
 
426
    def test_usage(self):
 
427
        self.assertRaises(SyntaxError, self.run_script, '$ mv')
 
428
        self.assertRaises(SyntaxError, self.run_script, '$ mv f')
 
429
        self.assertRaises(SyntaxError, self.run_script, '$ mv f1 f2 f3')
 
430
 
 
431
    def test_move_file(self):
 
432
        self.run_script('$ echo content >file')
 
433
        self.failUnlessExists('file')
 
434
        self.run_script('$ mv file new_name')
 
435
        self.failIfExists('file')
 
436
        self.failUnlessExists('new_name')
 
437
 
 
438
    def test_move_unknown_file(self):
 
439
        self.assertRaises(AssertionError,
 
440
                          self.run_script, '$ mv unknown does-not-exist')
 
441
 
 
442
    def test_move_dir(self):
 
443
        self.run_script("""
 
444
$ mkdir dir
 
445
$ echo content >dir/file
 
446
""")
 
447
        self.run_script('$ mv dir new_name')
 
448
        self.failIfExists('dir')
 
449
        self.failUnlessExists('new_name')
 
450
        self.failUnlessExists('new_name/file')
 
451
 
 
452
    def test_move_file_into_dir(self):
 
453
        self.run_script("""
 
454
$ mkdir dir
 
455
$ echo content > file
 
456
""")
 
457
        self.run_script('$ mv file dir')
 
458
        self.failUnlessExists('dir')
 
459
        self.failIfExists('file')
 
460
        self.failUnlessExists('dir/file')
 
461