Skip to content
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
6 changes: 3 additions & 3 deletions src/poetry/console/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,11 +251,11 @@ def _run(self, io: IO) -> int:
# display the error cleanly unless the user uses verbose or debug
self._configure_global_options(io)

self._load_plugins(io)
with directory(self._working_directory):
self._load_plugins(io)

exit_code: int = 1
exit_code: int = 1

with directory(self._working_directory):
try:
exit_code = super()._run(io)
except PoetryRuntimeError as e:
Expand Down
78 changes: 68 additions & 10 deletions tests/console/test_application_global_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from pathlib import Path
from typing import TYPE_CHECKING
from typing import ClassVar

import pytest

Expand All @@ -13,7 +14,10 @@
from cleo.testers.application_tester import ApplicationTester

from poetry.console.application import Application
from poetry.console.commands.command import Command
from poetry.console.commands.version import VersionCommand
from poetry.plugins import ApplicationPlugin
from tests.helpers import mock_metadata_entry_points
from tests.helpers import switch_working_directory


Expand All @@ -27,11 +31,58 @@
NO_PYPROJECT_TOML_ERROR = "Poetry could not find a pyproject.toml file in"


class CheckProjectPathCommand(Command):
name = "check-project-path"

description = "Check Project Path Command"

def handle(self) -> int:
if not self.poetry.pyproject_path.exists():
raise RuntimeError(
f"Wrong project path in handle: {self.poetry.pyproject_path}\nWorking directory: {Path.cwd()}"
)

return 0


class EarlyPoetryAccessPlugin(ApplicationPlugin):
commands: ClassVar[list[type[Command]]] = [CheckProjectPathCommand]

def activate(self, application: Application) -> None:
super().activate(application)

# access application.poetry
# see https://github.com/nat-n/poethepoet/issues/288
if not application.poetry.pyproject_path.exists():
raise RuntimeError(
f"Wrong project path in activate: {application.poetry.pyproject_path}\nWorking directory: {Path.cwd()}"
)


@pytest.fixture
def with_early_poetry_access_plugin(mocker: MockerFixture) -> None:
mock_metadata_entry_points(mocker, EarlyPoetryAccessPlugin)


@pytest.fixture
def project_source_directory(fixture_copier: FixtureCopier) -> Path:
return fixture_copier("up_to_date_lock")


@pytest.fixture
def relative_project_source_directory(project_source_directory: Path) -> Path:
# ensure pre-conditions are met
cwd = Path.cwd()
assert project_source_directory.is_relative_to(cwd)

# construct relative path
relative_source_directory = project_source_directory.relative_to(cwd)
assert relative_source_directory.as_posix() != project_source_directory.as_posix()
assert not relative_source_directory.is_absolute()

return relative_source_directory


@pytest.fixture
def tester() -> ApplicationTester:
return ApplicationTester(Application())
Expand Down Expand Up @@ -149,20 +200,14 @@ def test_application_with_context_parameters(
def test_application_with_relative_project_parameter(
tester: ApplicationTester,
project_source_directory: Path,
relative_project_source_directory: Path,
with_mocked_version_command: None,
tmp_path_factory: TempPathFactory,
) -> None:
# ensure pre-conditions are met
cwd = Path.cwd()
assert project_source_directory.is_relative_to(cwd)

# construct relative path
relative_source_directory = project_source_directory.relative_to(cwd)
assert relative_source_directory.as_posix() != project_source_directory.as_posix()
assert not relative_source_directory.is_absolute()

# we expect application run to be executed within current cwd but project to be a subdirectory
args = f"--directory '{cwd}' --project {relative_source_directory} version"
# we expect application run to be executed within current cwd
# but project to be a subdirectory
args = f"--directory '{cwd}' --project {relative_project_source_directory} version"

# we switch cwd to a new temporary directory unrelated to the project directory
new_working_dir = tmp_path_factory.mktemp("unrelated-working-directory")
Expand All @@ -181,6 +226,19 @@ def test_application_with_relative_project_parameter(
""")


def test_application_with_relative_directory_parameter_and_early_poetry_access_plugin(
tester: ApplicationTester,
with_early_poetry_access_plugin: None,
relative_project_source_directory: Path,
) -> None:
"""see https://github.com/nat-n/poethepoet/issues/288"""
tester.execute(
f"--directory {relative_project_source_directory} check-project-path"
)

assert tester.status_code == 0, tester.io.fetch_error()


@pytest.mark.parametrize(
("parameter", "check", "result"),
[
Expand Down
7 changes: 6 additions & 1 deletion tests/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,10 +277,15 @@ def mock_metadata_entry_points(
name: str = "my-plugin",
dist: metadata.Distribution | None = None,
) -> None:
def patched_entry_points(*args: Any, **kwargs: Any) -> list[metadata.EntryPoint]:
if "group" in kwargs and kwargs["group"] != getattr(cls, "group", None):
return []
return [make_entry_point_from_plugin(name, cls, dist)]

mocker.patch.object(
metadata,
"entry_points",
return_value=[make_entry_point_from_plugin(name, cls, dist)],
side_effect=patched_entry_points,
)


Expand Down
2 changes: 2 additions & 0 deletions tests/plugins/test_plugin_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ class MyCommandPlugin(ApplicationPlugin):


class InvalidPlugin:
group = "poetry.plugin"

def activate(self, poetry: Poetry, io: IO) -> None:
io.write_line("Updating version")
poetry.package.version = Version.parse("9.9.9")
Expand Down
Loading