Skip to content

Commit

Permalink
Merge pull request #12894 from notatallshaw/add-pytest-ruff-checks
Browse files Browse the repository at this point in the history
  • Loading branch information
uranusjr authored Aug 7, 2024
2 parents 3259c06 + 05d79d8 commit e98cc5c
Show file tree
Hide file tree
Showing 41 changed files with 210 additions and 195 deletions.
1 change: 1 addition & 0 deletions news/12894.trivial.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add Ruff's Pytest rules to the tests directory.
12 changes: 6 additions & 6 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ def tmp_path(request: pytest.FixtureRequest, tmp_path: Path) -> Iterator[Path]:
shutil.rmtree(tmp_path, ignore_errors=True)


@pytest.fixture()
@pytest.fixture
def tmpdir(tmp_path: Path) -> Path:
"""Override Pytest's ``tmpdir`` with our pathlib implementation.
Expand Down Expand Up @@ -477,7 +477,7 @@ def virtualenv_template(
setuptools_install: Path,
wheel_install: Path,
coverage_install: Path,
) -> Iterator[VirtualEnvironment]:
) -> VirtualEnvironment:
venv_type: VirtualEnvironmentType
if request.config.getoption("--use-venv"):
venv_type = "venv"
Expand Down Expand Up @@ -522,7 +522,7 @@ def virtualenv_template(
# it's not reused by mistake from one of the copies.
venv_template = tmpdir / "venv_template"
venv.move(venv_template)
yield venv
return venv


@pytest.fixture(scope="session")
Expand All @@ -538,14 +538,14 @@ def factory(tmpdir: Path) -> VirtualEnvironment:
@pytest.fixture
def virtualenv(
virtualenv_factory: Callable[[Path], VirtualEnvironment], tmpdir: Path
) -> Iterator[VirtualEnvironment]:
) -> VirtualEnvironment:
"""
Return a virtual environment which is unique to each test function
invocation created inside of a sub directory of the test function's
temporary directory. The returned object is a
``tests.lib.venv.VirtualEnvironment`` object.
"""
yield virtualenv_factory(tmpdir.joinpath("workspace", "venv"))
return virtualenv_factory(tmpdir.joinpath("workspace", "venv"))


@pytest.fixture(scope="session")
Expand Down Expand Up @@ -973,7 +973,7 @@ def do_GET(self) -> None:
self._seen_paths.add(self.path)


@pytest.fixture(scope="function")
@pytest.fixture
def html_index_with_onetime_server(
html_index_for_packages: Path,
) -> Iterator[http.server.ThreadingHTTPServer]:
Expand Down
8 changes: 4 additions & 4 deletions tests/functional/test_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -1234,7 +1234,7 @@ def test_download_use_pep517_propagation(
assert len(downloads) == 2


@pytest.fixture(scope="function")
@pytest.fixture
def download_local_html_index(
script: PipTestEnvironment,
html_index_for_packages: Path,
Expand Down Expand Up @@ -1265,7 +1265,7 @@ def run_for_generated_index(
return run_for_generated_index


@pytest.fixture(scope="function")
@pytest.fixture
def download_server_html_index(
script: PipTestEnvironment,
tmpdir: Path,
Expand Down Expand Up @@ -1450,11 +1450,11 @@ def test_produces_error_for_mismatched_package_name_in_metadata(

@pytest.mark.parametrize(
"requirement",
(
[
"requires-simple-extra==0.1",
"REQUIRES_SIMPLE-EXTRA==0.1",
"REQUIRES....simple-_-EXTRA==0.1",
),
],
)
def test_canonicalizes_package_name_before_verifying_metadata(
download_local_html_index: Callable[..., Tuple[TestPipResult, Path]],
Expand Down
30 changes: 15 additions & 15 deletions tests/functional/test_fast_deps.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
from os.path import basename
from typing import Iterable

import pytest
from pip._vendor.packaging.utils import canonicalize_name
from pytest import mark

from pip._internal.utils.misc import hash_file
from tests.lib import PipTestEnvironment, TestData, TestPipResult
Expand All @@ -30,13 +30,13 @@ def assert_installed(script: PipTestEnvironment, names: str) -> None:
assert installed.issuperset(map(canonicalize_name, names))


@mark.network
@mark.parametrize(
("requirement", "expected"),
(
@pytest.mark.network
@pytest.mark.parametrize(
"requirement, expected",
[
("Paste==3.4.2", ("Paste", "six")),
("Paste[flup]==3.4.2", ("Paste", "six", "flup")),
),
],
)
def test_install_from_pypi(
requirement: str, expected: str, script: PipTestEnvironment
Expand All @@ -45,13 +45,13 @@ def test_install_from_pypi(
assert_installed(script, expected)


@mark.network
@mark.parametrize(
("requirement", "expected"),
(
@pytest.mark.network
@pytest.mark.parametrize(
"requirement, expected",
[
("Paste==3.4.2", ("Paste-3.4.2-*.whl", "six-*.whl")),
("Paste[flup]==3.4.2", ("Paste-3.4.2-*.whl", "six-*.whl", "flup-*")),
),
],
)
def test_download_from_pypi(
requirement: str, expected: Iterable[str], script: PipTestEnvironment
Expand All @@ -61,7 +61,7 @@ def test_download_from_pypi(
assert all(fnmatch.filter(created, f) for f in expected)


@mark.network
@pytest.mark.network
def test_build_wheel_with_deps(data: TestData, script: PipTestEnvironment) -> None:
result = pip(script, "wheel", os.fspath(data.packages / "requiresPaste"))
created = [basename(f) for f in result.files_created]
Expand All @@ -70,7 +70,7 @@ def test_build_wheel_with_deps(data: TestData, script: PipTestEnvironment) -> No
assert fnmatch.filter(created, "six-*.whl")


@mark.network
@pytest.mark.network
def test_require_hash(script: PipTestEnvironment, tmp_path: pathlib.Path) -> None:
reqs = tmp_path / "requirements.txt"
reqs.write_text(
Expand All @@ -91,7 +91,7 @@ def test_require_hash(script: PipTestEnvironment, tmp_path: pathlib.Path) -> Non
assert fnmatch.filter(created, "idna-2.10*")


@mark.network
@pytest.mark.network
def test_hash_mismatch(script: PipTestEnvironment, tmp_path: pathlib.Path) -> None:
reqs = tmp_path / "requirements.txt"
reqs.write_text("idna==2.10 --hash=sha256:irna")
Expand All @@ -105,7 +105,7 @@ def test_hash_mismatch(script: PipTestEnvironment, tmp_path: pathlib.Path) -> No
assert "DO NOT MATCH THE HASHES" in result.stderr


@mark.network
@pytest.mark.network
def test_hash_mismatch_existing_download_for_metadata_only_wheel(
script: PipTestEnvironment, tmp_path: pathlib.Path
) -> None:
Expand Down
41 changes: 19 additions & 22 deletions tests/functional/test_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@
)


@pytest.mark.parametrize("command", ("install", "wheel"))
@pytest.mark.parametrize("variant", ("missing_setuptools", "bad_setuptools"))
@pytest.mark.parametrize("command", ["install", "wheel"])
@pytest.mark.parametrize("variant", ["missing_setuptools", "bad_setuptools"])
def test_pep518_uses_build_env(
script: PipTestEnvironment,
data: TestData,
Expand Down Expand Up @@ -105,16 +105,13 @@ def test_pep518_refuses_conflicting_requires(
result = script.pip_install_local(
"-f", script.scratch_path, project_dir, expect_error=True
)
assert result.returncode != 0
assert (
result.returncode != 0
and (
f"Some build dependencies for {project_dir.as_uri()} conflict "
"with PEP 517/518 supported "
"requirements: setuptools==1.0 is incompatible with "
"setuptools>=40.8.0."
)
in result.stderr
), str(result)
f"Some build dependencies for {project_dir.as_uri()} conflict "
"with PEP 517/518 supported "
"requirements: setuptools==1.0 is incompatible with "
"setuptools>=40.8.0."
) in result.stderr, str(result)


def test_pep518_refuses_invalid_requires(
Expand Down Expand Up @@ -243,10 +240,10 @@ def test_pep518_with_namespace_package(
)


@pytest.mark.parametrize("command", ("install", "wheel"))
@pytest.mark.parametrize("command", ["install", "wheel"])
@pytest.mark.parametrize(
"package",
("pep518_forkbomb", "pep518_twin_forkbombs_first", "pep518_twin_forkbombs_second"),
["pep518_forkbomb", "pep518_twin_forkbombs_first", "pep518_twin_forkbombs_second"],
)
def test_pep518_forkbombs(
script: PipTestEnvironment,
Expand Down Expand Up @@ -1253,7 +1250,7 @@ def test_install_nonlocal_compatible_wheel_path(
assert result.returncode == ERROR


@pytest.mark.parametrize("opt", ("--target", "--prefix"))
@pytest.mark.parametrize("opt", ["--target", "--prefix"])
def test_install_with_target_or_prefix_and_scripts_no_warning(
opt: str, script: PipTestEnvironment
) -> None:
Expand Down Expand Up @@ -2027,7 +2024,7 @@ def test_install_pep508_with_url_in_install_requires(


@pytest.mark.network
@pytest.mark.parametrize("index", (PyPI.simple_url, TestPyPI.simple_url))
@pytest.mark.parametrize("index", [PyPI.simple_url, TestPyPI.simple_url])
def test_install_from_test_pypi_with_ext_url_dep_is_blocked(
script: PipTestEnvironment, index: str
) -> None:
Expand Down Expand Up @@ -2309,10 +2306,10 @@ def test_error_all_yanked_files_and_no_pin(
expect_error=True,
)
# Make sure an error is raised
assert (
result.returncode == 1
and "ERROR: No matching distribution found for simple\n" in result.stderr
), str(result)
assert result.returncode == 1
assert "ERROR: No matching distribution found for simple\n" in result.stderr, str(
result
)


@pytest.mark.parametrize(
Expand Down Expand Up @@ -2392,7 +2389,7 @@ def test_install_skip_work_dir_pkg(script: PipTestEnvironment, data: TestData) -


@pytest.mark.parametrize(
"package_name", ("simple-package", "simple_package", "simple.package")
"package_name", ["simple-package", "simple_package", "simple.package"]
)
def test_install_verify_package_name_normalization(
script: PipTestEnvironment, package_name: str
Expand Down Expand Up @@ -2449,7 +2446,7 @@ def install_find_links(

@pytest.mark.parametrize(
"with_target_dir",
(True, False),
[True, False],
)
def test_install_dry_run_nothing_installed(
script: PipTestEnvironment,
Expand Down Expand Up @@ -2618,7 +2615,7 @@ def test_install_pip_prints_req_chain_pypi(script: PipTestEnvironment) -> None:
)


@pytest.mark.parametrize("common_prefix", ("", "linktest-1.0/"))
@pytest.mark.parametrize("common_prefix", ["", "linktest-1.0/"])
def test_install_sdist_links(script: PipTestEnvironment, common_prefix: str) -> None:
"""
Test installing an sdist with hard and symbolic links.
Expand Down
2 changes: 1 addition & 1 deletion tests/functional/test_install_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ def keyring_provider_implementation(request: pytest.FixtureRequest) -> str:
return request.param


@pytest.fixture()
@pytest.fixture
def flags(
request: pytest.FixtureRequest,
interactive: bool,
Expand Down
9 changes: 4 additions & 5 deletions tests/functional/test_install_reqs.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class ArgRecordingSdistMaker(Protocol):
def __call__(self, name: str, **kwargs: Any) -> ArgRecordingSdist: ...


@pytest.fixture()
@pytest.fixture
def arg_recording_sdist_maker(
script: PipTestEnvironment,
) -> ArgRecordingSdistMaker:
Expand Down Expand Up @@ -624,10 +624,9 @@ def test_install_distribution_duplicate_extras(
) -> None:
to_install = data.packages.joinpath("LocalExtras")
package_name = f"{to_install}[bar]"
with pytest.raises(AssertionError):
result = script.pip_install_local(package_name, package_name)
expected = f"Double requirement given: {package_name}"
assert expected in result.stderr
result = script.pip_install_local(package_name, package_name)
unexpected = f"Double requirement given: {package_name}"
assert unexpected not in result.stderr


def test_install_distribution_union_with_constraints(
Expand Down
2 changes: 1 addition & 1 deletion tests/functional/test_invalid_versions_and_specifiers.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,4 @@ def test_show_require_invalid_version(
elif select_backend().NAME == "pkg_resources":
assert "Required-by: \n" in result.stdout
else:
assert False, "Unknown metadata backend"
pytest.fail("Unknown metadata backend")
2 changes: 1 addition & 1 deletion tests/functional/test_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -749,4 +749,4 @@ def test_list_pep610_editable(script: PipTestEnvironment) -> None:
assert item["editable_project_location"]
break
else:
assert False, "package 'testpkg' not found in pip list result"
pytest.fail("package 'testpkg' not found in pip list result")
12 changes: 6 additions & 6 deletions tests/functional/test_new_resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def assert_editable(script: PipTestEnvironment, *args: str) -> None:
), f"{args!r} not all found in {script.site_packages_path!r}"


@pytest.fixture()
@pytest.fixture
def make_fake_wheel(script: PipTestEnvironment) -> MakeFakeWheel:
def _make_fake_wheel(name: str, version: str, wheel_tag: str) -> pathlib.Path:
wheel_house = script.scratch_path.joinpath("wheelhouse")
Expand Down Expand Up @@ -2299,8 +2299,8 @@ def test_new_resolver_dont_backtrack_on_extra_if_base_constrained(
script.assert_installed(pkg="1.0", dep="1.0")


@pytest.mark.parametrize("swap_order", (True, False))
@pytest.mark.parametrize("two_extras", (True, False))
@pytest.mark.parametrize("swap_order", [True, False])
@pytest.mark.parametrize("two_extras", [True, False])
def test_new_resolver_dont_backtrack_on_extra_if_base_constrained_in_requirement(
script: PipTestEnvironment, swap_order: bool, two_extras: bool
) -> None:
Expand Down Expand Up @@ -2337,8 +2337,8 @@ def test_new_resolver_dont_backtrack_on_extra_if_base_constrained_in_requirement
script.assert_installed(pkg="1.0", dep="1.0")


@pytest.mark.parametrize("swap_order", (True, False))
@pytest.mark.parametrize("two_extras", (True, False))
@pytest.mark.parametrize("swap_order", [True, False])
@pytest.mark.parametrize("two_extras", [True, False])
def test_new_resolver_dont_backtrack_on_conflicting_constraints_on_extras(
tmpdir: pathlib.Path,
virtualenv: VirtualEnvironment,
Expand Down Expand Up @@ -2518,7 +2518,7 @@ def test_new_resolver_works_when_failing_package_builds_are_disallowed(
script.assert_installed(pkg2="1.0", pkg1="1.0")


@pytest.mark.parametrize("swap_order", (True, False))
@pytest.mark.parametrize("swap_order", [True, False])
def test_new_resolver_comes_from_with_extra(
script: PipTestEnvironment, swap_order: bool
) -> None:
Expand Down
2 changes: 1 addition & 1 deletion tests/functional/test_new_resolver_target.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
MakeFakeWheel = Callable[[str], str]


@pytest.fixture()
@pytest.fixture
def make_fake_wheel(script: PipTestEnvironment) -> MakeFakeWheel:
def _make_fake_wheel(wheel_tag: str) -> str:
wheel_house = script.scratch_path.joinpath("wheelhouse")
Expand Down
2 changes: 1 addition & 1 deletion tests/functional/test_new_resolver_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def test_new_resolver_install_user_conflict_in_user_site(
result.did_not_create(base_2_dist_info)


@pytest.fixture()
@pytest.fixture
def patch_dist_in_site_packages(virtualenv: VirtualEnvironment) -> None:
# Since the tests are run from a virtualenv, and to avoid the "Will not
# install to the usersite because it will lack sys.path precedence..."
Expand Down
Loading

0 comments on commit e98cc5c

Please sign in to comment.