-
-
Notifications
You must be signed in to change notification settings - Fork 607
/
pex_test.py
373 lines (332 loc) · 14.8 KB
/
pex_test.py
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
# Copyright 2019 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).
import json
import os.path
import zipfile
from dataclasses import dataclass
from typing import Dict, Iterable, Iterator, List, Optional, Tuple, cast
import pytest
from pkg_resources import Requirement
from pants.backend.python.rules import download_pex_bin
from pants.backend.python.rules.pex import (
Pex,
PexInterpreterConstraints,
PexPlatforms,
PexRequest,
PexRequirements,
)
from pants.backend.python.rules.pex import rules as pex_rules
from pants.backend.python.subsystems import python_native_code, subprocess_environment
from pants.backend.python.target_types import PythonInterpreterCompatibility
from pants.engine.addresses import Address
from pants.engine.fs import CreateDigest, Digest, DirectoryToMaterialize, FileContent
from pants.engine.process import Process, ProcessResult
from pants.engine.rules import RootRule
from pants.engine.selectors import Params
from pants.engine.target import FieldSet
from pants.python.python_setup import PythonSetup
from pants.testutil.engine.util import create_subsystem
from pants.testutil.external_tool_test_base import ExternalToolTestBase
from pants.testutil.option.util import create_options_bootstrapper
from pants.util.frozendict import FrozenDict
def test_merge_interpreter_constraints() -> None:
def assert_merged(*, inp: List[List[str]], expected: List[str]) -> None:
assert PexInterpreterConstraints.merge_constraint_sets(inp) == expected
# Multiple constraint sets get merged so that they are ANDed.
# A & B => A & B
assert_merged(inp=[["CPython==2.7.*"], ["CPython==3.6.*"]], expected=["CPython==2.7.*,==3.6.*"])
# Multiple constraints within a single constraint set are kept separate so that they are ORed.
# A | B => A | B
assert_merged(
inp=[["CPython==2.7.*", "CPython==3.6.*"]], expected=["CPython==2.7.*", "CPython==3.6.*"]
)
# Input constraints already were ANDed.
# A => A
assert_merged(inp=[["CPython>=2.7,<3"]], expected=["CPython>=2.7,<3"])
# Both AND and OR.
# (A | B) & C => (A & B) | (B & C)
assert_merged(
inp=[["CPython>=2.7,<3", "CPython>=3.5"], ["CPython==3.6.*"]],
expected=["CPython>=2.7,<3,==3.6.*", "CPython>=3.5,==3.6.*"],
)
# A & B & (C | D) => (A & B & C) | (A & B & D)
assert_merged(
inp=[["CPython==2.7.*"], ["CPython==3.6.*"], ["CPython==3.7.*", "CPython==3.8.*"]],
expected=["CPython==2.7.*,==3.6.*,==3.7.*", "CPython==2.7.*,==3.6.*,==3.8.*"],
)
# (A | B) & (C | D) => (A & C) | (A & D) | (B & C) | (B & D)
assert_merged(
inp=[["CPython>=2.7,<3", "CPython>=3.5"], ["CPython==3.6.*", "CPython==3.7.*"]],
expected=[
"CPython>=2.7,<3,==3.6.*",
"CPython>=2.7,<3,==3.7.*",
"CPython>=3.5,==3.6.*",
"CPython>=3.5,==3.7.*",
],
)
# A & (B | C | D) & (E | F) & G =>
# (A & B & E & G) | (A & B & F & G) | (A & C & E & G) | (A & C & F & G) | (A & D & E & G) | (A & D & F & G)
assert_merged(
inp=[
["CPython==3.6.5"],
["CPython==2.7.14", "CPython==2.7.15", "CPython==2.7.16"],
["CPython>=3.6", "CPython==3.5.10"],
["CPython>3.8"],
],
expected=[
"CPython==2.7.14,==3.5.10,==3.6.5,>3.8",
"CPython==2.7.14,>=3.6,==3.6.5,>3.8",
"CPython==2.7.15,==3.5.10,==3.6.5,>3.8",
"CPython==2.7.15,>=3.6,==3.6.5,>3.8",
"CPython==2.7.16,==3.5.10,==3.6.5,>3.8",
"CPython==2.7.16,>=3.6,==3.6.5,>3.8",
],
)
# Deduplicate between constraint_sets
# (A | B) & (A | B) => A | B. Naively, this should actually resolve as follows:
# (A | B) & (A | B) => (A & A) | (A & B) | (B & B) => A | (A & B) | B.
# But, we first deduplicate each constraint_set. (A | B) & (A | B) can be rewritten as
# X & X => X.
assert_merged(
inp=[["CPython==2.7.*", "CPython==3.6.*"], ["CPython==2.7.*", "CPython==3.6.*"]],
expected=["CPython==2.7.*", "CPython==3.6.*"],
)
# (A | B) & C & (A | B) => (A & C) | (B & C). Alternatively, this can be rewritten as
# X & Y & X => X & Y.
assert_merged(
inp=[
["CPython>=2.7,<3", "CPython>=3.5"],
["CPython==3.6.*"],
["CPython>=3.5", "CPython>=2.7,<3"],
],
expected=["CPython>=2.7,<3,==3.6.*", "CPython>=3.5,==3.6.*"],
)
# No specifiers
assert_merged(inp=[["CPython"]], expected=["CPython"])
assert_merged(inp=[["CPython"], ["CPython==3.7.*"]], expected=["CPython==3.7.*"])
# No interpreter is shorthand for CPython, which is how Pex behaves
assert_merged(inp=[[">=3.5"], ["CPython==3.7.*"]], expected=["CPython>=3.5,==3.7.*"])
# Different Python interpreters, which are guaranteed to fail when ANDed but are safe when ORed.
with pytest.raises(ValueError):
PexInterpreterConstraints.merge_constraint_sets([["CPython==3.7.*"], ["PyPy==43.0"]])
assert_merged(inp=[["CPython==3.7.*", "PyPy==43.0"]], expected=["CPython==3.7.*", "PyPy==43.0"])
# Ensure we can handle empty input.
assert_merged(inp=[], expected=[])
@dataclass(frozen=True)
class MockFieldSet(FieldSet):
compatibility: PythonInterpreterCompatibility
@classmethod
def create_for_test(cls, address: str, compat: Optional[str]) -> "MockFieldSet":
addr = Address.parse(address)
return cls(address=addr, compatibility=PythonInterpreterCompatibility(compat, address=addr))
def test_group_field_sets_by_constraints() -> None:
py2_fs = MockFieldSet.create_for_test("//:py2", ">=2.7,<3")
py3_fs = [
MockFieldSet.create_for_test("//:py3", "==3.6.*"),
MockFieldSet.create_for_test("//:py3_second", "==3.6.*"),
]
no_constraints_fs = MockFieldSet.create_for_test("//:no_constraints", None)
assert PexInterpreterConstraints.group_field_sets_by_constraints(
[py2_fs, *py3_fs, no_constraints_fs],
python_setup=create_subsystem(PythonSetup, interpreter_constraints=[]),
) == FrozenDict(
{
PexInterpreterConstraints(): (no_constraints_fs,),
PexInterpreterConstraints(["CPython>=2.7,<3"]): (py2_fs,),
PexInterpreterConstraints(["CPython==3.6.*"]): tuple(py3_fs),
}
)
@dataclass(frozen=True)
class ExactRequirement:
project_name: str
version: str
@classmethod
def parse(cls, requirement: str) -> "ExactRequirement":
req = Requirement.parse(requirement)
assert len(req.specs) == 1, (
"Expected an exact requirement with only 1 specifier, given {requirement} with "
"{count} specifiers".format(requirement=requirement, count=len(req.specs))
)
operator, version = req.specs[0]
assert operator == "==", (
"Expected an exact requirement using only the '==' specifier, given {requirement} "
"using the {operator!r} operator".format(requirement=requirement, operator=operator)
)
return cls(project_name=req.project_name, version=version)
def parse_requirements(requirements: Iterable[str]) -> Iterator[ExactRequirement]:
for requirement in requirements:
yield ExactRequirement.parse(requirement)
class PexTest(ExternalToolTestBase):
@classmethod
def rules(cls):
return (
*super().rules(),
*pex_rules(),
*download_pex_bin.rules(),
*python_native_code.rules(),
*subprocess_environment.rules(),
RootRule(PexRequest),
)
def create_pex_and_get_all_data(
self,
*,
requirements=PexRequirements(),
entry_point=None,
interpreter_constraints=PexInterpreterConstraints(),
platforms=PexPlatforms(),
sources: Optional[Digest] = None,
additional_inputs: Optional[Digest] = None,
additional_pants_args: Tuple[str, ...] = (),
additional_pex_args: Tuple[str, ...] = (),
) -> Dict:
request = PexRequest(
output_filename="test.pex",
requirements=requirements,
interpreter_constraints=interpreter_constraints,
platforms=platforms,
entry_point=entry_point,
sources=sources,
additional_inputs=additional_inputs,
additional_args=additional_pex_args,
)
pex = self.request_single_product(
Pex,
Params(
request,
create_options_bootstrapper(
args=["--backend-packages=pants.backend.python", *additional_pants_args]
),
),
)
self.scheduler.materialize_directory(DirectoryToMaterialize(pex.digest))
pex_path = os.path.join(self.build_root, "test.pex")
with zipfile.ZipFile(pex_path, "r") as zipfp:
with zipfp.open("PEX-INFO", "r") as pex_info:
pex_info_content = pex_info.readline().decode()
pex_list = zipfp.namelist()
return {
"pex": pex,
"local_path": pex_path,
"info": json.loads(pex_info_content),
"files": pex_list,
}
def create_pex_and_get_pex_info(
self,
*,
requirements=PexRequirements(),
entry_point=None,
interpreter_constraints=PexInterpreterConstraints(),
platforms=PexPlatforms(),
sources: Optional[Digest] = None,
additional_pants_args: Tuple[str, ...] = (),
additional_pex_args: Tuple[str, ...] = (),
) -> Dict:
return cast(
Dict,
self.create_pex_and_get_all_data(
requirements=requirements,
entry_point=entry_point,
interpreter_constraints=interpreter_constraints,
platforms=platforms,
sources=sources,
additional_pants_args=additional_pants_args,
additional_pex_args=additional_pex_args,
)["info"],
)
def test_pex_execution(self) -> None:
sources = self.request_single_product(
Digest,
CreateDigest(
(
FileContent(path="main.py", content=b'print("from main")'),
FileContent(path="subdir/sub.py", content=b'print("from sub")'),
)
),
)
pex_output = self.create_pex_and_get_all_data(entry_point="main", sources=sources)
pex_files = pex_output["files"]
assert "pex" not in pex_files
assert "main.py" in pex_files
assert "subdir/sub.py" in pex_files
# We reasonably expect there to be a python interpreter on the test-running
# process's path.
env = {"PATH": os.getenv("PATH", "")}
process = Process(
argv=("python", "test.pex"),
env=env,
input_digest=pex_output["pex"].digest,
description="Run the pex and make sure it works",
)
result = self.request_single_product(ProcessResult, process)
assert result.stdout == b"from main\n"
def test_resolves_dependencies(self) -> None:
requirements = PexRequirements(["six==1.12.0", "jsonschema==2.6.0", "requests==2.23.0"])
pex_info = self.create_pex_and_get_pex_info(requirements=requirements)
# NB: We do not check for transitive dependencies, which PEX-INFO will include. We only check
# that at least the dependencies we requested are included.
assert set(parse_requirements(requirements)).issubset(
set(parse_requirements(pex_info["requirements"]))
)
def test_requirement_constraints(self) -> None:
# This is intentionally old; a constraint will resolve us to a more modern version.
direct_dep = "requests==1.0.0"
constraints = [
"requests==2.23.0",
"certifi==2019.6.16",
"chardet==3.0.2",
"idna==2.7",
"urllib3==1.25.6",
]
self.create_file("constraints.txt", "\n".join(constraints))
pex_info = self.create_pex_and_get_pex_info(
requirements=PexRequirements([direct_dep]),
additional_pants_args=("--python-setup-requirement-constraints=constraints.txt",),
)
assert set(parse_requirements(pex_info["requirements"])) == set(
parse_requirements(constraints)
)
def test_entry_point(self) -> None:
entry_point = "pydoc"
pex_info = self.create_pex_and_get_pex_info(entry_point=entry_point)
assert pex_info["entry_point"] == entry_point
def test_interpreter_constraints(self) -> None:
constraints = PexInterpreterConstraints(["CPython>=2.7,<3", "CPython>=3.6"])
pex_info = self.create_pex_and_get_pex_info(interpreter_constraints=constraints)
assert set(pex_info["interpreter_constraints"]) == set(constraints)
def test_additional_args(self) -> None:
pex_info = self.create_pex_and_get_pex_info(additional_pex_args=("--not-zip-safe",))
assert pex_info["zip_safe"] is False
def test_platforms(self) -> None:
# We use Python 2.7, rather than Python 3, to ensure that the specified platform is
# actually used.
platforms = PexPlatforms(["linux-x86_64-cp-27-cp27mu"])
constraints = PexInterpreterConstraints(["CPython>=2.7,<3", "CPython>=3.6"])
pex_output = self.create_pex_and_get_all_data(
requirements=PexRequirements(["cryptography==2.9"]),
platforms=platforms,
interpreter_constraints=constraints,
)
assert any(
"cryptography-2.9-cp27-cp27mu-manylinux2010_x86_64.whl" in fp
for fp in pex_output["files"]
)
assert not any("cryptography-2.9-cp27-cp27m-" in fp for fp in pex_output["files"])
assert not any("cryptography-2.9-cp35-abi3" in fp for fp in pex_output["files"])
# NB: Platforms override interpreter constraints.
assert pex_output["info"]["interpreter_constraints"] == []
def test_additional_inputs(self) -> None:
# We use pex's --preamble-file option to set a custom preamble from a file.
# This verifies that the file was indeed provided as additional input to the pex call.
preamble_file = "custom_preamble.txt"
preamble = "#!CUSTOM PREAMBLE\n"
additional_inputs = self.request_single_product(
Digest, CreateDigest([FileContent(path=preamble_file, content=preamble.encode())])
)
additional_pex_args = (f"--preamble-file={preamble_file}",)
pex_output = self.create_pex_and_get_all_data(
additional_inputs=additional_inputs, additional_pex_args=additional_pex_args
)
with zipfile.ZipFile(pex_output["local_path"], "r") as zipfp:
with zipfp.open("__main__.py", "r") as main:
main_content = main.read().decode()
assert main_content[: len(preamble)] == preamble