Skip to content

Commit acaed39

Browse files
authored
fix: -o should be relative to the cwd (#130)
Fixes: #77 Also includes: - refactor(tests): drop custom tmp_dir fixture in favor of tmp_path - refactor(tests): correctness and removal of dead code - refactor(tests): simplify fixture path logic
1 parent 371c334 commit acaed39

File tree

7 files changed

+166
-189
lines changed

7 files changed

+166
-189
lines changed

src/poetry_plugin_export/command.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from __future__ import annotations
22

3+
from pathlib import Path
4+
35
from cleo.helpers import option
46
from poetry.console.commands.group_command import GroupCommand
57
from poetry.core.packages.dependency_group import MAIN_GROUP
@@ -99,4 +101,4 @@ def handle(self) -> None:
99101
exporter.with_hashes(not self.option("without-hashes"))
100102
exporter.with_credentials(self.option("with-credentials"))
101103
exporter.with_urls(not self.option("without-urls"))
102-
exporter.export(fmt, self.poetry.file.parent, output or self.io)
104+
exporter.export(fmt, Path.cwd(), output or self.io)

tests/command/conftest.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from __future__ import annotations
22

3-
from pathlib import Path
43
from typing import TYPE_CHECKING
54

65
import pytest
@@ -15,6 +14,8 @@
1514

1615

1716
if TYPE_CHECKING:
17+
from pathlib import Path
18+
1819
from poetry.installation.executor import Executor
1920
from poetry.poetry import Poetry
2021
from poetry.utils.env import Env
@@ -30,8 +31,8 @@ def app(poetry: Poetry) -> PoetryTestApplication:
3031

3132

3233
@pytest.fixture
33-
def env(tmp_dir: str) -> MockEnv:
34-
path = Path(tmp_dir) / ".venv"
34+
def env(tmp_path: Path) -> MockEnv:
35+
path = tmp_path / ".venv"
3536
path.mkdir(parents=True)
3637
return MockEnv(path=path, is_venv=True)
3738

tests/command/test_command_export.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414

1515
if TYPE_CHECKING:
16+
from pathlib import Path
17+
1618
from _pytest.monkeypatch import MonkeyPatch
1719
from cleo.testers.command_tester import CommandTester
1820
from poetry.poetry import Poetry
@@ -89,10 +91,13 @@ def tester(
8991
return command_tester_factory("export", poetry=poetry)
9092

9193

92-
def _export_requirements(tester: CommandTester, poetry: Poetry) -> None:
93-
tester.execute("--format requirements.txt --output requirements.txt")
94+
def _export_requirements(tester: CommandTester, poetry: Poetry, tmp_path: Path) -> None:
95+
from tests.helpers import as_cwd
96+
97+
with as_cwd(tmp_path):
98+
tester.execute("--format requirements.txt --output requirements.txt")
9499

95-
requirements = poetry.file.parent / "requirements.txt"
100+
requirements = tmp_path / "requirements.txt"
96101
assert requirements.exists()
97102

98103
with requirements.open(encoding="utf-8") as f:
@@ -108,17 +113,17 @@ def _export_requirements(tester: CommandTester, poetry: Poetry) -> None:
108113

109114

110115
def test_export_exports_requirements_txt_file_locks_if_no_lock_file(
111-
tester: CommandTester, poetry: Poetry
116+
tester: CommandTester, poetry: Poetry, tmp_path: Path
112117
) -> None:
113118
assert not poetry.locker.lock.exists()
114-
_export_requirements(tester, poetry)
119+
_export_requirements(tester, poetry, tmp_path)
115120
assert "The lock file does not exist. Locking." in tester.io.fetch_error()
116121

117122

118123
def test_export_exports_requirements_txt_uses_lock_file(
119-
tester: CommandTester, poetry: Poetry, do_lock: None
124+
tester: CommandTester, poetry: Poetry, tmp_path: Path, do_lock: None
120125
) -> None:
121-
_export_requirements(tester, poetry)
126+
_export_requirements(tester, poetry, tmp_path)
122127
assert "The lock file does not exist. Locking." not in tester.io.fetch_error()
123128

124129

tests/conftest.py

Lines changed: 9 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
from __future__ import annotations
22

3-
import shutil
43
import sys
5-
import tempfile
64

75
from pathlib import Path
86
from typing import TYPE_CHECKING
97
from typing import Any
10-
from typing import Iterator
118

129
import pytest
1310

@@ -27,7 +24,6 @@
2724
from poetry.poetry import Poetry
2825
from pytest_mock import MockerFixture
2926

30-
from tests.types import FixtureDirGetter
3127
from tests.types import ProjectFactory
3228

3329

@@ -52,15 +48,11 @@ def all(self) -> dict[str, Any]:
5248

5349

5450
@pytest.fixture
55-
def config_cache_dir(tmp_dir: str) -> Path:
56-
path = Path(tmp_dir) / ".cache" / "pypoetry"
51+
def config_cache_dir(tmp_path: Path) -> Path:
52+
path = tmp_path / ".cache" / "pypoetry"
5753
path.mkdir(parents=True)
58-
return path
59-
6054

61-
@pytest.fixture
62-
def config_virtualenvs_path(config_cache_dir: Path) -> Path:
63-
return config_cache_dir / "virtualenvs"
55+
return path
6456

6557

6658
@pytest.fixture
@@ -102,25 +94,13 @@ def config(
10294

10395

10496
@pytest.fixture
105-
def tmp_dir() -> Iterator[str]:
106-
dir_ = tempfile.mkdtemp(prefix="poetry_")
107-
108-
yield dir_
109-
110-
shutil.rmtree(dir_)
97+
def fixture_root() -> Path:
98+
return Path(__file__).parent / "fixtures"
11199

112100

113101
@pytest.fixture
114-
def fixture_base() -> Path:
115-
return Path(__file__).parent.joinpath("fixtures")
116-
117-
118-
@pytest.fixture
119-
def fixture_dir(fixture_base: Path) -> FixtureDirGetter:
120-
def _fixture_dir(name: str) -> Path:
121-
return fixture_base / name
122-
123-
return _fixture_dir
102+
def fixture_root_uri(fixture_root: Path) -> str:
103+
return fixture_root.as_uri()
124104

125105

126106
@pytest.fixture()
@@ -150,14 +130,12 @@ def default_python(current_python: tuple[int, int, int]) -> str:
150130

151131
@pytest.fixture
152132
def project_factory(
153-
tmp_dir: str,
133+
tmp_path: Path,
154134
config: Config,
155135
repo: Repository,
156136
installed: Repository,
157137
default_python: str,
158138
) -> ProjectFactory:
159-
workspace = Path(tmp_dir)
160-
161139
def _factory(
162140
name: str,
163141
dependencies: dict[str, str] | None = None,
@@ -166,7 +144,7 @@ def _factory(
166144
poetry_lock_content: str | None = None,
167145
install_deps: bool = True,
168146
) -> Poetry:
169-
project_dir = workspace / f"poetry-fixture-{name}"
147+
project_dir = tmp_path / f"poetry-fixture-{name}"
170148
dependencies = dependencies or {}
171149
dev_dependencies = dev_dependencies or {}
172150

tests/helpers.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
from __future__ import annotations
22

3+
import os
4+
5+
from contextlib import contextmanager
36
from typing import TYPE_CHECKING
47
from typing import Any
8+
from typing import Iterator
59

610
from poetry.console.application import Application
711
from poetry.core.toml.file import TOMLFile
@@ -109,3 +113,13 @@ def _execute_update(self, operation: Operation) -> int:
109113

110114
def _execute_remove(self, operation: Operation) -> int:
111115
return 0
116+
117+
118+
@contextmanager
119+
def as_cwd(path: Path) -> Iterator[Path]:
120+
old_cwd = os.getcwd()
121+
os.chdir(path)
122+
try:
123+
yield path
124+
finally:
125+
os.chdir(old_cwd)

0 commit comments

Comments
 (0)