diff options
author | Todd Leonhardt <todd.leonhardt@gmail.com> | 2017-09-22 15:48:23 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-09-22 15:48:23 -0400 |
commit | b49ec49efa5fe8c87f4447d4f821c41ff9a20325 (patch) | |
tree | 183b8d633867ac441934a657b76148da5bbc83a5 /tests/test_cmd2.py | |
parent | 7530674e171a57adc599740fb68eeac0313ee2ae (diff) | |
parent | 532bb1c185896e3564f858057517bbbd408dc0cb (diff) | |
download | cmd2-git-b49ec49efa5fe8c87f4447d4f821c41ff9a20325.tar.gz |
Merge pull request #227 from alevy03/support-load-in-onecmd_plus_hooks
Provide method to run multiple commands w/o a cmdloop.
Diffstat (limited to 'tests/test_cmd2.py')
-rw-r--r-- | tests/test_cmd2.py | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/tests/test_cmd2.py b/tests/test_cmd2.py index 11b03163..0ea3db4d 100644 --- a/tests/test_cmd2.py +++ b/tests/test_cmd2.py @@ -401,6 +401,61 @@ def test_load_with_utf8_file(base_app, capsys, request): assert base_app._current_script_dir == sdir +def test_load_nested_loads(base_app, request): + # Verify that loading a script with nested load commands works correctly, + # and loads the nested script commands in the correct order. The recursive + # loads don't happen all at once, but as the commands are interpreted. So, + # we will need to drain the cmdqueue and inspect the stdout to see if all + # steps were executed in the expected order. + test_dir = os.path.dirname(request.module.__file__) + filename = os.path.join(test_dir, 'scripts', 'nested.txt') + assert base_app.cmdqueue == [] + + # Load the top level script and then run the command queue until all + # commands have been exhausted. + initial_load = 'load ' + filename + run_cmd(base_app, initial_load) + while base_app.cmdqueue: + base_app.onecmd_plus_hooks(base_app.cmdqueue.pop(0)) + + # Check that the right commands were executed. + expected = """ +%s +_relative_load precmds.txt +set abbrev on +set colors on +help +shortcuts +_relative_load postcmds.txt +set abbrev off +set colors off""" % initial_load + assert run_cmd(base_app, 'history -s') == normalize(expected) + + +def test_base_runcmds_plus_hooks(base_app, request): + # Make sure that runcmds_plus_hooks works as intended. I.E. to run multiple + # commands and process any commands added, by them, to the command queue. + test_dir = os.path.dirname(request.module.__file__) + prefilepath = os.path.join(test_dir, 'scripts', 'precmds.txt') + postfilepath = os.path.join(test_dir, 'scripts', 'postcmds.txt') + assert base_app.cmdqueue == [] + + base_app.runcmds_plus_hooks(['load ' + prefilepath, + 'help', + 'shortcuts', + 'load ' + postfilepath]) + expected = """ +load %s +set abbrev on +set colors on +help +shortcuts +load %s +set abbrev off +set colors off""" % (prefilepath, postfilepath) + assert run_cmd(base_app, 'history -s') == normalize(expected) + + def test_base_relative_load(base_app, request): test_dir = os.path.dirname(request.module.__file__) filename = os.path.join(test_dir, 'script.txt') |