Skip to content

Commit 3a7dff3

Browse files
committed
Made small tweak to do_py to improve testability
1 parent fafdc4b commit 3a7dff3

File tree

3 files changed

+9
-7
lines changed

3 files changed

+9
-7
lines changed

cmd2/cmd2.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2695,9 +2695,11 @@ def do_py(self, arg: str) -> bool:
26952695
Non-python commands can be issued with ``pyscript_name("your command")``.
26962696
Run python code from external script files with ``run("script.py")``
26972697
"""
2698-
from .pyscript_bridge import PyscriptBridge
2698+
from .pyscript_bridge import PyscriptBridge, CommandResult
26992699
if self._in_py:
2700-
self.perror("Recursively entering interactive Python consoles is not allowed.", traceback_war=False)
2700+
err = "Recursively entering interactive Python consoles is not allowed."
2701+
self.perror(err, traceback_war=False)
2702+
self._last_result = CommandResult('', err)
27012703
return False
27022704
self._in_py = True
27032705

tests/test_cmd2.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -220,13 +220,13 @@ def test_base_run_pyscript(base_app, capsys, request):
220220
out, err = capsys.readouterr()
221221
assert out == expected
222222

223-
def test_recursive_pyscript_not_allowed(base_app, capsys, request):
223+
def test_recursive_pyscript_not_allowed(base_app, request):
224224
test_dir = os.path.dirname(request.module.__file__)
225225
python_script = os.path.join(test_dir, 'scripts', 'recursive.py')
226-
expected = 'ERROR: Recursively entering interactive Python consoles is not allowed.\n'
226+
expected = 'Recursively entering interactive Python consoles is not allowed.'
227227

228228
run_cmd(base_app, "pyscript {}".format(python_script))
229-
out, err = capsys.readouterr()
229+
err = base_app._last_result.stderr
230230
assert err == expected
231231

232232
def test_pyscript_with_nonexist_file(base_app, capsys):

tests/test_pyscript.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ def ps_echo():
140140
('help', 'help.py'),
141141
('help media', 'help_media.py'),
142142
])
143-
def test_pyscript_help(ps_app, capsys, request, command, pyscript_file):
143+
def test_pyscript_help(ps_app, request, command, pyscript_file):
144144
test_dir = os.path.dirname(request.module.__file__)
145145
python_script = os.path.join(test_dir, 'pyscript', pyscript_file)
146146
expected = run_cmd(ps_app, command)
@@ -169,7 +169,7 @@ def test_pyscript_help(ps_app, capsys, request, command, pyscript_file):
169169
('foo 11 22 33 44 55 66 -ccc', 'foo3.py'),
170170
('bar 11 22', 'bar1.py'),
171171
])
172-
def test_pyscript_out(ps_app, capsys, request, command, pyscript_file):
172+
def test_pyscript_out(ps_app, request, command, pyscript_file):
173173
test_dir = os.path.dirname(request.module.__file__)
174174
python_script = os.path.join(test_dir, 'pyscript', pyscript_file)
175175
expected = run_cmd(ps_app, command)

0 commit comments

Comments
 (0)