15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25
from bzrlib.tests import script
25
from breezy.tests import script
28
28
class TestSyntax(tests.TestCase):
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'))
33
33
def test_comment_multiple_lines(self):
35
35
(['bar'], None, None, None),
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.
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.
51
51
However we do want to be able to match commands that emit blank lines.
54
54
(['bar'], None, '\n', None),
56
56
script._script_to_commands("""
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'))
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'"],
69
script._script_to_commands(story))
66
story = """$ brz commit -m 'two words'"""
67
self.assertEqual([(['brz', 'commit', '-m', "'two words'"],
69
script._script_to_commands(story))
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"'],
75
script._script_to_commands(story))
72
story = """$ brz commit -m "two words" """
73
self.assertEqual([(['brz', 'commit', '-m', '"two words"'],
75
script._script_to_commands(story))
77
77
def test_command_with_input(self):
79
79
[(['cat', '>file'], 'content\n', None, None)],
80
80
script._script_to_commands('$ cat >file\n<content\n'))
83
83
# scripts are commonly given indented within the test source code, and
84
84
# common indentation is stripped off
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))
94
94
def test_command_with_output(self):
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))
104
104
def test_command_with_error(self):
107
2>bzr: ERROR: Not a branch: "foo"
107
2>brz: ERROR: Not a branch: "foo"
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))
113
113
def test_input_without_command(self):
114
114
self.assertRaises(SyntaxError, script._script_to_commands, '<input')
119
119
def test_command_with_backquotes(self):
121
$ foo = `bzr file-id toto`
121
$ foo = `brz file-id toto`
123
self.assertEquals([(['foo', '=', '`bzr file-id toto`'],
125
script._script_to_commands(story))
123
self.assertEqual([(['foo', '=', '`brz file-id toto`'],
125
script._script_to_commands(story))
128
128
class TestRedirections(tests.TestCase):
143
143
self._check('foo', None, None, ['bar', 'baz'], ['bar', '<foo', 'baz'])
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'])
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+', [], ['<', '>', '>>'])
155
self._check(None, '', 'w+', [], ['>'])
156
self._check(None, '', 'a+', [], ['>>'])
157
self._check('>', '', 'a+', [], ['<', '>', '>>'])
161
160
class TestExecution(script.TestCaseWithTransportAndScript):
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")
177
176
def test_blank_output_mismatches_output(self):
178
177
"""If you give output, the output must actually be blank.
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.
183
182
self.assertRaises(AssertionError,
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"""
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)
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)
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)
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')
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')
348
347
def test_cat_bogus_input_file(self):
349
348
self.run_script("""
397
397
self.assertRaises(ValueError, self.run_script, '$ cd ..')
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("""
405
self.assertEquals(osutils.pathjoin(self.test_dir, 'dir'),
405
self.assertEqual(osutils.pathjoin(self.test_dir, 'dir'),
408
408
self.run_script('$ cd')
409
self.assertEquals(self.test_dir, osutils.getcwd())
412
class TestBzr(script.TestCaseWithTransportAndScript):
414
def test_bzr_smoke(self):
409
self.assertEqual(self.test_dir, osutils.getcwd())
412
class TestBrz(script.TestCaseWithTransportAndScript):
414
def test_brz_smoke(self):
415
415
self.run_script("""
417
417
Created a standalone tree (format: ...)
419
419
self.assertPathExists('branch')
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)
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)
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)
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')
466
466
def test_empty_line_in_output_is_respected(self):
467
467
self.run_script("""
560
560
if ui.ui_factory.get_boolean(
562
# 'bzrlib.tests.test_script.confirm',
562
# 'breezy.tests.test_script.confirm',
565
565
self.outf.write('Do it!\n')
570
570
class TestUserInteraction(script.TestCaseWithMemoryTransportAndScript):
572
572
def test_confirm_action(self):
573
573
"""You can write tests that demonstrate user confirmation.
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.
579
579
commands.builtin_command_registry.register(cmd_test_confirm)
580
self.addCleanup(commands.builtin_command_registry.remove, 'test-confirm')
581
commands.builtin_command_registry.remove, 'test-confirm')
581
582
self.run_script("""
583
584
2>Really do it? ([y]es, [n]o): yes
587
588
2>Really do it? ([y]es, [n]o): no
592
594
class TestShelve(script.TestCaseWithTransportAndScript):
595
597
super(TestShelve, self).setUp()
596
598
self.run_script("""
598
600
Created a standalone tree (format: 2a)
600
602
$ echo foo > file
603
$ bzr commit -m 'file added'
605
$ brz commit -m 'file added'
604
606
2>Committing to:...test/
606
608
2>Committed revision 1.
621
623
null_output_matches_anything=True)
622
624
self.run_script("""
627
629
def test_dont_shelve(self):
628
630
# We intentionally provide no input here to test EOF
630
$ bzr shelve -m 'shelve bar'
631
2>Shelve? ([y]es, [N]o, [f]inish, [q]uit):
632
2>No changes to shelve.
634
null_output_matches_anything=True)
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)