File: test_cache.py

package info (click to toggle)
pdm 2.23.1-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,552 kB
  • sloc: python: 24,995; javascript: 34; makefile: 24
file content (172 lines) | stat: -rw-r--r-- 5,164 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import pytest
from unearth import Link

from pdm.models.cached_package import CachedPackage
from tests import FIXTURES


@pytest.fixture
def prepare_wheel_cache(project):
    cache_dir = project.cache("wheels")
    (cache_dir / "arbitrary/path").mkdir(parents=True)
    for name in (
        "foo-0.1.0.whl",
        "bar-0.2.0.whl",
        "baz-0.3.0.whl",
        "foo_bar-0.4.0.whl",
    ):
        (cache_dir / "arbitrary/path" / name).touch()


@pytest.fixture
def prepare_http_cache(project):
    cache_dir = project.cache("http")
    (cache_dir / "arbitrary/path").mkdir(parents=True)
    for name in (
        "foo-0.1.0.tar.gz",
        "bar-0.2.0.zip",
        "baz-0.3.0.tar.gz",
        "foobar-0.4.0.tar.gz",
    ):
        (cache_dir / "arbitrary/path" / name).touch()


@pytest.mark.usefixtures("prepare_wheel_cache")
def test_cache_list(project, pdm):
    result = pdm(["cache", "list"], obj=project)
    assert result.exit_code == 0

    for name in (
        "foo-0.1.0.whl",
        "bar-0.2.0.whl",
        "baz-0.3.0.whl",
        "foo_bar-0.4.0.whl",
    ):
        assert name in result.output


@pytest.mark.usefixtures("prepare_wheel_cache")
def test_cache_list_pattern(project, pdm):
    result = pdm(["cache", "list", "ba*"], obj=project)
    assert result.exit_code == 0

    for name in (
        "bar-0.2.0.whl",
        "baz-0.3.0.whl",
    ):
        assert name in result.output

    for name in (
        "foo-0.1.0.whl",
        "foo_bar-0.4.0.whl",
    ):
        assert name not in result.output


@pytest.mark.usefixtures("prepare_wheel_cache", "prepare_http_cache")
def test_cache_remove_pattern(project, pdm):
    result = pdm(["cache", "remove", "ba*"], obj=project)
    assert result.exit_code == 0

    for name in (
        "bar-0.2.0.whl",
        "baz-0.3.0.whl",
    ):
        assert not (project.cache("wheels") / "arbitrary/path" / name).exists()

    for name in (
        "foo-0.1.0.whl",
        "foo_bar-0.4.0.whl",
    ):
        assert (project.cache("wheels") / "arbitrary/path" / name).exists()

    assert (project.cache("http") / "arbitrary/path/foo-0.1.0.tar.gz").exists()


@pytest.mark.usefixtures("prepare_wheel_cache", "prepare_http_cache")
def test_cache_remove_wildcard(project, pdm):
    result = pdm(["cache", "remove", "*"], obj=project)
    assert result.exit_code == 0

    for name in (
        "bar-0.2.0.whl",
        "baz-0.3.0.whl",
        "foo-0.1.0.whl",
        "foo_bar-0.4.0.whl",
    ):
        assert not (project.cache("wheels") / "arbitrary/path" / name).exists()

    assert (project.cache("http") / "arbitrary/path/foo-0.1.0.tar.gz").exists()


@pytest.mark.usefixtures("prepare_wheel_cache", "prepare_http_cache")
def test_cache_clear(project, pdm):
    result = pdm(["cache", "clear"], obj=project)
    assert result.exit_code == 0

    for name in (
        "bar-0.2.0.whl",
        "baz-0.3.0.whl",
        "foo-0.1.0.whl",
        "foo_bar-0.4.0.whl",
    ):
        assert not (project.cache("wheels") / "arbitrary/path" / name).exists()

    assert not (project.cache("http") / "arbitrary/path/foo-0.1.0.tar.gz").exists()


@pytest.mark.usefixtures("prepare_wheel_cache", "prepare_http_cache")
def test_cache_remove_no_pattern(project, pdm):
    result = pdm(["cache", "remove"], obj=project)
    assert result.exit_code != 0


@pytest.mark.usefixtures("prepare_wheel_cache", "prepare_http_cache")
def test_cache_info(project, pdm):
    result = pdm(["cache", "info"], obj=project)
    assert result.exit_code == 0

    lines = result.output.splitlines()
    assert "Files: 4" in lines[4]
    assert "Files: 4" in lines[6]


@pytest.mark.parametrize(
    "url,hash",
    [
        (
            "http://fixtures.test/artifacts/demo-0.0.1.tar.gz",
            "sha256:d57bf5e3b8723e4fc68275159dcc4ca983d86d4c84220a4d715d491401f27db2",
        ),
        (
            f"file://{(FIXTURES / 'artifacts/demo-0.0.1.tar.gz').as_posix()}",
            "sha256:d57bf5e3b8723e4fc68275159dcc4ca983d86d4c84220a4d715d491401f27db2",
        ),
        (
            "http://fixtures.test/artifacts/demo-0.0.1.tar.gz#sha384=9130e5e4912bc78b"
            "1ffabbf406d56bc74b9165b0adc8c627168b7b563b80d5ff6c30e269398d01144ee52aa3"
            "3292682d",
            "sha384:9130e5e4912bc78b1ffabbf406d56bc74b9165b0adc8c627168b7b563b80d5ff6c30e269398d01144ee52aa33292682d",
        ),
        (
            "http://fixtures.test/artifacts/demo-0.0.1.tar.gz#md5=5218509812c9fcb4646adde8fd3307e1",
            "sha256:d57bf5e3b8723e4fc68275159dcc4ca983d86d4c84220a4d715d491401f27db2",
        ),
    ],
)
def test_hash_cache(project, url, hash):
    hash_cache = project.make_hash_cache()
    assert hash_cache.get_hash(Link(url), project.environment.session) == hash


def test_clear_package_cache(project, pdm):
    pkg = CachedPackage(project.cache("packages") / "test_package.whl.cache")
    pkg.path.mkdir(parents=True)
    refer_pkg = project.root / "refer_pkg"
    refer_pkg.mkdir()
    pkg.add_referrer(str(refer_pkg))
    assert len(pkg.referrers) == 1

    refer_pkg.rmdir()
    pdm(["cache", "clear", "packages"], obj=project, strict=True)
    assert not pkg.path.exists()