~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_script.py

  • Committer: Martin Pool
  • Date: 2010-02-25 06:17:27 UTC
  • mfrom: (5055 +trunk)
  • mto: This revision was merged to the branch mainline in revision 5057.
  • Revision ID: mbp@sourcefrog.net-20100225061727-4sd9lt0qmdc6087t
merge news

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 
17
17
 
18
18
from bzrlib import (
19
 
    commands,
20
19
    osutils,
21
20
    tests,
22
 
    trace,
23
 
    ui,
24
21
    )
25
22
from bzrlib.tests import script
26
23
 
30
27
    def test_comment_is_ignored(self):
31
28
        self.assertEquals([], script._script_to_commands('#comment\n'))
32
29
 
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
 
 
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
 
            """))
 
30
    def test_empty_line_is_ignored(self):
 
31
        self.assertEquals([], script._script_to_commands('\n'))
60
32
 
61
33
    def test_simple_command(self):
62
34
        self.assertEquals([(['cd', 'trunk'], None, None, None)],
79
51
            [(['cat', '>file'], 'content\n', None, None)],
80
52
            script._script_to_commands('$ cat >file\n<content\n'))
81
53
 
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
 
 
94
54
    def test_command_with_output(self):
95
55
        story = """
96
56
$ bzr add
161
121
class TestExecution(script.TestCaseWithTransportAndScript):
162
122
 
163
123
    def test_unknown_command(self):
164
 
        """A clear error is reported for commands that aren't recognised
165
 
 
166
 
        Testing the attributes of the SyntaxError instance is equivalent to
167
 
        using traceback.format_exception_only and comparing with:
168
 
          File "<string>", line 1
169
 
            foo --frob
170
 
            ^
171
 
        SyntaxError: Command not found "foo"
172
 
        """
173
 
        e = self.assertRaises(SyntaxError, self.run_script, "$ foo --frob")
174
 
        self.assertContainsRe(e.msg, "not found.*foo")
175
 
        self.assertEquals(e.text, "foo --frob")
176
 
 
177
 
    def test_blank_output_mismatches_output(self):
178
 
        """If you give output, the output must actually be blank.
179
 
        
180
 
        See <https://bugs.launchpad.net/bzr/+bug/637830>: previously blank
181
 
        output was a wildcard.  Now you must say ... if you want that.
182
 
        """
183
 
        self.assertRaises(AssertionError,
184
 
            self.run_script,
185
 
            """
186
 
            $ echo foo
187
 
            """)
188
 
 
189
 
    def test_null_output_matches_option(self):
190
 
        """If you want null output to be a wild card, you can pass 
191
 
        null_output_matches_anything to run_script"""
192
 
        self.run_script(
193
 
            """
194
 
            $ echo foo
195
 
            """, null_output_matches_anything=True)
196
 
 
197
 
    def test_ellipsis_everything(self):
198
 
        """A simple ellipsis matches everything."""
199
 
        self.run_script("""
200
 
        $ echo foo
201
 
        ...
202
 
        """)
203
 
 
204
 
    def test_ellipsis_matches_empty(self):
205
 
        self.run_script("""
206
 
        $ cd .
207
 
        ...
208
 
        """)
 
124
        self.assertRaises(SyntaxError, self.run_script, 'foo')
209
125
 
210
126
    def test_stops_on_unexpected_output(self):
211
127
        story = """
215
131
"""
216
132
        self.assertRaises(AssertionError, self.run_script, story)
217
133
 
 
134
 
218
135
    def test_stops_on_unexpected_error(self):
219
136
        story = """
220
137
$ cat
234
151
        # The status matters, not the output
235
152
        story = """
236
153
$ bzr init
237
 
...
238
154
$ cat >file
239
155
<Hello
240
156
$ bzr add file
241
 
...
242
157
$ bzr commit -m 'adding file'
243
 
2>...
244
158
"""
245
159
        self.run_script(story)
246
160
 
292
206
cat dog "chicken" 'dragon'
293
207
""")
294
208
 
295
 
    def test_verbosity_isolated(self):
296
 
        """Global verbosity is isolated from commands run in scripts.
297
 
        """
298
 
        # see also 656694; we should get rid of global verbosity
299
 
        self.run_script("""
300
 
        $ bzr init --quiet a
301
 
        """)
302
 
        self.assertEquals(trace.is_quiet(), False)
303
 
 
304
209
 
305
210
class TestCat(script.TestCaseWithTransportAndScript):
306
211
 
383
288
$ mkdir ../dir2
384
289
$ cd ..
385
290
""")
386
 
        self.assertPathExists('dir')
387
 
        self.assertPathExists('dir2')
 
291
        self.failUnlessExists('dir')
 
292
        self.failUnlessExists('dir2')
388
293
 
389
294
 
390
295
class TestCd(script.TestCaseWithTransportAndScript):
412
317
class TestBzr(script.TestCaseWithTransportAndScript):
413
318
 
414
319
    def test_bzr_smoke(self):
415
 
        self.run_script("""
416
 
            $ bzr init branch
417
 
            Created a standalone tree (format: ...)
418
 
            """)
419
 
        self.assertPathExists('branch')
 
320
        self.run_script('$ bzr init branch')
 
321
        self.failUnlessExists('branch')
420
322
 
421
323
 
422
324
class TestEcho(script.TestCaseWithMemoryTransportAndScript):
463
365
        self.assertEquals(None, err)
464
366
        self.assertFileEqual('hello\nhappy\n', 'file')
465
367
 
466
 
    def test_empty_line_in_output_is_respected(self):
467
 
        self.run_script("""
468
 
            $ echo
469
 
 
470
 
            $ echo bar
471
 
            bar
472
 
            """)
473
 
 
474
368
 
475
369
class TestRm(script.TestCaseWithTransportAndScript):
476
370
 
480
374
 
481
375
    def test_rm_file(self):
482
376
        self.run_script('$ echo content >file')
483
 
        self.assertPathExists('file')
 
377
        self.failUnlessExists('file')
484
378
        self.run_script('$ rm file')
485
 
        self.assertPathDoesNotExist('file')
 
379
        self.failIfExists('file')
486
380
 
487
381
    def test_rm_file_force(self):
488
 
        self.assertPathDoesNotExist('file')
 
382
        self.failIfExists('file')
489
383
        self.run_script('$ rm -f file')
490
 
        self.assertPathDoesNotExist('file')
 
384
        self.failIfExists('file')
491
385
 
492
386
    def test_rm_files(self):
493
387
        self.run_script("""
494
388
$ echo content >file
495
389
$ echo content >file2
496
390
""")
497
 
        self.assertPathExists('file2')
 
391
        self.failUnlessExists('file2')
498
392
        self.run_script('$ rm file file2')
499
 
        self.assertPathDoesNotExist('file2')
 
393
        self.failIfExists('file2')
500
394
 
501
395
    def test_rm_dir(self):
502
396
        self.run_script('$ mkdir dir')
503
 
        self.assertPathExists('dir')
 
397
        self.failUnlessExists('dir')
504
398
        self.run_script("""
505
399
$ rm dir
506
400
2>rm: cannot remove 'dir': Is a directory
507
401
""")
508
 
        self.assertPathExists('dir')
 
402
        self.failUnlessExists('dir')
509
403
 
510
404
    def test_rm_dir_recursive(self):
511
405
        self.run_script("""
512
406
$ mkdir dir
513
407
$ rm -r dir
514
408
""")
515
 
        self.assertPathDoesNotExist('dir')
 
409
        self.failIfExists('dir')
516
410
 
517
411
 
518
412
class TestMv(script.TestCaseWithTransportAndScript):
524
418
 
525
419
    def test_move_file(self):
526
420
        self.run_script('$ echo content >file')
527
 
        self.assertPathExists('file')
 
421
        self.failUnlessExists('file')
528
422
        self.run_script('$ mv file new_name')
529
 
        self.assertPathDoesNotExist('file')
530
 
        self.assertPathExists('new_name')
 
423
        self.failIfExists('file')
 
424
        self.failUnlessExists('new_name')
531
425
 
532
426
    def test_move_unknown_file(self):
533
427
        self.assertRaises(AssertionError,
539
433
$ echo content >dir/file
540
434
""")
541
435
        self.run_script('$ mv dir new_name')
542
 
        self.assertPathDoesNotExist('dir')
543
 
        self.assertPathExists('new_name')
544
 
        self.assertPathExists('new_name/file')
 
436
        self.failIfExists('dir')
 
437
        self.failUnlessExists('new_name')
 
438
        self.failUnlessExists('new_name/file')
545
439
 
546
440
    def test_move_file_into_dir(self):
547
441
        self.run_script("""
549
443
$ echo content > file
550
444
""")
551
445
        self.run_script('$ mv file dir')
552
 
        self.assertPathExists('dir')
553
 
        self.assertPathDoesNotExist('file')
554
 
        self.assertPathExists('dir/file')
555
 
 
556
 
 
557
 
class cmd_test_confirm(commands.Command):
558
 
 
559
 
    def run(self):
560
 
        if ui.ui_factory.get_boolean(
561
 
            u'Really do it',
562
 
            # 'bzrlib.tests.test_script.confirm',
563
 
            # {}
564
 
            ):
565
 
            self.outf.write('Do it!\n')
566
 
        else:
567
 
            print 'ok, no'
568
 
 
569
 
 
570
 
class TestUserInteraction(script.TestCaseWithMemoryTransportAndScript):
571
 
 
572
 
    def test_confirm_action(self):
573
 
        """You can write tests that demonstrate user confirmation.
574
 
        
575
 
        Specifically, ScriptRunner does't care if the output line for the prompt
576
 
        isn't terminated by a newline from the program; it's implicitly terminated 
577
 
        by the input.
578
 
        """
579
 
        commands.builtin_command_registry.register(cmd_test_confirm)
580
 
        self.addCleanup(commands.builtin_command_registry.remove, 'test-confirm')
581
 
        self.run_script("""
582
 
            $ bzr test-confirm
583
 
            2>Really do it? [y/n]: 
584
 
            <yes
585
 
            Do it!
586
 
            $ bzr test-confirm
587
 
            2>Really do it? [y/n]: 
588
 
            <no
589
 
            ok, no
590
 
            """)
 
446
        self.failUnlessExists('dir')
 
447
        self.failIfExists('file')
 
448
        self.failUnlessExists('dir/file')
591
449