Skip to content

Commit

Permalink
Migrate tests to use pathlib.Path
Browse files Browse the repository at this point in the history
The pip-specific Path implementation has been removed, and all its
usages replaced by pathlib.Path. The tmpdir and tmpdir_factory fixtures
are also removed, and all usages are replaced by tmp_path and
tmp_path_factory, which use pathlib.Path.

As anyone might expect, the bulk of the work is simply adding str()
everywhere.
  • Loading branch information
uranusjr committed Jun 7, 2022
1 parent e58a8a5 commit ab05851
Show file tree
Hide file tree
Showing 78 changed files with 1,872 additions and 1,909 deletions.
185 changes: 87 additions & 98 deletions tests/conftest.py

Large diffs are not rendered by default.

13 changes: 7 additions & 6 deletions tests/functional/test_broken_stdout.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import os
import pathlib
import subprocess
from typing import List, Tuple

from tests.lib.path import Path

_BROKEN_STDOUT_RETURN_CODE = 120


Expand Down Expand Up @@ -47,13 +45,16 @@ def test_broken_stdout_pipe(deprecated_python: bool) -> None:
assert returncode == _BROKEN_STDOUT_RETURN_CODE


def test_broken_stdout_pipe__log_option(deprecated_python: bool, tmpdir: Path) -> None:
def test_broken_stdout_pipe__log_option(
deprecated_python: bool,
tmp_path: pathlib.Path,
) -> None:
"""
Test a broken pipe to stdout when --log is passed.
"""
log_path = os.path.join(str(tmpdir), "log.txt")
log_path = tmp_path.joinpath("log.txt")
stderr, returncode = setup_broken_stdout_test(
["pip", "--log", log_path, "list"],
["pip", "--log", str(log_path), "list"],
deprecated_python=deprecated_python,
)

Expand Down
18 changes: 9 additions & 9 deletions tests/functional/test_build_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,11 @@ def run_with_build_env(
" ",
)
)
args = ["python", build_env_script]
args = ["python", str(build_env_script)]
if test_script_contents is not None:
test_script = script.scratch_path / "test.py"
test_script.write_text(dedent(test_script_contents))
args.append(test_script)
args.append(str(test_script))
return script.run(*args)


Expand All @@ -89,7 +89,7 @@ def test_build_env_allow_empty_requirements_install() -> None:
def test_build_env_allow_only_one_install(script: PipTestEnvironment) -> None:
create_basic_wheel_for_package(script, "foo", "1.0")
create_basic_wheel_for_package(script, "bar", "1.0")
finder = make_test_finder(find_links=[script.scratch_path])
finder = make_test_finder(find_links=[str(script.scratch_path)])
build_env = BuildEnvironment()
for prefix in ("normal", "overlay"):
build_env.install_requirements(
Expand All @@ -112,7 +112,7 @@ def test_build_env_requirements_check(script: PipTestEnvironment) -> None:
create_basic_wheel_for_package(script, "bar", "3.0")
create_basic_wheel_for_package(script, "other", "0.5")

script.pip_install_local("-f", script.scratch_path, "foo", "bar", "other")
script.pip_install_local("-f", str(script.scratch_path), "foo", "bar", "other")

run_with_build_env(
script,
Expand Down Expand Up @@ -211,19 +211,19 @@ def test_build_env_isolation(script: PipTestEnvironment) -> None:
pkg_whl = create_basic_wheel_for_package(script, "pkg", "1.0")

# Install it to site packages.
script.pip_install_local(pkg_whl)
script.pip_install_local(str(pkg_whl))

# And a copy in the user site.
script.pip_install_local("--ignore-installed", "--user", pkg_whl)
script.pip_install_local("--ignore-installed", "--user", str(pkg_whl))

# And to another directory available through a .pth file.
target = script.scratch_path / "pth_install"
script.pip_install_local("-t", target, pkg_whl)
(script.site_packages_path / "build_requires.pth").write_text(str(target) + "\n")
script.pip_install_local("-t", str(target), str(pkg_whl))
(script.site_packages_path / "build_requires.pth").write_text(f"{target}\n")

# And finally to yet another directory available through PYTHONPATH.
target = script.scratch_path / "pypath_install"
script.pip_install_local("-t", target, pkg_whl)
script.pip_install_local("-t", str(target), str(pkg_whl))
script.environ["PYTHONPATH"] = target

run_with_build_env(
Expand Down
26 changes: 13 additions & 13 deletions tests/functional/test_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def test_basic_check_missing_dependency(script: PipTestEnvironment) -> None:
install_requires=["missing==0.1"],
)
# Let's install pkga without its dependency
res = script.pip("install", "--no-index", pkga_path, "--no-deps")
res = script.pip("install", "--no-index", str(pkga_path), "--no-deps")
assert "Successfully installed pkga-1.0" in res.stdout, str(res)

result = script.pip("check", expect_error=True)
Expand All @@ -49,7 +49,7 @@ def test_basic_check_broken_dependency(script: PipTestEnvironment) -> None:
install_requires=["broken>=1.0"],
)
# Let's install pkga without its dependency
res = script.pip("install", "--no-index", pkga_path, "--no-deps")
res = script.pip("install", "--no-index", str(pkga_path), "--no-deps")
assert "Successfully installed pkga-1.0" in res.stdout, str(res)

# Setup broken==0.1
Expand All @@ -62,7 +62,7 @@ def test_basic_check_broken_dependency(script: PipTestEnvironment) -> None:
res = script.pip(
"install",
"--no-index",
broken_path,
str(broken_path),
"--no-warn-conflicts",
)
assert "Successfully installed broken-0.1" in res.stdout, str(res)
Expand All @@ -84,7 +84,7 @@ def test_basic_check_broken_dependency_and_missing_dependency(
install_requires=["broken>=1.0"],
)
# Let's install pkga without its dependency
res = script.pip("install", "--no-index", pkga_path, "--no-deps")
res = script.pip("install", "--no-index", str(pkga_path), "--no-deps")
assert "Successfully installed pkga-1.0" in res.stdout, str(res)

# Setup broken==0.1
Expand All @@ -95,7 +95,7 @@ def test_basic_check_broken_dependency_and_missing_dependency(
install_requires=["missing"],
)
# Let's install broken==0.1
res = script.pip("install", "--no-index", broken_path, "--no-deps")
res = script.pip("install", "--no-index", str(broken_path), "--no-deps")
assert "Successfully installed broken-0.1" in res.stdout, str(res)

result = script.pip("check", expect_error=True)
Expand All @@ -118,7 +118,7 @@ def test_check_complicated_name_missing(script: PipTestEnvironment) -> None:
)

# Without dependency
result = script.pip("install", "--no-index", package_a_path, "--no-deps")
result = script.pip("install", "--no-index", str(package_a_path), "--no-deps")
assert "Successfully installed package-A-1.0" in result.stdout, str(result)

result = script.pip("check", expect_error=True)
Expand All @@ -141,13 +141,13 @@ def test_check_complicated_name_broken(script: PipTestEnvironment) -> None:
)

# With broken dependency
result = script.pip("install", "--no-index", package_a_path, "--no-deps")
result = script.pip("install", "--no-index", str(package_a_path), "--no-deps")
assert "Successfully installed package-A-1.0" in result.stdout, str(result)

result = script.pip(
"install",
"--no-index",
dependency_b_path_incompatible,
str(dependency_b_path_incompatible),
"--no-deps",
)
assert "Successfully installed dependency-b-0.1" in result.stdout
Expand All @@ -174,13 +174,13 @@ def test_check_complicated_name_clean(script: PipTestEnvironment) -> None:
version="1.0",
)

result = script.pip("install", "--no-index", package_a_path, "--no-deps")
result = script.pip("install", "--no-index", str(package_a_path), "--no-deps")
assert "Successfully installed package-A-1.0" in result.stdout, str(result)

result = script.pip(
"install",
"--no-index",
dependency_b_path,
str(dependency_b_path),
"--no-deps",
)
assert "Successfully installed dependency-b-1.0" in result.stdout
Expand All @@ -202,7 +202,7 @@ def test_check_considers_conditional_reqs(script: PipTestEnvironment) -> None:
],
)

result = script.pip("install", "--no-index", package_a_path, "--no-deps")
result = script.pip("install", "--no-index", str(package_a_path), "--no-deps")
assert "Successfully installed package-A-1.0" in result.stdout, str(result)

result = script.pip("check", expect_error=True)
Expand All @@ -222,7 +222,7 @@ def test_check_development_versions_are_also_considered(
install_requires=["depend>=1.0"],
)
# Let's install pkga without its dependency
res = script.pip("install", "--no-index", pkga_path, "--no-deps")
res = script.pip("install", "--no-index", str(pkga_path), "--no-deps")
assert "Successfully installed pkga-1.0" in res.stdout, str(res)

# Setup depend==1.1.0.dev0
Expand All @@ -235,7 +235,7 @@ def test_check_development_versions_are_also_considered(
res = script.pip(
"install",
"--no-index",
depend_path,
str(depend_path),
"--no-warn-conflicts",
)
assert "Successfully installed depend-1.1.0.dev0" in res.stdout, str(res)
Expand Down
22 changes: 11 additions & 11 deletions tests/functional/test_completion.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import os
import pathlib
import sys
from typing import TYPE_CHECKING, Optional, Tuple
from typing import TYPE_CHECKING, Tuple, Union

import pytest

from tests.conftest import ScriptFactory
from tests.lib import PipTestEnvironment, TestData, TestPipResult
from tests.lib.path import Path

if TYPE_CHECKING:
from typing import Protocol
Expand Down Expand Up @@ -83,15 +83,15 @@

@pytest.fixture(scope="session")
def script_with_launchers(
tmpdir_factory: pytest.TempdirFactory,
tmp_path_factory: pytest.TempPathFactory,
script_factory: ScriptFactory,
common_wheels: Path,
pip_src: Path,
common_wheels: pathlib.Path,
pip_src: pathlib.Path,
) -> PipTestEnvironment:
tmpdir = Path(str(tmpdir_factory.mktemp("script_with_launchers")))
tmpdir = tmp_path_factory.mktemp("script_with_launchers")
script = script_factory(tmpdir.joinpath("workspace"))
# Re-install pip so we get the launchers.
script.pip_install_local("-f", common_wheels, pip_src)
script.pip_install_local("-f", str(common_wheels), str(pip_src))
return script


Expand All @@ -112,15 +112,15 @@ def test_completion_for_supported_shells(

@pytest.fixture(scope="session")
def autocomplete_script(
tmpdir_factory: pytest.TempdirFactory, script_factory: ScriptFactory
tmp_path_factory: pytest.TempPathFactory, script_factory: ScriptFactory
) -> PipTestEnvironment:
tmpdir = Path(str(tmpdir_factory.mktemp("autocomplete_script")))
tmpdir = tmp_path_factory.mktemp("autocomplete_script")
return script_factory(tmpdir.joinpath("workspace"))


class DoAutocomplete(Protocol):
def __call__(
self, words: str, cword: str, cwd: Optional[str] = None
self, words: str, cword: str, cwd: Union[pathlib.Path, str, None] = None
) -> Tuple[TestPipResult, PipTestEnvironment]:
...

Expand All @@ -133,7 +133,7 @@ def autocomplete(
autocomplete_script.environ["PIP_AUTO_COMPLETE"] = "1"

def do_autocomplete(
words: str, cword: str, cwd: Optional[str] = None
words: str, cword: str, cwd: Union[pathlib.Path, str, None] = None
) -> Tuple[TestPipResult, PipTestEnvironment]:
autocomplete_script.environ["COMP_WORDS"] = words
autocomplete_script.environ["COMP_CWORD"] = cword
Expand Down
10 changes: 5 additions & 5 deletions tests/functional/test_config_settings.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import json
import pathlib
from typing import Tuple
from zipfile import ZipFile

from tests.lib import PipTestEnvironment
from tests.lib.path import Path

PYPROJECT_TOML = """\
[build-system]
Expand Down Expand Up @@ -85,7 +85,7 @@ def build_wheel(
'''


def make_project(path: Path) -> Tuple[str, str, Path]:
def make_project(path: pathlib.Path) -> Tuple[str, str, pathlib.Path]:
name = "foo"
version = "1.0"
project_dir = path / name
Expand All @@ -102,7 +102,7 @@ def test_backend_sees_config(script: PipTestEnvironment) -> None:
"wheel",
"--config-settings",
"FOO=Hello",
project_dir,
str(project_dir),
)
wheel_file_name = f"{name}-{version}-py3-none-any.whl"
wheel_file_path = script.cwd / wheel_file_name
Expand All @@ -118,7 +118,7 @@ def test_install_sees_config(script: PipTestEnvironment) -> None:
"install",
"--config-settings",
"FOO=Hello",
project_dir,
str(project_dir),
)
config = script.site_packages_path / "config.json"
with open(config, "rb") as f:
Expand All @@ -132,7 +132,7 @@ def test_install_editable_sees_config(script: PipTestEnvironment) -> None:
"--config-settings",
"FOO=Hello",
"--editable",
project_dir,
str(project_dir),
)
config = script.site_packages_path / "config.json"
with open(config, "rb") as f:
Expand Down
Loading

0 comments on commit ab05851

Please sign in to comment.