Skip to content

Commit

Permalink
refactor(tests): simplify fixture path logic
Browse files Browse the repository at this point in the history
  • Loading branch information
neersighted committed Oct 1, 2022
1 parent 46a8b37 commit 0eb0685
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 36 deletions.
12 changes: 4 additions & 8 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
from poetry.poetry import Poetry
from pytest_mock import MockerFixture

from tests.types import FixtureDirGetter
from tests.types import ProjectFactory


Expand Down Expand Up @@ -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()
Expand Down
35 changes: 14 additions & 21 deletions tests/test_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
from poetry.poetry import Poetry

from tests.conftest import Config
from tests.types import FixtureDirGetter


class Locker(BaseLocker):
Expand All @@ -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
Expand Down Expand Up @@ -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]
{
Expand Down Expand Up @@ -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]
{
Expand Down Expand Up @@ -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]
{
Expand Down Expand Up @@ -1402,15 +1395,15 @@ 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)}
"""

assert content == expected


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]
{
Expand Down Expand Up @@ -1444,15 +1437,15 @@ 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}
"""

assert content == expected


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]
{
Expand Down Expand Up @@ -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)}
"""
Expand Down
7 changes: 0 additions & 7 deletions tests/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -37,8 +35,3 @@ def __call__(
install_deps: bool = True,
) -> Poetry:
...


class FixtureDirGetter(Protocol):
def __call__(self, name: str) -> Path:
...

0 comments on commit 0eb0685

Please sign in to comment.