diff options
author | Kevin Van Brunt <kmvanbrunt@gmail.com> | 2020-04-11 13:04:52 -0400 |
---|---|---|
committer | Kevin Van Brunt <kmvanbrunt@gmail.com> | 2020-04-11 13:04:52 -0400 |
commit | 2bac6bcda3b7c5b2ba7093955acf232dd89f9f0f (patch) | |
tree | 7f5e029e5a079faf14453655292d6ca9f56a6dee | |
parent | 0d4be64b6ec76fcf5f87933293dbc7c134a32cf0 (diff) | |
download | cmd2-git-2bac6bcda3b7c5b2ba7093955acf232dd89f9f0f.tar.gz |
Made some optional arguments with defaults keyword-only.
Added unit test for echo argument to pyscript app() command.
Removed _relative_load from hidden commands since that command was renamed.
-rw-r--r-- | cmd2/cmd2.py | 6 | ||||
-rw-r--r-- | cmd2/py_bridge.py | 11 | ||||
-rw-r--r-- | cmd2/utils.py | 2 | ||||
-rw-r--r-- | tests/pyscript/echo.py | 9 | ||||
-rw-r--r-- | tests/test_run_pyscript.py | 9 |
5 files changed, 27 insertions, 10 deletions
diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py index a8661e3c..de7e7f5a 100644 --- a/cmd2/cmd2.py +++ b/cmd2/cmd2.py @@ -213,7 +213,7 @@ class Cmd(cmd.Cmd): self.self_in_py = False # Commands to exclude from the help menu and tab completion - self.hidden_commands = ['eof', '_relative_load', '_relative_run_script'] + self.hidden_commands = ['eof', '_relative_run_script'] # Initialize history self._persistent_history_length = persistent_history_length @@ -258,7 +258,7 @@ class Cmd(cmd.Cmd): self.sigint_protection = utils.ContextFlag() # If the current command created a process to pipe to, then this will be a ProcReader object. - # Otherwise it will be None. Its used to know when a pipe process can be killed and/or waited upon. + # Otherwise it will be None. It's used to know when a pipe process can be killed and/or waited upon. self._cur_pipe_proc_reader = None # Used to keep track of whether we are redirecting or piping output @@ -3904,7 +3904,7 @@ class Cmd(cmd.Cmd): alert_msg += '\n' update_terminal = True - # Set the prompt if its changed + # Set the prompt if it's changed if new_prompt is not None and new_prompt != self.prompt: self.prompt = new_prompt diff --git a/cmd2/py_bridge.py b/cmd2/py_bridge.py index 6624d7ad..0dc04ca6 100644 --- a/cmd2/py_bridge.py +++ b/cmd2/py_bridge.py @@ -74,25 +74,26 @@ class PyBridge: attributes.insert(0, 'cmd_echo') return attributes - def __call__(self, command: str, echo: Optional[bool] = None) -> CommandResult: + def __call__(self, command: str, *, echo: Optional[bool] = None) -> CommandResult: """ Provide functionality to call application commands by calling PyBridge ex: app('help') :param command: command line being run - :param echo: if True, output will be echoed to stdout/stderr while the command runs - this temporarily overrides the value of self.cmd_echo + :param echo: If provided, this temporarily overrides the value of self.cmd_echo while the + command runs. If True, output will be echoed to stdout/stderr. (Defaults to None) + """ if echo is None: echo = self.cmd_echo # This will be used to capture _cmd2_app.stdout and sys.stdout - copy_cmd_stdout = StdSim(self._cmd2_app.stdout, echo) + copy_cmd_stdout = StdSim(self._cmd2_app.stdout, echo=echo) # Pause the storing of stdout until onecmd_plus_hooks enables it copy_cmd_stdout.pause_storage = True # This will be used to capture sys.stderr - copy_stderr = StdSim(sys.stderr, echo) + copy_stderr = StdSim(sys.stderr, echo=echo) self._cmd2_app.last_result = None diff --git a/cmd2/utils.py b/cmd2/utils.py index 8b5e9cc8..03ede2a3 100644 --- a/cmd2/utils.py +++ b/cmd2/utils.py @@ -434,7 +434,7 @@ class StdSim: Class to simulate behavior of sys.stdout or sys.stderr. Stores contents in internal buffer and optionally echos to the inner stream it is simulating. """ - def __init__(self, inner_stream, echo: bool = False, + def __init__(self, inner_stream, *, echo: bool = False, encoding: str = 'utf-8', errors: str = 'replace') -> None: """ StdSim Initializer diff --git a/tests/pyscript/echo.py b/tests/pyscript/echo.py new file mode 100644 index 00000000..d95e19db --- /dev/null +++ b/tests/pyscript/echo.py @@ -0,0 +1,9 @@ +# flake8: noqa F821 +# Tests echo argument to app() +app.cmd_echo = False + +# echo defaults to current setting which is False, so this help text should not be echoed to pytest's stdout +app('help alias') + +# pytest's stdout should have this help text written to it +app('help edit', echo=True) diff --git a/tests/test_run_pyscript.py b/tests/test_run_pyscript.py index 811fd688..f050f508 100644 --- a/tests/test_run_pyscript.py +++ b/tests/test_run_pyscript.py @@ -91,7 +91,6 @@ def test_run_pyscript_dir(base_app, request): python_script = os.path.join(test_dir, 'pyscript', 'pyscript_dir.py') out, err = run_cmd(base_app, 'run_pyscript {}'.format(python_script)) - assert out assert out[0] == "['cmd_echo']" def test_run_pyscript_stdout_capture(base_app, request): @@ -123,3 +122,11 @@ def test_run_pyscript_environment(base_app, request): out, err = run_cmd(base_app, 'run_pyscript {}'.format(python_script)) assert out[0] == "PASSED" + +def test_run_pyscript_echp(base_app, request): + test_dir = os.path.dirname(request.module.__file__) + python_script = os.path.join(test_dir, 'pyscript', 'echo.py') + out, err = run_cmd(base_app, 'run_pyscript {}'.format(python_script)) + + # Only the edit help text should have been echoed to pytest's stdout + assert out[0] == "Usage: edit [-h] [file_path]" |