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

tests: stop using tmp_dir fixture in favor of pytest.tmp_path fixture #7412

Merged
merged 3 commits into from
Apr 15, 2023
Merged
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
6 changes: 6 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ jobs:
if: ${{ matrix.os == 'Windows' }}
run: echo "$APPDATA\Python\Scripts" >> $GITHUB_PATH

- name: Enable long paths for git on Windows
if: ${{ matrix.os == 'Windows' }}
# Enable handling long path names (+260 char) on the Windows platform
# https://docs.microsoft.com/en-us/windows/win32/fileio/naming-a-file#maximum-path-length-limitation
run: git config --system core.longpaths true

- name: Configure poetry
run: poetry config virtualenvs.in-project true

Expand Down
2 changes: 1 addition & 1 deletion src/poetry/utils/pip.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,4 @@ def pip_install(
try:
return environment.run_pip(*args)
except EnvCommandError as e:
raise PoetryException(f"Failed to install {path.as_posix()}") from e
raise PoetryException(f"Failed to install {path}") from e
40 changes: 14 additions & 26 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import re
import shutil
import sys
import tempfile

from contextlib import suppress
from pathlib import Path
Expand All @@ -28,7 +27,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 @@ -169,8 +167,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 @@ -219,8 +217,10 @@ 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:
path = tmp_path / "config"
path.mkdir()
return path


@pytest.fixture(autouse=True)
Expand Down Expand Up @@ -296,18 +296,8 @@ def _fixture_dir(name: str) -> Path:


@pytest.fixture
def tmp_dir() -> Iterator[str]:
dir_ = tempfile.mkdtemp(prefix="poetry_")
martin-kokos marked this conversation as resolved.
Show resolved Hide resolved
path = Path(dir_)

yield path.resolve().as_posix()

remove_directory(path, force=True)


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

Expand Down Expand Up @@ -348,14 +338,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 All @@ -380,9 +370,7 @@ def _factory(
project_dir.mkdir(parents=True, exist_ok=True)

if pyproject_content:
with project_dir.joinpath("pyproject.toml").open(
"w", encoding="utf-8"
) as f:
with (project_dir / "pyproject.toml").open("w", encoding="utf-8") as f:
f.write(pyproject_content)
else:
assert name is not None
Expand Down Expand Up @@ -446,10 +434,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)
path = fixture_base / relative_path
target = target or (tmp_path / relative_path)
target.parent.mkdir(parents=True, exist_ok=True)

if target.exists():
Expand Down
8 changes: 4 additions & 4 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:
martin-kokos marked this conversation as resolved.
Show resolved Hide resolved
return tmp_path


@pytest.fixture(scope="module")
Expand All @@ -49,7 +49,7 @@ def venvs_in_cache_dirs(
) -> list[str]:
directories = []
for version in python_versions:
directory = venv_cache.joinpath(f"{venv_name}-py{version}")
directory = venv_cache / f"{venv_name}-py{version}"
directory.mkdir(parents=True, exist_ok=True)
directories.append(directory.name)
return directories
Expand Down
6 changes: 2 additions & 4 deletions tests/console/commands/env/test_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def test_none_activated(
):
mocker.patch("poetry.utils.env.EnvManager.get", return_value=env)
tester.execute()
expected = "\n".join(venvs_in_cache_dirs).strip()
expected = "\n".join(venvs_in_cache_dirs)
assert tester.io.fetch_output().strip() == expected


Expand All @@ -50,9 +50,7 @@ def test_activated(
venv_activate_37: None,
):
tester.execute()
expected = (
"\n".join(venvs_in_cache_dirs).strip().replace("py3.7", "py3.7 (Activated)")
)
expected = "\n".join(venvs_in_cache_dirs).replace("py3.7", "py3.7 (Activated)")
assert tester.io.fetch_output().strip() == expected


Expand Down
6 changes: 3 additions & 3 deletions tests/console/commands/self/test_show_plugins.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
from typing import Any
from typing import Callable
Expand All @@ -17,6 +16,7 @@

if TYPE_CHECKING:
from os import PathLike
from pathlib import Path

from cleo.io.io import IO
from cleo.testers.command_tester import CommandTester
Expand Down 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 tmp_path / path

return MockDistribution()

Expand Down
18 changes: 10 additions & 8 deletions tests/console/commands/test_new.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,18 +155,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
options.append(path.as_posix())
path = tmp_path / directory
options.append(str(path))
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(
martin-kokos marked this conversation as resolved.
Show resolved Hide resolved
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 All @@ -191,7 +193,7 @@ def test_respect_prefer_active_on_new(
config: Config,
mocker: MockerFixture,
tester: CommandTester,
tmp_dir: str,
tmp_path: Path,
):
from poetry.utils.env import GET_PYTHON_VERSION_ONELINER

Expand All @@ -208,8 +210,8 @@ def mock_check_output(cmd: str, *_: Any, **__: Any) -> str:
config.config["virtualenvs"]["prefer-active-python"] = prefer_active

package = "package"
path = Path(tmp_dir) / package
options = [path.as_posix()]
path = tmp_path / package
options = [str(path)]
tester.execute(" ".join(options))

pyproject_file = path / "pyproject.toml"
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
4 changes: 3 additions & 1 deletion tests/inspection/test_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ def demo_wheel() -> Path:

@pytest.fixture
def source_dir(tmp_path: Path) -> Path:
return Path(tmp_path.as_posix())
path = tmp_path / "source"
path.mkdir()
return path


@pytest.fixture
Expand Down
Loading