-
Notifications
You must be signed in to change notification settings - Fork 54
/
test_utils.py
326 lines (267 loc) · 10.3 KB
/
test_utils.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
# coding: utf-8
from __future__ import unicode_literals
import os
import six
from pytest import mark, raises
from six.moves import shlex_quote
from piptools.scripts.compile import cli as compile_cli
from piptools.utils import (
as_tuple,
dedup,
flat_map,
force_text,
format_requirement,
format_specifier,
fs_str,
get_compile_command,
get_hashes_from_ireq,
is_pinned_requirement,
is_url_requirement,
name_from_req,
)
def test_format_requirement(from_line):
ireq = from_line("test==1.2")
assert format_requirement(ireq) == "test==1.2"
def test_format_requirement_url(from_line):
ireq = from_line("https://example.com/example.zip")
assert format_requirement(ireq) == "https://example.com/example.zip"
def test_format_requirement_editable_vcs(from_editable):
ireq = from_editable("git+git://fake.org/x/y.git#egg=y")
assert format_requirement(ireq) == "-e git+git://fake.org/x/y.git#egg=y"
def test_format_requirement_editable_vcs_with_password(from_editable):
ireq = from_editable("git+git://user:password@fake.org/x/y.git#egg=y")
assert (
format_requirement(ireq) == "-e git+git://user:password@fake.org/x/y.git#egg=y"
)
def test_format_requirement_editable_local_path(from_editable):
ireq = from_editable("file:///home/user/package")
assert format_requirement(ireq) == "-e file:///home/user/package"
def test_format_requirement_ireq_with_hashes(from_line):
ireq = from_line("pytz==2017.2")
ireq_hashes = [
"sha256:d1d6729c85acea5423671382868627129432fba9a89ecbb248d8d1c7a9f01c67",
"sha256:f5c056e8f62d45ba8215e5cb8f50dfccb198b4b9fbea8500674f3443e4689589",
]
expected = (
"pytz==2017.2 \\\n"
" --hash=sha256:d1d6729c85acea542367138286"
"8627129432fba9a89ecbb248d8d1c7a9f01c67 \\\n"
" --hash=sha256:f5c056e8f62d45ba8215e5cb8f5"
"0dfccb198b4b9fbea8500674f3443e4689589"
)
assert format_requirement(ireq, hashes=ireq_hashes) == expected
def test_format_requirement_ireq_with_hashes_and_markers(from_line):
ireq = from_line("pytz==2017.2")
marker = 'python_version<"3.0"'
ireq_hashes = [
"sha256:d1d6729c85acea5423671382868627129432fba9a89ecbb248d8d1c7a9f01c67",
"sha256:f5c056e8f62d45ba8215e5cb8f50dfccb198b4b9fbea8500674f3443e4689589",
]
expected = (
'pytz==2017.2 ; python_version<"3.0" \\\n'
" --hash=sha256:d1d6729c85acea542367138286"
"8627129432fba9a89ecbb248d8d1c7a9f01c67 \\\n"
" --hash=sha256:f5c056e8f62d45ba8215e5cb8f5"
"0dfccb198b4b9fbea8500674f3443e4689589"
)
assert format_requirement(ireq, marker, hashes=ireq_hashes) == expected
def test_format_specifier(from_line):
ireq = from_line("foo")
assert format_specifier(ireq) == "<any>"
ireq = from_line("foo==1.2")
assert format_specifier(ireq) == "==1.2"
ireq = from_line("foo>1.2,~=1.1,<1.5")
assert format_specifier(ireq) == "~=1.1,>1.2,<1.5"
ireq = from_line("foo~=1.1,<1.5,>1.2")
assert format_specifier(ireq) == "~=1.1,>1.2,<1.5"
def test_as_tuple(from_line):
ireq = from_line("foo==1.1")
name, version, extras = as_tuple(ireq)
assert name == "foo"
assert version == "1.1"
assert extras == ()
ireq = from_line("foo[extra1,extra2]==1.1")
name, version, extras = as_tuple(ireq)
assert name == "foo"
assert version == "1.1"
assert extras == ("extra1", "extra2")
# Non-pinned versions aren't accepted
should_be_rejected = ["foo==1.*", "foo~=1.1,<1.5,>1.2", "foo"]
for spec in should_be_rejected:
ireq = from_line(spec)
with raises(TypeError):
as_tuple(ireq)
def test_flat_map():
assert [1, 2, 4, 1, 3, 9] == list(flat_map(lambda x: [1, x, x * x], [2, 3]))
def test_dedup():
assert list(dedup([3, 1, 2, 4, 3, 5])) == [3, 1, 2, 4, 5]
def test_get_hashes_from_ireq(from_line):
ireq = from_line(
"pytz==2017.2",
options={
"hashes": {
"sha256": [
"d1d6729c85acea5423671382868627129432fba9a89ecbb248d8d1c7a9f01c67",
"f5c056e8f62d45ba8215e5cb8f50dfccb198b4b9fbea8500674f3443e4689589",
]
}
},
)
expected = [
"sha256:d1d6729c85acea5423671382868627129432fba9a89ecbb248d8d1c7a9f01c67",
"sha256:f5c056e8f62d45ba8215e5cb8f50dfccb198b4b9fbea8500674f3443e4689589",
]
assert get_hashes_from_ireq(ireq) == expected
@mark.parametrize(
("line", "expected"),
[
("django==1.8", True),
("django===1.8", True),
("django>1.8", False),
("django~=1.8", False),
("django==1.*", False),
("file:///example.zip", False),
("https://example.com/example.zip", False),
],
)
def test_is_pinned_requirement(from_line, line, expected):
ireq = from_line(line)
assert is_pinned_requirement(ireq) is expected
def test_is_pinned_requirement_editable(from_editable):
ireq = from_editable("git+git://fake.org/x/y.git#egg=y")
assert not is_pinned_requirement(ireq)
@mark.parametrize(
("line", "expected"),
[
("django==1.8", False),
("django", False),
("file:///example.zip", True),
("https://example.com/example.zip", True),
("https://example.com/example.zip#egg=example", True),
("git+git://github.com/jazzband/pip-tools@master", True),
("../example.zip", True),
("/example.zip", True),
],
)
def test_is_url_requirement(from_line, line, expected):
ireq = from_line(line)
assert is_url_requirement(ireq) is expected
def test_name_from_req(from_line):
ireq = from_line("django==1.8")
assert name_from_req(ireq.req) == "django"
def test_name_from_req_with_project_name(from_line):
ireq = from_line("foo==1.8")
ireq.req.project_name = "bar"
assert name_from_req(ireq.req) == "bar"
def test_fs_str():
assert fs_str("some path component/Something") == "some path component/Something"
assert isinstance(fs_str("whatever"), str)
@mark.skipif(six.PY2, reason="Not supported in py2")
def test_fs_str_with_bytes():
with raises(AssertionError):
fs_str(b"whatever")
@mark.parametrize(
"value, expected_text", [(None, ""), (42, "42"), ("foo", "foo"), ("bãr", "bãr")]
)
def test_force_text(value, expected_text):
assert force_text(value) == expected_text
@mark.parametrize(
"cli_args, expected_command",
[
# Check empty args
([], "pip-compile"),
# Check all options which will be excluded from command
(["-v"], "pip-compile"),
(["--verbose"], "pip-compile"),
(["-n"], "pip-compile"),
(["--dry-run"], "pip-compile"),
(["-q"], "pip-compile"),
(["--quiet"], "pip-compile"),
(["-r"], "pip-compile"),
(["--rebuild"], "pip-compile"),
(["-U"], "pip-compile"),
(["--upgrade"], "pip-compile"),
(["-P", "django"], "pip-compile"),
(["--upgrade-package", "django"], "pip-compile"),
# Check options
(["--max-rounds", "42"], "pip-compile --max-rounds=42"),
(["--index-url", "https://foo"], "pip-compile --index-url=https://foo"),
# Check that short options will be expanded to long options
(["-p"], "pip-compile --pre"),
(["-f", "links"], "pip-compile --find-links=links"),
(["-i", "https://foo"], "pip-compile --index-url=https://foo"),
# Check positive flags
(["--generate-hashes"], "pip-compile --generate-hashes"),
(["--pre"], "pip-compile --pre"),
(["--allow-unsafe"], "pip-compile --allow-unsafe"),
# Check negative flags
(["--no-index"], "pip-compile --no-index"),
(["--no-emit-trusted-host"], "pip-compile --no-emit-trusted-host"),
(["--no-annotate"], "pip-compile --no-annotate"),
# Check that default values will be removed from the command
(["--emit-trusted-host"], "pip-compile"),
(["--annotate"], "pip-compile"),
(["--index"], "pip-compile"),
(["--max-rounds=10"], "pip-compile"),
(["--no-build-isolation"], "pip-compile"),
# Check options with multiple values
(
["--find-links", "links1", "--find-links", "links2"],
"pip-compile --find-links=links1 --find-links=links2",
),
# Check that option values will be quoted
(["-f", "foo;bar"], "pip-compile --find-links='foo;bar'"),
(["-f", "συνδέσεις"], "pip-compile --find-links='συνδέσεις'"),
(["-o", "my file.txt"], "pip-compile --output-file='my file.txt'"),
(["-o", "απαιτήσεις.txt"], "pip-compile --output-file='απαιτήσεις.txt'"),
],
)
def test_get_compile_command(tmpdir_cwd, cli_args, expected_command):
"""
Test general scenarios for the get_compile_command function.
"""
with compile_cli.make_context("pip-compile", cli_args) as ctx:
assert get_compile_command(ctx) == expected_command
@mark.parametrize(
"filename", ["requirements.in", "my requirements.in", "απαιτήσεις.txt"]
)
def test_get_compile_command_with_files(tmpdir_cwd, filename):
"""
Test that get_compile_command returns a command with correct
and sanitized file names.
"""
os.mkdir("sub")
path = os.path.join("sub", filename)
with open(path, "w"):
pass
args = [path, "--output-file", "requirements.txt"]
with compile_cli.make_context("pip-compile", args) as ctx:
assert get_compile_command(
ctx
) == "pip-compile --output-file=requirements.txt {src_file}".format(
src_file=shlex_quote(path)
)
def test_get_compile_command_sort_args(tmpdir_cwd):
"""
Test that get_compile_command correctly sorts arguments.
The order is "pip-compile {sorted options} {sorted src files}".
"""
with open("setup.py", "w"), open("requirements.in", "w"):
pass
args = [
"--no-index",
"--no-emit-trusted-host",
"--no-annotate",
"setup.py",
"--find-links",
"foo",
"--find-links",
"bar",
"requirements.in",
]
with compile_cli.make_context("pip-compile", args) as ctx:
assert get_compile_command(ctx) == (
"pip-compile --find-links=bar --find-links=foo "
"--no-annotate --no-emit-trusted-host --no-index "
"requirements.in setup.py"
)