Skip to content

Commit 034ee38

Browse files
committed
Added unit test for stdout capture in pyscript
1 parent ca7f24b commit 034ee38

File tree

3 files changed

+39
-2
lines changed

3 files changed

+39
-2
lines changed

tests/pyscript/stdout_capture.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# flake8: noqa F821
2+
# This script demonstrates when output of a command finalization hook is captured by a pyscript app() call
3+
import sys
4+
5+
# The unit test framework passes in the string being printed by the command finalization hook
6+
hook_output = sys.argv[1]
7+
8+
# hook_output will not be captured because there are no nested calls to onecmd_plus_hooks
9+
res = app('help')
10+
if hook_output not in res.stdout:
11+
print("PASSED")
12+
else:
13+
print("FAILED")
14+
15+
# hook_output will be captured in the nested call to onecmd_plus_hooks that occurs in do_history()
16+
res = app('history -r -1')
17+
if hook_output in res.stdout:
18+
print("PASSED")
19+
else:
20+
print("FAILED")

tests/test_plugin.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,13 +205,13 @@ def cmdfinalization_hook(self, data: plugin.CommandFinalizationData) -> plugin.C
205205
return data
206206

207207
def cmdfinalization_hook_stop(self, data: cmd2.plugin.CommandFinalizationData) -> cmd2.plugin.CommandFinalizationData:
208-
"""A postparsing hook which requests application exit"""
208+
"""A command finalization hook which requests application exit"""
209209
self.called_cmdfinalization += 1
210210
data.stop = True
211211
return data
212212

213213
def cmdfinalization_hook_exception(self, data: cmd2.plugin.CommandFinalizationData) -> cmd2.plugin.CommandFinalizationData:
214-
"""A postparsing hook which raises an exception"""
214+
"""A command finalization hook which raises an exception"""
215215
self.called_cmdfinalization += 1
216216
raise ValueError
217217

tests/test_pyscript.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,16 @@
44
Unit/functional testing for pytest in cmd2
55
"""
66
import os
7+
from cmd2 import plugin
78

89
from .conftest import run_cmd
910

11+
HOOK_OUTPUT = "TEST_OUTPUT"
12+
13+
def cmdfinalization_hook(data: plugin.CommandFinalizationData) -> plugin.CommandFinalizationData:
14+
"""A cmdfinalization_hook hook which requests application exit"""
15+
print(HOOK_OUTPUT)
16+
return data
1017

1118
def test_pyscript_help(base_app, request):
1219
test_dir = os.path.dirname(request.module.__file__)
@@ -23,3 +30,13 @@ def test_pyscript_dir(base_app, request):
2330
out, err = run_cmd(base_app, 'pyscript {}'.format(python_script))
2431
assert out
2532
assert out[0] == "['cmd_echo']"
33+
34+
35+
def test_pyscript_stdout_capture(base_app, request):
36+
base_app.register_cmdfinalization_hook(cmdfinalization_hook)
37+
test_dir = os.path.dirname(request.module.__file__)
38+
python_script = os.path.join(test_dir, 'pyscript', 'stdout_capture.py')
39+
out, err = run_cmd(base_app, 'pyscript {} {}'.format(python_script, HOOK_OUTPUT))
40+
41+
assert out[0] == "PASSED"
42+
assert out[1] == "PASSED"

0 commit comments

Comments
 (0)