25
28
class TestSyntax(tests.TestCase):
27
30
def test_comment_is_ignored(self):
28
self.assertEquals([], script._script_to_commands('#comment\n'))
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'))
33
def test_comment_multiple_lines(self):
35
(['bar'], None, None, None),
37
script._script_to_commands("""
38
# this comment is ignored
44
def test_trim_blank_lines(self):
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
49
need special syntax to avoid them.
51
However we do want to be able to match commands that emit blank lines.
54
(['bar'], None, '\n', None),
56
script._script_to_commands("""
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'))
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))
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))
49
77
def test_command_with_input(self):
51
79
[(['cat', '>file'], 'content\n', None, None)],
52
80
script._script_to_commands('$ cat >file\n<content\n'))
82
def test_indented(self):
83
# scripts are commonly given indented within the test source code, and
84
# common indentation is stripped off
90
self.assertEqual([(['bzr', 'add'], None,
91
'adding file\nadding file2\n', None)],
92
script._script_to_commands(story))
54
94
def test_command_with_output(self):
60
self.assertEquals([(['bzr', 'add'], None,
100
self.assertEqual([(['bzr', 'add'], None,
61
101
'adding file\nadding file2\n', None)],
62
102
script._script_to_commands(story))
121
161
class TestExecution(script.TestCaseWithTransportAndScript):
123
163
def test_unknown_command(self):
124
self.assertRaises(SyntaxError, self.run_script, 'foo')
164
"""A clear error is reported for commands that aren't recognised
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
171
SyntaxError: Command not found "foo"
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")
177
def test_blank_output_mismatches_output(self):
178
"""If you give output, the output must actually be blank.
180
See <https://bugs.launchpad.net/bzr/+bug/637830>: previously blank
181
output was a wildcard. Now you must say ... if you want that.
183
self.assertRaises(AssertionError,
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"""
195
""", null_output_matches_anything=True)
197
def test_ellipsis_everything(self):
198
"""A simple ellipsis matches everything."""
204
def test_ellipsis_matches_empty(self):
126
210
def test_stops_on_unexpected_output(self):
215
310
def test_cat_input_to_output(self):
216
311
retcode, out, err = self.run_command(['cat'],
217
312
'content\n', 'content\n', None)
218
self.assertEquals('content\n', out)
219
self.assertEquals(None, err)
313
self.assertEqual('content\n', out)
314
self.assertEqual(None, err)
221
316
def test_cat_file_to_output(self):
222
317
self.build_tree_contents([('file', 'content\n')])
223
318
retcode, out, err = self.run_command(['cat', 'file'],
224
319
None, 'content\n', None)
225
self.assertEquals('content\n', out)
226
self.assertEquals(None, err)
320
self.assertEqual('content\n', out)
321
self.assertEqual(None, err)
228
323
def test_cat_input_to_file(self):
229
324
retcode, out, err = self.run_command(['cat', '>file'],
230
325
'content\n', None, None)
231
326
self.assertFileEqual('content\n', 'file')
232
self.assertEquals(None, out)
233
self.assertEquals(None, err)
327
self.assertEqual(None, out)
328
self.assertEqual(None, err)
234
329
retcode, out, err = self.run_command(['cat', '>>file'],
235
330
'more\n', None, None)
236
331
self.assertFileEqual('content\nmore\n', 'file')
237
self.assertEquals(None, out)
238
self.assertEquals(None, err)
332
self.assertEqual(None, out)
333
self.assertEqual(None, err)
240
335
def test_cat_file_to_file(self):
241
336
self.build_tree_contents([('file', 'content\n')])
302
397
self.assertRaises(ValueError, self.run_script, '$ cd ..')
304
399
def test_cd_dir_and_back_home(self):
305
self.assertEquals(self.test_dir, osutils.getcwd())
400
self.assertEqual(self.test_dir, osutils.getcwd())
306
401
self.run_script("""
310
self.assertEquals(osutils.pathjoin(self.test_dir, 'dir'),
405
self.assertEqual(osutils.pathjoin(self.test_dir, 'dir'),
311
406
osutils.getcwd())
313
408
self.run_script('$ cd')
314
self.assertEquals(self.test_dir, osutils.getcwd())
409
self.assertEqual(self.test_dir, osutils.getcwd())
317
412
class TestBzr(script.TestCaseWithTransportAndScript):
319
414
def test_bzr_smoke(self):
320
self.run_script('$ bzr init branch')
321
self.failUnlessExists('branch')
417
Created a standalone tree (format: ...)
419
self.assertPathExists('branch')
324
422
class TestEcho(script.TestCaseWithMemoryTransportAndScript):
338
436
def test_echo_to_output(self):
339
437
retcode, out, err = self.run_command(['echo'], None, '\n', None)
340
self.assertEquals('\n', out)
341
self.assertEquals(None, err)
438
self.assertEqual('\n', out)
439
self.assertEqual(None, err)
343
441
def test_echo_some_to_output(self):
344
442
retcode, out, err = self.run_command(['echo', 'hello'],
345
443
None, 'hello\n', None)
346
self.assertEquals('hello\n', out)
347
self.assertEquals(None, err)
444
self.assertEqual('hello\n', out)
445
self.assertEqual(None, err)
349
447
def test_echo_more_output(self):
350
448
retcode, out, err = self.run_command(
351
449
['echo', 'hello', 'happy', 'world'],
352
450
None, 'hello happy world\n', None)
353
self.assertEquals('hello happy world\n', out)
354
self.assertEquals(None, err)
451
self.assertEqual('hello happy world\n', out)
452
self.assertEqual(None, err)
356
454
def test_echo_appended(self):
357
455
retcode, out, err = self.run_command(['echo', 'hello', '>file'],
358
456
None, None, None)
359
self.assertEquals(None, out)
360
self.assertEquals(None, err)
457
self.assertEqual(None, out)
458
self.assertEqual(None, err)
361
459
self.assertFileEqual('hello\n', 'file')
362
460
retcode, out, err = self.run_command(['echo', 'happy', '>>file'],
363
461
None, None, None)
364
self.assertEquals(None, out)
365
self.assertEquals(None, err)
462
self.assertEqual(None, out)
463
self.assertEqual(None, err)
366
464
self.assertFileEqual('hello\nhappy\n', 'file')
466
def test_empty_line_in_output_is_respected(self):
369
475
class TestRm(script.TestCaseWithTransportAndScript):
375
481
def test_rm_file(self):
376
482
self.run_script('$ echo content >file')
377
self.failUnlessExists('file')
483
self.assertPathExists('file')
378
484
self.run_script('$ rm file')
379
self.failIfExists('file')
485
self.assertPathDoesNotExist('file')
381
487
def test_rm_file_force(self):
382
self.failIfExists('file')
488
self.assertPathDoesNotExist('file')
383
489
self.run_script('$ rm -f file')
384
self.failIfExists('file')
490
self.assertPathDoesNotExist('file')
386
492
def test_rm_files(self):
387
493
self.run_script("""
388
494
$ echo content >file
389
495
$ echo content >file2
391
self.failUnlessExists('file2')
497
self.assertPathExists('file2')
392
498
self.run_script('$ rm file file2')
393
self.failIfExists('file2')
499
self.assertPathDoesNotExist('file2')
395
501
def test_rm_dir(self):
396
502
self.run_script('$ mkdir dir')
397
self.failUnlessExists('dir')
503
self.assertPathExists('dir')
398
504
self.run_script("""
400
506
2>rm: cannot remove 'dir': Is a directory
402
self.failUnlessExists('dir')
508
self.assertPathExists('dir')
404
510
def test_rm_dir_recursive(self):
405
511
self.run_script("""
409
self.failIfExists('dir')
515
self.assertPathDoesNotExist('dir')
412
518
class TestMv(script.TestCaseWithTransportAndScript):
419
525
def test_move_file(self):
420
526
self.run_script('$ echo content >file')
421
self.failUnlessExists('file')
527
self.assertPathExists('file')
422
528
self.run_script('$ mv file new_name')
423
self.failIfExists('file')
424
self.failUnlessExists('new_name')
529
self.assertPathDoesNotExist('file')
530
self.assertPathExists('new_name')
426
532
def test_move_unknown_file(self):
427
533
self.assertRaises(AssertionError,
443
549
$ echo content > file
445
551
self.run_script('$ mv file dir')
446
self.failUnlessExists('dir')
447
self.failIfExists('file')
448
self.failUnlessExists('dir/file')
552
self.assertPathExists('dir')
553
self.assertPathDoesNotExist('file')
554
self.assertPathExists('dir/file')
557
class cmd_test_confirm(commands.Command):
560
if ui.ui_factory.get_boolean(
562
# 'bzrlib.tests.test_script.confirm',
565
self.outf.write('Do it!\n')
570
class TestUserInteraction(script.TestCaseWithMemoryTransportAndScript):
572
def test_confirm_action(self):
573
"""You can write tests that demonstrate user confirmation.
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.
579
commands.builtin_command_registry.register(cmd_test_confirm)
580
self.addCleanup(commands.builtin_command_registry.remove, 'test-confirm')
583
2>Really do it? ([y]es, [n]o): yes
587
2>Really do it? ([y]es, [n]o): no
592
class TestShelve(script.TestCaseWithTransportAndScript):
595
super(TestShelve, self).setUp()
598
Created a standalone tree (format: 2a)
603
$ bzr commit -m 'file added'
604
2>Committing to:...test/
606
2>Committed revision 1.
610
def test_shelve(self):
612
$ bzr shelve -m 'shelve bar'
613
2>Shelve? ([y]es, [N]o, [f]inish, [q]uit): yes
617
2>Shelve 1 change(s)? ([y]es, [N]o, [f]inish, [q]uit): yes
619
2>Changes shelved with id "1".
621
null_output_matches_anything=True)
627
def test_dont_shelve(self):
628
# 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)