From efe5310dca1ff45b7e6a8d9f579fcef1406f242d Mon Sep 17 00:00:00 2001 From: Kevin Van Brunt Date: Fri, 14 Jun 2019 23:51:26 -0400 Subject: Renamed load to run_script and _relative_load to _relative_run_script --- cmd2/cmd2.py | 81 +++++++++++++++++++++++++++++++++++------------------------- 1 file changed, 48 insertions(+), 33 deletions(-) diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py index 7fcb38d6..301f1660 100644 --- a/cmd2/cmd2.py +++ b/cmd2/cmd2.py @@ -6,9 +6,9 @@ To use, simply import cmd2.Cmd instead of cmd.Cmd; use precisely as though you were using the standard library's cmd, while enjoying the extra features. Searchable command history (commands: "history") -Load commands from file, save to file, edit commands in file +Run commands from file, save to file, edit commands in file Multi-line commands -Special-character shortcut commands (beyond cmd's "@" and "!") +Special-character shortcut commands (beyond cmd's "?" and "!") Settable environment parameters Parsing commands with `argparse` argument parsers (flags) Redirection to file or paste buffer (clipboard) with > or >> @@ -327,7 +327,7 @@ class Cmd(cmd.Cmd): Line-oriented command interpreters are often useful for test harnesses, internal tools, and rapid prototypes. """ - DEFAULT_SHORTCUTS = {'?': 'help', '!': 'shell', '@': 'load', '@@': '_relative_load'} + DEFAULT_SHORTCUTS = {'?': 'help', '!': 'shell', '@': 'run_script', '@@': '_relative_run_script'} DEFAULT_EDITOR = utils.find_editor() def __init__(self, completekey: str = 'tab', stdin=None, stdout=None, *, @@ -343,7 +343,7 @@ class Cmd(cmd.Cmd): :param stdout: (optional) alternate output file object, if not specified, sys.stdout is used :param persistent_history_file: (optional) file path to load a persistent cmd2 command history from :param persistent_history_length: (optional) max number of history items to write to the persistent history file - :param startup_script: (optional) file path to a a script to load and execute at startup + :param startup_script: (optional) file path to a script to execute at startup :param use_ipython: (optional) should the "ipy" command be included for an embedded IPython shell :param allow_cli_args: (optional) if True, then cmd2 will process command line arguments as either commands to be run or, if -t is specified, transcript files to run. @@ -398,7 +398,7 @@ class Cmd(cmd.Cmd): 'timing': 'Report execution times'} # Commands to exclude from the help menu and tab completion - self.hidden_commands = ['eof', '_relative_load'] + self.hidden_commands = ['eof', '_relative_run_script'] # Commands to exclude from the history command # initialize history @@ -429,7 +429,8 @@ class Cmd(cmd.Cmd): # Built-in commands don't make use of this. It is purely there for user-defined commands and convenience. self._last_result = None - # Used load command to store the current script dir as a LIFO queue to support _relative_load command + # Used by run_script command to store the current script dir as + # a LIFO queue to support _relative_run_script command self._script_dir = [] # Context manager used to protect critical sections in the main thread from stopping due to a KeyboardInterrupt @@ -460,11 +461,11 @@ class Cmd(cmd.Cmd): # Commands that will run at the beginning of the command loop self._startup_commands = [] - # If a startup script is provided, then add it in the queue to load + # If a startup script is provided, then execute it in the startup commands if startup_script is not None: startup_script = os.path.abspath(os.path.expanduser(startup_script)) if os.path.exists(startup_script) and os.path.getsize(startup_script) > 0: - self._startup_commands.append("load '{}'".format(startup_script)) + self._startup_commands.append("run_script '{}'".format(startup_script)) # Transcript files to run instead of interactive command loop self._transcript_files = None @@ -3412,7 +3413,7 @@ class Cmd(cmd.Cmd): fobj.write('{}\n'.format(command.raw)) try: self.do_edit(fname) - return self.do_load(fname) + return self.do_run_script(fname) finally: os.remove(fname) elif args.output_file: @@ -3623,27 +3624,33 @@ class Cmd(cmd.Cmd): else: return None - load_description = ("Run commands in script file that is encoded as either ASCII or UTF-8 text\n" - "\n" - "Script should contain one command per line, just like the command would be\n" - "typed in the console.\n" - "\n" - "If the -r/--record_transcript flag is used, this command instead records\n" - "the output of the script commands to a transcript for testing purposes.\n" - ) + run_script_description = ("Run commands in script file that is encoded as either ASCII or UTF-8 text\n" + "\n" + "Script should contain one command per line, just like the command would be\n" + "typed in the console.\n" + "\n" + "If the -r/--record_transcript flag is used, this command instead records\n" + "the output of the script commands to a transcript for testing purposes.\n" + ) - load_parser = ACArgumentParser(description=load_description) - setattr(load_parser.add_argument('-t', '--transcript', help='record the output of the script as a transcript file'), + run_script_parser = ACArgumentParser(description=run_script_description) + setattr(run_script_parser.add_argument('-t', '--transcript', + help='record the output of the script as a transcript file'), ACTION_ARG_CHOICES, ('path_complete',)) - setattr(load_parser.add_argument('script_path', help="path to the script file"), + setattr(run_script_parser.add_argument('script_path', help="path to the script file"), ACTION_ARG_CHOICES, ('path_complete',)) - @with_argparser(load_parser) - def do_load(self, args: argparse.Namespace) -> Optional[bool]: + @with_argparser(run_script_parser) + def do_run_script(self, args: argparse.Namespace) -> Optional[bool]: """ Run commands in script file that is encoded as either ASCII or UTF-8 text :return: True if running of commands should stop """ + if args.__statement__.command == "load": + self.perror("load has been renamed and will be removed in the next release," + "please use run_script instead\n", + traceback_war=False, err_color=Fore.LIGHTYELLOW_EX) + expanded_path = os.path.abspath(os.path.expanduser(args.script_path)) # Make sure the path exists and we can access it @@ -3690,19 +3697,24 @@ class Cmd(cmd.Cmd): if orig_script_dir_count != len(self._script_dir): self._script_dir.pop() - relative_load_description = load_description - relative_load_description += ("\n\n" - "If this is called from within an already-running script, the filename will be\n" - "interpreted relative to the already-running script's directory.") + # load has been deprecated + do_load = do_run_script + + relative_run_script_description = run_script_description + relative_run_script_description += ( + "\n\n" + "If this is called from within an already-running script, the filename will be\n" + "interpreted relative to the already-running script's directory.") - relative_load_epilog = ("Notes:\n" - " This command is intended to only be used within text file scripts.") + relative_run_script_epilog = ("Notes:\n" + " This command is intended to only be used within text file scripts.") - relative_load_parser = ACArgumentParser(description=relative_load_description, epilog=relative_load_epilog) - relative_load_parser.add_argument('file_path', help='a file path pointing to a script') + relative_run_script_parser = ACArgumentParser(description=relative_run_script_description, + epilog=relative_run_script_epilog) + relative_run_script_parser.add_argument('file_path', help='a file path pointing to a script') - @with_argparser(relative_load_parser) - def do__relative_load(self, args: argparse.Namespace) -> Optional[bool]: + @with_argparser(relative_run_script_parser) + def do__relative_run_script(self, args: argparse.Namespace) -> Optional[bool]: """ Run commands in script file that is encoded as either ASCII or UTF-8 text :return: True if running of commands should stop @@ -3710,7 +3722,10 @@ class Cmd(cmd.Cmd): file_path = args.file_path # NOTE: Relative path is an absolute path, it is just relative to the current script directory relative_path = os.path.join(self._current_script_dir or '', file_path) - return self.do_load(relative_path) + return self.do_run_script(relative_path) + + # _relative_load has been deprecated + do__relative_load = do__relative_run_script def run_transcript_tests(self, transcript_paths: List[str]) -> None: """Runs transcript tests for provided file(s). -- cgit v1.2.1 From 8d882f529f564bbb03769e2454a73a01984dc11b Mon Sep 17 00:00:00 2001 From: Kevin Van Brunt Date: Fri, 14 Jun 2019 23:57:03 -0400 Subject: Renamed pyscript to run_pyscript --- cmd2/cmd2.py | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py index 301f1660..f9d6281f 100644 --- a/cmd2/cmd2.py +++ b/cmd2/cmd2.py @@ -3241,16 +3241,21 @@ class Cmd(cmd.Cmd): return bridge.stop - pyscript_parser = ACArgumentParser() - setattr(pyscript_parser.add_argument('script_path', help='path to the script file'), + run_pyscript_parser = ACArgumentParser() + setattr(run_pyscript_parser.add_argument('script_path', help='path to the script file'), ACTION_ARG_CHOICES, ('path_complete',)) - setattr(pyscript_parser.add_argument('script_arguments', nargs=argparse.REMAINDER, - help='arguments to pass to script'), + setattr(run_pyscript_parser.add_argument('script_arguments', nargs=argparse.REMAINDER, + help='arguments to pass to script'), ACTION_ARG_CHOICES, ('path_complete',)) - @with_argparser(pyscript_parser) - def do_pyscript(self, args: argparse.Namespace) -> bool: + @with_argparser(run_pyscript_parser) + def do_run_pyscript(self, args: argparse.Namespace) -> bool: """Run a Python script file inside the console""" + if args.__statement__.command == "pyscript": + self.perror("pyscript has been renamed and will be removed in the next release, " + "please use run_pyscript instead\n", + traceback_war=False, err_color=Fore.LIGHTYELLOW_EX) + script_path = os.path.expanduser(args.script_path) py_return = False @@ -3274,6 +3279,9 @@ class Cmd(cmd.Cmd): return py_return + # pyscript is deprecated + do_pyscript = do_run_pyscript + # Only include the do_ipy() method if IPython is available on the system if ipython_available: # pragma: no cover @with_argparser(ACArgumentParser()) @@ -3647,7 +3655,7 @@ class Cmd(cmd.Cmd): :return: True if running of commands should stop """ if args.__statement__.command == "load": - self.perror("load has been renamed and will be removed in the next release," + self.perror("load has been renamed and will be removed in the next release, " "please use run_script instead\n", traceback_war=False, err_color=Fore.LIGHTYELLOW_EX) -- cgit v1.2.1 From 122ff0c664383aff780902d66cb7e990aa8d810b Mon Sep 17 00:00:00 2001 From: Kevin Van Brunt Date: Sat, 15 Jun 2019 00:09:26 -0400 Subject: Fixing unit tests --- cmd2/cmd2.py | 2 +- tests/conftest.py | 10 ++++++---- tests/test_cmd2.py | 4 ++-- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py index f9d6281f..667e221d 100644 --- a/cmd2/cmd2.py +++ b/cmd2/cmd2.py @@ -398,7 +398,7 @@ class Cmd(cmd.Cmd): 'timing': 'Report execution times'} # Commands to exclude from the help menu and tab completion - self.hidden_commands = ['eof', '_relative_run_script'] + self.hidden_commands = ['eof', '_relative_load', '_relative_run_script'] # Commands to exclude from the history command # initialize history diff --git a/tests/conftest.py b/tests/conftest.py index 1e124219..d20d060a 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -28,8 +28,8 @@ except ImportError: # Help text for base cmd2.Cmd application BASE_HELP = """Documented commands (type help ): ======================================== -alias help load py quit shell -edit history macro pyscript set shortcuts +alias help load py quit run_script shell +edit history macro pyscript run_pyscript set shortcuts """ # noqa: W291 BASE_HELP_VERBOSE = """ @@ -44,6 +44,8 @@ macro Manage macros py Invoke Python command or shell pyscript Run a Python script file inside the console quit Exit this application +run_pyscript Run a Python script file inside the console +run_script Run commands in script file that is encoded as either ASCII or UTF-8 text set Set a settable parameter or show current settings of parameters shell Execute a command as if at the OS prompt shortcuts List available shortcuts @@ -89,8 +91,8 @@ formatting: SHORTCUTS_TXT = """Shortcuts for other commands: !: shell ?: help -@: load -@@: _relative_load +@: run_script +@@: _relative_run_script """ # Output from the show command with default settings diff --git a/tests/test_cmd2.py b/tests/test_cmd2.py index 72643308..f79eb983 100644 --- a/tests/test_cmd2.py +++ b/tests/test_cmd2.py @@ -1934,8 +1934,8 @@ def test_onecmd_raw_str_quit(outsim_app): def test_get_all_commands(base_app): # Verify that the base app has the expected commands commands = base_app.get_all_commands() - expected_commands = ['_relative_load', 'alias', 'edit', 'eof', 'help', 'history', 'load', 'macro', - 'py', 'pyscript', 'quit', 'set', 'shell', 'shortcuts'] + expected_commands = ['_relative_load', '_relative_run_script', 'alias', 'edit', 'eof', 'help', 'history', 'load', + 'macro', 'py', 'pyscript', 'quit', 'run_pyscript', 'run_script', 'set', 'shell', 'shortcuts'] assert commands == expected_commands def test_get_help_topics(base_app): -- cgit v1.2.1 From 255dd16c426a79531f4bb4a2f58e91c24bb2c2f0 Mon Sep 17 00:00:00 2001 From: Kevin Van Brunt Date: Sat, 15 Jun 2019 00:21:34 -0400 Subject: Updated comment --- examples/alias_startup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/alias_startup.py b/examples/alias_startup.py index b765e34c..052d1367 100755 --- a/examples/alias_startup.py +++ b/examples/alias_startup.py @@ -2,7 +2,7 @@ # coding=utf-8 """A simple example demonstrating the following: 1) How to add custom command aliases using the alias command - 2) How to load an initialization script at startup + 2) How to run an initialization script at startup """ import os import cmd2 -- cgit v1.2.1 From 710a9e23f0070e7548ad3405c73cf4a8b92610a6 Mon Sep 17 00:00:00 2001 From: Kevin Van Brunt Date: Sat, 15 Jun 2019 00:22:25 -0400 Subject: Fixed unit tests --- tests/scripts/recursive.py | 2 +- tests/test_cmd2.py | 45 ++++++++++++++++++++++++--------------------- 2 files changed, 25 insertions(+), 22 deletions(-) diff --git a/tests/scripts/recursive.py b/tests/scripts/recursive.py index 3359d31e..7d37e540 100644 --- a/tests/scripts/recursive.py +++ b/tests/scripts/recursive.py @@ -5,4 +5,4 @@ Example demonstrating that running a Python script recursively inside another Python script isn't allowed """ app.cmd_echo = True -app('pyscript ../script.py') +app('run_pyscript ../script.py') diff --git a/tests/test_cmd2.py b/tests/test_cmd2.py index f79eb983..6427a1af 100644 --- a/tests/test_cmd2.py +++ b/tests/test_cmd2.py @@ -258,31 +258,31 @@ def test_base_run_pyscript(base_app, capsys, request): python_script = os.path.join(test_dir, 'script.py') expected = 'This is a python script running ...' - out, err = run_cmd(base_app, "pyscript {}".format(python_script)) + out, err = run_cmd(base_app, "run_pyscript {}".format(python_script)) assert expected in out -def test_recursive_pyscript_not_allowed(base_app, request): +def test_recursive_run_pyscript_not_allowed(base_app, request): test_dir = os.path.dirname(request.module.__file__) python_script = os.path.join(test_dir, 'scripts', 'recursive.py') expected = 'Recursively entering interactive Python consoles is not allowed.' - out, err = run_cmd(base_app, "pyscript {}".format(python_script)) + out, err = run_cmd(base_app, "run_pyscript {}".format(python_script)) assert err[0] == expected -def test_pyscript_with_nonexist_file(base_app): +def test_run_pyscript_with_nonexist_file(base_app): python_script = 'does_not_exist.py' - out, err = run_cmd(base_app, "pyscript {}".format(python_script)) + out, err = run_cmd(base_app, "run_pyscript {}".format(python_script)) assert "Error opening script file" in err[0] -def test_pyscript_with_exception(base_app, request): +def test_run_pyscript_with_exception(base_app, request): test_dir = os.path.dirname(request.module.__file__) python_script = os.path.join(test_dir, 'scripts', 'raises_exception.py') - out, err = run_cmd(base_app, "pyscript {}".format(python_script)) + out, err = run_cmd(base_app, "run_pyscript {}".format(python_script)) assert err[0].startswith('Traceback') assert "TypeError: unsupported operand type(s) for +: 'int' and 'str'" in err[-1] -def test_pyscript_requires_an_argument(base_app): - out, err = run_cmd(base_app, "pyscript") +def test_run_pyscript_requires_an_argument(base_app): + out, err = run_cmd(base_app, "run_pyscript") assert "the following arguments are required: script_path" in err[1] @@ -950,8 +950,8 @@ def test_custom_help_menu(help_app): expected = normalize(""" Documented commands (type help ): ======================================== -alias help load py quit shell squat -edit history macro pyscript set shortcuts +alias help load py quit run_script shell squat +edit history macro pyscript run_pyscript set shortcuts Undocumented commands: ====================== @@ -1020,7 +1020,8 @@ cat_nodoc diddly Other ===== -alias help history load macro py pyscript quit set shell shortcuts +alias history macro pyscript run_pyscript set shortcuts +help load py quit run_script shell Undocumented commands: ====================== @@ -1052,6 +1053,8 @@ macro Manage macros py Invoke Python command or shell pyscript Run a Python script file inside the console quit Exit this application +run_pyscript Run a Python script file inside the console +run_script Run commands in script file that is encoded as either ASCII or UTF-8 text set Set a settable parameter or show current settings of parameters shell Execute a command as if at the OS prompt shortcuts List available shortcuts @@ -1600,7 +1603,7 @@ invalid_command_name = [ def test_get_alias_names(base_app): assert len(base_app.aliases) == 0 - run_cmd(base_app, 'alias create fake pyscript') + run_cmd(base_app, 'alias create fake run_pyscript') run_cmd(base_app, 'alias create ls !ls -hal') assert len(base_app.aliases) == 2 assert sorted(base_app.get_alias_names()) == ['fake', 'ls'] @@ -1621,7 +1624,7 @@ def test_alias_no_subcommand(base_app): def test_alias_create(base_app): # Create the alias - out, err = run_cmd(base_app, 'alias create fake pyscript') + out, err = run_cmd(base_app, 'alias create fake run_pyscript') assert out == normalize("Alias 'fake' created") # Use the alias @@ -1630,11 +1633,11 @@ def test_alias_create(base_app): # See a list of aliases out, err = run_cmd(base_app, 'alias list') - assert out == normalize('alias create fake pyscript') + assert out == normalize('alias create fake run_pyscript') # Look up the new alias out, err = run_cmd(base_app, 'alias list fake') - assert out == normalize('alias create fake pyscript') + assert out == normalize('alias create fake run_pyscript') def test_alias_create_with_quoted_value(base_app): """Demonstrate that quotes in alias value will be preserved (except for redirectors and terminators)""" @@ -1675,7 +1678,7 @@ def test_alias_list_invalid_alias(base_app): def test_alias_delete(base_app): # Create an alias - run_cmd(base_app, 'alias create fake pyscript') + run_cmd(base_app, 'alias create fake run_pyscript') # Delete the alias out, err = run_cmd(base_app, 'alias delete fake') @@ -1712,7 +1715,7 @@ def test_macro_no_subcommand(base_app): def test_macro_create(base_app): # Create the macro - out, err = run_cmd(base_app, 'macro create fake pyscript') + out, err = run_cmd(base_app, 'macro create fake run_pyscript') assert out == normalize("Macro 'fake' created") # Use the macro @@ -1721,11 +1724,11 @@ def test_macro_create(base_app): # See a list of macros out, err = run_cmd(base_app, 'macro list') - assert out == normalize('macro create fake pyscript') + assert out == normalize('macro create fake run_pyscript') # Look up the new macro out, err = run_cmd(base_app, 'macro list fake') - assert out == normalize('macro create fake pyscript') + assert out == normalize('macro create fake run_pyscript') def test_macro_create_with_quoted_value(base_app): """Demonstrate that quotes in macro value will be preserved (except for redirectors and terminators)""" @@ -1829,7 +1832,7 @@ def test_macro_list_invalid_macro(base_app): def test_macro_delete(base_app): # Create an macro - run_cmd(base_app, 'macro create fake pyscript') + run_cmd(base_app, 'macro create fake run_pyscript') # Delete the macro out, err = run_cmd(base_app, 'macro delete fake') -- cgit v1.2.1 From e4983ab320a63f0c43085360133a1bcfa0d67fdd Mon Sep 17 00:00:00 2001 From: Kevin Van Brunt Date: Sat, 15 Jun 2019 00:36:01 -0400 Subject: Fixing unit tests --- cmd2/cmd2.py | 5 +++ tests/relative_multiple.txt | 2 +- tests/scripts/nested.txt | 4 +-- tests/scripts/one_down.txt | 2 +- tests/test_cmd2.py | 84 +++++++++++++++++---------------------------- 5 files changed, 40 insertions(+), 57 deletions(-) diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py index 667e221d..93100c5d 100644 --- a/cmd2/cmd2.py +++ b/cmd2/cmd2.py @@ -3727,6 +3727,11 @@ class Cmd(cmd.Cmd): Run commands in script file that is encoded as either ASCII or UTF-8 text :return: True if running of commands should stop """ + if args.__statement__.command == "_relative_load": + self.perror("_relative_load has been renamed and will be removed in the next release, " + "please use _relative_run_script instead\n", + traceback_war=False, err_color=Fore.LIGHTYELLOW_EX) + file_path = args.file_path # NOTE: Relative path is an absolute path, it is just relative to the current script directory relative_path = os.path.join(self._current_script_dir or '', file_path) diff --git a/tests/relative_multiple.txt b/tests/relative_multiple.txt index bbd11739..a2d564ef 100644 --- a/tests/relative_multiple.txt +++ b/tests/relative_multiple.txt @@ -1 +1 @@ -_relative_load scripts/one_down.txt +_relative_run_script scripts/one_down.txt diff --git a/tests/scripts/nested.txt b/tests/scripts/nested.txt index ac3b4748..88b0a80c 100644 --- a/tests/scripts/nested.txt +++ b/tests/scripts/nested.txt @@ -1,4 +1,4 @@ -_relative_load precmds.txt +_relative_run_script precmds.txt help shortcuts -_relative_load postcmds.txt +_relative_run_script postcmds.txt diff --git a/tests/scripts/one_down.txt b/tests/scripts/one_down.txt index b87ff844..f92a061d 100644 --- a/tests/scripts/one_down.txt +++ b/tests/scripts/one_down.txt @@ -1 +1 @@ -_relative_load ../script.txt +_relative_run_script ../script.txt diff --git a/tests/test_cmd2.py b/tests/test_cmd2.py index 6427a1af..174df200 100644 --- a/tests/test_cmd2.py +++ b/tests/test_cmd2.py @@ -291,7 +291,7 @@ def test_base_error(base_app): assert "is not a recognized command" in err[0] -def test_base_load(base_app, request): +def test_base_run_script(base_app, request): test_dir = os.path.dirname(request.module.__file__) filename = os.path.join(test_dir, 'script.txt') @@ -299,7 +299,7 @@ def test_base_load(base_app, request): assert base_app._current_script_dir is None # Get output out the script - script_out, script_err = run_cmd(base_app, 'load {}'.format(filename)) + script_out, script_err = run_cmd(base_app, 'run_script {}'.format(filename)) assert base_app._script_dir == [] assert base_app._current_script_dir is None @@ -318,52 +318,32 @@ def test_base_load(base_app, request): assert script_out == manual_out assert script_err == manual_err -def test_load_with_empty_args(base_app): - # The way the load command works, we can't directly capture its stdout or stderr - out, err = run_cmd(base_app, 'load') - - # The load command requires a file path argument, so we should get an error message +def test_run_script_with_empty_args(base_app): + out, err = run_cmd(base_app, 'run_script') assert "the following arguments are required" in err[1] - -def test_load_with_nonexistent_file(base_app, capsys): - # The way the load command works, we can't directly capture its stdout or stderr - out, err = run_cmd(base_app, 'load does_not_exist.txt') - - # The load command requires a path to an existing file +def test_run_script_with_nonexistent_file(base_app, capsys): + out, err = run_cmd(base_app, 'run_script does_not_exist.txt') assert "does not exist" in err[0] -def test_load_with_directory(base_app, request): +def test_run_script_with_directory(base_app, request): test_dir = os.path.dirname(request.module.__file__) - - # The way the load command works, we can't directly capture its stdout or stderr - out, err = run_cmd(base_app, 'load {}'.format(test_dir)) - + out, err = run_cmd(base_app, 'run_script {}'.format(test_dir)) assert "is not a file" in err[0] -def test_load_with_empty_file(base_app, request): +def test_run_script_with_empty_file(base_app, request): test_dir = os.path.dirname(request.module.__file__) filename = os.path.join(test_dir, 'scripts', 'empty.txt') - - # The way the load command works, we can't directly capture its stdout or stderr - out, err = run_cmd(base_app, 'load {}'.format(filename)) - - # The load command requires non-empty script files + out, err = run_cmd(base_app, 'run_script {}'.format(filename)) assert "is empty" in err[0] - -def test_load_with_binary_file(base_app, request): +def test_run_script_with_binary_file(base_app, request): test_dir = os.path.dirname(request.module.__file__) filename = os.path.join(test_dir, 'scripts', 'binary.bin') - - # The way the load command works, we can't directly capture its stdout or stderr - out, err = run_cmd(base_app, 'load {}'.format(filename)) - - # The load command requires non-empty scripts files + out, err = run_cmd(base_app, 'run_script {}'.format(filename)) assert "is not an ASCII or UTF-8 encoded text file" in err[0] - -def test_load_with_utf8_file(base_app, request): +def test_run_script_with_utf8_file(base_app, request): test_dir = os.path.dirname(request.module.__file__) filename = os.path.join(test_dir, 'scripts', 'utf8.txt') @@ -371,7 +351,7 @@ def test_load_with_utf8_file(base_app, request): assert base_app._current_script_dir is None # Get output out the script - script_out, script_err = run_cmd(base_app, 'load {}'.format(filename)) + script_out, script_err = run_cmd(base_app, 'run_script {}'.format(filename)) assert base_app._script_dir == [] assert base_app._current_script_dir is None @@ -390,51 +370,49 @@ def test_load_with_utf8_file(base_app, request): assert script_out == manual_out assert script_err == manual_err - -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. +def test_run_script_nested_run_scripts(base_app, request): + # Verify that running a script with nested run_script commands works correctly, + # and runs the nested script commands in the correct order. test_dir = os.path.dirname(request.module.__file__) filename = os.path.join(test_dir, 'scripts', 'nested.txt') - # Load the top level script - initial_load = 'load ' + filename - run_cmd(base_app, initial_load) + # Run the top level script + initial_run = 'run_script ' + filename + run_cmd(base_app, initial_run) # Check that the right commands were executed. expected = """ %s -_relative_load precmds.txt +_relative_run_script precmds.txt set colors Always help shortcuts -_relative_load postcmds.txt -set colors Never""" % initial_load +_relative_run_script postcmds.txt +set colors Never""" % initial_run out, err = run_cmd(base_app, 'history -s') assert out == normalize(expected) - def test_base_runcmds_plus_hooks(base_app, request): 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') - base_app.runcmds_plus_hooks(['load ' + prefilepath, + base_app.runcmds_plus_hooks(['run_script ' + prefilepath, 'help', 'shortcuts', - 'load ' + postfilepath]) + 'run_script ' + postfilepath]) expected = """ -load %s +run_script %s set colors Always help shortcuts -load %s +run_script %s set colors Never""" % (prefilepath, postfilepath) out, err = run_cmd(base_app, 'history -s') assert out == normalize(expected) -def test_base_relative_load(base_app, request): +def test_base_relative_run_script(base_app, request): test_dir = os.path.dirname(request.module.__file__) filename = os.path.join(test_dir, 'script.txt') @@ -442,7 +420,7 @@ def test_base_relative_load(base_app, request): assert base_app._current_script_dir is None # Get output out the script - script_out, script_err = run_cmd(base_app, 'load {}'.format(filename)) + script_out, script_err = run_cmd(base_app, 'run_script {}'.format(filename)) assert base_app._script_dir == [] assert base_app._current_script_dir is None @@ -461,8 +439,8 @@ def test_base_relative_load(base_app, request): assert script_out == manual_out assert script_err == manual_err -def test_relative_load_requires_an_argument(base_app): - out, err = run_cmd(base_app, '_relative_load') +def test_relative_run_script_requires_an_argument(base_app): + out, err = run_cmd(base_app, '_relative_run_script') assert 'Error: the following arguments' in err[1] -- cgit v1.2.1 From 294cf8d4b2fd7254958c28211e5997bd45aa81cf Mon Sep 17 00:00:00 2001 From: Kevin Van Brunt Date: Sat, 15 Jun 2019 00:38:02 -0400 Subject: Fixed unit test --- tests/transcripts/from_cmdloop.txt | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/transcripts/from_cmdloop.txt b/tests/transcripts/from_cmdloop.txt index 84d7f8fc..76056fc6 100644 --- a/tests/transcripts/from_cmdloop.txt +++ b/tests/transcripts/from_cmdloop.txt @@ -5,8 +5,9 @@ Documented commands (type help ): ======================================== -alias help load mumble orate pyscript say shell speak/ */ -edit history macro nothing py quit set shortcuts/ */ +alias history mumble py run_pyscript set speak/ */ +edit load nothing pyscript run_script shell/ */ +help macro orate quit say shortcuts/ */ (Cmd) help say usage: speak [-h] [-p] [-s] [-r REPEAT]/ */ -- cgit v1.2.1 From df8679b3331a8aebd3af4bbe2a0c6e625346f348 Mon Sep 17 00:00:00 2001 From: Kevin Van Brunt Date: Sat, 15 Jun 2019 00:51:20 -0400 Subject: Fixed unit tests --- tests/test_completion.py | 2 +- tests/test_history.py | 2 +- tests/test_parsing.py | 6 +++--- tests/test_pyscript.py | 12 ++++++------ 4 files changed, 11 insertions(+), 11 deletions(-) diff --git a/tests/test_completion.py b/tests/test_completion.py index 0a16bc28..b70de7bc 100644 --- a/tests/test_completion.py +++ b/tests/test_completion.py @@ -149,7 +149,7 @@ def test_complete_exception(cmd2_app, capsys): def test_complete_macro(base_app, request): # Create the macro - out, err = run_cmd(base_app, 'macro create fake pyscript {1}') + out, err = run_cmd(base_app, 'macro create fake run_pyscript {1}') assert out == normalize("Macro 'fake' created") # Macros do path completion diff --git a/tests/test_history.py b/tests/test_history.py index a06bce87..973d8cff 100644 --- a/tests/test_history.py +++ b/tests/test_history.py @@ -275,7 +275,7 @@ def parser(): '42': 'theanswer', 'l': '!ls -al', 'anothermultiline': 'multiline', - 'fake': 'pyscript'}, + 'fake': 'run_pyscript'}, shortcuts=[('?', 'help'), ('!', 'shell')] ) return parser diff --git a/tests/test_parsing.py b/tests/test_parsing.py index 3bd635a1..13a535c0 100644 --- a/tests/test_parsing.py +++ b/tests/test_parsing.py @@ -20,7 +20,7 @@ def parser(): '42': 'theanswer', 'l': '!ls -al', 'anothermultiline': 'multiline', - 'fake': 'pyscript'}, + 'fake': 'run_pyscript'}, shortcuts=[('?', 'help'), ('!', 'shell')] ) return parser @@ -705,8 +705,8 @@ def test_parse_command_only_expands_alias(parser): assert statement == 'foobar.py "somebody.py' assert statement.args == statement assert statement.arg_list == [] - assert statement.command == 'pyscript' - assert statement.command_and_args == 'pyscript foobar.py "somebody.py' + assert statement.command == 'run_pyscript' + assert statement.command_and_args == 'run_pyscript foobar.py "somebody.py' assert statement.multiline_command == '' assert statement.raw == line assert statement.terminator == '' diff --git a/tests/test_pyscript.py b/tests/test_pyscript.py index 8da4b35a..aeed6fef 100644 --- a/tests/test_pyscript.py +++ b/tests/test_pyscript.py @@ -1,7 +1,7 @@ # coding=utf-8 # flake8: noqa E302 """ -Unit/functional testing for pytest in cmd2 +Unit/functional testing for run_pytest in cmd2 """ import os from cmd2 import plugin @@ -19,7 +19,7 @@ def test_pyscript_help(base_app, request): test_dir = os.path.dirname(request.module.__file__) python_script = os.path.join(test_dir, 'pyscript', 'help.py') out1, err1 = run_cmd(base_app, 'help') - out2, err2 = run_cmd(base_app, 'pyscript {}'.format(python_script)) + out2, err2 = run_cmd(base_app, 'run_pyscript {}'.format(python_script)) assert out1 and out1 == out2 @@ -27,7 +27,7 @@ def test_pyscript_dir(base_app, request): test_dir = os.path.dirname(request.module.__file__) python_script = os.path.join(test_dir, 'pyscript', 'pyscript_dir.py') - out, err = run_cmd(base_app, 'pyscript {}'.format(python_script)) + out, err = run_cmd(base_app, 'run_pyscript {}'.format(python_script)) assert out assert out[0] == "['cmd_echo']" @@ -36,7 +36,7 @@ def test_pyscript_stdout_capture(base_app, request): base_app.register_cmdfinalization_hook(cmdfinalization_hook) test_dir = os.path.dirname(request.module.__file__) python_script = os.path.join(test_dir, 'pyscript', 'stdout_capture.py') - out, err = run_cmd(base_app, 'pyscript {} {}'.format(python_script, HOOK_OUTPUT)) + out, err = run_cmd(base_app, 'run_pyscript {} {}'.format(python_script, HOOK_OUTPUT)) assert out[0] == "PASSED" assert out[1] == "PASSED" @@ -47,10 +47,10 @@ def test_pyscript_stop(base_app, request): # help.py doesn't run any commands that returns True for stop python_script = os.path.join(test_dir, 'pyscript', 'help.py') - stop = base_app.onecmd_plus_hooks('pyscript {}'.format(python_script)) + stop = base_app.onecmd_plus_hooks('run_pyscript {}'.format(python_script)) assert not stop # stop.py runs the quit command which does return True for stop python_script = os.path.join(test_dir, 'pyscript', 'stop.py') - stop = base_app.onecmd_plus_hooks('pyscript {}'.format(python_script)) + stop = base_app.onecmd_plus_hooks('run_pyscript {}'.format(python_script)) assert stop -- cgit v1.2.1 From beb2b37e5488965f7b2e240ea7453234bcbd3cd1 Mon Sep 17 00:00:00 2001 From: Kevin Van Brunt Date: Sat, 15 Jun 2019 00:53:23 -0400 Subject: Updated documentation --- examples/python_scripting.py | 4 ++-- examples/scripts/conditional.py | 2 +- examples/scripts/nested.txt | 6 +++--- examples/scripts/save_help_text.py | 2 +- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/examples/python_scripting.py b/examples/python_scripting.py index da7d0f6a..88bc2f98 100755 --- a/examples/python_scripting.py +++ b/examples/python_scripting.py @@ -9,8 +9,8 @@ learn to create scripts. However, there comes a time when technical end users want more capability and power. In particular it is common that users will want to create a script with conditional control flow - where the next command run will depend on the results -from the previous command. This is where the ability to run Python scripts inside a cmd2 application via the pyscript -command and the "pyscript