summaryrefslogtreecommitdiff
path: root/tests/functional/test_config_settings.py
blob: f3975de2af5ab727b338d220dd0330b78b8ab17f (plain)
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
import json
import tarfile
from pathlib import Path
from typing import List, Optional, Tuple
from zipfile import ZipFile

from pip._internal.utils.urls import path_to_url
from tests.lib import PipTestEnvironment, create_basic_sdist_for_package

PYPROJECT_TOML = """\
[build-system]
requires = []
build-backend = "dummy_backend:main"
backend-path = ["backend"]
"""

BACKEND_SRC = '''
import csv
import json
import os.path
from zipfile import ZipFile
import hashlib
import base64
import io

WHEEL = """\
Wheel-Version: 1.0
Generator: dummy_backend 1.0
Root-Is-Purelib: true
Tag: py3-none-any
"""

METADATA = """\
Metadata-Version: 2.1
Name: {project}
Version: {version}
Summary: A dummy package
Author: None
Author-email: none@example.org
License: MIT
{requires_dist}
"""

def make_wheel(z, project, version, requires_dist, files):
    record = []
    def add_file(name, data):
        data = data.encode("utf-8")
        z.writestr(name, data)
        digest = hashlib.sha256(data).digest()
        hash = base64.urlsafe_b64encode(digest).rstrip(b"=").decode("ASCII")
        record.append((name, f"sha256={hash}", len(data)))
    distinfo = f"{project}-{version}.dist-info"
    add_file(f"{distinfo}/WHEEL", WHEEL)
    add_file(f"{distinfo}/METADATA", METADATA.format(
        project=project, version=version, requires_dist=requires_dist
    ))
    for name, data in files:
        add_file(name, data)
    record_name = f"{distinfo}/RECORD"
    record.append((record_name, "", ""))
    b = io.BytesIO()
    rec = io.TextIOWrapper(b, newline="", encoding="utf-8")
    w = csv.writer(rec)
    w.writerows(record)
    z.writestr(record_name, b.getvalue())
    rec.close()


class Backend:
    def build_wheel(
        self,
        wheel_directory,
        config_settings=None,
        metadata_directory=None
    ):
        if config_settings is None:
            config_settings = {}
        w = os.path.join(wheel_directory, "{{name}}-1.0-py3-none-any.whl")
        with open(w, "wb") as f:
            with ZipFile(f, "w") as z:
                make_wheel(
                    z, "{{name}}", "1.0", "{{requires_dist}}",
                    [("{{name}}-config.json", json.dumps(config_settings))]
                )
        return "{{name}}-1.0-py3-none-any.whl"

    build_editable = build_wheel

main = Backend()
'''


def make_project(
    path: Path, name: str = "foo", dependencies: Optional[List[str]] = None
) -> Tuple[str, str, Path]:
    version = "1.0"
    project_dir = path / name
    backend = project_dir / "backend"
    backend.mkdir(parents=True)
    (project_dir / "pyproject.toml").write_text(PYPROJECT_TOML)
    requires_dist = [f"Requires-Dist: {dep}" for dep in dependencies or []]
    (backend / "dummy_backend.py").write_text(
        BACKEND_SRC.replace("{{name}}", name).replace(
            "{{requires_dist}}", "\n".join(requires_dist)
        )
    )
    return name, version, project_dir


def test_backend_sees_config(script: PipTestEnvironment) -> None:
    name, version, project_dir = make_project(script.scratch_path)
    script.pip(
        "wheel",
        "--config-settings",
        "FOO=Hello",
        project_dir,
    )
    wheel_file_name = f"{name}-{version}-py3-none-any.whl"
    wheel_file_path = script.cwd / wheel_file_name
    with open(wheel_file_path, "rb") as f:
        with ZipFile(f) as z:
            output = z.read(f"{name}-config.json")
            assert json.loads(output) == {"FOO": "Hello"}


def test_backend_sees_config_reqs(script: PipTestEnvironment) -> None:
    name, version, project_dir = make_project(script.scratch_path)
    script.scratch_path.joinpath("reqs.txt").write_text(
        f"{project_dir} --config-settings FOO=Hello"
    )
    script.pip("wheel", "-r", "reqs.txt")
    wheel_file_name = f"{name}-{version}-py3-none-any.whl"
    wheel_file_path = script.cwd / wheel_file_name
    with open(wheel_file_path, "rb") as f:
        with ZipFile(f) as z:
            output = z.read(f"{name}-config.json")
            assert json.loads(output) == {"FOO": "Hello"}


def test_backend_sees_config_via_constraint(script: PipTestEnvironment) -> None:
    name, version, project_dir = make_project(script.scratch_path)
    constraints_file = script.scratch_path / "constraints.txt"
    constraints_file.write_text(f"{name} @ {path_to_url(str(project_dir))}")
    script.pip(
        "wheel",
        "--config-settings",
        "FOO=Hello",
        "-c",
        "constraints.txt",
        name,
    )
    wheel_file_name = f"{name}-{version}-py3-none-any.whl"
    wheel_file_path = script.cwd / wheel_file_name
    with open(wheel_file_path, "rb") as f:
        with ZipFile(f) as z:
            output = z.read(f"{name}-config.json")
            assert json.loads(output) == {"FOO": "Hello"}


def test_backend_sees_config_via_sdist(script: PipTestEnvironment) -> None:
    name, version, project_dir = make_project(script.scratch_path)
    dists_dir = script.scratch_path / "dists"
    dists_dir.mkdir()
    with tarfile.open(dists_dir / f"{name}-{version}.tar.gz", "w:gz") as dist_tar:
        dist_tar.add(project_dir, arcname=name)
    script.pip(
        "wheel",
        "--config-settings",
        "FOO=Hello",
        "-f",
        dists_dir,
        name,
    )
    wheel_file_name = f"{name}-{version}-py3-none-any.whl"
    wheel_file_path = script.cwd / wheel_file_name
    with open(wheel_file_path, "rb") as f:
        with ZipFile(f) as z:
            output = z.read(f"{name}-config.json")
            assert json.loads(output) == {"FOO": "Hello"}


def test_req_file_does_not_see_config(script: PipTestEnvironment) -> None:
    """Test that CLI config settings do not propagate to requirement files."""
    name, _, project_dir = make_project(script.scratch_path)
    reqs_file = script.scratch_path / "reqs.txt"
    reqs_file.write_text(f"{project_dir}")
    script.pip(
        "install",
        "--config-settings",
        "FOO=Hello",
        "-r",
        reqs_file,
    )
    config = script.site_packages_path / f"{name}-config.json"
    with open(config, "rb") as f:
        assert json.load(f) == {}


def test_dep_does_not_see_config(script: PipTestEnvironment) -> None:
    """Test that CLI config settings do not propagate to dependencies."""
    _, _, bar_project_dir = make_project(script.scratch_path, name="bar")
    _, _, foo_project_dir = make_project(
        script.scratch_path,
        name="foo",
        dependencies=[f"bar @ {path_to_url(str(bar_project_dir))}"],
    )
    script.pip(
        "install",
        "--config-settings",
        "FOO=Hello",
        foo_project_dir,
    )
    foo_config = script.site_packages_path / "foo-config.json"
    with open(foo_config, "rb") as f:
        assert json.load(f) == {"FOO": "Hello"}
    bar_config = script.site_packages_path / "bar-config.json"
    with open(bar_config, "rb") as f:
        assert json.load(f) == {}


def test_dep_in_req_file_does_not_see_config(script: PipTestEnvironment) -> None:
    """Test that CLI config settings do not propagate to dependencies found in
    requirement files."""
    _, _, bar_project_dir = make_project(script.scratch_path, name="bar")
    _, _, foo_project_dir = make_project(
        script.scratch_path,
        name="foo",
        dependencies=["bar"],
    )
    reqs_file = script.scratch_path / "reqs.txt"
    reqs_file.write_text(f"bar @ {path_to_url(str(bar_project_dir))}")
    script.pip(
        "install",
        "--config-settings",
        "FOO=Hello",
        "-r",
        reqs_file,
        foo_project_dir,
    )
    foo_config = script.site_packages_path / "foo-config.json"
    with open(foo_config, "rb") as f:
        assert json.load(f) == {"FOO": "Hello"}
    bar_config = script.site_packages_path / "bar-config.json"
    with open(bar_config, "rb") as f:
        assert json.load(f) == {}


def test_install_sees_config(script: PipTestEnvironment) -> None:
    name, _, project_dir = make_project(script.scratch_path)
    script.pip(
        "install",
        "--config-settings",
        "FOO=Hello",
        project_dir,
    )
    config = script.site_packages_path / f"{name}-config.json"
    with open(config, "rb") as f:
        assert json.load(f) == {"FOO": "Hello"}


def test_install_sees_config_reqs(script: PipTestEnvironment) -> None:
    name, _, project_dir = make_project(script.scratch_path)
    script.scratch_path.joinpath("reqs.txt").write_text(
        f"{project_dir} --config-settings FOO=Hello"
    )
    script.pip("install", "-r", "reqs.txt")
    config = script.site_packages_path / f"{name}-config.json"
    with open(config, "rb") as f:
        assert json.load(f) == {"FOO": "Hello"}


def test_install_editable_sees_config(script: PipTestEnvironment) -> None:
    name, _, project_dir = make_project(script.scratch_path)
    script.pip(
        "install",
        "--config-settings",
        "FOO=Hello",
        "--editable",
        project_dir,
    )
    config = script.site_packages_path / f"{name}-config.json"
    with open(config, "rb") as f:
        assert json.load(f) == {"FOO": "Hello"}


def test_install_config_reqs(script: PipTestEnvironment) -> None:
    name, _, project_dir = make_project(script.scratch_path)
    a_sdist = create_basic_sdist_for_package(
        script,
        "foo",
        "1.0",
        {"pyproject.toml": PYPROJECT_TOML, "backend/dummy_backend.py": BACKEND_SRC},
    )
    script.scratch_path.joinpath("reqs.txt").write_text(
        f'{project_dir} --config-settings "--build-option=--cffi" '
        '--config-settings "--build-option=--avx2" '
        "--config-settings FOO=BAR"
    )
    script.pip("install", "--no-index", "-f", str(a_sdist.parent), "-r", "reqs.txt")
    script.assert_installed(foo="1.0")
    config = script.site_packages_path / f"{name}-config.json"
    with open(config, "rb") as f:
        assert json.load(f) == {"--build-option": ["--cffi", "--avx2"], "FOO": "BAR"}