28
25
class TestSyntax(tests.TestCase):
30
27
def test_comment_is_ignored(self):
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("""
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'))
61
33
def test_simple_command(self):
62
self.assertEqual([(['cd', 'trunk'], None, None, None)],
34
self.assertEquals([(['cd', 'trunk'], None, None, None)],
63
35
script._script_to_commands('$ cd trunk'))
65
37
def test_command_with_single_quoted_param(self):
66
38
story = """$ bzr commit -m 'two words'"""
67
self.assertEqual([(['bzr', 'commit', '-m', "'two words'"],
39
self.assertEquals([(['bzr', 'commit', '-m', "'two words'"],
68
40
None, None, None)],
69
41
script._script_to_commands(story))
71
43
def test_command_with_double_quoted_param(self):
72
44
story = """$ bzr commit -m "two words" """
73
self.assertEqual([(['bzr', 'commit', '-m', '"two words"'],
45
self.assertEquals([(['bzr', 'commit', '-m', '"two words"'],
74
46
None, None, None)],
75
47
script._script_to_commands(story))
77
49
def test_command_with_input(self):
79
51
[(['cat', '>file'], 'content\n', None, None)],
80
52
script._script_to_commands('$ cat >file\n<content\n'))
107
79
2>bzr: ERROR: Not a branch: "foo"
109
self.assertEqual([(['bzr', 'branch', 'foo'],
81
self.assertEquals([(['bzr', 'branch', 'foo'],
110
82
None, None, 'bzr: ERROR: Not a branch: "foo"\n')],
111
83
script._script_to_commands(story))
161
133
class TestExecution(script.TestCaseWithTransportAndScript):
163
135
def test_unknown_command(self):
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):
136
self.assertRaises(SyntaxError, self.run_script, 'foo')
210
138
def test_stops_on_unexpected_output(self):
310
227
def test_cat_input_to_output(self):
311
228
retcode, out, err = self.run_command(['cat'],
312
229
'content\n', 'content\n', None)
313
self.assertEqual('content\n', out)
314
self.assertEqual(None, err)
230
self.assertEquals('content\n', out)
231
self.assertEquals(None, err)
316
233
def test_cat_file_to_output(self):
317
234
self.build_tree_contents([('file', 'content\n')])
318
235
retcode, out, err = self.run_command(['cat', 'file'],
319
236
None, 'content\n', None)
320
self.assertEqual('content\n', out)
321
self.assertEqual(None, err)
237
self.assertEquals('content\n', out)
238
self.assertEquals(None, err)
323
240
def test_cat_input_to_file(self):
324
241
retcode, out, err = self.run_command(['cat', '>file'],
325
242
'content\n', None, None)
326
243
self.assertFileEqual('content\n', 'file')
327
self.assertEqual(None, out)
328
self.assertEqual(None, err)
244
self.assertEquals(None, out)
245
self.assertEquals(None, err)
329
246
retcode, out, err = self.run_command(['cat', '>>file'],
330
247
'more\n', None, None)
331
248
self.assertFileEqual('content\nmore\n', 'file')
332
self.assertEqual(None, out)
333
self.assertEqual(None, err)
249
self.assertEquals(None, out)
250
self.assertEquals(None, err)
335
252
def test_cat_file_to_file(self):
336
253
self.build_tree_contents([('file', 'content\n')])
397
314
self.assertRaises(ValueError, self.run_script, '$ cd ..')
399
316
def test_cd_dir_and_back_home(self):
400
self.assertEqual(self.test_dir, osutils.getcwd())
317
self.assertEquals(self.test_dir, osutils.getcwd())
401
318
self.run_script("""
405
self.assertEqual(osutils.pathjoin(self.test_dir, 'dir'),
322
self.assertEquals(osutils.pathjoin(self.test_dir, 'dir'),
406
323
osutils.getcwd())
408
325
self.run_script('$ cd')
409
self.assertEqual(self.test_dir, osutils.getcwd())
326
self.assertEquals(self.test_dir, osutils.getcwd())
412
329
class TestBzr(script.TestCaseWithTransportAndScript):
414
331
def test_bzr_smoke(self):
417
Created a standalone tree (format: ...)
419
self.assertPathExists('branch')
332
self.run_script('$ bzr init branch')
333
self.failUnlessExists('branch')
422
336
class TestEcho(script.TestCaseWithMemoryTransportAndScript):
436
350
def test_echo_to_output(self):
437
351
retcode, out, err = self.run_command(['echo'], None, '\n', None)
438
self.assertEqual('\n', out)
439
self.assertEqual(None, err)
352
self.assertEquals('\n', out)
353
self.assertEquals(None, err)
441
355
def test_echo_some_to_output(self):
442
356
retcode, out, err = self.run_command(['echo', 'hello'],
443
357
None, 'hello\n', None)
444
self.assertEqual('hello\n', out)
445
self.assertEqual(None, err)
358
self.assertEquals('hello\n', out)
359
self.assertEquals(None, err)
447
361
def test_echo_more_output(self):
448
362
retcode, out, err = self.run_command(
449
363
['echo', 'hello', 'happy', 'world'],
450
364
None, 'hello happy world\n', None)
451
self.assertEqual('hello happy world\n', out)
452
self.assertEqual(None, err)
365
self.assertEquals('hello happy world\n', out)
366
self.assertEquals(None, err)
454
368
def test_echo_appended(self):
455
369
retcode, out, err = self.run_command(['echo', 'hello', '>file'],
456
370
None, None, None)
457
self.assertEqual(None, out)
458
self.assertEqual(None, err)
371
self.assertEquals(None, out)
372
self.assertEquals(None, err)
459
373
self.assertFileEqual('hello\n', 'file')
460
374
retcode, out, err = self.run_command(['echo', 'happy', '>>file'],
461
375
None, None, None)
462
self.assertEqual(None, out)
463
self.assertEqual(None, err)
376
self.assertEquals(None, out)
377
self.assertEquals(None, err)
464
378
self.assertFileEqual('hello\nhappy\n', 'file')
466
def test_empty_line_in_output_is_respected(self):
475
381
class TestRm(script.TestCaseWithTransportAndScript):
481
387
def test_rm_file(self):
482
388
self.run_script('$ echo content >file')
483
self.assertPathExists('file')
389
self.failUnlessExists('file')
484
390
self.run_script('$ rm file')
485
self.assertPathDoesNotExist('file')
391
self.failIfExists('file')
487
393
def test_rm_file_force(self):
488
self.assertPathDoesNotExist('file')
394
self.failIfExists('file')
489
395
self.run_script('$ rm -f file')
490
self.assertPathDoesNotExist('file')
396
self.failIfExists('file')
492
398
def test_rm_files(self):
493
399
self.run_script("""
494
400
$ echo content >file
495
401
$ echo content >file2
497
self.assertPathExists('file2')
403
self.failUnlessExists('file2')
498
404
self.run_script('$ rm file file2')
499
self.assertPathDoesNotExist('file2')
405
self.failIfExists('file2')
501
407
def test_rm_dir(self):
502
408
self.run_script('$ mkdir dir')
503
self.assertPathExists('dir')
409
self.failUnlessExists('dir')
504
410
self.run_script("""
506
412
2>rm: cannot remove 'dir': Is a directory
508
self.assertPathExists('dir')
414
self.failUnlessExists('dir')
510
416
def test_rm_dir_recursive(self):
511
417
self.run_script("""
515
self.assertPathDoesNotExist('dir')
421
self.failIfExists('dir')
518
424
class TestMv(script.TestCaseWithTransportAndScript):
525
431
def test_move_file(self):
526
432
self.run_script('$ echo content >file')
527
self.assertPathExists('file')
433
self.failUnlessExists('file')
528
434
self.run_script('$ mv file new_name')
529
self.assertPathDoesNotExist('file')
530
self.assertPathExists('new_name')
435
self.failIfExists('file')
436
self.failUnlessExists('new_name')
532
438
def test_move_unknown_file(self):
533
439
self.assertRaises(AssertionError,
549
455
$ echo content > file
551
457
self.run_script('$ mv file dir')
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)
458
self.failUnlessExists('dir')
459
self.failIfExists('file')
460
self.failUnlessExists('dir/file')