Skip to content

Commit

Permalink
fix: issue when reassigned project to something else during excepti…
Browse files Browse the repository at this point in the history
…on in console (ApeWorX#2082)
  • Loading branch information
antazoey committed May 9, 2024
1 parent d2cdb0d commit e5fbdaf
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
13 changes: 11 additions & 2 deletions src/ape_console/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
from ape._cli import cli
from ape.exceptions import Abort, ApeException, handle_ape_exception
from ape.logging import logger
from ape.managers import ProjectManager
from ape.types import AddressType
from ape.utils import cached_property
from ape.utils import ManagerAccessMixin, cached_property


@magics_class
Expand Down Expand Up @@ -75,7 +76,15 @@ def bal(self, line: str = ""):


def custom_exception_handler(self, etype, value, tb, tb_offset=None):
if not handle_ape_exception(value, [self.user_ns["project"].path]):
project = self.user_ns["project"]
if isinstance(project, ProjectManager):
path = project.path
else:
# This happens if assigned the variable `project` in your session
# to something other than ``ape.project``.
path = ManagerAccessMixin.project_manager.path

if not handle_ape_exception(value, [path]):
logger.error(Abort.from_ape_exception(value).format_message())


Expand Down
24 changes: 23 additions & 1 deletion tests/functional/test_console.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
import pytest

from ape import Project
from ape.utils import create_tempdir
from ape.utils import ManagerAccessMixin, create_tempdir
from ape_console._cli import console
from ape_console.plugin import custom_exception_handler


@pytest.fixture(autouse=True)
Expand Down Expand Up @@ -51,3 +52,24 @@ def test_console_custom_project(mock_console, mock_ape_console_extras):

# Ensure sys.path was updated correctly.
assert sys.path[0] == str(project.path)


def test_custom_exception_handler_handles_non_ape_project(mocker):
"""
If the user has assigned the variable ``project`` to something else
in their active ``ape console`` session, the exception handler
**SHOULD NOT** attempt to use its ``.path``.
"""
session = mocker.MagicMock()
session.user_ns = {"project": 123} # Like doing `project = 123` in a console.

err = Exception()

handler_patch = mocker.patch("ape_console.plugin.handle_ape_exception")

# Execute - this was failing before the fix.
custom_exception_handler(session, None, err, None)

# We are expecting the local project's path in the handler.
expected_path = ManagerAccessMixin.project_manager.path
handler_patch.assert_called_once_with(err, [expected_path])

0 comments on commit e5fbdaf

Please sign in to comment.