summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorKevin Van Brunt <kmvanbrunt@gmail.com>2019-04-23 23:38:06 -0400
committerKevin Van Brunt <kmvanbrunt@gmail.com>2019-04-23 23:38:06 -0400
commit034ee380d549cee507e80344e87bc016549750eb (patch)
tree556c808b2ad3d2c5004cfc2f9277865ee14eaab7 /tests
parentca7f24b238cdfd67adddec0146e7519352589484 (diff)
downloadcmd2-git-034ee380d549cee507e80344e87bc016549750eb.tar.gz
Added unit test for stdout capture in pyscript
Diffstat (limited to 'tests')
-rw-r--r--tests/pyscript/stdout_capture.py20
-rw-r--r--tests/test_plugin.py4
-rw-r--r--tests/test_pyscript.py17
3 files changed, 39 insertions, 2 deletions
diff --git a/tests/pyscript/stdout_capture.py b/tests/pyscript/stdout_capture.py
new file mode 100644
index 00000000..dfc39627
--- /dev/null
+++ b/tests/pyscript/stdout_capture.py
@@ -0,0 +1,20 @@
+# flake8: noqa F821
+# This script demonstrates when output of a command finalization hook is captured by a pyscript app() call
+import sys
+
+# The unit test framework passes in the string being printed by the command finalization hook
+hook_output = sys.argv[1]
+
+# hook_output will not be captured because there are no nested calls to onecmd_plus_hooks
+res = app('help')
+if hook_output not in res.stdout:
+ print("PASSED")
+else:
+ print("FAILED")
+
+# hook_output will be captured in the nested call to onecmd_plus_hooks that occurs in do_history()
+res = app('history -r -1')
+if hook_output in res.stdout:
+ print("PASSED")
+else:
+ print("FAILED")
diff --git a/tests/test_plugin.py b/tests/test_plugin.py
index 1f95017c..242b0d25 100644
--- a/tests/test_plugin.py
+++ b/tests/test_plugin.py
@@ -205,13 +205,13 @@ class Plugin:
return data
def cmdfinalization_hook_stop(self, data: cmd2.plugin.CommandFinalizationData) -> cmd2.plugin.CommandFinalizationData:
- """A postparsing hook which requests application exit"""
+ """A command finalization hook which requests application exit"""
self.called_cmdfinalization += 1
data.stop = True
return data
def cmdfinalization_hook_exception(self, data: cmd2.plugin.CommandFinalizationData) -> cmd2.plugin.CommandFinalizationData:
- """A postparsing hook which raises an exception"""
+ """A command finalization hook which raises an exception"""
self.called_cmdfinalization += 1
raise ValueError
diff --git a/tests/test_pyscript.py b/tests/test_pyscript.py
index 6981980b..4866548b 100644
--- a/tests/test_pyscript.py
+++ b/tests/test_pyscript.py
@@ -4,9 +4,16 @@
Unit/functional testing for pytest in cmd2
"""
import os
+from cmd2 import plugin
from .conftest import run_cmd
+HOOK_OUTPUT = "TEST_OUTPUT"
+
+def cmdfinalization_hook(data: plugin.CommandFinalizationData) -> plugin.CommandFinalizationData:
+ """A cmdfinalization_hook hook which requests application exit"""
+ print(HOOK_OUTPUT)
+ return data
def test_pyscript_help(base_app, request):
test_dir = os.path.dirname(request.module.__file__)
@@ -23,3 +30,13 @@ def test_pyscript_dir(base_app, request):
out, err = run_cmd(base_app, 'pyscript {}'.format(python_script))
assert out
assert out[0] == "['cmd_echo']"
+
+
+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))
+
+ assert out[0] == "PASSED"
+ assert out[1] == "PASSED"