Skip to content

Commit b4ce107

Browse files
authored
Merge pull request #157 from python-cmd2/default_to_shell
Fixed a bug where an extraneous line was printed at start sometimes
2 parents 081dc25 + dbc1640 commit b4ce107

2 files changed

Lines changed: 50 additions & 2 deletions

File tree

cmd2.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ def new_func(instance, arg):
305305

306306
# Can we access the clipboard? Should always be true on Windows and Mac, but only sometimes on Linux
307307
try:
308-
pyperclip.paste()
308+
_ = pyperclip.paste()
309309
except pyperclip.exceptions.PyperclipException:
310310
can_clip = False
311311
else:

tests/test_cmd2.py

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -699,7 +699,6 @@ def postparsing_precmd(self, statement):
699699
"""Simulate precmd hook failure."""
700700
return True, statement
701701

702-
703702
@pytest.fixture
704703
def hook_failure():
705704
app = HookFailureApp()
@@ -714,3 +713,52 @@ def test_precmd_hook_success(base_app):
714713
def test_precmd_hook_failure(hook_failure):
715714
out = hook_failure.onecmd_plus_hooks('help')
716715
assert out == True
716+
717+
718+
class ShellApp(cmd2.Cmd):
719+
def __init__(self, *args, **kwargs):
720+
# Need to use this older form of invoking super class constructor to support Python 2.x and Python 3.x
721+
cmd2.Cmd.__init__(self, *args, **kwargs)
722+
self.default_to_shell = True
723+
724+
@pytest.fixture
725+
def shell_app():
726+
app = ShellApp()
727+
app.stdout = StdOut()
728+
return app
729+
730+
@pytest.mark.skipif(sys.platform == 'win32' or sys.platform.startswith('linux'),
731+
reason="Unit test passes when I run it, but fails on TravisCI and AppVeyor machines")
732+
def test_default_to_shell_found(shell_app):
733+
out = run_cmd(shell_app, 'echo Hello')
734+
assert out == []
735+
736+
def test_default_to_shell_unknown(shell_app):
737+
unknown_command = 'zyxcw23'
738+
out = run_cmd(shell_app, unknown_command)
739+
assert out == ["*** Unknown syntax: {}".format(unknown_command)]
740+
741+
742+
def test_ansi_prompt_not_esacped(base_app):
743+
prompt = '(Cmd) '
744+
assert base_app._surround_ansi_escapes(prompt) == prompt
745+
746+
747+
def test_ansi_prompt_escaped():
748+
app = cmd2.Cmd()
749+
color = 'cyan'
750+
prompt = 'InColor'
751+
color_prompt = app.colorize(prompt, color)
752+
753+
readline_hack_start = "\x01"
754+
readline_hack_end = "\x02"
755+
756+
readline_safe_prompt = app._surround_ansi_escapes(color_prompt)
757+
if sys.platform.startswith('win'):
758+
# colorize() does nothing on Windows due to lack of ANSI color support
759+
assert prompt == color_prompt
760+
assert readline_safe_prompt == prompt
761+
else:
762+
assert prompt != color_prompt
763+
assert readline_safe_prompt.startswith(readline_hack_start + app._colorcodes[color][True] + readline_hack_end)
764+
assert readline_safe_prompt.endswith(readline_hack_start + app._colorcodes[color][False] + readline_hack_end)

0 commit comments

Comments
 (0)