Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exit console via continue during pytest interactive debugging #989

Merged
merged 2 commits into from
Mar 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion brownie/_cli/console.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class Console(code.InteractiveConsole):
# replaced with `prompt_toolkit.input.defaults.create_pipe_input`
prompt_input = None

def __init__(self, project=None, extra_locals=None):
def __init__(self, project=None, extra_locals=None, exit_on_continue=False):
"""
Launch the Brownie console.

Expand All @@ -95,6 +95,9 @@ def __init__(self, project=None, extra_locals=None):
Active Brownie project to include in the console's local namespace.
extra_locals: dict, optional
Additional variables to add to the console namespace.
exit_on_continue: bool, optional
If True, the `continue` command causes the console to
raise a SystemExit with error message "continue".
"""
console_settings = CONFIG.settings["console"]

Expand All @@ -103,6 +106,11 @@ def __init__(self, project=None, extra_locals=None):
_dir=dir, dir=self._dir, exit=_Quitter("exit"), quit=_Quitter("quit"), _console=self
)

self.exit_on_continue = exit_on_continue
if exit_on_continue:
# add continue to the locals so we can quickly reach it via completion hints
locals_dict["continue"] = True

if project:
project._update_and_register(locals_dict)

Expand Down Expand Up @@ -218,6 +226,10 @@ def runsource(self, source, filename="<input>", symbol="single"):
mode = self.compile_mode
self.compile_mode = "single"

if source == "continue" and self.exit_on_continue:
# used to differentiate exit and continue for pytest interactive debugging
raise SystemExit("continue")

try:
code = self.compile(source, filename, mode)
except (OverflowError, SyntaxError, ValueError):
Expand Down
14 changes: 8 additions & 6 deletions brownie/test/managers/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -466,13 +466,15 @@ def pytest_exception_interact(self, report, call):

try:
CONFIG.argv["cli"] = "console"
shell = Console(self.project, extra_locals=namespace)
shell.interact(
banner="\nInteractive mode enabled. Use quit() to continue running tests.",
exitmsg="",
shell = Console(self.project, extra_locals=namespace, exit_on_continue=True)
banner = (
"\nInteractive mode enabled. Type `continue` to"
" resume testing or `quit()` to halt execution."
)
except SystemExit:
pass
shell.interact(banner=banner, exitmsg="")
except SystemExit as exc:
if exc.code != "continue":
pytest.exit("Test execution halted due to SystemExit")
finally:
CONFIG.argv["cli"] = "test"

Expand Down