Skip to content

Commit 258ad43

Browse files
committed
Writing to stderr in default() and _report_disabled_command_usage() to make it easy to detect an error in pyscript
1 parent 677e8bb commit 258ad43

File tree

2 files changed

+20
-14
lines changed

2 files changed

+20
-14
lines changed

cmd2/cmd2.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2054,7 +2054,8 @@ def default(self, statement: Statement) -> Optional[bool]:
20542054

20552055
return self.do_shell(statement.command_and_args)
20562056
else:
2057-
self.poutput('*** {} is not a recognized command, alias, or macro\n'.format(statement.command))
2057+
self.perror('*** {} is not a recognized command, alias, or macro'.format(statement.command),
2058+
err_color=Fore.RESET, traceback_war=False)
20582059

20592060
def pseudo_raw_input(self, prompt: str) -> str:
20602061
"""Began life as a copy of cmd's cmdloop; like raw_input but
@@ -3706,7 +3707,7 @@ def _report_disabled_command_usage(self, *args, message_to_print: str, **kwargs)
37063707
:param message_to_print: the message reporting that the command is disabled
37073708
:param kwargs: not used
37083709
"""
3709-
self.poutput(message_to_print)
3710+
self.perror(message_to_print, err_color=Fore.RESET, traceback_war=False)
37103711

37113712
def cmdloop(self, intro: Optional[str] = None) -> None:
37123713
"""This is an outer wrapper around _cmdloop() which deals with extra features provided by cmd2.

tests/test_cmd2.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -292,9 +292,10 @@ def test_pyscript_requires_an_argument(base_app, capsys):
292292
assert "the following arguments are required: script_path" in err
293293

294294

295-
def test_base_error(base_app):
296-
out = run_cmd(base_app, 'meow')
297-
assert "is not a recognized command" in out[0]
295+
def test_base_error(base_app, capsys):
296+
run_cmd(base_app, 'meow')
297+
out, err = capsys.readouterr()
298+
assert "is not a recognized command" in err
298299

299300

300301
def test_base_load(base_app, request):
@@ -2199,23 +2200,27 @@ def disable_commands_app():
21992200
return app
22002201

22012202

2202-
def test_disable_and_enable_category(disable_commands_app):
2203+
def test_disable_and_enable_category(disable_commands_app, capsys):
22032204
# Disable the category
22042205
message_to_print = 'These commands are currently disabled'
22052206
disable_commands_app.disable_category(disable_commands_app.category_name, message_to_print)
22062207

22072208
# Make sure all the commands and help on those commands displays the message
2208-
out = run_cmd(disable_commands_app, 'has_help_func')
2209-
assert out == [message_to_print]
2209+
run_cmd(disable_commands_app, 'has_help_func')
2210+
out, err = capsys.readouterr()
2211+
assert err.startswith(message_to_print)
22102212

2211-
out = run_cmd(disable_commands_app, 'help has_help_func')
2212-
assert out == [message_to_print]
2213+
run_cmd(disable_commands_app, 'help has_help_func')
2214+
out, err = capsys.readouterr()
2215+
assert err.startswith(message_to_print)
22132216

2214-
out = run_cmd(disable_commands_app, 'has_no_help_func')
2215-
assert out == [message_to_print]
2217+
run_cmd(disable_commands_app, 'has_no_help_func')
2218+
out, err = capsys.readouterr()
2219+
assert err.startswith(message_to_print)
22162220

2217-
out = run_cmd(disable_commands_app, 'help has_no_help_func')
2218-
assert out == [message_to_print]
2221+
run_cmd(disable_commands_app, 'help has_no_help_func')
2222+
out, err = capsys.readouterr()
2223+
assert err.startswith(message_to_print)
22192224

22202225
visible_commands = disable_commands_app.get_visible_commands()
22212226
assert 'has_help_func' not in visible_commands

0 commit comments

Comments
 (0)