Skip to content

Commit

Permalink
fix: issue with console extras (#1787)
Browse files Browse the repository at this point in the history
  • Loading branch information
antazoey authored Dec 19, 2023
1 parent 351eebd commit 4be7183
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 5 deletions.
17 changes: 12 additions & 5 deletions src/ape_console/_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def import_extras_file(file_path) -> ModuleType:
return module


def load_console_extras(namespace: Dict[str, Any]) -> Dict[str, Any]:
def load_console_extras(**namespace: Any) -> Dict[str, Any]:
"""load and return namespace updates from ape_console_extras.py files if
they exist"""
global_extras = config.DATA_FOLDER.joinpath(CONSOLE_EXTRAS_FILENAME)
Expand Down Expand Up @@ -116,12 +116,15 @@ def console(project=None, verbose=None, extra_locals=None, embed=False):
namespace = {component: getattr(ape, component) for component in ape.__all__}
namespace["ape"] = ape

# NOTE: `ape_console_extras` only is meant to work with default namespace.
# Load extras before local namespace to avoid console extras receiving
# the wrong values for its arguments.
sys.path.insert(0, getcwd())
console_extras = load_console_extras(**namespace)

if extra_locals:
namespace.update(extra_locals)

sys.path.insert(0, getcwd())
console_extras = load_console_extras(namespace)

if console_extras:
namespace.update(console_extras)

Expand All @@ -133,13 +136,17 @@ def console(project=None, verbose=None, extra_locals=None, embed=False):
# Required for click.testing.CliRunner support.
embed = True

_launch_console(namespace, ipy_config, embed, banner)


def _launch_console(namespace: Dict, ipy_config: IPythonConfig, embed: bool, banner: str):
ipython_kwargs = {"user_ns": namespace, "config": ipy_config}
if embed:
IPython.embed(**ipython_kwargs, colors="Neutral", banner1=banner)
else:
ipy_config.TerminalInteractiveShell.colors = "Neutral"
ipy_config.TerminalInteractiveShell.banner1 = banner
console_config = cast(ConsoleConfig, ape.config.get_config("console"))
console_config = cast(ConsoleConfig, namespace["ape"].config.get_config("console"))
ipy_config.InteractiveShellApp.extensions.append("ape_console.plugin")
if console_config.plugins:
ipy_config.InteractiveShellApp.extensions.extend(console_config.plugins)
Expand Down
33 changes: 33 additions & 0 deletions tests/functional/test_console.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import pytest

from ape_console._cli import console


@pytest.fixture(autouse=True)
def mock_console(mocker):
"""Prevent console from actually launching."""
return mocker.patch("ape_console._cli._launch_console")


@pytest.fixture(autouse=True)
def mock_ape_console_extras(mocker):
"""Prevent actually loading console extras files."""
return mocker.patch("ape_console._cli.load_console_extras")


def test_console_extras_uses_ape_namespace(mocker, mock_console, mock_ape_console_extras):
"""
Test that if console is given extras, those are included in the console
but not as args to the extras files, as those files expect items from the
default ape namespace.
"""
accounts_custom = mocker.MagicMock()
extras = {"accounts": accounts_custom}
console(extra_locals=extras)

# Show extras file still load using Ape namespace.
actual = mock_ape_console_extras.call_args[1]
assert actual["accounts"] != accounts_custom

# Show the custom accounts do get used in console.
assert mock_console.call_args[0][0]["accounts"] == accounts_custom

0 comments on commit 4be7183

Please sign in to comment.