From 5d0d9f17e7e2896d77680378c2982c66fe19cd0e Mon Sep 17 00:00:00 2001 From: Bjorn Neergaard Date: Fri, 30 Sep 2022 21:59:41 -0600 Subject: [PATCH] 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: - ...