Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removing tmp_dir test fixture in favor of pytest's tmp_path. #7129

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 10 additions & 20 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
from poetry.utils.env import EnvManager
from poetry.utils.env import SystemEnv
from poetry.utils.env import VirtualEnv
from poetry.utils.helpers import remove_directory
from tests.helpers import MOCK_DEFAULT_GIT_REVISION
from tests.helpers import TestLocker
from tests.helpers import TestRepository
Expand Down Expand Up @@ -166,8 +165,8 @@ def with_chained_null_keyring(mocker: MockerFixture) -> None:


@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

Expand Down Expand Up @@ -216,8 +215,8 @@ def config(


@pytest.fixture()
def config_dir(tmp_dir: str) -> Path:
return Path(tempfile.mkdtemp(prefix="poetry_config_", dir=tmp_dir))
def config_dir(tmp_path: Path) -> Path:
return Path(tempfile.mkdtemp(prefix="poetry_config_", dir=str(tmp_path)))


@pytest.fixture(autouse=True)
Expand Down Expand Up @@ -292,15 +291,6 @@ def _fixture_dir(name: str) -> Path:
return _fixture_dir


@pytest.fixture
def tmp_dir() -> Iterator[str]:
dir_ = tempfile.mkdtemp(prefix="poetry_")

yield Path(dir_).resolve().as_posix()

remove_directory(dir_, force=True)


@pytest.fixture
def mocked_open_files(mocker: MockerFixture) -> list:
files = []
Expand All @@ -317,8 +307,8 @@ def mocked_open(self: Path, *args: Any, **kwargs: Any) -> TextIO:


@pytest.fixture
def tmp_venv(tmp_dir: str) -> Iterator[VirtualEnv]:
venv_path = Path(tmp_dir) / "venv"
def tmp_venv(tmp_path: Path) -> Iterator[VirtualEnv]:
venv_path = tmp_path / "venv"

EnvManager.build_venv(str(venv_path))

Expand Down Expand Up @@ -359,14 +349,14 @@ def repo(http: type[httpretty.httpretty]) -> TestRepository:

@pytest.fixture
def project_factory(
tmp_dir: str,
tmp_path: Path,
config: Config,
repo: TestRepository,
installed: Repository,
default_python: str,
load_required_fixtures: None,
) -> ProjectFactory:
workspace = Path(tmp_dir)
workspace = tmp_path

def _factory(
name: str | None = None,
Expand Down Expand Up @@ -453,10 +443,10 @@ def set_simple_log_formatter() -> None:


@pytest.fixture
def fixture_copier(fixture_base: Path, tmp_dir: str) -> FixtureCopier:
def fixture_copier(fixture_base: Path, tmp_path: Path) -> FixtureCopier:
def _copy(relative_path: str, target: Path | None = None) -> Path:
path = fixture_base.joinpath(relative_path)
target = target or Path(tmp_dir, relative_path)
target = target or Path(tmp_path, relative_path)
target.parent.mkdir(parents=True, exist_ok=True)

if target.exists():
Expand Down
6 changes: 3 additions & 3 deletions tests/console/commands/env/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 @@ -12,6 +11,7 @@

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

from tests.helpers import PoetryTestApplication

Expand All @@ -25,8 +25,8 @@ def venv_name(app: PoetryTestApplication) -> str:


@pytest.fixture
def venv_cache(tmp_dir: str) -> Path:
return Path(tmp_dir)
def venv_cache(tmp_path: Path) -> Path:
return tmp_path


@pytest.fixture(scope="module")
Expand Down
4 changes: 2 additions & 2 deletions tests/console/commands/self/test_show_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def plugin_package(plugin_package_requires_dist: list[str]) -> Package:


@pytest.fixture()
def plugin_distro(plugin_package: Package, tmp_dir: str) -> metadata.Distribution:
def plugin_distro(plugin_package: Package, tmp_path: Path) -> metadata.Distribution:
class MockDistribution(metadata.Distribution):
def read_text(self, filename: str) -> str | None:
if filename == "METADATA":
Expand All @@ -81,7 +81,7 @@ def read_text(self, filename: str) -> str | None:
return None

def locate_file(self, path: PathLike[str]) -> PathLike[str]:
return Path(tmp_dir, path)
return Path(tmp_path, path)

return MockDistribution()

Expand Down
10 changes: 6 additions & 4 deletions tests/console/commands/test_new.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,18 +149,20 @@ def test_command_new(
package_path: str,
include_from: str | None,
tester: CommandTester,
tmp_dir: str,
tmp_path: Path,
):
path = Path(tmp_dir) / directory
path = tmp_path / directory
options.append(path.as_posix())
tester.execute(" ".join(options))
verify_project_directory(path, package_name, package_path, include_from)


@pytest.mark.parametrize(("fmt",), [(None,), ("md",), ("rst",), ("adoc",), ("creole",)])
def test_command_new_with_readme(fmt: str | None, tester: CommandTester, tmp_dir: str):
def test_command_new_with_readme(
fmt: str | None, tester: CommandTester, tmp_path: Path
):
package = "package"
path = Path(tmp_dir) / package
path = tmp_path / package
options = [path.as_posix()]

if fmt:
Expand Down
4 changes: 2 additions & 2 deletions tests/console/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ def installer() -> NoopInstaller:


@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)

Expand Down
32 changes: 16 additions & 16 deletions tests/installation/test_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@


@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)
Expand Down Expand Up @@ -104,13 +104,13 @@ def test_execute_executes_a_batch_of_operations(
config: Config,
pool: RepositoryPool,
io: BufferedIO,
tmp_dir: str,
tmp_path: Path,
mock_file_downloads: None,
env: MockEnv,
):
pip_install = mocker.patch("poetry.installation.executor.pip_install")

config.merge({"cache-dir": tmp_dir})
config.merge({"cache-dir": str(tmp_path)})

executor = Executor(env, pool, config, io)

Expand Down Expand Up @@ -205,13 +205,13 @@ def test_execute_prints_warning_for_yanked_package(
config: Config,
pool: RepositoryPool,
io: BufferedIO,
tmp_dir: str,
tmp_path: Path,
mock_file_downloads: None,
env: MockEnv,
operations: list[Operation],
has_warning: bool,
):
config.merge({"cache-dir": tmp_dir})
config.merge({"cache-dir": str(tmp_path)})

executor = Executor(env, pool, config, io)

Expand Down Expand Up @@ -293,11 +293,11 @@ def test_execute_works_with_ansi_output(
config: Config,
pool: RepositoryPool,
io_decorated: BufferedIO,
tmp_dir: str,
tmp_path: Path,
mock_file_downloads: None,
env: MockEnv,
):
config.merge({"cache-dir": tmp_dir})
config.merge({"cache-dir": str(tmp_path)})

executor = Executor(env, pool, config, io_decorated)

Expand Down Expand Up @@ -335,11 +335,11 @@ def test_execute_works_with_no_ansi_output(
config: Config,
pool: RepositoryPool,
io_not_decorated: BufferedIO,
tmp_dir: str,
tmp_path: Path,
mock_file_downloads: None,
env: MockEnv,
):
config.merge({"cache-dir": tmp_dir})
config.merge({"cache-dir": str(tmp_path)})

executor = Executor(env, pool, config, io_not_decorated)

Expand Down Expand Up @@ -424,7 +424,7 @@ def write_line(string: str, **kwargs: Any) -> None:
def test_executor_should_delete_incomplete_downloads(
config: Config,
io: BufferedIO,
tmp_dir: str,
tmp_path: Path,
mocker: MockerFixture,
pool: RepositoryPool,
mock_file_downloads: None,
Expand All @@ -433,7 +433,7 @@ def test_executor_should_delete_incomplete_downloads(
fixture = Path(__file__).parent.parent.joinpath(
"fixtures/distributions/demo-0.1.0-py2.py3-none-any.whl"
)
destination_fixture = Path(tmp_dir) / "tomlkit-0.5.3-py2.py3-none-any.whl"
destination_fixture = tmp_path / "tomlkit-0.5.3-py2.py3-none-any.whl"
shutil.copyfile(str(fixture), str(destination_fixture))
mocker.patch(
"poetry.installation.executor.Executor._download_archive",
Expand All @@ -445,10 +445,10 @@ def test_executor_should_delete_incomplete_downloads(
)
mocker.patch(
"poetry.installation.chef.Chef.get_cache_directory_for_link",
return_value=Path(tmp_dir),
return_value=tmp_path,
)

config.merge({"cache-dir": tmp_dir})
config.merge({"cache-dir": str(tmp_path)})

executor = Executor(env, pool, config, io)

Expand Down Expand Up @@ -727,7 +727,7 @@ def test_executer_fallback_on_poetry_create_error(
config: Config,
pool: RepositoryPool,
io: BufferedIO,
tmp_dir: str,
tmp_path: Path,
mock_file_downloads: None,
env: MockEnv,
):
Expand All @@ -740,7 +740,7 @@ def test_executer_fallback_on_poetry_create_error(
"poetry.factory.Factory.create_poetry", side_effect=RuntimeError
)

config.merge({"cache-dir": tmp_dir})
config.merge({"cache-dir": str(tmp_path)})

executor = Executor(env, pool, config, io)

Expand Down
12 changes: 6 additions & 6 deletions tests/masonry/builders/test_editable_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ def env_manager(simple_poetry: Poetry) -> EnvManager:


@pytest.fixture
def tmp_venv(tmp_dir: str, env_manager: EnvManager) -> VirtualEnv:
venv_path = Path(tmp_dir) / "venv"
def tmp_venv(tmp_path: Path, env_manager: EnvManager) -> VirtualEnv:
venv_path = tmp_path / "venv"

env_manager.build_venv(str(venv_path))

Expand Down Expand Up @@ -213,10 +213,10 @@ def test_builder_installs_proper_files_for_standard_packages(


def test_builder_falls_back_on_setup_and_pip_for_packages_with_build_scripts(
mocker: MockerFixture, extended_poetry: Poetry, tmp_dir: str
mocker: MockerFixture, extended_poetry: Poetry, tmp_path: Path
):
pip_install = mocker.patch("poetry.masonry.builders.editable.pip_install")
env = MockEnv(path=Path(tmp_dir) / "foo")
env = MockEnv(path=tmp_path / "foo")
builder = EditableBuilder(extended_poetry, env, NullIO())

builder.build()
Expand All @@ -226,10 +226,10 @@ def test_builder_falls_back_on_setup_and_pip_for_packages_with_build_scripts(
assert [] == env.executed


def test_builder_setup_generation_runs_with_pip_editable(tmp_dir: str) -> None:
def test_builder_setup_generation_runs_with_pip_editable(tmp_path: Path) -> None:
# create an isolated copy of the project
fixture = Path(__file__).parent.parent.parent / "fixtures" / "extended_project"
extended_project = Path(tmp_dir) / "extended_project"
extended_project = tmp_path / "extended_project"

shutil.copytree(fixture, extended_project)
assert extended_project.exists()
Expand Down
2 changes: 1 addition & 1 deletion tests/plugins/test_plugin_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def activate(self, poetry: Poetry, io: BufferedIO) -> None:


@pytest.fixture()
def poetry(tmp_dir: str, config: Config) -> Poetry:
def poetry(config: Config) -> Poetry:
poetry = Poetry(
CWD / "pyproject.toml",
{},
Expand Down
4 changes: 2 additions & 2 deletions tests/repositories/test_installed_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,9 @@ def test_load_successful(repository: InstalledRepository):


def test_load_successful_with_invalid_distribution(
caplog: LogCaptureFixture, mocker: MockerFixture, env: MockEnv, tmp_dir: str
caplog: LogCaptureFixture, mocker: MockerFixture, env: MockEnv, tmp_path: Path
) -> None:
invalid_dist_info = Path(tmp_dir) / "site-packages" / "invalid-0.1.0.dist-info"
invalid_dist_info = tmp_path / "site-packages" / "invalid-0.1.0.dist-info"
invalid_dist_info.mkdir(parents=True)
mocker.patch(
"poetry.utils._compat.metadata.Distribution.discover",
Expand Down
Loading