Skip to content

Commit

Permalink
Clean up tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
jsirois committed Nov 2, 2024
1 parent 00c5692 commit c77933d
Showing 1 changed file with 47 additions and 84 deletions.
131 changes: 47 additions & 84 deletions tests/integration/cli/commands/test_cache_prune.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from datetime import datetime, timedelta
from textwrap import dedent

import attr # vendor:skip
import colors # vendor:skip
import pytest

Expand All @@ -22,15 +23,15 @@
from pex.pip.version import PipVersion
from pex.typing import TYPE_CHECKING
from pex.variables import ENV
from testing import environment_as, make_env, run_pex_command
from testing import environment_as, run_pex_command
from testing.cli import run_pex3
from testing.pytest.tmp import Tempdir

if TYPE_CHECKING:
from typing import Iterable, Iterator, Optional


@pytest.fixture
@pytest.fixture(autouse=True)
def pex_root(tmpdir):
# type: (Tempdir) -> Iterator[str]
_pex_root = tmpdir.join("pex_root")
Expand All @@ -51,8 +52,8 @@ def lock(tmpdir):


def test_nothing_prunable(
pex_root, # type: str
pex, # type: str
pex_root, # type: str
):
# type: (...) -> None

Expand All @@ -71,11 +72,8 @@ def test_nothing_prunable(
assert pre_prune_du == DiskUsage.collect(pex_root)


def test_installed_wheel_prune_build_time(
pex_root, # type: str
pex, # type: str
):
# type: (...) -> None
def test_installed_wheel_prune_build_time(pex):
# type: (str) -> None

run_pex_command(args=["ansicolors==1.1.8", "-o", pex]).assert_success()
installed_wheels_size = DiskUsage.collect(CacheDir.INSTALLED_WHEELS.path()).size
Expand All @@ -92,8 +90,8 @@ def test_installed_wheel_prune_build_time(


def test_installed_wheel_prune_run_time(
pex_root, # type: str
pex, # type: str
pex_root, # type: str
):
# type: (...) -> None

Expand All @@ -120,13 +118,16 @@ def test_installed_wheel_prune_run_time(
assert 0 == DiskUsage.collect(CacheDir.USER_CODE.path()).size


def test_zipapp_prune(
pex_root, # type: str
pex, # type: str
tmpdir, # type: Tempdir
):
# type: (...) -> None
@attr.s(frozen=True)
class AnsicolorsZipappPex(object):
path = attr.ib() # type: str


@pytest.fixture
def ansicolors_zipapp_pex(tmpdir):
# type: (Tempdir) -> AnsicolorsZipappPex

pex = tmpdir.join("ansicolors-zipapp.pex")
with safe_open(tmpdir.join("src", "app.py"), "w") as fp:
fp.write(
dedent(
Expand All @@ -142,16 +143,33 @@ def test_zipapp_prune(
run_pex_command(
args=["ansicolors==1.1.8", "-D", "src", "-m" "app", "-o", pex], cwd=tmpdir.path
).assert_success()
pex_size = os.path.getsize(pex)
return AnsicolorsZipappPex(pex)


def execute_ansicolors_pex(pex):
# type: (AnsicolorsZipappPex) -> None

assert (
colors.green("Hello Cache!")
== subprocess.check_output(args=[pex.path]).decode("utf-8").strip()
)


def test_app_prune(
pex_root, # type: str
ansicolors_zipapp_pex, # type: AnsicolorsZipappPex
tmpdir, # type: Tempdir
):
# type: (...) -> None

pex_size = os.path.getsize(ansicolors_zipapp_pex.path)
installed_wheels_size = DiskUsage.collect(CacheDir.INSTALLED_WHEELS.path()).size
assert installed_wheels_size > 0
assert 0 == DiskUsage.collect(CacheDir.UNZIPPED_PEXES.path()).size
assert 0 == DiskUsage.collect(CacheDir.BOOTSTRAPS.path()).size
assert 0 == DiskUsage.collect(CacheDir.USER_CODE.path()).size

assert (
colors.green("Hello Cache!") == subprocess.check_output(args=[pex]).decode("utf-8").strip()
)
execute_ansicolors_pex(ansicolors_zipapp_pex)
pre_prune_du = DiskUsage.collect(pex_root)
assert (
DiskUsage.collect(CacheDir.INSTALLED_WHEELS.path()).size > installed_wheels_size
Expand Down Expand Up @@ -212,30 +230,12 @@ def expected_pip_wheels_plus(*names):


def test_zipapp_prune_shared_bootstrap(
pex_root, # type: str
pex, # type: str
ansicolors_zipapp_pex, # type: AnsicolorsZipappPex
tmpdir, # type: Tempdir
):
# type: (...) -> None

with safe_open(tmpdir.join("src", "app.py"), "w") as fp:
fp.write(
dedent(
"""\
import colors
if __name__ == "__main__":
print(colors.green("Hello Cache!"))
"""
)
)
run_pex_command(
args=["ansicolors==1.1.8", "-D", "src", "-m" "app", "-o", pex], cwd=tmpdir.path
).assert_success()
assert (
colors.green("Hello Cache!") == subprocess.check_output(args=[pex]).decode("utf-8").strip()
)
execute_ansicolors_pex(ansicolors_zipapp_pex)

empty_pex = tmpdir.join("empty.pex")
run_pex_command(args=["-o", empty_pex]).assert_success()
Expand All @@ -247,50 +247,25 @@ def test_zipapp_prune_shared_bootstrap(

assert_installed_wheels(
expected_pip_wheels_plus("ansicolors"),
(
message=(
"There should be an ansicolors wheel for the pex as well as pip, setuptools and wheel wheels "
"for at least 1 Pip."
),
)

set_last_access_one_day_ago(pex)
run_pex3(
"cache", "prune", "--older-than", "1 hour", env=make_env(PEX_VERBOSE=1)
).assert_success()
set_last_access_one_day_ago(ansicolors_zipapp_pex.path)
run_pex3("cache", "prune", "--older-than", "1 hour").assert_success()
assert [bootstrap] == list(BootstrapDir.iter_all())
assert_installed_wheels(expected_pip_wheels())


def test_zipapp_prune_shared_code(tmpdir):
# type: (Tempdir) -> None
pass


def test_zipapp_prune_shared_deps(
pex_root, # type: str
pex, # type: str
ansicolors_zipapp_pex, # type: AnsicolorsZipappPex
tmpdir, # type: Tempdir
):
# type: (...) -> None

with safe_open(tmpdir.join("src", "app.py"), "w") as fp:
fp.write(
dedent(
"""\
import colors
if __name__ == "__main__":
print(colors.green("Hello Cache!"))
"""
)
)
run_pex_command(
args=["ansicolors==1.1.8", "-D", "src", "-m" "app", "-o", pex], cwd=tmpdir.path
).assert_success()
assert (
colors.green("Hello Cache!") == subprocess.check_output(args=[pex]).decode("utf-8").strip()
)
execute_ansicolors_pex(ansicolors_zipapp_pex)

cowsay_pex = tmpdir.join("cowsay.pex")
with safe_open(tmpdir.join("exe.py"), "w") as fp:
Expand Down Expand Up @@ -318,28 +293,16 @@ def test_zipapp_prune_shared_deps(
assert_installed_wheels(expected_pip_wheels_plus("ansicolors", "cowsay"))

set_last_access_one_day_ago(cowsay_pex)
run_pex3(
"cache", "prune", "--older-than", "1 hour", env=make_env(PEX_VERBOSE=1)
).assert_success()
run_pex3("cache", "prune", "--older-than", "1 hour").assert_success()
assert_installed_wheels(expected_pip_wheels_plus("ansicolors"))


def test_venv_prune(tmpdir):
# type: (Tempdir) -> None
pass


def test_venv_prune_shared_deps(tmpdir):
# type: (Tempdir) -> None
pass


def test_venv_prune_symlinks(tmpdir):
def test_venv_prune_wheel_symlinks(tmpdir):
# type: (Tempdir) -> None
pass


def test_venv_prune_no_symlinks(tmpdir):
def test_venv_prune_wheel_copies(tmpdir):
# type: (Tempdir) -> None
pass

Expand Down

0 comments on commit c77933d

Please sign in to comment.