Skip to content

Commit

Permalink
tests: consistently use fixture_dir
Browse files Browse the repository at this point in the history
  • Loading branch information
radoering committed Apr 16, 2023
1 parent 4c37735 commit 3855bc5
Show file tree
Hide file tree
Showing 17 changed files with 419 additions and 476 deletions.
10 changes: 5 additions & 5 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,11 @@ def http() -> Iterator[type[httpretty.httpretty]]:
yield httpretty


@pytest.fixture
def project_root() -> Path:
return Path(__file__).parent.parent


@pytest.fixture(scope="session")
def fixture_base() -> Path:
return Path(__file__).parent / "fixtures"
Expand Down Expand Up @@ -417,11 +422,6 @@ def _factory(
return _factory


@pytest.fixture
def project_root() -> Path:
return Path(__file__).parent.parent


@pytest.fixture(autouse=True)
def set_simple_log_formatter() -> None:
"""
Expand Down
10 changes: 4 additions & 6 deletions tests/console/commands/self/test_update.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from __future__ import annotations

from pathlib import Path
from typing import TYPE_CHECKING

import pytest
Expand All @@ -20,12 +19,11 @@

from tests.helpers import TestRepository
from tests.types import CommandTesterFactory
from tests.types import FixtureDirGetter

FIXTURES = Path(__file__).parent.joinpath("fixtures")


@pytest.fixture()
def setup(mocker: MockerFixture, fixture_dir: Path):
@pytest.fixture
def setup(mocker: MockerFixture, fixture_dir: FixtureDirGetter) -> None:
mocker.patch.object(
Executor,
"_download",
Expand All @@ -46,7 +44,7 @@ def test_self_update_can_update_from_recommended_installation(
tester: CommandTester,
repo: TestRepository,
installed: TestRepository,
):
) -> None:
new_version = Version.parse(__version__).next_minor().text

old_poetry = Package("poetry", __version__)
Expand Down
15 changes: 9 additions & 6 deletions tests/console/commands/test_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,26 @@
import shutil
import tarfile

from pathlib import Path
from typing import TYPE_CHECKING

from poetry.factory import Factory


if TYPE_CHECKING:
from pathlib import Path

from poetry.utils.env import VirtualEnv
from tests.types import CommandTesterFactory
from tests.types import FixtureDirGetter


def test_build_with_multiple_readme_files(
tmp_path: Path, tmp_venv: VirtualEnv, command_tester_factory: CommandTesterFactory
):
source_dir = (
Path(__file__).parent.parent.parent / "fixtures" / "with_multiple_readme_files"
)
fixture_dir: FixtureDirGetter,
tmp_path: Path,
tmp_venv: VirtualEnv,
command_tester_factory: CommandTesterFactory,
) -> None:
source_dir = fixture_dir("with_multiple_readme_files")
target_dir = tmp_path / "project"
shutil.copytree(str(source_dir), str(target_dir))

Expand Down
24 changes: 10 additions & 14 deletions tests/console/commands/test_check.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from __future__ import annotations

from pathlib import Path
from typing import TYPE_CHECKING

import pytest
Expand All @@ -11,14 +10,15 @@
from pytest_mock import MockerFixture

from tests.types import CommandTesterFactory
from tests.types import FixtureDirGetter


@pytest.fixture()
def tester(command_tester_factory: CommandTesterFactory) -> CommandTester:
return command_tester_factory("check")


def test_check_valid(tester: CommandTester):
def test_check_valid(tester: CommandTester) -> None:
tester.execute()

expected = """\
Expand All @@ -28,17 +28,14 @@ def test_check_valid(tester: CommandTester):
assert tester.io.fetch_output() == expected


def test_check_invalid(mocker: MockerFixture, tester: CommandTester):
def test_check_invalid(
mocker: MockerFixture, tester: CommandTester, fixture_dir: FixtureDirGetter
) -> None:
from poetry.toml import TOMLFile

mocker.patch(
"poetry.poetry.Poetry.file",
return_value=TOMLFile(
Path(__file__).parent.parent.parent
/ "fixtures"
/ "invalid_pyproject"
/ "pyproject.toml"
),
return_value=TOMLFile(fixture_dir("invalid_pyproject") / "pyproject.toml"),
new_callable=mocker.PropertyMock,
)

Expand All @@ -61,13 +58,12 @@ def test_check_invalid(mocker: MockerFixture, tester: CommandTester):
assert tester.io.fetch_error() == expected


def test_check_private(mocker: MockerFixture, tester: CommandTester):
def test_check_private(
mocker: MockerFixture, tester: CommandTester, fixture_dir: FixtureDirGetter
) -> None:
mocker.patch(
"poetry.factory.Factory.locate",
return_value=Path(__file__).parent.parent.parent
/ "fixtures"
/ "private_pyproject"
/ "pyproject.toml",
return_value=fixture_dir("private_pyproject") / "pyproject.toml",
)

tester.execute()
Expand Down
14 changes: 8 additions & 6 deletions tests/console/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import os

from pathlib import Path
from typing import TYPE_CHECKING

import pytest
Expand All @@ -22,6 +21,7 @@

if TYPE_CHECKING:
from collections.abc import Iterator
from pathlib import Path

from pytest_mock import MockerFixture

Expand All @@ -31,6 +31,7 @@
from poetry.utils.env import Env
from tests.conftest import Config
from tests.types import CommandTesterFactory
from tests.types import FixtureDirGetter
from tests.types import ProjectFactory


Expand Down Expand Up @@ -96,11 +97,12 @@ def project_directory() -> str:


@pytest.fixture
def poetry(project_directory: str, project_factory: ProjectFactory) -> Poetry:
return project_factory(
name="simple",
source=Path(__file__).parent.parent / "fixtures" / project_directory,
)
def poetry(
project_directory: str,
project_factory: ProjectFactory,
fixture_dir: FixtureDirGetter,
) -> Poetry:
return project_factory(name="simple", source=fixture_dir(project_directory))


@pytest.fixture
Expand Down
12 changes: 2 additions & 10 deletions tests/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,6 @@ def get_dependency(
return Factory.create_dependency(name, constraint or "*", groups=groups)


def fixture(path: str | None = None) -> Path:
if path:
return FIXTURE_PATH / path
else:
return FIXTURE_PATH


def copy_or_symlink(source: Path, dest: Path) -> None:
if dest.is_symlink() or dest.is_file():
dest.unlink() # missing_ok is only available in Python >= 3.8
Expand Down Expand Up @@ -113,7 +106,7 @@ def mock_clone(
parsed = ParsedUrl.parse(url)
path = re.sub(r"(.git)?$", "", parsed.pathname.lstrip("/"))

folder = Path(__file__).parent / "fixtures" / "git" / parsed.resource / path
folder = FIXTURE_PATH / "git" / parsed.resource / path

if not source_root:
source_root = Path(Config.create().get("cache-dir")) / "src"
Expand All @@ -128,8 +121,7 @@ def mock_clone(
def mock_download(url: str, dest: Path) -> None:
parts = urllib.parse.urlparse(url)

fixtures = Path(__file__).parent / "fixtures"
fixture = fixtures / parts.path.lstrip("/")
fixture = FIXTURE_PATH / parts.path.lstrip("/")

copy_or_symlink(fixture, dest)

Expand Down
34 changes: 17 additions & 17 deletions tests/inspection/test_info.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from __future__ import annotations

from pathlib import Path
from subprocess import CalledProcessError
from typing import TYPE_CHECKING

Expand All @@ -13,10 +12,11 @@


if TYPE_CHECKING:
from pathlib import Path

from pytest_mock import MockerFixture

FIXTURE_DIR_BASE = Path(__file__).parent.parent / "fixtures"
FIXTURE_DIR_INSPECTIONS = FIXTURE_DIR_BASE / "inspection"
from tests.types import FixtureDirGetter


@pytest.fixture(autouse=True)
Expand All @@ -25,13 +25,13 @@ def pep517_metadata_mock() -> None:


@pytest.fixture
def demo_sdist() -> Path:
return FIXTURE_DIR_BASE / "distributions" / "demo-0.1.0.tar.gz"
def demo_sdist(fixture_dir: FixtureDirGetter) -> Path:
return fixture_dir("distributions") / "demo-0.1.0.tar.gz"


@pytest.fixture
def demo_wheel() -> Path:
return FIXTURE_DIR_BASE / "distributions" / "demo-0.1.0-py2.py3-none-any.whl"
def demo_wheel(fixture_dir: FixtureDirGetter) -> Path:
return fixture_dir("distributions") / "demo-0.1.0-py2.py3-none-any.whl"


@pytest.fixture
Expand Down Expand Up @@ -128,15 +128,15 @@ def test_info_from_bdist(demo_wheel: Path) -> None:
demo_check_info(info)


def test_info_from_poetry_directory() -> None:
def test_info_from_poetry_directory(fixture_dir: FixtureDirGetter) -> None:
info = PackageInfo.from_directory(
FIXTURE_DIR_INSPECTIONS / "demo", disable_build=True
fixture_dir("inspection") / "demo", disable_build=True
)
demo_check_info(info)


def test_info_from_poetry_directory_fallback_on_poetry_create_error(
mocker: MockerFixture,
mocker: MockerFixture, fixture_dir: FixtureDirGetter
) -> None:
mock_create_poetry = mocker.patch(
"poetry.inspection.info.Factory.create_poetry", side_effect=RuntimeError
Expand All @@ -146,16 +146,16 @@ def test_info_from_poetry_directory_fallback_on_poetry_create_error(
"poetry.inspection.info.get_pep517_metadata"
)

PackageInfo.from_directory(FIXTURE_DIR_INSPECTIONS / "demo_poetry_package")
PackageInfo.from_directory(fixture_dir("inspection") / "demo_poetry_package")

assert mock_create_poetry.call_count == 1
assert mock_get_poetry_package.call_count == 1
assert mock_get_pep517_metadata.call_count == 1


def test_info_from_requires_txt() -> None:
def test_info_from_requires_txt(fixture_dir: FixtureDirGetter) -> None:
info = PackageInfo.from_metadata(
FIXTURE_DIR_INSPECTIONS / "demo_only_requires_txt.egg-info"
fixture_dir("inspection") / "demo_only_requires_txt.egg-info"
)
assert info is not None
demo_check_info(info)
Expand All @@ -171,9 +171,9 @@ def test_info_from_setup_cfg(demo_setup_cfg: Path) -> None:
demo_check_info(info, requires_dist={"package"})


def test_info_no_setup_pkg_info_no_deps() -> None:
def test_info_no_setup_pkg_info_no_deps(fixture_dir: FixtureDirGetter) -> None:
info = PackageInfo.from_directory(
FIXTURE_DIR_INSPECTIONS / "demo_no_setup_pkg_info_no_deps",
fixture_dir("inspection") / "demo_no_setup_pkg_info_no_deps",
disable_build=True,
)
assert info.name == "demo"
Expand Down Expand Up @@ -250,8 +250,8 @@ def test_info_setup_missing_mandatory_should_trigger_pep517(
assert spy.call_count == 1


def test_info_prefer_poetry_config_over_egg_info() -> None:
def test_info_prefer_poetry_config_over_egg_info(fixture_dir: FixtureDirGetter) -> None:
info = PackageInfo.from_directory(
FIXTURE_DIR_INSPECTIONS / "demo_with_obsolete_egg_info"
fixture_dir("inspection") / "demo_with_obsolete_egg_info"
)
demo_check_info(info)
10 changes: 6 additions & 4 deletions tests/installation/test_pip_installer.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

from poetry.utils.env import VirtualEnv
from tests.conftest import Config
from tests.types import FixtureDirGetter


@pytest.fixture
Expand Down Expand Up @@ -278,7 +279,10 @@ def test_install_with_trusted_host(config: Config, env: NullEnv) -> None:


def test_install_directory_fallback_on_poetry_create_error(
mocker: MockerFixture, tmp_venv: VirtualEnv, pool: RepositoryPool
mocker: MockerFixture,
tmp_venv: VirtualEnv,
pool: RepositoryPool,
fixture_dir: FixtureDirGetter,
) -> None:
mock_create_poetry = mocker.patch(
"poetry.factory.Factory.create_poetry", side_effect=RuntimeError
Expand All @@ -293,9 +297,7 @@ def test_install_directory_fallback_on_poetry_create_error(
"demo",
"1.0.0",
source_type="directory",
source_url=str(
Path(__file__).parent.parent / "fixtures/inspection/demo_poetry_package"
),
source_url=str(fixture_dir("inspection") / "demo_poetry_package"),
)

installer = PipInstaller(tmp_venv, NullIO(), pool)
Expand Down
Loading

0 comments on commit 3855bc5

Please sign in to comment.