Skip to content

Commit 3c51b9f

Browse files
authored
Merge pull request #538 from python-cmd2/unit_tests
Fix pyscript related unit tests
2 parents c889064 + 3a7dff3 commit 3c51b9f

File tree

3 files changed

+22
-24
lines changed

3 files changed

+22
-24
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: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def _do_media_movies(self, args) -> None:
2020
if not args.command:
2121
self.do_help(['media movies'])
2222
else:
23-
print('media movies ' + str(args.__dict__))
23+
self.poutput('media movies ' + str(args.__dict__))
2424

2525
def _do_media_shows(self, args) -> None:
2626
if not args.command:
@@ -29,7 +29,7 @@ def _do_media_shows(self, args) -> None:
2929
if not args.command:
3030
self.do_help(['media shows'])
3131
else:
32-
print('media shows ' + str(args.__dict__))
32+
self.poutput('media shows ' + str(args.__dict__))
3333

3434
media_parser = argparse_completer.ACArgumentParser(prog='media')
3535

@@ -84,7 +84,7 @@ def do_media(self, args):
8484

8585
@with_argparser(foo_parser)
8686
def do_foo(self, args):
87-
print('foo ' + str(args.__dict__))
87+
self.poutput('foo ' + str(args.__dict__))
8888
if self._in_py:
8989
FooResult = namedtuple_with_defaults('FooResult',
9090
['counter', 'trueval', 'constval',
@@ -110,7 +110,7 @@ def do_bar(self, args):
110110
out += '{'
111111
for key in keys:
112112
out += "'{}':'{}'".format(key, arg_dict[key])
113-
print(out)
113+
self.poutput(out)
114114

115115

116116
@pytest.fixture
@@ -126,7 +126,7 @@ def __init__(self):
126126
self.pyscript_name = 'custom'
127127

128128
def do_echo(self, out):
129-
print(out)
129+
self.poutput(out)
130130

131131

132132
@pytest.fixture
@@ -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,16 +169,14 @@ 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)
175-
run_cmd(ps_app, command)
176-
expected, _ = capsys.readouterr()
175+
expected = run_cmd(ps_app, command)
176+
assert expected
177177

178-
assert len(expected) > 0
179-
run_cmd(ps_app, 'pyscript {}'.format(python_script))
180-
out, _ = capsys.readouterr()
181-
assert len(out) > 0
178+
out = run_cmd(ps_app, 'pyscript {}'.format(python_script))
179+
assert out
182180
assert out == expected
183181

184182

@@ -227,14 +225,12 @@ def test_pyscript_dir(ps_app, capsys, request, expected, pyscript_file):
227225
assert out == expected
228226

229227

230-
def test_pyscript_custom_name(ps_echo, capsys, request):
228+
def test_pyscript_custom_name(ps_echo, request):
231229
message = 'blah!'
232230

233231
test_dir = os.path.dirname(request.module.__file__)
234232
python_script = os.path.join(test_dir, 'pyscript', 'custom_echo.py')
235233

236-
run_cmd(ps_echo, 'pyscript {}'.format(python_script))
237-
expected, _ = capsys.readouterr()
238-
assert len(expected) > 0
239-
expected = expected.splitlines()
240-
assert message == expected[0]
234+
out = run_cmd(ps_echo, 'pyscript {}'.format(python_script))
235+
assert out
236+
assert message == out[0]

0 commit comments

Comments
 (0)