From 0383c2c049cc86270c074ba77d74945142ec3436 Mon Sep 17 00:00:00 2001 From: Bjorn Neergaard Date: Fri, 30 Sep 2022 21:20:17 -0600 Subject: [PATCH 1/4] fix: -o should be relative to the cwd --- src/poetry_plugin_export/command.py | 4 +++- tests/command/test_command_export.py | 19 ++++++++++++------- tests/helpers.py | 14 ++++++++++++++ 3 files changed, 29 insertions(+), 8 deletions(-) diff --git a/src/poetry_plugin_export/command.py b/src/poetry_plugin_export/command.py index 1deadd0..5cdfa7e 100644 --- a/src/poetry_plugin_export/command.py +++ b/src/poetry_plugin_export/command.py @@ -1,5 +1,7 @@ from __future__ import annotations +from pathlib import Path + from cleo.helpers import option from poetry.console.commands.group_command import GroupCommand from poetry.core.packages.dependency_group import MAIN_GROUP @@ -99,4 +101,4 @@ def handle(self) -> None: exporter.with_hashes(not self.option("without-hashes")) exporter.with_credentials(self.option("with-credentials")) exporter.with_urls(not self.option("without-urls")) - exporter.export(fmt, self.poetry.file.parent, output or self.io) + exporter.export(fmt, Path.cwd(), output or self.io) diff --git a/tests/command/test_command_export.py b/tests/command/test_command_export.py index cf7fb37..b58e140 100644 --- a/tests/command/test_command_export.py +++ b/tests/command/test_command_export.py @@ -13,6 +13,8 @@ if TYPE_CHECKING: + from pathlib import Path + from _pytest.monkeypatch import MonkeyPatch from cleo.testers.command_tester import CommandTester from poetry.poetry import Poetry @@ -89,10 +91,13 @@ def tester( return command_tester_factory("export", poetry=poetry) -def _export_requirements(tester: CommandTester, poetry: Poetry) -> None: - tester.execute("--format requirements.txt --output requirements.txt") +def _export_requirements(tester: CommandTester, poetry: Poetry, tmp_path: Path) -> None: + from tests.helpers import as_cwd + + with as_cwd(tmp_path): + tester.execute("--format requirements.txt --output requirements.txt") - requirements = poetry.file.parent / "requirements.txt" + requirements = tmp_path / "requirements.txt" assert requirements.exists() with requirements.open(encoding="utf-8") as f: @@ -108,17 +113,17 @@ def _export_requirements(tester: CommandTester, poetry: Poetry) -> None: def test_export_exports_requirements_txt_file_locks_if_no_lock_file( - tester: CommandTester, poetry: Poetry + tester: CommandTester, poetry: Poetry, tmp_path: Path ) -> None: assert not poetry.locker.lock.exists() - _export_requirements(tester, poetry) + _export_requirements(tester, poetry, tmp_path) assert "The lock file does not exist. Locking." in tester.io.fetch_error() def test_export_exports_requirements_txt_uses_lock_file( - tester: CommandTester, poetry: Poetry, do_lock: None + tester: CommandTester, poetry: Poetry, tmp_path: Path, do_lock: None ) -> None: - _export_requirements(tester, poetry) + _export_requirements(tester, poetry, tmp_path) assert "The lock file does not exist. Locking." not in tester.io.fetch_error() diff --git a/tests/helpers.py b/tests/helpers.py index 472e8ca..4ab87c0 100644 --- a/tests/helpers.py +++ b/tests/helpers.py @@ -1,7 +1,11 @@ from __future__ import annotations +import os + +from contextlib import contextmanager from typing import TYPE_CHECKING from typing import Any +from typing import Iterator from poetry.console.application import Application from poetry.core.toml.file import TOMLFile @@ -109,3 +113,13 @@ def _execute_update(self, operation: Operation) -> int: def _execute_remove(self, operation: Operation) -> int: return 0 + + +@contextmanager +def as_cwd(path: Path) -> Iterator[Path]: + old_cwd = os.getcwd() + os.chdir(path) + try: + yield path + finally: + os.chdir(old_cwd) From 11003cea002b8bc110fce1222bc5374270347075 Mon Sep 17 00:00:00 2001 From: Bjorn Neergaard Date: Fri, 30 Sep 2022 21:28:28 -0600 Subject: [PATCH 2/4] refactor(tests): drop custom tmp_dir fixture in favor of tmp_path --- tests/command/conftest.py | 7 +- tests/conftest.py | 22 +--- tests/test_exporter.py | 228 +++++++++++++++++++------------------- 3 files changed, 122 insertions(+), 135 deletions(-) diff --git a/tests/command/conftest.py b/tests/command/conftest.py index d368623..87a52a9 100644 --- a/tests/command/conftest.py +++ b/tests/command/conftest.py @@ -1,6 +1,5 @@ from __future__ import annotations -from pathlib import Path from typing import TYPE_CHECKING import pytest @@ -15,6 +14,8 @@ if TYPE_CHECKING: + from pathlib import Path + from poetry.installation.executor import Executor from poetry.poetry import Poetry from poetry.utils.env import Env @@ -30,8 +31,8 @@ def app(poetry: Poetry) -> PoetryTestApplication: @pytest.fixture -def env(tmp_dir: str) -> MockEnv: - path = Path(tmp_dir) / ".venv" +def env(tmp_path: Path) -> MockEnv: + path = tmp_path / ".venv" path.mkdir(parents=True) return MockEnv(path=path, is_venv=True) diff --git a/tests/conftest.py b/tests/conftest.py index 0d5e2ae..b4bd927 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,13 +1,10 @@ from __future__ import annotations -import shutil import sys -import tempfile from pathlib import Path from typing import TYPE_CHECKING from typing import Any -from typing import Iterator import pytest @@ -52,8 +49,8 @@ def all(self) -> dict[str, Any]: @pytest.fixture -def config_cache_dir(tmp_dir: str) -> Path: - path = Path(tmp_dir) / ".cache" / "pypoetry" +def config_cache_dir(tmp_path: Path) -> Path: + path = tmp_path / ".cache" / "pypoetry" path.mkdir(parents=True) return path @@ -101,15 +98,6 @@ def config( return c -@pytest.fixture -def tmp_dir() -> Iterator[str]: - dir_ = tempfile.mkdtemp(prefix="poetry_") - - yield dir_ - - shutil.rmtree(dir_) - - @pytest.fixture def fixture_base() -> Path: return Path(__file__).parent.joinpath("fixtures") @@ -150,14 +138,12 @@ def default_python(current_python: tuple[int, int, int]) -> str: @pytest.fixture def project_factory( - tmp_dir: str, + tmp_path: Path, config: Config, repo: Repository, installed: Repository, default_python: str, ) -> ProjectFactory: - workspace = Path(tmp_dir) - def _factory( name: str, dependencies: dict[str, str] | None = None, @@ -166,7 +152,7 @@ def _factory( poetry_lock_content: str | None = None, install_deps: bool = True, ) -> Poetry: - project_dir = workspace / f"poetry-fixture-{name}" + project_dir = tmp_path / f"poetry-fixture-{name}" dependencies = dependencies or {} dev_dependencies = dev_dependencies or {} diff --git a/tests/test_exporter.py b/tests/test_exporter.py index 2f86e9b..f673b17 100644 --- a/tests/test_exporter.py +++ b/tests/test_exporter.py @@ -108,7 +108,7 @@ def set_package_requires(poetry: Poetry, skip: set[str] | None = None) -> None: def test_exporter_can_export_requirements_txt_with_standard_packages( - tmp_dir: str, poetry: Poetry + tmp_path: Path, poetry: Poetry ) -> None: poetry.locker.mock_lock_data( # type: ignore[attr-defined] { @@ -138,9 +138,9 @@ def test_exporter_can_export_requirements_txt_with_standard_packages( set_package_requires(poetry) exporter = Exporter(poetry) - exporter.export("requirements.txt", Path(tmp_dir), "requirements.txt") + exporter.export("requirements.txt", tmp_path, "requirements.txt") - with (Path(tmp_dir) / "requirements.txt").open(encoding="utf-8") as f: + with (tmp_path / "requirements.txt").open(encoding="utf-8") as f: content = f.read() expected = f"""\ @@ -152,7 +152,7 @@ def test_exporter_can_export_requirements_txt_with_standard_packages( def test_exporter_can_export_requirements_txt_with_standard_packages_and_markers( - tmp_dir: str, poetry: Poetry + tmp_path: Path, poetry: Poetry ) -> None: poetry.locker.mock_lock_data( # type: ignore[attr-defined] { @@ -192,9 +192,9 @@ def test_exporter_can_export_requirements_txt_with_standard_packages_and_markers set_package_requires(poetry) exporter = Exporter(poetry) - exporter.export("requirements.txt", Path(tmp_dir), "requirements.txt") + exporter.export("requirements.txt", tmp_path, "requirements.txt") - with (Path(tmp_dir) / "requirements.txt").open(encoding="utf-8") as f: + with (tmp_path / "requirements.txt").open(encoding="utf-8") as f: content = f.read() expected = f"""\ @@ -207,7 +207,7 @@ def test_exporter_can_export_requirements_txt_with_standard_packages_and_markers def test_exporter_can_export_requirements_txt_poetry( - tmp_dir: str, poetry: Poetry + tmp_path: Path, poetry: Poetry ) -> None: """Regression test for #3254""" @@ -286,9 +286,9 @@ def test_exporter_can_export_requirements_txt_poetry( ) exporter = Exporter(poetry) - exporter.export("requirements.txt", Path(tmp_dir), "requirements.txt") + exporter.export("requirements.txt", tmp_path, "requirements.txt") - with (Path(tmp_dir) / "requirements.txt").open(encoding="utf-8") as f: + with (tmp_path / "requirements.txt").open(encoding="utf-8") as f: content = f.read() # The dependency graph: @@ -328,7 +328,7 @@ def test_exporter_can_export_requirements_txt_poetry( def test_exporter_can_export_requirements_txt_pyinstaller( - tmp_dir: str, poetry: Poetry + tmp_path: Path, poetry: Poetry ) -> None: """Regression test for #3254""" @@ -375,9 +375,9 @@ def test_exporter_can_export_requirements_txt_pyinstaller( set_package_requires(poetry, skip={"altgraph", "macholib"}) exporter = Exporter(poetry) - exporter.export("requirements.txt", Path(tmp_dir), "requirements.txt") + exporter.export("requirements.txt", tmp_path, "requirements.txt") - with (Path(tmp_dir) / "requirements.txt").open(encoding="utf-8") as f: + with (tmp_path / "requirements.txt").open(encoding="utf-8") as f: content = f.read() # Rationale for the results: @@ -410,7 +410,7 @@ def test_exporter_can_export_requirements_txt_pyinstaller( def test_exporter_can_export_requirements_txt_with_nested_packages_and_markers( - tmp_dir: str, poetry: Poetry + tmp_path: Path, poetry: Poetry ) -> None: poetry.locker.mock_lock_data( # type: ignore[attr-defined] { @@ -460,9 +460,9 @@ def test_exporter_can_export_requirements_txt_with_nested_packages_and_markers( set_package_requires(poetry, skip={"b", "c", "d"}) exporter = Exporter(poetry) - exporter.export("requirements.txt", Path(tmp_dir), "requirements.txt") + exporter.export("requirements.txt", tmp_path, "requirements.txt") - with (Path(tmp_dir) / "requirements.txt").open(encoding="utf-8") as f: + with (tmp_path / "requirements.txt").open(encoding="utf-8") as f: content = f.read() marker_py = MARKER_PY27.union(MARKER_PY36_ONLY) @@ -505,7 +505,7 @@ def test_exporter_can_export_requirements_txt_with_nested_packages_and_markers( ], ) def test_exporter_can_export_requirements_txt_with_nested_packages_and_markers_any( - tmp_dir: str, poetry: Poetry, dev: bool, lines: list[str] + tmp_path: Path, poetry: Poetry, dev: bool, lines: list[str] ) -> None: poetry.locker.mock_lock_data( # type: ignore[attr-defined] { @@ -550,16 +550,16 @@ def test_exporter_can_export_requirements_txt_with_nested_packages_and_markers_a exporter = Exporter(poetry) if dev: exporter.only_groups([MAIN_GROUP, "dev"]) - exporter.export("requirements.txt", Path(tmp_dir), "requirements.txt") + exporter.export("requirements.txt", tmp_path, "requirements.txt") - with (Path(tmp_dir) / "requirements.txt").open(encoding="utf-8") as f: + with (tmp_path / "requirements.txt").open(encoding="utf-8") as f: content = f.read() assert content.strip() == "\n".join(lines) def test_exporter_can_export_requirements_txt_with_standard_packages_and_hashes( - tmp_dir: str, poetry: Poetry + tmp_path: Path, poetry: Poetry ) -> None: poetry.locker.mock_lock_data( # type: ignore[attr-defined] { @@ -592,9 +592,9 @@ def test_exporter_can_export_requirements_txt_with_standard_packages_and_hashes( set_package_requires(poetry) exporter = Exporter(poetry) - exporter.export("requirements.txt", Path(tmp_dir), "requirements.txt") + exporter.export("requirements.txt", tmp_path, "requirements.txt") - with (Path(tmp_dir) / "requirements.txt").open(encoding="utf-8") as f: + with (tmp_path / "requirements.txt").open(encoding="utf-8") as f: content = f.read() expected = f"""\ @@ -608,7 +608,7 @@ def test_exporter_can_export_requirements_txt_with_standard_packages_and_hashes( def test_exporter_can_export_requirements_txt_with_standard_packages_and_sorted_hashes( - tmp_dir: str, poetry: Poetry + tmp_path: Path, poetry: Poetry ) -> None: poetry.locker.mock_lock_data( # type: ignore[attr-defined] { @@ -647,9 +647,9 @@ def test_exporter_can_export_requirements_txt_with_standard_packages_and_sorted_ set_package_requires(poetry) exporter = Exporter(poetry) - exporter.export("requirements.txt", Path(tmp_dir), "requirements.txt") + exporter.export("requirements.txt", tmp_path, "requirements.txt") - with (Path(tmp_dir) / "requirements.txt").open(encoding="utf-8") as f: + with (tmp_path / "requirements.txt").open(encoding="utf-8") as f: content = f.read() expected = f"""\ @@ -665,7 +665,7 @@ def test_exporter_can_export_requirements_txt_with_standard_packages_and_sorted_ def test_exporter_can_export_requirements_txt_with_standard_packages_and_hashes_disabled( # noqa: E501 - tmp_dir: str, poetry: Poetry + tmp_path: Path, poetry: Poetry ) -> None: poetry.locker.mock_lock_data( # type: ignore[attr-defined] { @@ -699,9 +699,9 @@ def test_exporter_can_export_requirements_txt_with_standard_packages_and_hashes_ exporter = Exporter(poetry) exporter.with_hashes(False) - exporter.export("requirements.txt", Path(tmp_dir), "requirements.txt") + exporter.export("requirements.txt", tmp_path, "requirements.txt") - with (Path(tmp_dir) / "requirements.txt").open(encoding="utf-8") as f: + with (tmp_path / "requirements.txt").open(encoding="utf-8") as f: content = f.read() expected = f"""\ @@ -713,7 +713,7 @@ def test_exporter_can_export_requirements_txt_with_standard_packages_and_hashes_ def test_exporter_exports_requirements_txt_without_dev_packages_by_default( - tmp_dir: str, poetry: Poetry + tmp_path: Path, poetry: Poetry ) -> None: poetry.locker.mock_lock_data( # type: ignore[attr-defined] { @@ -746,9 +746,9 @@ def test_exporter_exports_requirements_txt_without_dev_packages_by_default( set_package_requires(poetry) exporter = Exporter(poetry) - exporter.export("requirements.txt", Path(tmp_dir), "requirements.txt") + exporter.export("requirements.txt", tmp_path, "requirements.txt") - with (Path(tmp_dir) / "requirements.txt").open(encoding="utf-8") as f: + with (tmp_path / "requirements.txt").open(encoding="utf-8") as f: content = f.read() expected = f"""\ @@ -760,7 +760,7 @@ def test_exporter_exports_requirements_txt_without_dev_packages_by_default( def test_exporter_exports_requirements_txt_with_dev_packages_if_opted_in( - tmp_dir: str, poetry: Poetry + tmp_path: Path, poetry: Poetry ) -> None: poetry.locker.mock_lock_data( # type: ignore[attr-defined] { @@ -794,9 +794,9 @@ def test_exporter_exports_requirements_txt_with_dev_packages_if_opted_in( exporter = Exporter(poetry) exporter.only_groups([MAIN_GROUP, "dev"]) - exporter.export("requirements.txt", Path(tmp_dir), "requirements.txt") + exporter.export("requirements.txt", tmp_path, "requirements.txt") - with (Path(tmp_dir) / "requirements.txt").open(encoding="utf-8") as f: + with (tmp_path / "requirements.txt").open(encoding="utf-8") as f: content = f.read() expected = f"""\ @@ -810,7 +810,7 @@ def test_exporter_exports_requirements_txt_with_dev_packages_if_opted_in( def test_exporter_exports_requirements_txt_without_groups_if_set_explicity( - tmp_dir: str, poetry: Poetry + tmp_path: Path, poetry: Poetry ) -> None: poetry.locker.mock_lock_data( # type: ignore[attr-defined] { @@ -844,16 +844,16 @@ def test_exporter_exports_requirements_txt_without_groups_if_set_explicity( exporter = Exporter(poetry) exporter.only_groups([]) - exporter.export("requirements.txt", Path(tmp_dir), "requirements.txt") + exporter.export("requirements.txt", tmp_path, "requirements.txt") - with (Path(tmp_dir) / "requirements.txt").open(encoding="utf-8") as f: + with (tmp_path / "requirements.txt").open(encoding="utf-8") as f: content = f.read() assert content == "\n" def test_exporter_exports_requirements_txt_without_optional_packages( - tmp_dir: str, poetry: Poetry + tmp_path: Path, poetry: Poetry ) -> None: poetry.locker.mock_lock_data( # type: ignore[attr-defined] { @@ -887,9 +887,9 @@ def test_exporter_exports_requirements_txt_without_optional_packages( exporter = Exporter(poetry) exporter.only_groups([MAIN_GROUP, "dev"]) - exporter.export("requirements.txt", Path(tmp_dir), "requirements.txt") + exporter.export("requirements.txt", tmp_path, "requirements.txt") - with (Path(tmp_dir) / "requirements.txt").open(encoding="utf-8") as f: + with (tmp_path / "requirements.txt").open(encoding="utf-8") as f: content = f.read() expected = f"""\ @@ -930,7 +930,7 @@ def test_exporter_exports_requirements_txt_without_optional_packages( ], ) def test_exporter_exports_requirements_txt_with_optional_packages( - tmp_dir: str, + tmp_path: Path, poetry: Poetry, extras: bool | list[str] | None, lines: list[str], @@ -981,11 +981,11 @@ def test_exporter_exports_requirements_txt_with_optional_packages( exporter.with_extras(extras) exporter.export( "requirements.txt", - Path(tmp_dir), + tmp_path, "requirements.txt", ) - with (Path(tmp_dir) / "requirements.txt").open(encoding="utf-8") as f: + with (tmp_path / "requirements.txt").open(encoding="utf-8") as f: content = f.read() expected = "\n".join(lines) @@ -994,7 +994,7 @@ def test_exporter_exports_requirements_txt_with_optional_packages( def test_exporter_can_export_requirements_txt_with_git_packages( - tmp_dir: str, poetry: Poetry + tmp_path: Path, poetry: Poetry ) -> None: poetry.locker.mock_lock_data( # type: ignore[attr-defined] { @@ -1022,9 +1022,9 @@ def test_exporter_can_export_requirements_txt_with_git_packages( set_package_requires(poetry) exporter = Exporter(poetry) - exporter.export("requirements.txt", Path(tmp_dir), "requirements.txt") + exporter.export("requirements.txt", tmp_path, "requirements.txt") - with (Path(tmp_dir) / "requirements.txt").open(encoding="utf-8") as f: + with (tmp_path / "requirements.txt").open(encoding="utf-8") as f: content = f.read() expected = f"""\ @@ -1035,7 +1035,7 @@ def test_exporter_can_export_requirements_txt_with_git_packages( def test_exporter_can_export_requirements_txt_with_nested_packages( - tmp_dir: str, poetry: Poetry + tmp_path: Path, poetry: Poetry ) -> None: poetry.locker.mock_lock_data( # type: ignore[attr-defined] { @@ -1076,9 +1076,9 @@ def test_exporter_can_export_requirements_txt_with_nested_packages( set_package_requires(poetry, skip={"foo"}) exporter = Exporter(poetry) - exporter.export("requirements.txt", Path(tmp_dir), "requirements.txt") + exporter.export("requirements.txt", tmp_path, "requirements.txt") - with (Path(tmp_dir) / "requirements.txt").open(encoding="utf-8") as f: + with (tmp_path / "requirements.txt").open(encoding="utf-8") as f: content = f.read() expected = f"""\ @@ -1090,7 +1090,7 @@ def test_exporter_can_export_requirements_txt_with_nested_packages( def test_exporter_can_export_requirements_txt_with_nested_packages_cyclic( - tmp_dir: str, poetry: Poetry + tmp_path: Path, poetry: Poetry ) -> None: poetry.locker.mock_lock_data( # type: ignore[attr-defined] { @@ -1130,9 +1130,9 @@ def test_exporter_can_export_requirements_txt_with_nested_packages_cyclic( set_package_requires(poetry, skip={"bar", "baz"}) exporter = Exporter(poetry) - exporter.export("requirements.txt", Path(tmp_dir), "requirements.txt") + exporter.export("requirements.txt", tmp_path, "requirements.txt") - with (Path(tmp_dir) / "requirements.txt").open(encoding="utf-8") as f: + with (tmp_path / "requirements.txt").open(encoding="utf-8") as f: content = f.read() expected = f"""\ @@ -1145,7 +1145,7 @@ def test_exporter_can_export_requirements_txt_with_nested_packages_cyclic( def test_exporter_can_export_requirements_txt_with_nested_packages_and_multiple_markers( - tmp_dir: str, poetry: Poetry + tmp_path: Path, poetry: Poetry ) -> None: poetry.locker.mock_lock_data( # type: ignore[attr-defined] { @@ -1201,9 +1201,9 @@ def test_exporter_can_export_requirements_txt_with_nested_packages_and_multiple_ exporter = Exporter(poetry) exporter.with_hashes(False) - exporter.export("requirements.txt", Path(tmp_dir), "requirements.txt") + exporter.export("requirements.txt", tmp_path, "requirements.txt") - with (Path(tmp_dir) / "requirements.txt").open(encoding="utf-8") as f: + with (tmp_path / "requirements.txt").open(encoding="utf-8") as f: content = f.read() marker_py_not_windows = MARKER_PY.intersect( @@ -1219,7 +1219,7 @@ def test_exporter_can_export_requirements_txt_with_nested_packages_and_multiple_ def test_exporter_can_export_requirements_txt_with_git_packages_and_markers( - tmp_dir: str, poetry: Poetry + tmp_path: Path, poetry: Poetry ) -> None: poetry.locker.mock_lock_data( # type: ignore[attr-defined] { @@ -1248,9 +1248,9 @@ def test_exporter_can_export_requirements_txt_with_git_packages_and_markers( set_package_requires(poetry) exporter = Exporter(poetry) - exporter.export("requirements.txt", Path(tmp_dir), "requirements.txt") + exporter.export("requirements.txt", tmp_path, "requirements.txt") - with (Path(tmp_dir) / "requirements.txt").open(encoding="utf-8") as f: + with (tmp_path / "requirements.txt").open(encoding="utf-8") as f: content = f.read() expected = f"""\ @@ -1261,7 +1261,7 @@ def test_exporter_can_export_requirements_txt_with_git_packages_and_markers( def test_exporter_can_export_requirements_txt_with_directory_packages( - tmp_dir: str, poetry: Poetry, working_directory: Path + tmp_path: Path, poetry: Poetry, working_directory: Path ) -> None: poetry.locker.mock_lock_data( # type: ignore[attr-defined] { @@ -1289,9 +1289,9 @@ def test_exporter_can_export_requirements_txt_with_directory_packages( set_package_requires(poetry) exporter = Exporter(poetry) - exporter.export("requirements.txt", Path(tmp_dir), "requirements.txt") + exporter.export("requirements.txt", tmp_path, "requirements.txt") - with (Path(tmp_dir) / "requirements.txt").open(encoding="utf-8") as f: + with (tmp_path / "requirements.txt").open(encoding="utf-8") as f: content = f.read() expected = f"""\ @@ -1302,7 +1302,7 @@ def test_exporter_can_export_requirements_txt_with_directory_packages( def test_exporter_can_export_requirements_txt_with_nested_directory_packages( - tmp_dir: str, poetry: Poetry, working_directory: Path + tmp_path: Path, poetry: Poetry, working_directory: Path ) -> None: poetry.locker.mock_lock_data( # type: ignore[attr-defined] { @@ -1360,9 +1360,9 @@ def test_exporter_can_export_requirements_txt_with_nested_directory_packages( set_package_requires(poetry) exporter = Exporter(poetry) - exporter.export("requirements.txt", Path(tmp_dir), "requirements.txt") + exporter.export("requirements.txt", tmp_path, "requirements.txt") - with (Path(tmp_dir) / "requirements.txt").open(encoding="utf-8") as f: + with (tmp_path / "requirements.txt").open(encoding="utf-8") as f: content = f.read() root_uri = f"{working_directory.as_uri()}/tests/fixtures" @@ -1376,7 +1376,7 @@ def test_exporter_can_export_requirements_txt_with_nested_directory_packages( def test_exporter_can_export_requirements_txt_with_directory_packages_and_markers( - tmp_dir: str, poetry: Poetry, working_directory: Path + tmp_path: Path, poetry: Poetry, working_directory: Path ) -> None: poetry.locker.mock_lock_data( # type: ignore[attr-defined] { @@ -1405,9 +1405,9 @@ def test_exporter_can_export_requirements_txt_with_directory_packages_and_marker set_package_requires(poetry) exporter = Exporter(poetry) - exporter.export("requirements.txt", Path(tmp_dir), "requirements.txt") + exporter.export("requirements.txt", tmp_path, "requirements.txt") - with (Path(tmp_dir) / "requirements.txt").open(encoding="utf-8") as f: + with (tmp_path / "requirements.txt").open(encoding="utf-8") as f: content = f.read() expected = f"""\ @@ -1419,7 +1419,7 @@ def test_exporter_can_export_requirements_txt_with_directory_packages_and_marker def test_exporter_can_export_requirements_txt_with_file_packages( - tmp_dir: str, poetry: Poetry, working_directory: Path + tmp_path: Path, poetry: Poetry, working_directory: Path ) -> None: poetry.locker.mock_lock_data( # type: ignore[attr-defined] { @@ -1447,9 +1447,9 @@ def test_exporter_can_export_requirements_txt_with_file_packages( set_package_requires(poetry) exporter = Exporter(poetry) - exporter.export("requirements.txt", Path(tmp_dir), "requirements.txt") + exporter.export("requirements.txt", tmp_path, "requirements.txt") - with (Path(tmp_dir) / "requirements.txt").open(encoding="utf-8") as f: + with (tmp_path / "requirements.txt").open(encoding="utf-8") as f: content = f.read() expected = f"""\ @@ -1461,7 +1461,7 @@ def test_exporter_can_export_requirements_txt_with_file_packages( def test_exporter_can_export_requirements_txt_with_file_packages_and_markers( - tmp_dir: str, poetry: Poetry, working_directory: Path + tmp_path: Path, poetry: Poetry, working_directory: Path ) -> None: poetry.locker.mock_lock_data( # type: ignore[attr-defined] { @@ -1490,9 +1490,9 @@ def test_exporter_can_export_requirements_txt_with_file_packages_and_markers( set_package_requires(poetry) exporter = Exporter(poetry) - exporter.export("requirements.txt", Path(tmp_dir), "requirements.txt") + exporter.export("requirements.txt", tmp_path, "requirements.txt") - with (Path(tmp_dir) / "requirements.txt").open(encoding="utf-8") as f: + with (tmp_path / "requirements.txt").open(encoding="utf-8") as f: content = f.read() uri = f"{working_directory.as_uri()}/tests/fixtures/distributions/demo-0.1.0.tar.gz" @@ -1504,7 +1504,7 @@ def test_exporter_can_export_requirements_txt_with_file_packages_and_markers( def test_exporter_exports_requirements_txt_with_legacy_packages( - tmp_dir: str, poetry: Poetry + tmp_path: Path, poetry: Poetry ) -> None: poetry.pool.add_repository( LegacyRepository( @@ -1549,9 +1549,9 @@ def test_exporter_exports_requirements_txt_with_legacy_packages( exporter = Exporter(poetry) exporter.only_groups([MAIN_GROUP, "dev"]) - exporter.export("requirements.txt", Path(tmp_dir), "requirements.txt") + exporter.export("requirements.txt", tmp_path, "requirements.txt") - with (Path(tmp_dir) / "requirements.txt").open(encoding="utf-8") as f: + with (tmp_path / "requirements.txt").open(encoding="utf-8") as f: content = f.read() expected = f"""\ @@ -1567,7 +1567,7 @@ def test_exporter_exports_requirements_txt_with_legacy_packages( def test_exporter_exports_requirements_txt_with_url_false( - tmp_dir: str, poetry: Poetry + tmp_path: Path, poetry: Poetry ) -> None: poetry.pool.add_repository( LegacyRepository( @@ -1613,9 +1613,9 @@ def test_exporter_exports_requirements_txt_with_url_false( exporter = Exporter(poetry) exporter.only_groups([MAIN_GROUP, "dev"]) exporter.with_urls(False) - exporter.export("requirements.txt", Path(tmp_dir), "requirements.txt") + exporter.export("requirements.txt", tmp_path, "requirements.txt") - with (Path(tmp_dir) / "requirements.txt").open(encoding="utf-8") as f: + with (tmp_path / "requirements.txt").open(encoding="utf-8") as f: content = f.read() expected = f"""\ @@ -1629,7 +1629,7 @@ def test_exporter_exports_requirements_txt_with_url_false( def test_exporter_exports_requirements_txt_with_legacy_packages_trusted_host( - tmp_dir: str, poetry: Poetry + tmp_path: Path, poetry: Poetry ) -> None: poetry.pool.add_repository( LegacyRepository( @@ -1665,9 +1665,9 @@ def test_exporter_exports_requirements_txt_with_legacy_packages_trusted_host( set_package_requires(poetry) exporter = Exporter(poetry) exporter.only_groups([MAIN_GROUP, "dev"]) - exporter.export("requirements.txt", Path(tmp_dir), "requirements.txt") + exporter.export("requirements.txt", tmp_path, "requirements.txt") - with (Path(tmp_dir) / "requirements.txt").open(encoding="utf-8") as f: + with (tmp_path / "requirements.txt").open(encoding="utf-8") as f: content = f.read() expected = f"""\ @@ -1702,7 +1702,7 @@ def test_exporter_exports_requirements_txt_with_legacy_packages_trusted_host( ], ) def test_exporter_exports_requirements_txt_with_dev_extras( - tmp_dir: str, poetry: Poetry, dev: bool, expected: list[str] + tmp_path: Path, poetry: Poetry, dev: bool, expected: list[str] ) -> None: poetry.locker.mock_lock_data( # type: ignore[attr-defined] { @@ -1749,16 +1749,16 @@ def test_exporter_exports_requirements_txt_with_dev_extras( exporter = Exporter(poetry) if dev: exporter.only_groups([MAIN_GROUP, "dev"]) - exporter.export("requirements.txt", Path(tmp_dir), "requirements.txt") + exporter.export("requirements.txt", tmp_path, "requirements.txt") - with (Path(tmp_dir) / "requirements.txt").open(encoding="utf-8") as f: + with (tmp_path / "requirements.txt").open(encoding="utf-8") as f: content = f.read() assert content == "\n".join(expected) + "\n" def test_exporter_exports_requirements_txt_with_legacy_packages_and_duplicate_sources( - tmp_dir: str, poetry: Poetry + tmp_path: Path, poetry: Poetry ) -> None: poetry.pool.add_repository( LegacyRepository( @@ -1827,9 +1827,9 @@ def test_exporter_exports_requirements_txt_with_legacy_packages_and_duplicate_so exporter = Exporter(poetry) exporter.only_groups([MAIN_GROUP, "dev"]) - exporter.export("requirements.txt", Path(tmp_dir), "requirements.txt") + exporter.export("requirements.txt", tmp_path, "requirements.txt") - with (Path(tmp_dir) / "requirements.txt").open(encoding="utf-8") as f: + with (tmp_path / "requirements.txt").open(encoding="utf-8") as f: content = f.read() expected = f"""\ @@ -1848,7 +1848,7 @@ def test_exporter_exports_requirements_txt_with_legacy_packages_and_duplicate_so def test_exporter_exports_requirements_txt_with_default_and_secondary_sources( - tmp_dir: str, poetry: Poetry + tmp_path: Path, poetry: Poetry ) -> None: poetry.pool.remove_repository("PyPI") poetry.config.merge( @@ -1935,9 +1935,9 @@ def test_exporter_exports_requirements_txt_with_default_and_secondary_sources( exporter = Exporter(poetry) exporter.only_groups([MAIN_GROUP, "dev"]) exporter.with_credentials() - exporter.export("requirements.txt", Path(tmp_dir), "requirements.txt") + exporter.export("requirements.txt", tmp_path, "requirements.txt") - with (Path(tmp_dir) / "requirements.txt").open(encoding="utf-8") as f: + with (tmp_path / "requirements.txt").open(encoding="utf-8") as f: content = f.read() expected = f"""\ @@ -1956,7 +1956,7 @@ def test_exporter_exports_requirements_txt_with_default_and_secondary_sources( def test_exporter_exports_requirements_txt_with_legacy_packages_and_credentials( - tmp_dir: str, poetry: Poetry, config: Config + tmp_path: Path, poetry: Poetry, config: Config ) -> None: poetry.config.merge( { @@ -2007,11 +2007,11 @@ def test_exporter_exports_requirements_txt_with_legacy_packages_and_credentials( exporter.with_credentials() exporter.export( "requirements.txt", - Path(tmp_dir), + tmp_path, "requirements.txt", ) - with (Path(tmp_dir) / "requirements.txt").open(encoding="utf-8") as f: + with (tmp_path / "requirements.txt").open(encoding="utf-8") as f: content = f.read() expected = f"""\ @@ -2027,7 +2027,7 @@ def test_exporter_exports_requirements_txt_with_legacy_packages_and_credentials( def test_exporter_exports_requirements_txt_to_standard_output( - tmp_dir: str, poetry: Poetry + tmp_path: Path, poetry: Poetry ) -> None: poetry.locker.mock_lock_data( # type: ignore[attr-defined] { @@ -2058,7 +2058,7 @@ def test_exporter_exports_requirements_txt_to_standard_output( exporter = Exporter(poetry) io = BufferedIO() - exporter.export("requirements.txt", Path(tmp_dir), io) + exporter.export("requirements.txt", tmp_path, io) expected = f"""\ bar==4.5.6 ; {MARKER_PY} @@ -2069,7 +2069,7 @@ def test_exporter_exports_requirements_txt_to_standard_output( def test_exporter_doesnt_confuse_repeated_packages( - tmp_dir: str, poetry: Poetry + tmp_path: Path, poetry: Poetry ) -> None: # Testcase derived from . poetry.locker.mock_lock_data( # type: ignore[attr-defined] @@ -2173,7 +2173,7 @@ def test_exporter_doesnt_confuse_repeated_packages( exporter = Exporter(poetry) exporter.only_groups([MAIN_GROUP, "dev"]) io = BufferedIO() - exporter.export("requirements.txt", Path(tmp_dir), io) + exporter.export("requirements.txt", tmp_path, io) expected = f"""\ celery==5.1.2 ; {MARKER_PY36_ONLY} @@ -2189,7 +2189,7 @@ def test_exporter_doesnt_confuse_repeated_packages( def test_exporter_handles_extras_next_to_non_extras( - tmp_dir: str, poetry: Poetry + tmp_path: Path, poetry: Poetry ) -> None: # Testcase similar to the solver testcase added at #5305. poetry.locker.mock_lock_data( # type: ignore[attr-defined] @@ -2287,7 +2287,7 @@ def test_exporter_handles_extras_next_to_non_extras( exporter = Exporter(poetry) io = BufferedIO() - exporter.export("requirements.txt", Path(tmp_dir), io) + exporter.export("requirements.txt", tmp_path, io) expected = f"""\ localstack-ext==1.0.0 ; {MARKER_PY36} @@ -2301,7 +2301,7 @@ def test_exporter_handles_extras_next_to_non_extras( def test_exporter_handles_overlapping_python_versions( - tmp_dir: str, poetry: Poetry + tmp_path: Path, poetry: Poetry ) -> None: # Testcase derived from # https://github.com/python-poetry/poetry-plugin-export/issues/32. @@ -2390,7 +2390,7 @@ def test_exporter_handles_overlapping_python_versions( exporter = Exporter(poetry) io = BufferedIO() - exporter.export("requirements.txt", Path(tmp_dir), io) + exporter.export("requirements.txt", tmp_path, io) expected = f"""\ ipython==7.16.3 ; {MARKER_PY36_ONLY} @@ -2415,7 +2415,7 @@ def test_exporter_handles_overlapping_python_versions( ], ) def test_exporter_omits_unwanted_extras( - tmp_dir: str, poetry: Poetry, with_extras: bool, expected: list[str] + tmp_path: Path, poetry: Poetry, with_extras: bool, expected: list[str] ) -> None: # Testcase derived from # https://github.com/python-poetry/poetry/issues/5779 @@ -2474,7 +2474,7 @@ def test_exporter_omits_unwanted_extras( exporter = Exporter(poetry) if with_extras: exporter.only_groups(["with-extras"]) - exporter.export("requirements.txt", Path(tmp_dir), io) + exporter.export("requirements.txt", tmp_path, io) assert io.fetch_output() == "\n".join(expected) + "\n" @@ -2502,7 +2502,7 @@ def test_exporter_omits_unwanted_extras( ], ) def test_exporter_omits_and_includes_extras_for_txt_formats( - tmp_dir: str, poetry: Poetry, fmt: str, expected: list[str] + tmp_path: Path, poetry: Poetry, fmt: str, expected: list[str] ) -> None: poetry.locker.mock_lock_data( # type: ignore[attr-defined] { @@ -2553,16 +2553,16 @@ def test_exporter_omits_and_includes_extras_for_txt_formats( set_package_requires(poetry) exporter = Exporter(poetry) - exporter.export(fmt, Path(tmp_dir), "exported.txt") + exporter.export(fmt, tmp_path, "exported.txt") - with (Path(tmp_dir) / "exported.txt").open(encoding="utf-8") as f: + with (tmp_path / "exported.txt").open(encoding="utf-8") as f: content = f.read() assert content == "\n".join(expected) + "\n" def test_exporter_raises_exception_for_constraints_txt_with_editable_packages( - tmp_dir: str, poetry: Poetry + tmp_path: Path, poetry: Poetry ) -> None: poetry.locker.mock_lock_data( # type: ignore[attr-defined] { @@ -2605,12 +2605,12 @@ def test_exporter_raises_exception_for_constraints_txt_with_editable_packages( with pytest.raises(RuntimeError): exporter = Exporter(poetry) - exporter.export("constraints.txt", Path(tmp_dir), "constraints.txt") + exporter.export("constraints.txt", tmp_path, "constraints.txt") - assert not (Path(tmp_dir) / "constraints.txt").exists() + assert not (tmp_path / "constraints.txt").exists() -def test_exporter_respects_package_sources(tmp_dir: str, poetry: Poetry) -> None: +def test_exporter_respects_package_sources(tmp_path: Path, poetry: Poetry) -> None: poetry.locker.mock_lock_data( # type: ignore[attr-defined] { "package": [ @@ -2675,7 +2675,7 @@ def test_exporter_respects_package_sources(tmp_dir: str, poetry: Poetry) -> None io = BufferedIO() exporter = Exporter(poetry) - exporter.export("requirements.txt", Path(tmp_dir), io) + exporter.export("requirements.txt", tmp_path, io) expected = f"""\ foo @ https://example.com/foo-darwin.whl ; {MARKER_PY36} and {MARKER_DARWIN} @@ -2685,7 +2685,7 @@ def test_exporter_respects_package_sources(tmp_dir: str, poetry: Poetry) -> None assert io.fetch_output() == expected -def test_exporter_tolerates_non_existent_extra(tmp_dir: str, poetry: Poetry) -> None: +def test_exporter_tolerates_non_existent_extra(tmp_path: Path, poetry: Poetry) -> None: # foo actually has a 'bar' extra, but pyproject.toml mistakenly references a 'baz' # extra. poetry.locker.mock_lock_data( # type: ignore[attr-defined] @@ -2730,9 +2730,9 @@ def test_exporter_tolerates_non_existent_extra(tmp_dir: str, poetry: Poetry) -> poetry._package = root exporter = Exporter(poetry) - exporter.export("requirements.txt", Path(tmp_dir), "requirements.txt") + exporter.export("requirements.txt", tmp_path, "requirements.txt") - with (Path(tmp_dir) / "requirements.txt").open(encoding="utf-8") as f: + with (tmp_path / "requirements.txt").open(encoding="utf-8") as f: content = f.read() expected = f"""\ From 63765eb357d081dfc1ae9d5fa6d5c2f37e39fbc5 Mon Sep 17 00:00:00 2001 From: Bjorn Neergaard Date: Fri, 30 Sep 2022 21:37:12 -0600 Subject: [PATCH 3/4] refactor(tests): correctness and removal of dead code --- tests/conftest.py | 6 +----- tests/test_exporter.py | 35 +++++++++++++---------------------- 2 files changed, 14 insertions(+), 27 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index b4bd927..72f6735 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -52,12 +52,8 @@ def all(self) -> dict[str, Any]: def config_cache_dir(tmp_path: Path) -> Path: path = tmp_path / ".cache" / "pypoetry" path.mkdir(parents=True) - return path - -@pytest.fixture -def config_virtualenvs_path(config_cache_dir: Path) -> Path: - return config_cache_dir / "virtualenvs" + return path @pytest.fixture diff --git a/tests/test_exporter.py b/tests/test_exporter.py index f673b17..8ddf2fc 100644 --- a/tests/test_exporter.py +++ b/tests/test_exporter.py @@ -3,7 +3,6 @@ from pathlib import Path from typing import TYPE_CHECKING from typing import Any -from typing import Iterator import pytest @@ -38,7 +37,6 @@ if TYPE_CHECKING: from poetry.poetry import Poetry - from pytest_mock import MockerFixture from tests.conftest import Config from tests.types import FixtureDirGetter @@ -69,15 +67,8 @@ def _get_content_hash(self) -> str: @pytest.fixture -def working_directory() -> Path: - return Path(__file__).parent.parent - - -@pytest.fixture(autouse=True) -def mock_path_cwd( - mocker: MockerFixture, working_directory: Path -) -> Iterator[MockerFixture]: - yield mocker.patch("pathlib.Path.cwd", return_value=working_directory) +def plugin_root_uri() -> str: + return Path(__file__).parent.parent.as_uri() @pytest.fixture() @@ -809,7 +800,7 @@ def test_exporter_exports_requirements_txt_with_dev_packages_if_opted_in( assert content == expected -def test_exporter_exports_requirements_txt_without_groups_if_set_explicity( +def test_exporter_exports_requirements_txt_without_groups_if_set_explicitly( tmp_path: Path, poetry: Poetry ) -> None: poetry.locker.mock_lock_data( # type: ignore[attr-defined] @@ -1261,7 +1252,7 @@ def test_exporter_can_export_requirements_txt_with_git_packages_and_markers( def test_exporter_can_export_requirements_txt_with_directory_packages( - tmp_path: Path, poetry: Poetry, working_directory: Path + tmp_path: Path, poetry: Poetry, plugin_root_uri: str ) -> None: poetry.locker.mock_lock_data( # type: ignore[attr-defined] { @@ -1295,14 +1286,14 @@ def test_exporter_can_export_requirements_txt_with_directory_packages( content = f.read() expected = f"""\ -foo @ {working_directory.as_uri()}/tests/fixtures/sample_project ; {MARKER_PY} +foo @ {plugin_root_uri}/tests/fixtures/sample_project ; {MARKER_PY} """ assert content == expected def test_exporter_can_export_requirements_txt_with_nested_directory_packages( - tmp_path: Path, poetry: Poetry, working_directory: Path + tmp_path: Path, poetry: Poetry, plugin_root_uri: str ) -> None: poetry.locker.mock_lock_data( # type: ignore[attr-defined] { @@ -1365,7 +1356,7 @@ def test_exporter_can_export_requirements_txt_with_nested_directory_packages( with (tmp_path / "requirements.txt").open(encoding="utf-8") as f: content = f.read() - root_uri = f"{working_directory.as_uri()}/tests/fixtures" + root_uri = f"{plugin_root_uri}/tests/fixtures" expected = f"""\ bar @ {root_uri}/project_with_nested_local/bar ; {MARKER_PY} baz @ {root_uri}/project_with_nested_local ; {MARKER_PY} @@ -1376,7 +1367,7 @@ def test_exporter_can_export_requirements_txt_with_nested_directory_packages( def test_exporter_can_export_requirements_txt_with_directory_packages_and_markers( - tmp_path: Path, poetry: Poetry, working_directory: Path + tmp_path: Path, poetry: Poetry, plugin_root_uri: str ) -> None: poetry.locker.mock_lock_data( # type: ignore[attr-defined] { @@ -1411,7 +1402,7 @@ def test_exporter_can_export_requirements_txt_with_directory_packages_and_marker content = f.read() expected = f"""\ -foo @ {working_directory.as_uri()}/tests/fixtures/sample_project ;\ +foo @ {plugin_root_uri}/tests/fixtures/sample_project ;\ {MARKER_PY27.union(MARKER_PY36_ONLY)} """ @@ -1419,7 +1410,7 @@ def test_exporter_can_export_requirements_txt_with_directory_packages_and_marker def test_exporter_can_export_requirements_txt_with_file_packages( - tmp_path: Path, poetry: Poetry, working_directory: Path + tmp_path: Path, poetry: Poetry, plugin_root_uri: str ) -> None: poetry.locker.mock_lock_data( # type: ignore[attr-defined] { @@ -1453,7 +1444,7 @@ def test_exporter_can_export_requirements_txt_with_file_packages( content = f.read() expected = f"""\ -foo @ {working_directory.as_uri()}/tests/fixtures/distributions/demo-0.1.0.tar.gz ;\ +foo @ {plugin_root_uri}/tests/fixtures/distributions/demo-0.1.0.tar.gz ;\ {MARKER_PY} """ @@ -1461,7 +1452,7 @@ def test_exporter_can_export_requirements_txt_with_file_packages( def test_exporter_can_export_requirements_txt_with_file_packages_and_markers( - tmp_path: Path, poetry: Poetry, working_directory: Path + tmp_path: Path, poetry: Poetry, plugin_root_uri: str ) -> None: poetry.locker.mock_lock_data( # type: ignore[attr-defined] { @@ -1495,7 +1486,7 @@ def test_exporter_can_export_requirements_txt_with_file_packages_and_markers( with (tmp_path / "requirements.txt").open(encoding="utf-8") as f: content = f.read() - uri = f"{working_directory.as_uri()}/tests/fixtures/distributions/demo-0.1.0.tar.gz" + uri = f"{plugin_root_uri}/tests/fixtures/distributions/demo-0.1.0.tar.gz" expected = f"""\ foo @ {uri} ; {MARKER_PY27.union(MARKER_PY36_ONLY)} """ From 5d0d9f17e7e2896d77680378c2982c66fe19cd0e Mon Sep 17 00:00:00 2001 From: Bjorn Neergaard Date: Fri, 30 Sep 2022 21:59:41 -0600 Subject: [PATCH 4/4] refactor(tests): simplify fixture path logic --- tests/conftest.py | 12 ++++-------- tests/test_exporter.py | 35 ++++++++++++++--------------------- tests/types.py | 7 ------- 3 files changed, 18 insertions(+), 36 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index 72f6735..2a61753 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -24,7 +24,6 @@ from poetry.poetry import Poetry from pytest_mock import MockerFixture - from tests.types import FixtureDirGetter from tests.types import ProjectFactory @@ -95,16 +94,13 @@ def config( @pytest.fixture -def fixture_base() -> Path: - return Path(__file__).parent.joinpath("fixtures") +def fixture_root() -> Path: + return Path(__file__).parent / "fixtures" @pytest.fixture -def fixture_dir(fixture_base: Path) -> FixtureDirGetter: - def _fixture_dir(name: str) -> Path: - return fixture_base / name - - return _fixture_dir +def fixture_root_uri(fixture_root: Path) -> str: + return fixture_root.as_uri() @pytest.fixture() diff --git a/tests/test_exporter.py b/tests/test_exporter.py index 8ddf2fc..be1a6c5 100644 --- a/tests/test_exporter.py +++ b/tests/test_exporter.py @@ -39,7 +39,6 @@ from poetry.poetry import Poetry from tests.conftest import Config - from tests.types import FixtureDirGetter class Locker(BaseLocker): @@ -66,19 +65,14 @@ def _get_content_hash(self) -> str: return "123456789" -@pytest.fixture -def plugin_root_uri() -> str: - return Path(__file__).parent.parent.as_uri() - - @pytest.fixture() def locker() -> Locker: return Locker() @pytest.fixture -def poetry(fixture_dir: FixtureDirGetter, locker: Locker) -> Poetry: - p = Factory().create_poetry(fixture_dir("sample_project")) +def poetry(fixture_root: Path, locker: Locker) -> Poetry: + p = Factory().create_poetry(fixture_root / "sample_project") p._locker = locker return p @@ -1252,7 +1246,7 @@ def test_exporter_can_export_requirements_txt_with_git_packages_and_markers( def test_exporter_can_export_requirements_txt_with_directory_packages( - tmp_path: Path, poetry: Poetry, plugin_root_uri: str + tmp_path: Path, poetry: Poetry, fixture_root_uri: str ) -> None: poetry.locker.mock_lock_data( # type: ignore[attr-defined] { @@ -1286,14 +1280,14 @@ def test_exporter_can_export_requirements_txt_with_directory_packages( content = f.read() expected = f"""\ -foo @ {plugin_root_uri}/tests/fixtures/sample_project ; {MARKER_PY} +foo @ {fixture_root_uri}/sample_project ; {MARKER_PY} """ assert content == expected def test_exporter_can_export_requirements_txt_with_nested_directory_packages( - tmp_path: Path, poetry: Poetry, plugin_root_uri: str + tmp_path: Path, poetry: Poetry, fixture_root_uri: str ) -> None: poetry.locker.mock_lock_data( # type: ignore[attr-defined] { @@ -1356,18 +1350,17 @@ def test_exporter_can_export_requirements_txt_with_nested_directory_packages( with (tmp_path / "requirements.txt").open(encoding="utf-8") as f: content = f.read() - root_uri = f"{plugin_root_uri}/tests/fixtures" expected = f"""\ -bar @ {root_uri}/project_with_nested_local/bar ; {MARKER_PY} -baz @ {root_uri}/project_with_nested_local ; {MARKER_PY} -foo @ {root_uri}/sample_project ; {MARKER_PY} +bar @ {fixture_root_uri}/project_with_nested_local/bar ; {MARKER_PY} +baz @ {fixture_root_uri}/project_with_nested_local ; {MARKER_PY} +foo @ {fixture_root_uri}/sample_project ; {MARKER_PY} """ assert content == expected def test_exporter_can_export_requirements_txt_with_directory_packages_and_markers( - tmp_path: Path, poetry: Poetry, plugin_root_uri: str + tmp_path: Path, poetry: Poetry, fixture_root_uri: str ) -> None: poetry.locker.mock_lock_data( # type: ignore[attr-defined] { @@ -1402,7 +1395,7 @@ def test_exporter_can_export_requirements_txt_with_directory_packages_and_marker content = f.read() expected = f"""\ -foo @ {plugin_root_uri}/tests/fixtures/sample_project ;\ +foo @ {fixture_root_uri}/sample_project ;\ {MARKER_PY27.union(MARKER_PY36_ONLY)} """ @@ -1410,7 +1403,7 @@ def test_exporter_can_export_requirements_txt_with_directory_packages_and_marker def test_exporter_can_export_requirements_txt_with_file_packages( - tmp_path: Path, poetry: Poetry, plugin_root_uri: str + tmp_path: Path, poetry: Poetry, fixture_root_uri: str ) -> None: poetry.locker.mock_lock_data( # type: ignore[attr-defined] { @@ -1444,7 +1437,7 @@ def test_exporter_can_export_requirements_txt_with_file_packages( content = f.read() expected = f"""\ -foo @ {plugin_root_uri}/tests/fixtures/distributions/demo-0.1.0.tar.gz ;\ +foo @ {fixture_root_uri}/distributions/demo-0.1.0.tar.gz ;\ {MARKER_PY} """ @@ -1452,7 +1445,7 @@ def test_exporter_can_export_requirements_txt_with_file_packages( def test_exporter_can_export_requirements_txt_with_file_packages_and_markers( - tmp_path: Path, poetry: Poetry, plugin_root_uri: str + tmp_path: Path, poetry: Poetry, fixture_root_uri: str ) -> None: poetry.locker.mock_lock_data( # type: ignore[attr-defined] { @@ -1486,7 +1479,7 @@ def test_exporter_can_export_requirements_txt_with_file_packages_and_markers( with (tmp_path / "requirements.txt").open(encoding="utf-8") as f: content = f.read() - uri = f"{plugin_root_uri}/tests/fixtures/distributions/demo-0.1.0.tar.gz" + uri = f"{fixture_root_uri}/distributions/demo-0.1.0.tar.gz" expected = f"""\ foo @ {uri} ; {MARKER_PY27.union(MARKER_PY36_ONLY)} """ diff --git a/tests/types.py b/tests/types.py index c50ea19..3a1d6c7 100644 --- a/tests/types.py +++ b/tests/types.py @@ -5,8 +5,6 @@ if TYPE_CHECKING: - from pathlib import Path - from cleo.testers.command_tester import CommandTester from poetry.installation import Installer from poetry.installation.executor import Executor @@ -37,8 +35,3 @@ def __call__( install_deps: bool = True, ) -> Poetry: ... - - -class FixtureDirGetter(Protocol): - def __call__(self, name: str) -> Path: - ...