Repository: pypa/pip
File: tests/functional/test_cache.py (Python)

Code before:
        ("zzz-4.5.7", os.path.join(destination, "zzz-4.5.7-py3-none-any.whl")),
        ("zzz-7.8.9", os.path.join(destination, "zzz-7.8.9-py3-none-any.whl")),
    ]

    for _name, filename in files:
        with open(filename, "w"):
            pass

    return files


@pytest.fixture
def empty_wheel_cache(wheel_cache_dir: str) -> None:
    if os.path.exists(wheel_cache_dir):
        shutil.rmtree(wheel_cache_dir)


def list_matches_wheel(wheel_name: str, result: TestPipResult) -> bool:
    """Returns True if any line in `result`, which should be the output of
    a `pip cache list` call, matches `wheel_name`.

    E.g., If wheel_name is `foo-1.2.3` it searches for a line starting with
          `- foo-1.2.3-py3-none-any.whl `."""
    lines = result.stdout.splitlines()
    expected = f" - {wheel_name}-py3-none-any.whl "
    return any((line.startswith(expected) for line in lines))


def list_matches_wheel_abspath(wheel_name: str, result: TestPipResult) -> bool:
    """Returns True if any line in `result`, which should be the output of
    a `pip cache list --format=abspath` call, is a valid path and belongs to
    `wheel_name`.

    E.g., If wheel_name is `foo-1.2.3` it searches for a line starting with
          `foo-1.2.3-py3-none-any.whl`."""
    lines = result.stdout.splitlines()
    expected = f"{wheel_name}-py3-none-any.whl"
    return any(
        (
            (os.path.basename(line).startswith(expected) and os.path.exists(line))
            for line in lines
        )
    )


RemoveMatches = Callable[[str, TestPipResult], bool]


@pytest.fixture
def remove_matches_http(http_cache_dir: str) -> RemoveMatches:
    """Returns True if any line in `result`, which should be the output of

Review comment (suggestion):
```suggestion
    return any(line.startswith(expected) for line in lines)
```

Code after:
        ("zzz-4.5.7", os.path.join(destination, "zzz-4.5.7-py3-none-any.whl")),
        ("zzz-7.8.9", os.path.join(destination, "zzz-7.8.9-py3-none-any.whl")),
    ]

    for _name, filename in files:
        with open(filename, "w"):
            pass

    return files


@pytest.fixture
def empty_wheel_cache(wheel_cache_dir: str) -> None:
    if os.path.exists(wheel_cache_dir):
        shutil.rmtree(wheel_cache_dir)


def list_matches_wheel(wheel_name: str, result: TestPipResult) -> bool:
    """Returns True if any line in `result`, which should be the output of
    a `pip cache list` call, matches `wheel_name`.

    E.g., If wheel_name is `foo-1.2.3` it searches for a line starting with
          `- foo-1.2.3-py3-none-any.whl `."""
    lines = result.stdout.splitlines()
    expected = f" - {wheel_name}-py3-none-any.whl "
    return any(line.startswith(expected) for line in lines)


def list_matches_wheel_abspath(wheel_name: str, result: TestPipResult) -> bool:
    """Returns True if any line in `result`, which should be the output of
    a `pip cache list --format=abspath` call, is a valid path and belongs to
    `wheel_name`.

    E.g., If wheel_name is `foo-1.2.3` it searches for a line starting with
          `foo-1.2.3-py3-none-any.whl`."""
    lines = result.stdout.splitlines()
    expected = f"{wheel_name}-py3-none-any.whl"
    return any(
        (
            (os.path.basename(line).startswith(expected) and os.path.exists(line))
            for line in lines
        )
    )


RemoveMatches = Callable[[str, TestPipResult], bool]


@pytest.fixture
def remove_matches_http(http_cache_dir: str) -> RemoveMatches:
    """Returns True if any line in `result`, which should be the output of