forked from pantsbuild/pants
-
Notifications
You must be signed in to change notification settings - Fork 0
/
python_requirements_test.py
158 lines (143 loc) · 6.03 KB
/
python_requirements_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
# Copyright 2020 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).
from textwrap import dedent
from typing import Iterable
import pytest
from pkg_resources import Requirement
from pants.backend.python.macros.python_requirements import PythonRequirements
from pants.backend.python.target_types import PythonRequirementLibrary, PythonRequirementsFile
from pants.base.specs import AddressSpecs, DescendantAddresses, FilesystemSpecs, Specs
from pants.engine.addresses import Address
from pants.engine.internals.scheduler import ExecutionError
from pants.engine.rules import QueryRule
from pants.engine.target import Targets
from pants.option.options_bootstrapper import OptionsBootstrapper
from pants.testutil.option_util import create_options_bootstrapper
from pants.testutil.rule_runner import RuleRunner
@pytest.fixture
def rule_runner() -> RuleRunner:
return RuleRunner(
rules=[QueryRule(Targets, (OptionsBootstrapper, Specs))],
target_types=[PythonRequirementLibrary, PythonRequirementsFile],
context_aware_object_factories={"python_requirements": PythonRequirements},
)
def assert_python_requirements(
rule_runner: RuleRunner,
build_file_entry: str,
requirements_txt: str,
*,
expected_file_dep: PythonRequirementsFile,
expected_targets: Iterable[PythonRequirementLibrary],
requirements_txt_relpath: str = "requirements.txt",
) -> None:
rule_runner.add_to_build_file("", f"{build_file_entry}\n")
rule_runner.create_file(requirements_txt_relpath, requirements_txt)
targets = rule_runner.request_product(
Targets,
[
Specs(AddressSpecs([DescendantAddresses("")]), FilesystemSpecs([])),
create_options_bootstrapper(),
],
)
assert {expected_file_dep, *expected_targets} == set(targets)
def test_requirements_txt(rule_runner: RuleRunner) -> None:
"""This tests that we correctly create a new python_requirement_library for each entry in a
requirements.txt file.
Some edge cases:
* We ignore comments and options (values that start with `--`).
* If a module_mapping is given, and the project is in the map, we copy over a subset of the
mapping to the created target.
* Projects get normalized thanks to Requirement.parse().
"""
assert_python_requirements(
rule_runner,
"python_requirements(module_mapping={'ansicolors': ['colors']})",
dedent(
"""\
# Comment.
--find-links=https://duckduckgo.com
ansicolors>=1.18.0
Django==3.2 ; python_version>'3'
Un-Normalized-PROJECT # Inline comment.
pip@ git+https://github.com/pypa/pip.git
"""
),
expected_file_dep=PythonRequirementsFile(
{"sources": ["requirements.txt"]},
address=Address("", target_name="requirements.txt"),
),
expected_targets=[
PythonRequirementLibrary(
{
"dependencies": [":requirements.txt"],
"requirements": [Requirement.parse("ansicolors>=1.18.0")],
"module_mapping": {"ansicolors": ["colors"]},
},
address=Address("", target_name="ansicolors"),
),
PythonRequirementLibrary(
{
"dependencies": [":requirements.txt"],
"requirements": [Requirement.parse("Django==3.2 ; python_version>'3'")],
},
address=Address("", target_name="Django"),
),
PythonRequirementLibrary(
{
"dependencies": [":requirements.txt"],
"requirements": [Requirement.parse("Un_Normalized_PROJECT")],
},
address=Address("", target_name="Un-Normalized-PROJECT"),
),
PythonRequirementLibrary(
{
"dependencies": [":requirements.txt"],
"requirements": [Requirement.parse("pip@ git+https://github.com/pypa/pip.git")],
},
address=Address("", target_name="pip"),
),
],
)
def test_invalid_req(rule_runner: RuleRunner) -> None:
"""Test that we give a nice error message."""
with pytest.raises(ExecutionError) as exc:
assert_python_requirements(
rule_runner,
"python_requirements()",
"\n\nNot A Valid Req == 3.7",
expected_file_dep=PythonRequirementsFile({}, address=Address("doesnt_matter")),
expected_targets=[],
)
assert "Invalid requirement 'Not A Valid Req == 3.7' in requirements.txt at line 3" in str(
exc.value
)
# Give a nice error message if it looks like they're using pip VCS-style requirements.
with pytest.raises(ExecutionError) as exc:
assert_python_requirements(
rule_runner,
"python_requirements()",
"git+https://github.com/pypa/pip.git#egg=pip",
expected_file_dep=PythonRequirementsFile({}, address=Address("doesnt_matter")),
expected_targets=[],
)
assert "It looks like you're trying to use a pip VCS-style requirement?" in str(exc.value)
def test_relpath_override(rule_runner: RuleRunner) -> None:
assert_python_requirements(
rule_runner,
"python_requirements(requirements_relpath='subdir/requirements.txt')",
"ansicolors>=1.18.0",
requirements_txt_relpath="subdir/requirements.txt",
expected_file_dep=PythonRequirementsFile(
{"sources": ["subdir/requirements.txt"]},
address=Address("", target_name="subdir/requirements.txt"),
),
expected_targets=[
PythonRequirementLibrary(
{
"dependencies": [":subdir/requirements.txt"],
"requirements": [Requirement.parse("ansicolors>=1.18.0")],
},
address=Address("", target_name="ansicolors"),
),
],
)