~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_script.py

  • Committer: Vincent Ladeuil
  • Date: 2017-01-17 13:48:10 UTC
  • mfrom: (6615.3.6 merges)
  • mto: This revision was merged to the branch mainline in revision 6620.
  • Revision ID: v.ladeuil+lp@free.fr-20170117134810-j9p3lidfy6pfyfsc
Merge 2.7, resolving conflicts

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2009, 2010 Canonical Ltd
 
1
# Copyright (C) 2009, 2010, 2011, 2016 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
16
16
 
17
17
 
18
18
from bzrlib import (
 
19
    commands,
19
20
    osutils,
20
21
    tests,
 
22
    trace,
 
23
    ui,
21
24
    )
22
25
from bzrlib.tests import script
23
26
 
25
28
class TestSyntax(tests.TestCase):
26
29
 
27
30
    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'))
 
31
        self.assertEqual([], script._script_to_commands('#comment\n'))
 
32
 
 
33
    def test_comment_multiple_lines(self):
 
34
        self.assertEqual([
 
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.assertEqual([
 
54
            (['bar'], None, '\n', None),
 
55
            ],
 
56
            script._script_to_commands("""
 
57
            $bar
 
58
 
 
59
            """))
32
60
 
33
61
    def test_simple_command(self):
34
 
        self.assertEquals([(['cd', 'trunk'], None, None, None)],
 
62
        self.assertEqual([(['cd', 'trunk'], None, None, None)],
35
63
                           script._script_to_commands('$ cd trunk'))
36
64
 
37
65
    def test_command_with_single_quoted_param(self):
38
66
        story = """$ bzr commit -m 'two words'"""
39
 
        self.assertEquals([(['bzr', 'commit', '-m', "'two words'"],
 
67
        self.assertEqual([(['bzr', 'commit', '-m', "'two words'"],
40
68
                            None, None, None)],
41
69
                           script._script_to_commands(story))
42
70
 
43
71
    def test_command_with_double_quoted_param(self):
44
72
        story = """$ bzr commit -m "two words" """
45
 
        self.assertEquals([(['bzr', 'commit', '-m', '"two words"'],
 
73
        self.assertEqual([(['bzr', 'commit', '-m', '"two words"'],
46
74
                            None, None, None)],
47
75
                           script._script_to_commands(story))
48
76
 
49
77
    def test_command_with_input(self):
50
 
        self.assertEquals(
 
78
        self.assertEqual(
51
79
            [(['cat', '>file'], 'content\n', None, None)],
52
80
            script._script_to_commands('$ cat >file\n<content\n'))
53
81
 
59
87
            adding file
60
88
            adding file2
61
89
            """
62
 
        self.assertEquals([(['bzr', 'add'], None,
 
90
        self.assertEqual([(['bzr', 'add'], None,
63
91
                            'adding file\nadding file2\n', None)],
64
92
                          script._script_to_commands(story))
65
93
 
69
97
adding file
70
98
adding file2
71
99
"""
72
 
        self.assertEquals([(['bzr', 'add'], None,
 
100
        self.assertEqual([(['bzr', 'add'], None,
73
101
                            'adding file\nadding file2\n', None)],
74
102
                          script._script_to_commands(story))
75
103
 
78
106
$ bzr branch foo
79
107
2>bzr: ERROR: Not a branch: "foo"
80
108
"""
81
 
        self.assertEquals([(['bzr', 'branch', 'foo'],
 
109
        self.assertEqual([(['bzr', 'branch', 'foo'],
82
110
                            None, None, 'bzr: ERROR: Not a branch: "foo"\n')],
83
111
                          script._script_to_commands(story))
84
112
 
92
120
        story = """
93
121
$ foo = `bzr file-id toto`
94
122
"""
95
 
        self.assertEquals([(['foo', '=', '`bzr file-id toto`'],
 
123
        self.assertEqual([(['foo', '=', '`bzr file-id toto`'],
96
124
                            None, None, None)],
97
125
                          script._script_to_commands(story))
98
126
 
133
161
class TestExecution(script.TestCaseWithTransportAndScript):
134
162
 
135
163
    def test_unknown_command(self):
136
 
        self.assertRaises(SyntaxError, self.run_script, 'foo')
 
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.assertEqual(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
        """)
137
209
 
138
210
    def test_stops_on_unexpected_output(self):
139
211
        story = """
143
215
"""
144
216
        self.assertRaises(AssertionError, self.run_script, story)
145
217
 
146
 
 
147
218
    def test_stops_on_unexpected_error(self):
148
219
        story = """
149
220
$ cat
163
234
        # The status matters, not the output
164
235
        story = """
165
236
$ bzr init
 
237
...
166
238
$ cat >file
167
239
<Hello
168
240
$ bzr add file
 
241
...
169
242
$ bzr commit -m 'adding file'
 
243
2>...
170
244
"""
171
245
        self.run_script(story)
172
246
 
218
292
cat dog "chicken" 'dragon'
219
293
""")
220
294
 
 
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.assertEqual(trace.is_quiet(), False)
 
303
 
221
304
 
222
305
class TestCat(script.TestCaseWithTransportAndScript):
223
306
 
227
310
    def test_cat_input_to_output(self):
228
311
        retcode, out, err = self.run_command(['cat'],
229
312
                                             'content\n', 'content\n', None)
230
 
        self.assertEquals('content\n', out)
231
 
        self.assertEquals(None, err)
 
313
        self.assertEqual('content\n', out)
 
314
        self.assertEqual(None, err)
232
315
 
233
316
    def test_cat_file_to_output(self):
234
317
        self.build_tree_contents([('file', 'content\n')])
235
318
        retcode, out, err = self.run_command(['cat', 'file'],
236
319
                                             None, 'content\n', None)
237
 
        self.assertEquals('content\n', out)
238
 
        self.assertEquals(None, err)
 
320
        self.assertEqual('content\n', out)
 
321
        self.assertEqual(None, err)
239
322
 
240
323
    def test_cat_input_to_file(self):
241
324
        retcode, out, err = self.run_command(['cat', '>file'],
242
325
                                             'content\n', None, None)
243
326
        self.assertFileEqual('content\n', 'file')
244
 
        self.assertEquals(None, out)
245
 
        self.assertEquals(None, err)
 
327
        self.assertEqual(None, out)
 
328
        self.assertEqual(None, err)
246
329
        retcode, out, err = self.run_command(['cat', '>>file'],
247
330
                                             'more\n', None, None)
248
331
        self.assertFileEqual('content\nmore\n', 'file')
249
 
        self.assertEquals(None, out)
250
 
        self.assertEquals(None, err)
 
332
        self.assertEqual(None, out)
 
333
        self.assertEqual(None, err)
251
334
 
252
335
    def test_cat_file_to_file(self):
253
336
        self.build_tree_contents([('file', 'content\n')])
300
383
$ mkdir ../dir2
301
384
$ cd ..
302
385
""")
303
 
        self.failUnlessExists('dir')
304
 
        self.failUnlessExists('dir2')
 
386
        self.assertPathExists('dir')
 
387
        self.assertPathExists('dir2')
305
388
 
306
389
 
307
390
class TestCd(script.TestCaseWithTransportAndScript):
314
397
        self.assertRaises(ValueError, self.run_script, '$ cd ..')
315
398
 
316
399
    def test_cd_dir_and_back_home(self):
317
 
        self.assertEquals(self.test_dir, osutils.getcwd())
 
400
        self.assertEqual(self.test_dir, osutils.getcwd())
318
401
        self.run_script("""
319
402
$ mkdir dir
320
403
$ cd dir
321
404
""")
322
 
        self.assertEquals(osutils.pathjoin(self.test_dir, 'dir'),
 
405
        self.assertEqual(osutils.pathjoin(self.test_dir, 'dir'),
323
406
                          osutils.getcwd())
324
407
 
325
408
        self.run_script('$ cd')
326
 
        self.assertEquals(self.test_dir, osutils.getcwd())
 
409
        self.assertEqual(self.test_dir, osutils.getcwd())
327
410
 
328
411
 
329
412
class TestBzr(script.TestCaseWithTransportAndScript):
330
413
 
331
414
    def test_bzr_smoke(self):
332
 
        self.run_script('$ bzr init branch')
333
 
        self.failUnlessExists('branch')
 
415
        self.run_script("""
 
416
            $ bzr init branch
 
417
            Created a standalone tree (format: ...)
 
418
            """)
 
419
        self.assertPathExists('branch')
334
420
 
335
421
 
336
422
class TestEcho(script.TestCaseWithMemoryTransportAndScript):
349
435
 
350
436
    def test_echo_to_output(self):
351
437
        retcode, out, err = self.run_command(['echo'], None, '\n', None)
352
 
        self.assertEquals('\n', out)
353
 
        self.assertEquals(None, err)
 
438
        self.assertEqual('\n', out)
 
439
        self.assertEqual(None, err)
354
440
 
355
441
    def test_echo_some_to_output(self):
356
442
        retcode, out, err = self.run_command(['echo', 'hello'],
357
443
                                             None, 'hello\n', None)
358
 
        self.assertEquals('hello\n', out)
359
 
        self.assertEquals(None, err)
 
444
        self.assertEqual('hello\n', out)
 
445
        self.assertEqual(None, err)
360
446
 
361
447
    def test_echo_more_output(self):
362
448
        retcode, out, err = self.run_command(
363
449
            ['echo', 'hello', 'happy', 'world'],
364
450
            None, 'hello happy world\n', None)
365
 
        self.assertEquals('hello happy world\n', out)
366
 
        self.assertEquals(None, err)
 
451
        self.assertEqual('hello happy world\n', out)
 
452
        self.assertEqual(None, err)
367
453
 
368
454
    def test_echo_appended(self):
369
455
        retcode, out, err = self.run_command(['echo', 'hello', '>file'],
370
456
                                             None, None, None)
371
 
        self.assertEquals(None, out)
372
 
        self.assertEquals(None, err)
 
457
        self.assertEqual(None, out)
 
458
        self.assertEqual(None, err)
373
459
        self.assertFileEqual('hello\n', 'file')
374
460
        retcode, out, err = self.run_command(['echo', 'happy', '>>file'],
375
461
                                             None, None, None)
376
 
        self.assertEquals(None, out)
377
 
        self.assertEquals(None, err)
 
462
        self.assertEqual(None, out)
 
463
        self.assertEqual(None, err)
378
464
        self.assertFileEqual('hello\nhappy\n', 'file')
379
465
 
 
466
    def test_empty_line_in_output_is_respected(self):
 
467
        self.run_script("""
 
468
            $ echo
 
469
 
 
470
            $ echo bar
 
471
            bar
 
472
            """)
 
473
 
380
474
 
381
475
class TestRm(script.TestCaseWithTransportAndScript):
382
476
 
386
480
 
387
481
    def test_rm_file(self):
388
482
        self.run_script('$ echo content >file')
389
 
        self.failUnlessExists('file')
 
483
        self.assertPathExists('file')
390
484
        self.run_script('$ rm file')
391
 
        self.failIfExists('file')
 
485
        self.assertPathDoesNotExist('file')
392
486
 
393
487
    def test_rm_file_force(self):
394
 
        self.failIfExists('file')
 
488
        self.assertPathDoesNotExist('file')
395
489
        self.run_script('$ rm -f file')
396
 
        self.failIfExists('file')
 
490
        self.assertPathDoesNotExist('file')
397
491
 
398
492
    def test_rm_files(self):
399
493
        self.run_script("""
400
494
$ echo content >file
401
495
$ echo content >file2
402
496
""")
403
 
        self.failUnlessExists('file2')
 
497
        self.assertPathExists('file2')
404
498
        self.run_script('$ rm file file2')
405
 
        self.failIfExists('file2')
 
499
        self.assertPathDoesNotExist('file2')
406
500
 
407
501
    def test_rm_dir(self):
408
502
        self.run_script('$ mkdir dir')
409
 
        self.failUnlessExists('dir')
 
503
        self.assertPathExists('dir')
410
504
        self.run_script("""
411
505
$ rm dir
412
506
2>rm: cannot remove 'dir': Is a directory
413
507
""")
414
 
        self.failUnlessExists('dir')
 
508
        self.assertPathExists('dir')
415
509
 
416
510
    def test_rm_dir_recursive(self):
417
511
        self.run_script("""
418
512
$ mkdir dir
419
513
$ rm -r dir
420
514
""")
421
 
        self.failIfExists('dir')
 
515
        self.assertPathDoesNotExist('dir')
422
516
 
423
517
 
424
518
class TestMv(script.TestCaseWithTransportAndScript):
430
524
 
431
525
    def test_move_file(self):
432
526
        self.run_script('$ echo content >file')
433
 
        self.failUnlessExists('file')
 
527
        self.assertPathExists('file')
434
528
        self.run_script('$ mv file new_name')
435
 
        self.failIfExists('file')
436
 
        self.failUnlessExists('new_name')
 
529
        self.assertPathDoesNotExist('file')
 
530
        self.assertPathExists('new_name')
437
531
 
438
532
    def test_move_unknown_file(self):
439
533
        self.assertRaises(AssertionError,
445
539
$ echo content >dir/file
446
540
""")
447
541
        self.run_script('$ mv dir new_name')
448
 
        self.failIfExists('dir')
449
 
        self.failUnlessExists('new_name')
450
 
        self.failUnlessExists('new_name/file')
 
542
        self.assertPathDoesNotExist('dir')
 
543
        self.assertPathExists('new_name')
 
544
        self.assertPathExists('new_name/file')
451
545
 
452
546
    def test_move_file_into_dir(self):
453
547
        self.run_script("""
455
549
$ echo content > file
456
550
""")
457
551
        self.run_script('$ mv file dir')
458
 
        self.failUnlessExists('dir')
459
 
        self.failIfExists('file')
460
 
        self.failUnlessExists('dir/file')
461
 
 
 
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
 
576
        prompt isn't terminated by a newline from the program; it's implicitly
 
577
        terminated 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]es, [n]o): yes
 
584
            <y
 
585
            Do it!
 
586
            $ bzr test-confirm
 
587
            2>Really do it? ([y]es, [n]o): no
 
588
            <n
 
589
            ok, no
 
590
            """)
 
591
 
 
592
class TestShelve(script.TestCaseWithTransportAndScript):
 
593
 
 
594
    def setUp(self):
 
595
        super(TestShelve, self).setUp()
 
596
        self.run_script("""
 
597
            $ bzr init test
 
598
            Created a standalone tree (format: 2a)
 
599
            $ cd test
 
600
            $ echo foo > file
 
601
            $ bzr add
 
602
            adding file
 
603
            $ bzr commit -m 'file added'
 
604
            2>Committing to:...test/
 
605
            2>added file
 
606
            2>Committed revision 1.
 
607
            $ echo bar > file
 
608
            """)
 
609
 
 
610
    def test_shelve(self):
 
611
        self.run_script("""
 
612
            $ bzr shelve -m 'shelve bar'
 
613
            2>Shelve? ([y]es, [N]o, [f]inish, [q]uit): yes
 
614
            <y
 
615
            2>Selected changes:
 
616
            2> M  file
 
617
            2>Shelve 1 change(s)? ([y]es, [N]o, [f]inish, [q]uit): yes
 
618
            <y
 
619
            2>Changes shelved with id "1".
 
620
            """,
 
621
                        null_output_matches_anything=True)
 
622
        self.run_script("""
 
623
            $ bzr shelve --list
 
624
              1: shelve bar
 
625
            """)
 
626
 
 
627
    def test_dont_shelve(self):
 
628
        # We intentionally provide no input here to test EOF
 
629
        self.run_script("""
 
630
            $ bzr shelve -m 'shelve bar'
 
631
            2>Shelve? ([y]es, [N]o, [f]inish, [q]uit): 
 
632
            2>No changes to shelve.
 
633
            """,
 
634
                        null_output_matches_anything=True)
 
635
        self.run_script("""
 
636
            $ bzr st
 
637
            modified:
 
638
              file
 
639
            """)