/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/tests/test_script.py

  • Committer: Breezy landing bot
  • Author(s): Colin Watson
  • Date: 2020-11-16 21:47:08 UTC
  • mfrom: (7521.1.1 remove-lp-workaround)
  • Revision ID: breezy.the.bot@gmail.com-20201116214708-jos209mgxi41oy15
Remove breezy.git workaround for bazaar.launchpad.net.

Merged from https://code.launchpad.net/~cjwatson/brz/remove-lp-workaround/+merge/393710

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
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
17
 
18
 
from bzrlib import (
 
18
from breezy import (
19
19
    commands,
20
20
    osutils,
21
21
    tests,
22
22
    trace,
23
23
    ui,
24
24
    )
25
 
from bzrlib.tests import script
 
25
from breezy.tests import script
26
26
 
27
27
 
28
28
class TestSyntax(tests.TestCase):
29
29
 
30
30
    def test_comment_is_ignored(self):
31
 
        self.assertEquals([], script._script_to_commands('#comment\n'))
 
31
        self.assertEqual([], script._script_to_commands('#comment\n'))
32
32
 
33
33
    def test_comment_multiple_lines(self):
34
 
        self.assertEquals([
 
34
        self.assertEqual([
35
35
            (['bar'], None, None, None),
36
36
            ],
37
37
            script._script_to_commands("""
44
44
    def test_trim_blank_lines(self):
45
45
        """Blank lines are respected, but trimmed at the start and end.
46
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 
 
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
49
        need special syntax to avoid them.
50
50
 
51
51
        However we do want to be able to match commands that emit blank lines.
52
52
        """
53
 
        self.assertEquals([
 
53
        self.assertEqual([
54
54
            (['bar'], None, '\n', None),
55
55
            ],
56
56
            script._script_to_commands("""
59
59
            """))
60
60
 
61
61
    def test_simple_command(self):
62
 
        self.assertEquals([(['cd', 'trunk'], None, None, None)],
63
 
                           script._script_to_commands('$ cd trunk'))
 
62
        self.assertEqual([(['cd', 'trunk'], None, None, None)],
 
63
                         script._script_to_commands('$ cd trunk'))
64
64
 
65
65
    def test_command_with_single_quoted_param(self):
66
 
        story = """$ bzr commit -m 'two words'"""
67
 
        self.assertEquals([(['bzr', 'commit', '-m', "'two words'"],
68
 
                            None, None, None)],
69
 
                           script._script_to_commands(story))
 
66
        story = """$ brz commit -m 'two words'"""
 
67
        self.assertEqual([(['brz', 'commit', '-m', "'two words'"],
 
68
                           None, None, None)],
 
69
                         script._script_to_commands(story))
70
70
 
71
71
    def test_command_with_double_quoted_param(self):
72
 
        story = """$ bzr commit -m "two words" """
73
 
        self.assertEquals([(['bzr', 'commit', '-m', '"two words"'],
74
 
                            None, None, None)],
75
 
                           script._script_to_commands(story))
 
72
        story = """$ brz commit -m "two words" """
 
73
        self.assertEqual([(['brz', 'commit', '-m', '"two words"'],
 
74
                           None, None, None)],
 
75
                         script._script_to_commands(story))
76
76
 
77
77
    def test_command_with_input(self):
78
 
        self.assertEquals(
 
78
        self.assertEqual(
79
79
            [(['cat', '>file'], 'content\n', None, None)],
80
80
            script._script_to_commands('$ cat >file\n<content\n'))
81
81
 
83
83
        # scripts are commonly given indented within the test source code, and
84
84
        # common indentation is stripped off
85
85
        story = """
86
 
            $ bzr add
 
86
            $ brz add
87
87
            adding file
88
88
            adding file2
89
89
            """
90
 
        self.assertEquals([(['bzr', 'add'], None,
91
 
                            'adding file\nadding file2\n', None)],
92
 
                          script._script_to_commands(story))
 
90
        self.assertEqual([(['brz', 'add'], None,
 
91
                           'adding file\nadding file2\n', None)],
 
92
                         script._script_to_commands(story))
93
93
 
94
94
    def test_command_with_output(self):
95
95
        story = """
96
 
$ bzr add
 
96
$ brz add
97
97
adding file
98
98
adding file2
99
99
"""
100
 
        self.assertEquals([(['bzr', 'add'], None,
101
 
                            'adding file\nadding file2\n', None)],
102
 
                          script._script_to_commands(story))
 
100
        self.assertEqual([(['brz', 'add'], None,
 
101
                           'adding file\nadding file2\n', None)],
 
102
                         script._script_to_commands(story))
103
103
 
104
104
    def test_command_with_error(self):
105
105
        story = """
106
 
$ bzr branch foo
107
 
2>bzr: ERROR: Not a branch: "foo"
 
106
$ brz branch foo
 
107
2>brz: ERROR: Not a branch: "foo"
108
108
"""
109
 
        self.assertEquals([(['bzr', 'branch', 'foo'],
110
 
                            None, None, 'bzr: ERROR: Not a branch: "foo"\n')],
111
 
                          script._script_to_commands(story))
 
109
        self.assertEqual([(['brz', 'branch', 'foo'],
 
110
                           None, None, 'brz: ERROR: Not a branch: "foo"\n')],
 
111
                         script._script_to_commands(story))
112
112
 
113
113
    def test_input_without_command(self):
114
114
        self.assertRaises(SyntaxError, script._script_to_commands, '<input')
118
118
 
119
119
    def test_command_with_backquotes(self):
120
120
        story = """
121
 
$ foo = `bzr file-id toto`
 
121
$ foo = `brz file-id toto`
122
122
"""
123
 
        self.assertEquals([(['foo', '=', '`bzr file-id toto`'],
124
 
                            None, None, None)],
125
 
                          script._script_to_commands(story))
 
123
        self.assertEqual([(['foo', '=', '`brz file-id toto`'],
 
124
                           None, None, None)],
 
125
                         script._script_to_commands(story))
126
126
 
127
127
 
128
128
class TestRedirections(tests.TestCase):
143
143
        self._check('foo', None, None, ['bar', 'baz'], ['bar', '<foo', 'baz'])
144
144
 
145
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'])
 
146
        self._check(None, 'foo', 'w+', [], ['>foo'])
 
147
        self._check(None, 'foo', 'w+', ['bar'], ['bar', '>foo'])
 
148
        self._check(None, 'foo', 'w+', ['bar'], ['bar', '>', 'foo'])
 
149
        self._check(None, 'foo', 'a+', [], ['>>foo'])
 
150
        self._check(None, 'foo', 'a+', ['bar'], ['bar', '>>foo'])
 
151
        self._check(None, 'foo', 'a+', ['bar'], ['bar', '>>', 'foo'])
152
152
 
153
153
    def test_redirection_syntax_errors(self):
154
154
        self._check('', None, None, [], ['<'])
155
 
        self._check(None, '', 'wb+', [], ['>'])
156
 
        self._check(None, '', 'ab+', [], ['>>'])
157
 
        self._check('>', '', 'ab+', [], ['<', '>', '>>'])
158
 
 
 
155
        self._check(None, '', 'w+', [], ['>'])
 
156
        self._check(None, '', 'a+', [], ['>>'])
 
157
        self._check('>', '', 'a+', [], ['<', '>', '>>'])
159
158
 
160
159
 
161
160
class TestExecution(script.TestCaseWithTransportAndScript):
172
171
        """
173
172
        e = self.assertRaises(SyntaxError, self.run_script, "$ foo --frob")
174
173
        self.assertContainsRe(e.msg, "not found.*foo")
175
 
        self.assertEquals(e.text, "foo --frob")
 
174
        self.assertEqual(e.text, "foo --frob")
176
175
 
177
176
    def test_blank_output_mismatches_output(self):
178
177
        """If you give output, the output must actually be blank.
179
 
        
 
178
 
180
179
        See <https://bugs.launchpad.net/bzr/+bug/637830>: previously blank
181
180
        output was a wildcard.  Now you must say ... if you want that.
182
181
        """
183
182
        self.assertRaises(AssertionError,
184
 
            self.run_script,
185
 
            """
 
183
                          self.run_script,
 
184
                          """
186
185
            $ echo foo
187
186
            """)
188
187
 
189
188
    def test_null_output_matches_option(self):
190
 
        """If you want null output to be a wild card, you can pass 
 
189
        """If you want null output to be a wild card, you can pass
191
190
        null_output_matches_anything to run_script"""
192
191
        self.run_script(
193
192
            """
219
218
        story = """
220
219
$ cat
221
220
<Hello
222
 
$ bzr not-a-command
 
221
$ brz not-a-command
223
222
"""
224
223
        self.assertRaises(AssertionError, self.run_script, story)
225
224
 
226
225
    def test_continue_on_expected_error(self):
227
226
        story = """
228
 
$ bzr not-a-command
 
227
$ brz not-a-command
229
228
2>..."not-a-command"
230
229
"""
231
230
        self.run_script(story)
233
232
    def test_continue_on_error_output(self):
234
233
        # The status matters, not the output
235
234
        story = """
236
 
$ bzr init
 
235
$ brz init
237
236
...
238
237
$ cat >file
239
238
<Hello
240
 
$ bzr add file
 
239
$ brz add file
241
240
...
242
 
$ bzr commit -m 'adding file'
 
241
$ brz commit -m 'adding file'
243
242
2>...
244
243
"""
245
244
        self.run_script(story)
256
255
"""
257
256
        self.run_script(story)
258
257
        story = """
259
 
$ bzr not-a-command
 
258
$ brz not-a-command
260
259
2>..."not-a-command"
261
260
"""
262
261
        self.run_script(story)
263
262
 
264
263
        story = """
265
 
$ bzr branch not-a-branch
266
 
2>bzr: ERROR: Not a branch...not-a-branch/".
 
264
$ brz branch not-a-branch
 
265
2>brz: ERROR: Not a branch...not-a-branch/".
267
266
"""
268
267
        self.run_script(story)
269
268
 
297
296
        """
298
297
        # see also 656694; we should get rid of global verbosity
299
298
        self.run_script("""
300
 
        $ bzr init --quiet a
 
299
        $ brz init --quiet a
301
300
        """)
302
 
        self.assertEquals(trace.is_quiet(), False)
 
301
        self.assertEqual(trace.is_quiet(), False)
303
302
 
304
303
 
305
304
class TestCat(script.TestCaseWithTransportAndScript):
310
309
    def test_cat_input_to_output(self):
311
310
        retcode, out, err = self.run_command(['cat'],
312
311
                                             'content\n', 'content\n', None)
313
 
        self.assertEquals('content\n', out)
314
 
        self.assertEquals(None, err)
 
312
        self.assertEqual('content\n', out)
 
313
        self.assertEqual(None, err)
315
314
 
316
315
    def test_cat_file_to_output(self):
317
 
        self.build_tree_contents([('file', 'content\n')])
 
316
        self.build_tree_contents([('file', b'content\n')])
318
317
        retcode, out, err = self.run_command(['cat', 'file'],
319
318
                                             None, 'content\n', None)
320
 
        self.assertEquals('content\n', out)
321
 
        self.assertEquals(None, err)
 
319
        self.assertEqual('content\n', out)
 
320
        self.assertEqual(None, err)
322
321
 
323
322
    def test_cat_input_to_file(self):
324
323
        retcode, out, err = self.run_command(['cat', '>file'],
325
324
                                             'content\n', None, None)
326
325
        self.assertFileEqual('content\n', 'file')
327
 
        self.assertEquals(None, out)
328
 
        self.assertEquals(None, err)
 
326
        self.assertEqual(None, out)
 
327
        self.assertEqual(None, err)
329
328
        retcode, out, err = self.run_command(['cat', '>>file'],
330
329
                                             'more\n', None, None)
331
330
        self.assertFileEqual('content\nmore\n', 'file')
332
 
        self.assertEquals(None, out)
333
 
        self.assertEquals(None, err)
 
331
        self.assertEqual(None, out)
 
332
        self.assertEqual(None, err)
334
333
 
335
334
    def test_cat_file_to_file(self):
336
 
        self.build_tree_contents([('file', 'content\n')])
 
335
        self.build_tree_contents([('file', b'content\n')])
337
336
        retcode, out, err = self.run_command(['cat', 'file', '>file2'],
338
337
                                             None, None, None)
339
 
        self.assertFileEqual('content\n', 'file2')
 
338
        self.assertFileEqual(b'content\n', 'file2')
340
339
 
341
340
    def test_cat_files_to_file(self):
342
 
        self.build_tree_contents([('cat', 'cat\n')])
343
 
        self.build_tree_contents([('dog', 'dog\n')])
 
341
        self.build_tree_contents([('cat', b'cat\n')])
 
342
        self.build_tree_contents([('dog', b'dog\n')])
344
343
        retcode, out, err = self.run_command(['cat', 'cat', 'dog', '>file'],
345
344
                                             None, None, None)
346
 
        self.assertFileEqual('cat\ndog\n', 'file')
 
345
        self.assertFileEqual(b'cat\ndog\n', 'file')
347
346
 
348
347
    def test_cat_bogus_input_file(self):
349
348
        self.run_script("""
374
373
 
375
374
    def test_mkdir_jailed(self):
376
375
        self.assertRaises(ValueError, self.run_script, '$ mkdir /out-of-jail')
377
 
        self.assertRaises(ValueError, self.run_script, '$ mkdir ../out-of-jail')
 
376
        self.assertRaises(ValueError, self.run_script,
 
377
                          '$ mkdir ../out-of-jail')
378
378
 
379
379
    def test_mkdir_in_jail(self):
380
380
        self.run_script("""
397
397
        self.assertRaises(ValueError, self.run_script, '$ cd ..')
398
398
 
399
399
    def test_cd_dir_and_back_home(self):
400
 
        self.assertEquals(self.test_dir, osutils.getcwd())
 
400
        self.assertEqual(self.test_dir, osutils.getcwd())
401
401
        self.run_script("""
402
402
$ mkdir dir
403
403
$ cd dir
404
404
""")
405
 
        self.assertEquals(osutils.pathjoin(self.test_dir, 'dir'),
406
 
                          osutils.getcwd())
 
405
        self.assertEqual(osutils.pathjoin(self.test_dir, 'dir'),
 
406
                         osutils.getcwd())
407
407
 
408
408
        self.run_script('$ cd')
409
 
        self.assertEquals(self.test_dir, osutils.getcwd())
410
 
 
411
 
 
412
 
class TestBzr(script.TestCaseWithTransportAndScript):
413
 
 
414
 
    def test_bzr_smoke(self):
 
409
        self.assertEqual(self.test_dir, osutils.getcwd())
 
410
 
 
411
 
 
412
class TestBrz(script.TestCaseWithTransportAndScript):
 
413
 
 
414
    def test_brz_smoke(self):
415
415
        self.run_script("""
416
 
            $ bzr init branch
 
416
            $ brz init branch
417
417
            Created a standalone tree (format: ...)
418
418
            """)
419
419
        self.assertPathExists('branch')
435
435
 
436
436
    def test_echo_to_output(self):
437
437
        retcode, out, err = self.run_command(['echo'], None, '\n', None)
438
 
        self.assertEquals('\n', out)
439
 
        self.assertEquals(None, err)
 
438
        self.assertEqual('\n', out)
 
439
        self.assertEqual(None, err)
440
440
 
441
441
    def test_echo_some_to_output(self):
442
442
        retcode, out, err = self.run_command(['echo', 'hello'],
443
443
                                             None, 'hello\n', None)
444
 
        self.assertEquals('hello\n', out)
445
 
        self.assertEquals(None, err)
 
444
        self.assertEqual('hello\n', out)
 
445
        self.assertEqual(None, err)
446
446
 
447
447
    def test_echo_more_output(self):
448
448
        retcode, out, err = self.run_command(
449
449
            ['echo', 'hello', 'happy', 'world'],
450
450
            None, 'hello happy world\n', None)
451
 
        self.assertEquals('hello happy world\n', out)
452
 
        self.assertEquals(None, err)
 
451
        self.assertEqual('hello happy world\n', out)
 
452
        self.assertEqual(None, err)
453
453
 
454
454
    def test_echo_appended(self):
455
455
        retcode, out, err = self.run_command(['echo', 'hello', '>file'],
456
456
                                             None, None, None)
457
 
        self.assertEquals(None, out)
458
 
        self.assertEquals(None, err)
459
 
        self.assertFileEqual('hello\n', 'file')
 
457
        self.assertEqual(None, out)
 
458
        self.assertEqual(None, err)
 
459
        self.assertFileEqual(b'hello\n', 'file')
460
460
        retcode, out, err = self.run_command(['echo', 'happy', '>>file'],
461
461
                                             None, None, None)
462
 
        self.assertEquals(None, out)
463
 
        self.assertEquals(None, err)
464
 
        self.assertFileEqual('hello\nhappy\n', 'file')
 
462
        self.assertEqual(None, out)
 
463
        self.assertEqual(None, err)
 
464
        self.assertFileEqual(b'hello\nhappy\n', 'file')
465
465
 
466
466
    def test_empty_line_in_output_is_respected(self):
467
467
        self.run_script("""
558
558
 
559
559
    def run(self):
560
560
        if ui.ui_factory.get_boolean(
561
 
            u'Really do it',
562
 
            # 'bzrlib.tests.test_script.confirm',
563
 
            # {}
564
 
            ):
 
561
                u'Really do it',
 
562
                # 'breezy.tests.test_script.confirm',
 
563
                # {}
 
564
                ):
565
565
            self.outf.write('Do it!\n')
566
566
        else:
567
 
            print 'ok, no'
 
567
            print('ok, no')
568
568
 
569
569
 
570
570
class TestUserInteraction(script.TestCaseWithMemoryTransportAndScript):
571
571
 
572
572
    def test_confirm_action(self):
573
573
        """You can write tests that demonstrate user confirmation.
574
 
        
 
574
 
575
575
        Specifically, ScriptRunner does't care if the output line for the
576
576
        prompt isn't terminated by a newline from the program; it's implicitly
577
577
        terminated by the input.
578
578
        """
579
579
        commands.builtin_command_registry.register(cmd_test_confirm)
580
 
        self.addCleanup(commands.builtin_command_registry.remove, 'test-confirm')
 
580
        self.addCleanup(
 
581
            commands.builtin_command_registry.remove, 'test-confirm')
581
582
        self.run_script("""
582
 
            $ bzr test-confirm
 
583
            $ brz test-confirm
583
584
            2>Really do it? ([y]es, [n]o): yes
584
585
            <y
585
586
            Do it!
586
 
            $ bzr test-confirm
 
587
            $ brz test-confirm
587
588
            2>Really do it? ([y]es, [n]o): no
588
589
            <n
589
590
            ok, no
590
591
            """)
591
592
 
 
593
 
592
594
class TestShelve(script.TestCaseWithTransportAndScript):
593
595
 
594
596
    def setUp(self):
595
597
        super(TestShelve, self).setUp()
596
598
        self.run_script("""
597
 
            $ bzr init test
 
599
            $ brz init test
598
600
            Created a standalone tree (format: 2a)
599
601
            $ cd test
600
602
            $ echo foo > file
601
 
            $ bzr add
 
603
            $ brz add
602
604
            adding file
603
 
            $ bzr commit -m 'file added'
 
605
            $ brz commit -m 'file added'
604
606
            2>Committing to:...test/
605
607
            2>added file
606
608
            2>Committed revision 1.
609
611
 
610
612
    def test_shelve(self):
611
613
        self.run_script("""
612
 
            $ bzr shelve -m 'shelve bar'
 
614
            $ brz shelve -m 'shelve bar'
613
615
            2>Shelve? ([y]es, [N]o, [f]inish, [q]uit): yes
614
616
            <y
615
617
            2>Selected changes:
620
622
            """,
621
623
                        null_output_matches_anything=True)
622
624
        self.run_script("""
623
 
            $ bzr shelve --list
 
625
            $ brz shelve --list
624
626
              1: shelve bar
625
627
            """)
626
628
 
627
629
    def test_dont_shelve(self):
628
630
        # 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
 
631
        self.run_script((
 
632
            "$ brz shelve -m 'shelve bar'\n"
 
633
            "2>Shelve? ([y]es, [N]o, [f]inish, [q]uit): \n"
 
634
            "2>No changes to shelve.\n"
 
635
            ), null_output_matches_anything=True)
 
636
        self.run_script("""
 
637
            $ brz st
637
638
            modified:
638
639
              file
639
640
            """)