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

Code before:
    http_cache_dir = os.path.join(cache_dir, "http")
    metadata_dir = os.path.join(wheel_cache_dir, "metadata_only")

    # Verify setup
    assert os.path.exists(selfcheck_path)
    assert os.path.exists(metadata_dir)
    assert os.path.exists(os.path.join(http_cache_dir, "empty1"))

    result = script.pip("cache", "purge", "--verbose", allow_stderr_warning=True)

    # Verify all cleanup happened
    assert not os.path.exists(selfcheck_path)
    assert "Removed legacy selfcheck.json file" in result.stdout
    assert not os.path.exists(metadata_dir)
    assert not os.path.exists(os.path.join(wheel_cache_dir, "completely_empty"))
    assert not os.path.exists(os.path.join(http_cache_dir, "empty1"))
    assert "Directories removed:" in result.stdout

    # Verify directory count is positive
    dir_count = int(
        [line for line in result.stdout.splitlines() if "Directories removed:" in line][
            0
        ]
        .split(":")[-1]
        .strip()
    )
    assert dir_count > 0


def test_cache_purge_with_mixed_content(
    script: PipTestEnvironment,
    populate_wheel_cache: list[tuple[str, str]],
    wheel_cache_dir: str,
) -> None:
    """Test purge removes both wheel files and empty directories."""
    # Add an empty directory alongside the wheels
    empty_dir = os.path.join(wheel_cache_dir, "empty_subdir")
    os.makedirs(empty_dir)

    result = script.pip("cache", "purge", "--verbose")

    # Verify wheels and empty directory were removed
    for _name, filepath in populate_wheel_cache:
        assert not os.path.exists(filepath)
    assert not os.path.exists(empty_dir)

    # Verify counts in output
    assert "Files removed:" in result.stdout
    assert "Directories removed:" in result.stdout
    lines = result.stdout.splitlines()
    file_line = [line for line in lines if "Files removed:" in line][0]

Review comment (suggestion):
```suggestion
    dir_count = int(re.findall(r"Directories removed: (\d+)", result.stdout)[0])
```

This would also need an import of `re` above.

Code after:
    selfcheck_path = os.path.join(cache_dir, "selfcheck.json")
    http_cache_dir = os.path.join(cache_dir, "http")
    metadata_dir = os.path.join(wheel_cache_dir, "metadata_only")

    # Verify setup
    assert os.path.exists(selfcheck_path)
    assert os.path.exists(metadata_dir)
    assert os.path.exists(os.path.join(http_cache_dir, "empty1"))

    result = script.pip("cache", "purge", "--verbose", allow_stderr_warning=True)

    # Verify all cleanup happened
    assert not os.path.exists(selfcheck_path)
    assert "Removed legacy selfcheck.json file" in result.stdout
    assert not os.path.exists(metadata_dir)
    assert not os.path.exists(os.path.join(wheel_cache_dir, "completely_empty"))
    assert not os.path.exists(os.path.join(http_cache_dir, "empty1"))
    assert "Directories removed:" in result.stdout

    # Verify directory count is positive
    dir_count = int(re.findall(r"Directories removed: (\d+)", result.stdout)[0])
    assert dir_count > 0


def test_cache_purge_with_mixed_content(
    script: PipTestEnvironment,
    populate_wheel_cache: list[tuple[str, str]],
    wheel_cache_dir: str,
) -> None:
    """Test purge removes both wheel files and empty directories."""
    # Add an empty directory alongside the wheels
    empty_dir = os.path.join(wheel_cache_dir, "empty_subdir")
    os.makedirs(empty_dir)

    result = script.pip("cache", "purge", "--verbose")

    # Verify wheels and empty directory were removed
    for _name, filepath in populate_wheel_cache:
        assert not os.path.exists(filepath)
    assert not os.path.exists(empty_dir)

    # Verify counts in output
    assert "Files removed:" in result.stdout
    assert "Directories removed:" in result.stdout
    files_removed = int(re.findall(r"Files removed: (\d+)", result.stdout)[0])
    assert files_removed == 4