Skip to content

Commit

Permalink
Merge pull request #10464 from jdufresne/typing-functional
Browse files Browse the repository at this point in the history
Complete type annotations of tests/functional/ directory
  • Loading branch information
pradyunsg authored Nov 19, 2021
2 parents 12dad05 + c71cf88 commit 4cdb516
Show file tree
Hide file tree
Showing 55 changed files with 1,995 additions and 982 deletions.
Empty file.
4 changes: 0 additions & 4 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,6 @@ follow_imports = skip
[mypy-pip._vendor.requests.*]
follow_imports = skip

# TODO: The following option should be removed at some point in the future.
[mypy-tests.functional.*]
allow_untyped_defs = True

[tool:pytest]
addopts = --ignore src/pip/_vendor --ignore tests/tests_cache -r aR --color=yes
xfail_strict = True
Expand Down
20 changes: 18 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,13 @@
from .lib.compat import nullcontext

if TYPE_CHECKING:
from typing import Protocol

from wsgi import WSGIApplication
else:
# TODO: Protocol was introduced in Python 3.8. Remove this branch when
# dropping support for Python 3.7.
Protocol = object


def pytest_addoption(parser: Parser) -> None:
Expand Down Expand Up @@ -442,10 +448,17 @@ def with_wheel(virtualenv: VirtualEnvironment, wheel_install: Path) -> None:
install_egg_link(virtualenv, "wheel", wheel_install)


class ScriptFactory(Protocol):
def __call__(
self, tmpdir: Path, virtualenv: Optional[VirtualEnvironment] = None
) -> PipTestEnvironment:
...


@pytest.fixture(scope="session")
def script_factory(
virtualenv_factory: Callable[[Path], VirtualEnvironment], deprecated_python: bool
) -> Callable[[Path, Optional[VirtualEnvironment]], PipTestEnvironment]:
) -> ScriptFactory:
def factory(
tmpdir: Path, virtualenv: Optional[VirtualEnvironment] = None
) -> PipTestEnvironment:
Expand Down Expand Up @@ -533,8 +546,11 @@ def deprecated_python() -> bool:
return sys.version_info[:2] in []


CertFactory = Callable[[], str]


@pytest.fixture(scope="session")
def cert_factory(tmpdir_factory: pytest.TempdirFactory) -> Callable[[], str]:
def cert_factory(tmpdir_factory: pytest.TempdirFactory) -> CertFactory:
def factory() -> str:
"""Returns path to cert/key file."""
output_path = Path(str(tmpdir_factory.mktemp("certs"))) / "cert.pem"
Expand Down
15 changes: 11 additions & 4 deletions tests/functional/test_broken_stdout.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
import os
import subprocess
from typing import List, Tuple

from tests.lib.path import Path

_BROKEN_STDOUT_RETURN_CODE = 120


def setup_broken_stdout_test(args, deprecated_python):
def setup_broken_stdout_test(
args: List[str], deprecated_python: bool
) -> Tuple[str, int]:
proc = subprocess.Popen(
args,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
# Call close() on stdout to cause a broken pipe.
assert proc.stdout is not None
proc.stdout.close()
returncode = proc.wait()
assert proc.stderr is not None
stderr = proc.stderr.read().decode("utf-8")

expected_msg = "ERROR: Pipe to stdout was broken"
Expand All @@ -24,7 +31,7 @@ def setup_broken_stdout_test(args, deprecated_python):
return stderr, returncode


def test_broken_stdout_pipe(deprecated_python):
def test_broken_stdout_pipe(deprecated_python: bool) -> None:
"""
Test a broken pipe to stdout.
"""
Expand All @@ -40,7 +47,7 @@ def test_broken_stdout_pipe(deprecated_python):
assert returncode == _BROKEN_STDOUT_RETURN_CODE


def test_broken_stdout_pipe__log_option(deprecated_python, tmpdir):
def test_broken_stdout_pipe__log_option(deprecated_python: bool, tmpdir: Path) -> None:
"""
Test a broken pipe to stdout when --log is passed.
"""
Expand All @@ -57,7 +64,7 @@ def test_broken_stdout_pipe__log_option(deprecated_python, tmpdir):
assert returncode == _BROKEN_STDOUT_RETURN_CODE


def test_broken_stdout_pipe__verbose(deprecated_python):
def test_broken_stdout_pipe__verbose(deprecated_python: bool) -> None:
"""
Test a broken pipe to stdout with verbose logging enabled.
"""
Expand Down
31 changes: 22 additions & 9 deletions tests/functional/test_build_env.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
from textwrap import dedent
from typing import Optional

import pytest

from pip._internal.build_env import BuildEnvironment
from tests.lib import create_basic_wheel_for_package, make_test_finder
from tests.lib import (
PipTestEnvironment,
TestPipResult,
create_basic_wheel_for_package,
make_test_finder,
)


def indent(text, prefix):
def indent(text: str, prefix: str) -> str:
return "\n".join((prefix if line else "") + line for line in text.split("\n"))


def run_with_build_env(script, setup_script_contents, test_script_contents=None):
def run_with_build_env(
script: PipTestEnvironment,
setup_script_contents: str,
test_script_contents: Optional[str] = None,
) -> TestPipResult:
build_env_script = script.scratch_path / "build_env.py"
build_env_script.write_text(
dedent(
Expand Down Expand Up @@ -66,13 +76,16 @@ def run_with_build_env(script, setup_script_contents, test_script_contents=None)
return script.run(*args)


def test_build_env_allow_empty_requirements_install():
def test_build_env_allow_empty_requirements_install() -> None:
finder = make_test_finder()
build_env = BuildEnvironment()
for prefix in ("normal", "overlay"):
build_env.install_requirements(None, [], prefix, None)
build_env.install_requirements(
finder, [], prefix, "Installing build dependencies"
)


def test_build_env_allow_only_one_install(script):
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])
Expand All @@ -91,7 +104,7 @@ def test_build_env_allow_only_one_install(script):
)


def test_build_env_requirements_check(script):
def test_build_env_requirements_check(script: PipTestEnvironment) -> None:

create_basic_wheel_for_package(script, "foo", "2.0")
create_basic_wheel_for_package(script, "bar", "1.0")
Expand Down Expand Up @@ -152,7 +165,7 @@ def test_build_env_requirements_check(script):
)


def test_build_env_overlay_prefix_has_priority(script):
def test_build_env_overlay_prefix_has_priority(script: PipTestEnvironment) -> None:
create_basic_wheel_for_package(script, "pkg", "2.0")
create_basic_wheel_for_package(script, "pkg", "4.3")
result = run_with_build_env(
Expand All @@ -171,7 +184,7 @@ def test_build_env_overlay_prefix_has_priority(script):


@pytest.mark.incompatible_with_test_venv
def test_build_env_isolation(script):
def test_build_env_isolation(script: PipTestEnvironment) -> None:

# Create dummy `pkg` wheel.
pkg_whl = create_basic_wheel_for_package(script, "pkg", "1.0")
Expand Down
Loading

0 comments on commit 4cdb516

Please sign in to comment.