Debian Codegraph @

testing/web-platform/tests/tools/third_party/packaging/tests/test_specifiers.py

raw
1 # This file is dual licensed under the terms of the Apache License, Version
2 # 2.0, and the BSD License. See the LICENSE file in the root of this repository
3 # for complete details.
4
5 import itertools
6 import operator
7 import warnings
8
9 import pytest
10
11 from packaging.specifiers import (
12 InvalidSpecifier,
13 LegacySpecifier,
14 Specifier,
15 SpecifierSet,
16 )
17 from packaging.version import LegacyVersion, Version, parse
18
19 from .test_version import LEGACY_VERSIONS, VERSIONS
20
21 LEGACY_SPECIFIERS = [
22 "==2.1.0.3",
23 "!=2.2.0.5",
24 "<=5",
25 ">=7.9a1",
26 "<1.0.dev1",
27 ">2.0.post1",
28 ]
29
30 SPECIFIERS = [
31 "~=2.0",
32 "==2.1.*",
33 "==2.1.0.3",
34 "!=2.2.*",
35 "!=2.2.0.5",
36 "<=5",
37 ">=7.9a1",
38 "<1.0.dev1",
39 ">2.0.post1",
40 "===lolwat",
41 ]
42
43
44 class TestSpecifier:
45 @pytest.mark.parametrize("specifier", SPECIFIERS)
46 def test_specifiers_valid(self, specifier):
47 Specifier(specifier)
48
49 @pytest.mark.parametrize(
50 "specifier",
51 [
52 # Operator-less specifier
53 "2.0",
54 # Invalid operator
55 "=>2.0",
56 # Version-less specifier
57 "==",
58 # Local segment on operators which don't support them
59 "~=1.0+5",
60 ">=1.0+deadbeef",
61 "<=1.0+abc123",
62 ">1.0+watwat",
63 "<1.0+1.0",
64 # Prefix matching on operators which don't support them
65 "~=1.0.*",
66 ">=1.0.*",
67 "<=1.0.*",
68 ">1.0.*",
69 "<1.0.*",
70 # Combination of local and prefix matching on operators which do
71 # support one or the other
72 "==1.0.*+5",
73 "!=1.0.*+deadbeef",
74 # Prefix matching cannot be used inside of a local version
75 "==1.0+5.*",
76 "!=1.0+deadbeef.*",
77 # Prefix matching must appear at the end
78 "==1.0.*.5",
79 # Compatible operator requires 2 digits in the release operator
80 "~=1",
81 # Cannot use a prefix matching after a .devN version
82 "==1.0.dev1.*",
83 "!=1.0.dev1.*",
84 ],
85 )
86 def test_specifiers_invalid(self, specifier):
87 with pytest.raises(InvalidSpecifier):
88 Specifier(specifier)
89
90 @pytest.mark.parametrize(
91 "version",
92 [
93 # Various development release incarnations
94 "1.0dev",
95 "1.0.dev",
96 "1.0dev1",
97 "1.0dev",
98 "1.0-dev",
99 "1.0-dev1",
100 "1.0DEV",
101 "1.0.DEV",
102 "1.0DEV1",
103 "1.0DEV",
104 "1.0.DEV1",
105 "1.0-DEV",
106 "1.0-DEV1",
107 # Various alpha incarnations
108 "1.0a",
109 "1.0.a",
110 "1.0.a1",
111 "1.0-a",
112 "1.0-a1",
113 "1.0alpha",
114 "1.0.alpha",
115 "1.0.alpha1",
116 "1.0-alpha",
117 "1.0-alpha1",
118 "1.0A",
119 "1.0.A",
120 "1.0.A1",
121 "1.0-A",
122 "1.0-A1",
123 "1.0ALPHA",
124 "1.0.ALPHA",
125 "1.0.ALPHA1",
126 "1.0-ALPHA",
127 "1.0-ALPHA1",
128 # Various beta incarnations
129 "1.0b",
130 "1.0.b",
131 "1.0.b1",
132 "1.0-b",
133 "1.0-b1",
134 "1.0beta",
135 "1.0.beta",
136 "1.0.beta1",
137 "1.0-beta",
138 "1.0-beta1",
139 "1.0B",
140 "1.0.B",
141 "1.0.B1",
142 "1.0-B",
143 "1.0-B1",
144 "1.0BETA",
145 "1.0.BETA",
146 "1.0.BETA1",
147 "1.0-BETA",
148 "1.0-BETA1",
149 # Various release candidate incarnations
150 "1.0c",
151 "1.0.c",
152 "1.0.c1",
153 "1.0-c",
154 "1.0-c1",
155 "1.0rc",
156 "1.0.rc",
157 "1.0.rc1",
158 "1.0-rc",
159 "1.0-rc1",
160 "1.0C",
161 "1.0.C",
162 "1.0.C1",
163 "1.0-C",
164 "1.0-C1",
165 "1.0RC",
166 "1.0.RC",
167 "1.0.RC1",
168 "1.0-RC",
169 "1.0-RC1",
170 # Various post release incarnations
171 "1.0post",
172 "1.0.post",
173 "1.0post1",
174 "1.0post",
175 "1.0-post",
176 "1.0-post1",
177 "1.0POST",
178 "1.0.POST",
179 "1.0POST1",
180 "1.0POST",
181 "1.0.POST1",
182 "1.0-POST",
183 "1.0-POST1",
184 "1.0-5",
185 # Local version case insensitivity
186 "1.0+AbC"
187 # Integer Normalization
188 "1.01",
189 "1.0a05",
190 "1.0b07",
191 "1.0c056",
192 "1.0rc09",
193 "1.0.post000",
194 "1.1.dev09000",
195 "00!1.2",
196 "0100!0.0",
197 # Various other normalizations
198 "v1.0",
199 " \r \f \v v1.0\t\n",
200 ],
201 )
202 def test_specifiers_normalized(self, version):
203 if "+" not in version:
204 ops = ["~=", "==", "!=", "<=", ">=", "<", ">"]
205 else:
206 ops = ["==", "!="]
207
208 for op in ops:
209 Specifier(op + version)
210
211 @pytest.mark.parametrize(
212 ("specifier", "expected"),
213 [
214 # Single item specifiers should just be reflexive
215 ("!=2.0", "!=2.0"),
216 ("<2.0", "<2.0"),
217 ("<=2.0", "<=2.0"),
218 ("==2.0", "==2.0"),
219 (">2.0", ">2.0"),
220 (">=2.0", ">=2.0"),
221 ("~=2.0", "~=2.0"),
222 # Spaces should be removed
223 ("< 2", "<2"),
224 ],
225 )
226 def test_specifiers_str_and_repr(self, specifier, expected):
227 spec = Specifier(specifier)
228
229 assert str(spec) == expected
230 assert repr(spec) == f"<Specifier({expected!r})>"
231
232 @pytest.mark.parametrize("specifier", SPECIFIERS)
233 def test_specifiers_hash(self, specifier):
234 assert hash(Specifier(specifier)) == hash(Specifier(specifier))
235
236 @pytest.mark.parametrize(
237 ("left", "right", "op"),
238 itertools.chain(
239 *
240 # Verify that the equal (==) operator works correctly
241 [[(x, x, operator.eq) for x in SPECIFIERS]]
242 +
243 # Verify that the not equal (!=) operator works correctly
244 [
245 [(x, y, operator.ne) for j, y in enumerate(SPECIFIERS) if i != j]
246 for i, x in enumerate(SPECIFIERS)
247 ]
248 ),
249 )
250 def test_comparison_true(self, left, right, op):
251 assert op(Specifier(left), Specifier(right))
252 assert op(left, Specifier(right))
253 assert op(Specifier(left), right)
254
255 @pytest.mark.parametrize(("left", "right"), [("==2.8.0", "==2.8")])
256 def test_comparison_canonicalizes(self, left, right):
257 assert Specifier(left) == Specifier(right)
258 assert left == Specifier(right)
259 assert Specifier(left) == right
260
261 @pytest.mark.parametrize(
262 ("left", "right", "op"),
263 itertools.chain(
264 *
265 # Verify that the equal (==) operator works correctly
266 [[(x, x, operator.ne) for x in SPECIFIERS]]
267 +
268 # Verify that the not equal (!=) operator works correctly
269 [
270 [(x, y, operator.eq) for j, y in enumerate(SPECIFIERS) if i != j]
271 for i, x in enumerate(SPECIFIERS)
272 ]
273 ),
274 )
275 def test_comparison_false(self, left, right, op):
276 assert not op(Specifier(left), Specifier(right))
277 assert not op(left, Specifier(right))
278 assert not op(Specifier(left), right)
279
280 def test_comparison_non_specifier(self):
281 assert Specifier("==1.0") != 12
282 assert not Specifier("==1.0") == 12
283 assert Specifier("==1.0") != "12"
284 assert not Specifier("==1.0") == "12"
285
286 @pytest.mark.parametrize(
287 ("version", "spec", "expected"),
288 [
289 (v, s, True)
290 for v, s in [
291 # Test the equality operation
292 ("2.0", "==2"),
293 ("2.0", "==2.0"),
294 ("2.0", "==2.0.0"),
295 ("2.0+deadbeef", "==2"),
296 ("2.0+deadbeef", "==2.0"),
297 ("2.0+deadbeef", "==2.0.0"),
298 ("2.0+deadbeef", "==2+deadbeef"),
299 ("2.0+deadbeef", "==2.0+deadbeef"),
300 ("2.0+deadbeef", "==2.0.0+deadbeef"),
301 ("2.0+deadbeef.0", "==2.0.0+deadbeef.00"),
302 # Test the equality operation with a prefix
303 ("2.dev1", "==2.*"),
304 ("2a1", "==2.*"),
305 ("2a1.post1", "==2.*"),
306 ("2b1", "==2.*"),
307 ("2b1.dev1", "==2.*"),
308 ("2c1", "==2.*"),
309 ("2c1.post1.dev1", "==2.*"),
310 ("2rc1", "==2.*"),
311 ("2", "==2.*"),
312 ("2.0", "==2.*"),
313 ("2.0.0", "==2.*"),
314 ("2.0.post1", "==2.0.post1.*"),
315 ("2.0.post1.dev1", "==2.0.post1.*"),
316 ("2.1+local.version", "==2.1.*"),
317 # Test the in-equality operation
318 ("2.1", "!=2"),
319 ("2.1", "!=2.0"),
320 ("2.0.1", "!=2"),
321 ("2.0.1", "!=2.0"),
322 ("2.0.1", "!=2.0.0"),
323 ("2.0", "!=2.0+deadbeef"),
324 # Test the in-equality operation with a prefix
325 ("2.0", "!=3.*"),
326 ("2.1", "!=2.0.*"),
327 # Test the greater than equal operation
328 ("2.0", ">=2"),
329 ("2.0", ">=2.0"),
330 ("2.0", ">=2.0.0"),
331 ("2.0.post1", ">=2"),
332 ("2.0.post1.dev1", ">=2"),
333 ("3", ">=2"),
334 # Test the less than equal operation
335 ("2.0", "<=2"),
336 ("2.0", "<=2.0"),
337 ("2.0", "<=2.0.0"),
338 ("2.0.dev1", "<=2"),
339 ("2.0a1", "<=2"),
340 ("2.0a1.dev1", "<=2"),
341 ("2.0b1", "<=2"),
342 ("2.0b1.post1", "<=2"),
343 ("2.0c1", "<=2"),
344 ("2.0c1.post1.dev1", "<=2"),
345 ("2.0rc1", "<=2"),
346 ("1", "<=2"),
347 # Test the greater than operation
348 ("3", ">2"),
349 ("2.1", ">2.0"),
350 ("2.0.1", ">2"),
351 ("2.1.post1", ">2"),
352 ("2.1+local.version", ">2"),
353 # Test the less than operation
354 ("1", "<2"),
355 ("2.0", "<2.1"),
356 ("2.0.dev0", "<2.1"),
357 # Test the compatibility operation
358 ("1", "~=1.0"),
359 ("1.0.1", "~=1.0"),
360 ("1.1", "~=1.0"),
361 ("1.9999999", "~=1.0"),
362 ("1.1", "~=1.0a1"),
363 # Test that epochs are handled sanely
364 ("2!1.0", "~=2!1.0"),
365 ("2!1.0", "==2!1.*"),
366 ("2!1.0", "==2!1.0"),
367 ("2!1.0", "!=1.0"),
368 ("1.0", "!=2!1.0"),
369 ("1.0", "<=2!0.1"),
370 ("2!1.0", ">=2.0"),
371 ("1.0", "<2!0.1"),
372 ("2!1.0", ">2.0"),
373 # Test some normalization rules
374 ("2.0.5", ">2.0dev"),
375 ]
376 ]
377 + [
378 (v, s, False)
379 for v, s in [
380 # Test the equality operation
381 ("2.1", "==2"),
382 ("2.1", "==2.0"),
383 ("2.1", "==2.0.0"),
384 ("2.0", "==2.0+deadbeef"),
385 # Test the equality operation with a prefix
386 ("2.0", "==3.*"),
387 ("2.1", "==2.0.*"),
388 # Test the in-equality operation
389 ("2.0", "!=2"),
390 ("2.0", "!=2.0"),
391 ("2.0", "!=2.0.0"),
392 ("2.0+deadbeef", "!=2"),
393 ("2.0+deadbeef", "!=2.0"),
394 ("2.0+deadbeef", "!=2.0.0"),
395 ("2.0+deadbeef", "!=2+deadbeef"),
396 ("2.0+deadbeef", "!=2.0+deadbeef"),
397 ("2.0+deadbeef", "!=2.0.0+deadbeef"),
398 ("2.0+deadbeef.0", "!=2.0.0+deadbeef.00"),
399 # Test the in-equality operation with a prefix
400 ("2.dev1", "!=2.*"),
401 ("2a1", "!=2.*"),
402 ("2a1.post1", "!=2.*"),
403 ("2b1", "!=2.*"),
404 ("2b1.dev1", "!=2.*"),
405 ("2c1", "!=2.*"),
406 ("2c1.post1.dev1", "!=2.*"),
407 ("2rc1", "!=2.*"),
408 ("2", "!=2.*"),
409 ("2.0", "!=2.*"),
410 ("2.0.0", "!=2.*"),
411 ("2.0.post1", "!=2.0.post1.*"),
412 ("2.0.post1.dev1", "!=2.0.post1.*"),
413 # Test the greater than equal operation
414 ("2.0.dev1", ">=2"),
415 ("2.0a1", ">=2"),
416 ("2.0a1.dev1", ">=2"),
417 ("2.0b1", ">=2"),
418 ("2.0b1.post1", ">=2"),
419 ("2.0c1", ">=2"),
420 ("2.0c1.post1.dev1", ">=2"),
421 ("2.0rc1", ">=2"),
422 ("1", ">=2"),
423 # Test the less than equal operation
424 ("2.0.post1", "<=2"),
425 ("2.0.post1.dev1", "<=2"),
426 ("3", "<=2"),
427 # Test the greater than operation
428 ("1", ">2"),
429 ("2.0.dev1", ">2"),
430 ("2.0a1", ">2"),
431 ("2.0a1.post1", ">2"),
432 ("2.0b1", ">2"),
433 ("2.0b1.dev1", ">2"),
434 ("2.0c1", ">2"),
435 ("2.0c1.post1.dev1", ">2"),
436 ("2.0rc1", ">2"),
437 ("2.0", ">2"),
438 ("2.0.post1", ">2"),
439 ("2.0.post1.dev1", ">2"),
440 ("2.0+local.version", ">2"),
441 # Test the less than operation
442 ("2.0.dev1", "<2"),
443 ("2.0a1", "<2"),
444 ("2.0a1.post1", "<2"),
445 ("2.0b1", "<2"),
446 ("2.0b2.dev1", "<2"),
447 ("2.0c1", "<2"),
448 ("2.0c1.post1.dev1", "<2"),
449 ("2.0rc1", "<2"),
450 ("2.0", "<2"),
451 ("2.post1", "<2"),
452 ("2.post1.dev1", "<2"),
453 ("3", "<2"),
454 # Test the compatibility operation
455 ("2.0", "~=1.0"),
456 ("1.1.0", "~=1.0.0"),
457 ("1.1.post1", "~=1.0.0"),
458 # Test that epochs are handled sanely
459 ("1.0", "~=2!1.0"),
460 ("2!1.0", "~=1.0"),
461 ("2!1.0", "==1.0"),
462 ("1.0", "==2!1.0"),
463 ("2!1.0", "==1.*"),
464 ("1.0", "==2!1.*"),
465 ("2!1.0", "!=2!1.0"),
466 ]
467 ],
468 )
469 def test_specifiers(self, version, spec, expected):
470 spec = Specifier(spec, prereleases=True)
471
472 if expected:
473 # Test that the plain string form works
474 assert version in spec
475 assert spec.contains(version)
476
477 # Test that the version instance form works
478 assert Version(version) in spec
479 assert spec.contains(Version(version))
480 else:
481 # Test that the plain string form works
482 assert version not in spec
483 assert not spec.contains(version)
484
485 # Test that the version instance form works
486 assert Version(version) not in spec
487 assert not spec.contains(Version(version))
488
489 @pytest.mark.parametrize(
490 ("version", "spec", "expected"),
491 [
492 # Test identity comparison by itself
493 ("lolwat", "===lolwat", True),
494 ("Lolwat", "===lolwat", True),
495 ("1.0", "===1.0", True),
496 ("nope", "===lolwat", False),
497 ("1.0.0", "===1.0", False),
498 ("1.0.dev0", "===1.0.dev0", True),
499 ],
500 )
501 def test_specifiers_identity(self, version, spec, expected):
502 spec = Specifier(spec)
503
504 if expected:
505 # Identity comparisons only support the plain string form
506 assert version in spec
507 else:
508 # Identity comparisons only support the plain string form
509 assert version not in spec
510
511 @pytest.mark.parametrize(
512 ("specifier", "expected"),
513 [
514 ("==1.0", False),
515 (">=1.0", False),
516 ("<=1.0", False),
517 ("~=1.0", False),
518 ("<1.0", False),
519 (">1.0", False),
520 ("<1.0.dev1", False),
521 (">1.0.dev1", False),
522 ("==1.0.*", False),
523 ("==1.0.dev1", True),
524 (">=1.0.dev1", True),
525 ("<=1.0.dev1", True),
526 ("~=1.0.dev1", True),
527 ],
528 )
529 def test_specifier_prereleases_detection(self, specifier, expected):
530 assert Specifier(specifier).prereleases == expected
531
532 @pytest.mark.parametrize(
533 ("specifier", "version", "expected"),
534 [
535 (">=1.0", "2.0.dev1", False),
536 (">=2.0.dev1", "2.0a1", True),
537 ("==2.0.*", "2.0a1.dev1", False),
538 ("==2.0a1.*", "2.0a1.dev1", True),
539 ("<=2.0", "1.0.dev1", False),
540 ("<=2.0.dev1", "1.0a1", True),
541 ],
542 )
543 def test_specifiers_prereleases(self, specifier, version, expected):
544 spec = Specifier(specifier)
545
546 if expected:
547 assert version in spec
548 spec.prereleases = False
549 assert version not in spec
550 else:
551 assert version not in spec
552 spec.prereleases = True
553 assert version in spec
554
555 @pytest.mark.parametrize(
556 ("specifier", "prereleases", "input", "expected"),
557 [
558 (">=1.0", None, ["2.0a1"], ["2.0a1"]),
559 (">=1.0.dev1", None, ["1.0", "2.0a1"], ["1.0", "2.0a1"]),
560 (">=1.0.dev1", False, ["1.0", "2.0a1"], ["1.0"]),
561 ],
562 )
563 def test_specifier_filter(self, specifier, prereleases, input, expected):
564 spec = Specifier(specifier)
565
566 kwargs = {"prereleases": prereleases} if prereleases is not None else {}
567
568 assert list(spec.filter(input, **kwargs)) == expected
569
570 @pytest.mark.xfail
571 def test_specifier_explicit_legacy(self):
572 assert Specifier("==1.0").contains(LegacyVersion("1.0"))
573
574 @pytest.mark.parametrize(
575 ("spec", "op"),
576 [
577 ("~=2.0", "~="),
578 ("==2.1.*", "=="),
579 ("==2.1.0.3", "=="),
580 ("!=2.2.*", "!="),
581 ("!=2.2.0.5", "!="),
582 ("<=5", "<="),
583 (">=7.9a1", ">="),
584 ("<1.0.dev1", "<"),
585 (">2.0.post1", ">"),
586 ("===lolwat", "==="),
587 ],
588 )
589 def test_specifier_operator_property(self, spec, op):
590 assert Specifier(spec).operator == op
591
592 @pytest.mark.parametrize(
593 ("spec", "version"),
594 [
595 ("~=2.0", "2.0"),
596 ("==2.1.*", "2.1.*"),
597 ("==2.1.0.3", "2.1.0.3"),
598 ("!=2.2.*", "2.2.*"),
599 ("!=2.2.0.5", "2.2.0.5"),
600 ("<=5", "5"),
601 (">=7.9a1", "7.9a1"),
602 ("<1.0.dev1", "1.0.dev1"),
603 (">2.0.post1", "2.0.post1"),
604 ("===lolwat", "lolwat"),
605 ],
606 )
607 def test_specifier_version_property(self, spec, version):
608 assert Specifier(spec).version == version
609
610 @pytest.mark.parametrize(
611 ("spec", "expected_length"),
612 [("", 0), ("==2.0", 1), (">=2.0", 1), (">=2.0,<3", 2), (">=2.0,<3,==2.4", 3)],
613 )
614 def test_length(self, spec, expected_length):
615 spec = SpecifierSet(spec)
616 assert len(spec) == expected_length
617
618 @pytest.mark.parametrize(
619 ("spec", "expected_items"),
620 [
621 ("", []),
622 ("==2.0", ["==2.0"]),
623 (">=2.0", [">=2.0"]),
624 (">=2.0,<3", [">=2.0", "<3"]),
625 (">=2.0,<3,==2.4", [">=2.0", "<3", "==2.4"]),
626 ],
627 )
628 def test_iteration(self, spec, expected_items):
629 spec = SpecifierSet(spec)
630 items = {str(item) for item in spec}
631 assert items == set(expected_items)
632
633
634 class TestLegacySpecifier:
635 def test_legacy_specifier_is_deprecated(self):
636 with warnings.catch_warnings(record=True) as w:
637 LegacySpecifier(">=some-legacy-version")
638 assert len(w) == 1
639 assert issubclass(w[0].category, DeprecationWarning)
640
641 @pytest.mark.parametrize(
642 ("version", "spec", "expected"),
643 [
644 (v, s, True)
645 for v, s in [
646 # Test the equality operation
647 ("2.0", "==2"),
648 ("2.0", "==2.0"),
649 ("2.0", "==2.0.0"),
650 # Test the in-equality operation
651 ("2.1", "!=2"),
652 ("2.1", "!=2.0"),
653 ("2.0.1", "!=2"),
654 ("2.0.1", "!=2.0"),
655 ("2.0.1", "!=2.0.0"),
656 # Test the greater than equal operation
657 ("2.0", ">=2"),
658 ("2.0", ">=2.0"),
659 ("2.0", ">=2.0.0"),
660 ("2.0.post1", ">=2"),
661 ("2.0.post1.dev1", ">=2"),
662 ("3", ">=2"),
663 # Test the less than equal operation
664 ("2.0", "<=2"),
665 ("2.0", "<=2.0"),
666 ("2.0", "<=2.0.0"),
667 ("2.0.dev1", "<=2"),
668 ("2.0a1", "<=2"),
669 ("2.0a1.dev1", "<=2"),
670 ("2.0b1", "<=2"),
671 ("2.0b1.post1", "<=2"),
672 ("2.0c1", "<=2"),
673 ("2.0c1.post1.dev1", "<=2"),
674 ("2.0rc1", "<=2"),
675 ("1", "<=2"),
676 # Test the greater than operation
677 ("3", ">2"),
678 ("2.1", ">2.0"),
679 # Test the less than operation
680 ("1", "<2"),
681 ("2.0", "<2.1"),
682 ]
683 ]
684 + [
685 (v, s, False)
686 for v, s in [
687 # Test the equality operation
688 ("2.1", "==2"),
689 ("2.1", "==2.0"),
690 ("2.1", "==2.0.0"),
691 # Test the in-equality operation
692 ("2.0", "!=2"),
693 ("2.0", "!=2.0"),
694 ("2.0", "!=2.0.0"),
695 # Test the greater than equal operation
696 ("2.0.dev1", ">=2"),
697 ("2.0a1", ">=2"),
698 ("2.0a1.dev1", ">=2"),
699 ("2.0b1", ">=2"),
700 ("2.0b1.post1", ">=2"),
701 ("2.0c1", ">=2"),
702 ("2.0c1.post1.dev1", ">=2"),
703 ("2.0rc1", ">=2"),
704 ("1", ">=2"),
705 # Test the less than equal operation
706 ("2.0.post1", "<=2"),
707 ("2.0.post1.dev1", "<=2"),
708 ("3", "<=2"),
709 # Test the greater than operation
710 ("1", ">2"),
711 ("2.0.dev1", ">2"),
712 ("2.0a1", ">2"),
713 ("2.0a1.post1", ">2"),
714 ("2.0b1", ">2"),
715 ("2.0b1.dev1", ">2"),
716 ("2.0c1", ">2"),
717 ("2.0c1.post1.dev1", ">2"),
718 ("2.0rc1", ">2"),
719 ("2.0", ">2"),
720 # Test the less than operation
721 ("3", "<2"),
722 ]
723 ],
724 )
725 def test_specifiers(self, version, spec, expected):
726 spec = LegacySpecifier(spec, prereleases=True)
727
728 if expected:
729 # Test that the plain string form works
730 assert version in spec
731 assert spec.contains(version)
732
733 # Test that the version instance form works
734 assert LegacyVersion(version) in spec
735 assert spec.contains(LegacyVersion(version))
736 else:
737 # Test that the plain string form works
738 assert version not in spec
739 assert not spec.contains(version)
740
741 # Test that the version instance form works
742 assert LegacyVersion(version) not in spec
743 assert not spec.contains(LegacyVersion(version))
744
745 def test_specifier_explicit_prereleases(self):
746 spec = LegacySpecifier(">=1.0")
747 assert not spec.prereleases
748 spec.prereleases = True
749 assert spec.prereleases
750
751 spec = LegacySpecifier(">=1.0", prereleases=False)
752 assert not spec.prereleases
753 spec.prereleases = True
754 assert spec.prereleases
755
756 spec = LegacySpecifier(">=1.0", prereleases=True)
757 assert spec.prereleases
758 spec.prereleases = False
759 assert not spec.prereleases
760
761 spec = LegacySpecifier(">=1.0", prereleases=True)
762 assert spec.prereleases
763 spec.prereleases = None
764 assert not spec.prereleases
765
766
767 class TestSpecifierSet:
768 @pytest.mark.parametrize("version", VERSIONS + LEGACY_VERSIONS)
769 def test_empty_specifier(self, version):
770 spec = SpecifierSet(prereleases=True)
771
772 assert version in spec
773 assert spec.contains(version)
774 assert parse(version) in spec
775 assert spec.contains(parse(version))
776
777 def test_specifier_prereleases_explicit(self):
778 spec = SpecifierSet()
779 assert not spec.prereleases
780 assert "1.0.dev1" not in spec
781 assert not spec.contains("1.0.dev1")
782 spec.prereleases = True
783 assert spec.prereleases
784 assert "1.0.dev1" in spec
785 assert spec.contains("1.0.dev1")
786
787 spec = SpecifierSet(prereleases=True)
788 assert spec.prereleases
789 assert "1.0.dev1" in spec
790 assert spec.contains("1.0.dev1")
791 spec.prereleases = False
792 assert not spec.prereleases
793 assert "1.0.dev1" not in spec
794 assert not spec.contains("1.0.dev1")
795
796 spec = SpecifierSet(prereleases=True)
797 assert spec.prereleases
798 assert "1.0.dev1" in spec
799 assert spec.contains("1.0.dev1")
800 spec.prereleases = None
801 assert not spec.prereleases
802 assert "1.0.dev1" not in spec
803 assert not spec.contains("1.0.dev1")
804
805 def test_specifier_contains_prereleases(self):
806 spec = SpecifierSet()
807 assert spec.prereleases is None
808 assert not spec.contains("1.0.dev1")
809 assert spec.contains("1.0.dev1", prereleases=True)
810
811 spec = SpecifierSet(prereleases=True)
812 assert spec.prereleases
813 assert spec.contains("1.0.dev1")
814 assert not spec.contains("1.0.dev1", prereleases=False)
815
816 @pytest.mark.parametrize(
817 ("specifier", "specifier_prereleases", "prereleases", "input", "expected"),
818 [
819 # General test of the filter method
820 ("", None, None, ["1.0", "2.0a1"], ["1.0"]),
821 (">=1.0.dev1", None, None, ["1.0", "2.0a1"], ["1.0", "2.0a1"]),
822 ("", None, None, ["1.0a1"], ["1.0a1"]),
823 ("", None, None, ["1.0", Version("2.0")], ["1.0", Version("2.0")]),
824 ("", None, None, ["2.0dog", "1.0"], ["1.0"]),
825 # Test overriding with the prereleases parameter on filter
826 ("", None, False, ["1.0a1"], []),
827 (">=1.0.dev1", None, False, ["1.0", "2.0a1"], ["1.0"]),
828 ("", None, True, ["1.0", "2.0a1"], ["1.0", "2.0a1"]),
829 # Test overriding with the overall specifier
830 ("", True, None, ["1.0", "2.0a1"], ["1.0", "2.0a1"]),
831 ("", False, None, ["1.0", "2.0a1"], ["1.0"]),
832 (">=1.0.dev1", True, None, ["1.0", "2.0a1"], ["1.0", "2.0a1"]),
833 (">=1.0.dev1", False, None, ["1.0", "2.0a1"], ["1.0"]),
834 ("", True, None, ["1.0a1"], ["1.0a1"]),
835 ("", False, None, ["1.0a1"], []),
836 ],
837 )
838 def test_specifier_filter(
839 self, specifier_prereleases, specifier, prereleases, input, expected
840 ):
841 if specifier_prereleases is None:
842 spec = SpecifierSet(specifier)
843 else:
844 spec = SpecifierSet(specifier, prereleases=specifier_prereleases)
845
846 kwargs = {"prereleases": prereleases} if prereleases is not None else {}
847
848 assert list(spec.filter(input, **kwargs)) == expected
849
850 def test_legacy_specifiers_combined(self):
851 spec = SpecifierSet("<3,>1-1-1")
852 assert "2.0" in spec
853
854 @pytest.mark.parametrize(
855 ("specifier", "expected"),
856 [
857 # Single item specifiers should just be reflexive
858 ("!=2.0", "!=2.0"),
859 ("<2.0", "<2.0"),
860 ("<=2.0", "<=2.0"),
861 ("==2.0", "==2.0"),
862 (">2.0", ">2.0"),
863 (">=2.0", ">=2.0"),
864 ("~=2.0", "~=2.0"),
865 # Spaces should be removed
866 ("< 2", "<2"),
867 # Multiple item specifiers should work
868 ("!=2.0,>1.0", "!=2.0,>1.0"),
869 ("!=2.0 ,>1.0", "!=2.0,>1.0"),
870 ],
871 )
872 def test_specifiers_str_and_repr(self, specifier, expected):
873 spec = SpecifierSet(specifier)
874
875 assert str(spec) == expected
876 assert repr(spec) == f"<SpecifierSet({expected!r})>"
877
878 @pytest.mark.parametrize("specifier", SPECIFIERS + LEGACY_SPECIFIERS)
879 def test_specifiers_hash(self, specifier):
880 assert hash(SpecifierSet(specifier)) == hash(SpecifierSet(specifier))
881
882 @pytest.mark.parametrize(
883 ("left", "right", "expected"), [(">2.0", "<5.0", ">2.0,<5.0")]
884 )
885 def test_specifiers_combine(self, left, right, expected):
886 result = SpecifierSet(left) & SpecifierSet(right)
887 assert result == SpecifierSet(expected)
888
889 result = SpecifierSet(left) & right
890 assert result == SpecifierSet(expected)
891
892 result = SpecifierSet(left, prereleases=True) & SpecifierSet(right)
893 assert result == SpecifierSet(expected)
894 assert result.prereleases
895
896 result = SpecifierSet(left, prereleases=False) & SpecifierSet(right)
897 assert result == SpecifierSet(expected)
898 assert not result.prereleases
899
900 result = SpecifierSet(left) & SpecifierSet(right, prereleases=True)
901 assert result == SpecifierSet(expected)
902 assert result.prereleases
903
904 result = SpecifierSet(left) & SpecifierSet(right, prereleases=False)
905 assert result == SpecifierSet(expected)
906 assert not result.prereleases
907
908 result = SpecifierSet(left, prereleases=True) & SpecifierSet(
909 right, prereleases=True
910 )
911 assert result == SpecifierSet(expected)
912 assert result.prereleases
913
914 result = SpecifierSet(left, prereleases=False) & SpecifierSet(
915 right, prereleases=False
916 )
917 assert result == SpecifierSet(expected)
918 assert not result.prereleases
919
920 with pytest.raises(ValueError):
921 result = SpecifierSet(left, prereleases=True) & SpecifierSet(
922 right, prereleases=False
923 )
924
925 with pytest.raises(ValueError):
926 result = SpecifierSet(left, prereleases=False) & SpecifierSet(
927 right, prereleases=True
928 )
929
930 def test_specifiers_combine_not_implemented(self):
931 with pytest.raises(TypeError):
932 SpecifierSet() & 12
933
934 @pytest.mark.parametrize(
935 ("left", "right", "op"),
936 itertools.chain(
937 *
938 # Verify that the equal (==) operator works correctly
939 [[(x, x, operator.eq) for x in SPECIFIERS]]
940 +
941 # Verify that the not equal (!=) operator works correctly
942 [
943 [(x, y, operator.ne) for j, y in enumerate(SPECIFIERS) if i != j]
944 for i, x in enumerate(SPECIFIERS)
945 ]
946 ),
947 )
948 def test_comparison_true(self, left, right, op):
949 assert op(SpecifierSet(left), SpecifierSet(right))
950 assert op(SpecifierSet(left), Specifier(right))
951 assert op(Specifier(left), SpecifierSet(right))
952 assert op(left, SpecifierSet(right))
953 assert op(SpecifierSet(left), right)
954
955 @pytest.mark.parametrize(
956 ("left", "right", "op"),
957 itertools.chain(
958 *
959 # Verify that the equal (==) operator works correctly
960 [[(x, x, operator.ne) for x in SPECIFIERS]]
961 +
962 # Verify that the not equal (!=) operator works correctly
963 [
964 [(x, y, operator.eq) for j, y in enumerate(SPECIFIERS) if i != j]
965 for i, x in enumerate(SPECIFIERS)
966 ]
967 ),
968 )
969 def test_comparison_false(self, left, right, op):
970 assert not op(SpecifierSet(left), SpecifierSet(right))
971 assert not op(SpecifierSet(left), Specifier(right))
972 assert not op(Specifier(left), SpecifierSet(right))
973 assert not op(left, SpecifierSet(right))
974 assert not op(SpecifierSet(left), right)
975
976 @pytest.mark.parametrize(("left", "right"), [("==2.8.0", "==2.8")])
977 def test_comparison_canonicalizes(self, left, right):
978 assert SpecifierSet(left) == SpecifierSet(right)
979 assert left == SpecifierSet(right)
980 assert SpecifierSet(left) == right
981
982 def test_comparison_non_specifier(self):
983 assert SpecifierSet("==1.0") != 12
984 assert not SpecifierSet("==1.0") == 12
985
986 @pytest.mark.parametrize(
987 ("version", "specifier", "expected"),
988 [
989 ("1.0.0+local", "==1.0.0", True),
990 ("1.0.0+local", "!=1.0.0", False),
991 ("1.0.0+local", "<=1.0.0", True),
992 ("1.0.0+local", ">=1.0.0", True),
993 ("1.0.0+local", "<1.0.0", False),
994 ("1.0.0+local", ">1.0.0", False),
995 ],
996 )
997 def test_comparison_ignores_local(self, version, specifier, expected):
998 assert (Version(version) in SpecifierSet(specifier)) == expected