File size: 27,208 Bytes
d40299b | 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 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 | # This file is dual licensed under the terms of the Apache License, Version
# 2.0, and the BSD License. See the LICENSE file in the root of this repository
# for complete details.
from __future__ import annotations
import pytest
from hypothesis import assume, given
from hypothesis import strategies as st
from packaging.specifiers import InvalidSpecifier, Specifier, SpecifierSet
from packaging.version import Version
from tests.property.strategies import (
SETTINGS,
multi_segment_versions,
nonlocal_versions,
ops,
pep440_versions,
release_versions,
small_ints,
versions_with_local,
)
pytestmark = pytest.mark.property
class TestSpecifierCommaIsAnd:
"""The comma (",") is equivalent to a logical **and** operator: a candidate
version must match all given version clauses in order to match the
specifier as a whole."""
@given(version=release_versions())
@SETTINGS
def test_comma_separated_is_conjunction(self, version: Version) -> None:
"""A version matches a multi-clause specifier iff it matches every clause."""
clauses = [f">={version}", f"<={version}"]
combined = SpecifierSet(",".join(clauses))
individual = [SpecifierSet(c) for c in clauses]
matches_all = all(version in s for s in individual)
assert (version in combined) == matches_all
@given(candidate=pep440_versions(), data=st.data())
@SETTINGS
def test_adding_clause_can_only_narrow(
self, candidate: Version, data: st.DataObject
) -> None:
"""Adding a clause to a specifier set can never cause a previously
excluded version to start matching."""
base_op = data.draw(ops)
base_ver = data.draw(release_versions())
extra_op = data.draw(ops)
extra_ver = data.draw(release_versions())
base_spec = SpecifierSet(f"{base_op}{base_ver}")
combined = SpecifierSet(f"{base_op}{base_ver},{extra_op}{extra_ver}")
if candidate in combined:
assert candidate in base_spec
@given(
first_op=ops,
first_ver=release_versions(),
second_op=ops,
second_ver=release_versions(),
candidate=pep440_versions(),
)
@SETTINGS
def test_intersection_is_idempotent(
self,
first_op: str,
first_ver: Version,
second_op: str,
second_ver: Version,
candidate: Version,
) -> None:
"""(A & B) & B accepts the same versions as A & B."""
combined = SpecifierSet(f"{first_op}{first_ver},{second_op}{second_ver}")
combined_double = SpecifierSet(
f"{first_op}{first_ver},{second_op}{second_ver},{second_op}{second_ver}"
)
assert combined.contains(
candidate, prereleases=True
) == combined_double.contains(candidate, prereleases=True)
@given(
first_op=ops,
first_ver=release_versions(),
second_op=ops,
second_ver=release_versions(),
third_op=ops,
third_ver=release_versions(),
candidate=pep440_versions(),
)
@SETTINGS
def test_intersection_is_associative(
self,
first_op: str,
first_ver: Version,
second_op: str,
second_ver: Version,
third_op: str,
third_ver: Version,
candidate: Version,
) -> None:
"""(A & B) & C accepts the same versions as A & (B & C)."""
combined = SpecifierSet(
f"{first_op}{first_ver},{second_op}{second_ver},{third_op}{third_ver}"
)
# Since comma is AND regardless of grouping, any ordering
# of the same clauses must agree on containment.
assert (candidate in combined) == (
SpecifierSet(f"{first_op}{first_ver}").contains(candidate, prereleases=True)
and SpecifierSet(f"{second_op}{second_ver}").contains(
candidate, prereleases=True
)
and SpecifierSet(f"{third_op}{third_ver}").contains(
candidate, prereleases=True
)
)
class TestSpecifierWhitespace:
"""Whitespace between a conditional operator and the following version
identifier is optional, as is the whitespace around the commas."""
@given(spec_ver=release_versions(), spaces=st.integers(min_value=0, max_value=3))
@SETTINGS
def test_whitespace_between_op_and_version(
self, spec_ver: Version, spaces: int
) -> None:
"""Whitespace between operator and version doesn't change semantics."""
ws = " " * spaces
spec_no_ws = Specifier(f"=={spec_ver}")
spec_ws = Specifier(f"=={ws}{spec_ver}")
assert spec_no_ws == spec_ws
@given(
v1=release_versions(),
v2=release_versions(),
spaces=st.integers(min_value=0, max_value=3),
)
@SETTINGS
def test_whitespace_around_commas(
self, v1: Version, v2: Version, spaces: int
) -> None:
"""Whitespace around commas doesn't change semantics."""
ws = " " * spaces
spec_no_ws = SpecifierSet(f">={v1},<={v2}")
spec_ws = SpecifierSet(f">={v1}{ws},{ws}<={v2}")
assert spec_no_ws == spec_ws
class TestLocalVersionIgnoredInSpecifiers:
"""Except where specifically noted below, local version identifiers MUST NOT be
permitted in version specifiers, and local version labels MUST be ignored
entirely when checking if candidate versions match a given version
specifier."""
@given(version=versions_with_local())
@SETTINGS
def test_local_ignored_for_ordering_specifiers(self, version: Version) -> None:
"""A version with a local label matches >= and <= on its public version."""
public = Version(str(version).split("+")[0])
spec = SpecifierSet(f">={public}")
assert version in spec
@given(version=versions_with_local())
@SETTINGS
def test_local_ignored_for_gt(self, version: Version) -> None:
"""Local labels are stripped before comparison for > specifiers."""
public = Version(str(version).split("+")[0])
# version's local is ignored, so it compares as public; public is not > public
spec = Specifier(f">{public}")
assert version not in spec
@given(version=versions_with_local())
@SETTINGS
def test_local_ignored_for_lt(self, version: Version) -> None:
"""Local labels are stripped before comparison for < specifiers."""
public = Version(str(version).split("+")[0])
spec = Specifier(f"<{public}")
assert version not in spec
@given(spec_ver=release_versions(), local=st.integers(min_value=0, max_value=5))
@SETTINGS
def test_local_not_permitted_in_ordering_specifiers(
self, spec_ver: Version, local: int
) -> None:
"""Local version identifiers must not be permitted in
>=, <=, >, < specifiers."""
local_str = f"+local{local}"
for op in [">=", "<=", ">", "<"]:
with pytest.raises(InvalidSpecifier):
Specifier(f"{op}{spec_ver}{local_str}")
class TestCompatibleReleaseDefinition:
"""A compatible release clause consists of the compatible release operator ``~=``
and a version identifier. It matches any candidate version that is expected
to be compatible with the specified version."""
@given(spec_ver=multi_segment_versions(), candidate=pep440_versions())
@SETTINGS
def test_compatible_release_matches_expected_compatible(
self, spec_ver: Version, candidate: Version
) -> None:
"""~=V matches candidate iff candidate >= V and candidate matches the
prefix wildcard derived from V."""
assume(spec_ver.epoch == candidate.epoch)
spec = Specifier(f"~={spec_ver}")
result = candidate in spec
# Also check via the expanded form
release = spec_ver.release
prefix = ".".join(str(s) for s in release[:-1])
epoch_prefix = f"{spec_ver.epoch}!" if spec_ver.epoch else ""
expanded = SpecifierSet(f">={spec_ver},=={epoch_prefix}{prefix}.*")
expanded_result = candidate in expanded
assert result == expanded_result
class TestCompatibleReleaseNoLocal:
"""The specified version identifier must be in the standard format described in
`Version scheme`_. Local version identifiers are NOT permitted in this
version specifier."""
@given(spec_ver=release_versions(), local=st.integers(min_value=0, max_value=5))
@SETTINGS
def test_local_not_permitted_in_compatible(
self, spec_ver: Version, local: int
) -> None:
"""~= with a local version identifier must raise InvalidSpecifier."""
assume(len(spec_ver.release) >= 2)
with pytest.raises(InvalidSpecifier):
Specifier(f"~={spec_ver}+local{local}")
class TestCompatibleReleaseEquivalence:
r"""For a given release identifier ``V.N``, the compatible release clause is
approximately equivalent to the pair of comparison clauses::
>= V.N, == V.*
This operator MUST NOT be used with a single segment version number such as
``~=1``."""
@given(
major=small_ints,
minor=small_ints,
candidate=pep440_versions(),
)
@SETTINGS
def test_two_segment_equivalence(
self, major: int, minor: int, candidate: Version
) -> None:
"""~= V.N is equivalent to >= V.N, == V.* for two-segment versions."""
v_str = f"{major}.{minor}"
spec_compat = Specifier(f"~={v_str}")
spec_expanded = SpecifierSet(f">={v_str},=={major}.*")
assert (candidate in spec_compat) == (candidate in spec_expanded)
@given(
major=small_ints,
minor=small_ints,
patch=small_ints,
candidate=pep440_versions(),
)
@SETTINGS
def test_three_segment_equivalence(
self, major: int, minor: int, patch: int, candidate: Version
) -> None:
"""~= V.N.P is equivalent to >= V.N.P, == V.N.*"""
v_str = f"{major}.{minor}.{patch}"
spec_compat = Specifier(f"~={v_str}")
spec_expanded = SpecifierSet(f">={v_str},=={major}.{minor}.*")
assert (candidate in spec_compat) == (candidate in spec_expanded)
@given(major=small_ints)
@SETTINGS
def test_single_segment_rejected(self, major: int) -> None:
"""~= with a single segment version number must be rejected."""
with pytest.raises(InvalidSpecifier):
Specifier(f"~={major}")
class TestCompatibleReleaseSuffixIgnored:
r"""If a pre-release, post-release or developmental release is named in a
compatible release clause as ``V.N.suffix``, then the suffix is ignored
when determining the required prefix match::
~= 2.2.post3
>= 2.2.post3, == 2.*
~= 1.4.5a4
>= 1.4.5a4, == 1.4.*"""
@given(candidate=pep440_versions())
@SETTINGS
def test_post_release_suffix_ignored(self, candidate: Version) -> None:
"""~= 2.2.post3 is equivalent to >= 2.2.post3, == 2.*"""
spec = Specifier("~=2.2.post3")
expanded = SpecifierSet(">=2.2.post3,==2.*")
assert (candidate in spec) == (candidate in expanded)
@given(candidate=pep440_versions())
@SETTINGS
def test_pre_release_suffix_ignored(self, candidate: Version) -> None:
"""~= 1.4.5a4 is equivalent to >= 1.4.5a4, == 1.4.*"""
spec = Specifier("~=1.4.5a4")
expanded = SpecifierSet(">=1.4.5a4,==1.4.*")
assert (candidate in spec) == (candidate in expanded)
@given(
base=multi_segment_versions(),
post=small_ints,
candidate=pep440_versions(),
)
@SETTINGS
def test_post_suffix_general(
self, base: Version, post: int, candidate: Version
) -> None:
"""~= V.N.postP uses V.* as the prefix (suffix ignored)."""
assume(base.pre is None and base.post is None and base.dev is None)
assume(len(base.release) >= 2)
v_str = f"{base}.post{post}"
release = base.release
prefix = ".".join(str(s) for s in release[:-1])
epoch_prefix = f"{base.epoch}!" if base.epoch else ""
spec = Specifier(f"~={v_str}")
expanded = SpecifierSet(f">={v_str},=={epoch_prefix}{prefix}.*")
assert (candidate in spec) == (candidate in expanded)
class TestCompatibleReleasePaddingZeros:
r"""The padding rules for release segment comparisons means that the assumed
degree of forward compatibility in a compatible release clause can be
controlled by appending additional zeros to the version specifier::
~= 2.2.0
>= 2.2.0, == 2.2.*
~= 1.4.5.0
>= 1.4.5.0, == 1.4.5.*"""
@given(candidate=pep440_versions())
@SETTINGS
def test_padding_narrows_two_to_three(self, candidate: Version) -> None:
"""~= 2.2.0 is equivalent to >= 2.2.0, == 2.2.*"""
spec = Specifier("~=2.2.0")
expanded = SpecifierSet(">=2.2.0,==2.2.*")
assert (candidate in spec) == (candidate in expanded)
@given(candidate=pep440_versions())
@SETTINGS
def test_padding_narrows_three_to_four(self, candidate: Version) -> None:
"""~= 1.4.5.0 is equivalent to >= 1.4.5.0, == 1.4.5.*"""
spec = Specifier("~=1.4.5.0")
expanded = SpecifierSet(">=1.4.5.0,==1.4.5.*")
assert (candidate in spec) == (candidate in expanded)
@given(
major=small_ints,
minor=small_ints,
candidate=pep440_versions(),
)
@SETTINGS
def test_padding_zero_changes_prefix_depth(
self, major: int, minor: int, candidate: Version
) -> None:
"""~= X.Y.0 matches a narrower set than ~= X.Y because the prefix
is == X.Y.* instead of == X.*"""
broad = Specifier(f"~={major}.{minor}")
narrow = Specifier(f"~={major}.{minor}.0")
if candidate in narrow:
assert candidate in broad
class TestVersionMatchingStrict:
r"""By default, the version matching operator is based on a strict equality
comparison: the specified version must be exactly the same as the requested
version. The *only* substitution performed is the zero padding of the
release segment to ensure the release segments are compared with the same
length."""
@given(version=nonlocal_versions())
@SETTINGS
def test_version_matches_itself(self, version: Version) -> None:
"""== V always matches V."""
spec = Specifier(f"=={version}")
assert version in spec
@given(version=release_versions())
@SETTINGS
def test_zero_padding_matches(self, version: Version) -> None:
"""== V.0 matches V due to zero padding (for final releases)."""
padded_str = f"{version}.0"
spec_from_padded = Specifier(f"=={padded_str}")
assert version in spec_from_padded
@given(version=release_versions())
@SETTINGS
def test_zero_padding_symmetric(self, version: Version) -> None:
"""If V matches == V.0, then V.0 matches == V."""
padded = Version(f"{version}.0")
spec_original = Specifier(f"=={version}")
assert padded in spec_original
class TestVersionPrefixMatching:
r"""Prefix matching may be requested instead of strict comparison, by appending
a trailing ``.*`` to the version identifier in the version matching clause.
This means that additional trailing segments will be ignored when
determining whether or not a version identifier matches the clause. If the
specified version includes only a release segment, then trailing components
(or the lack thereof) in the release segment are also ignored."""
@given(spec_ver=release_versions(), extra=small_ints)
@SETTINGS
def test_prefix_match_ignores_trailing_segments(
self, spec_ver: Version, extra: int
) -> None:
"""== V.* matches V.extra for any extra segment."""
spec = Specifier(f"=={spec_ver}.*")
extended = Version(f"{spec_ver}.{extra}")
assert extended in spec
@given(spec_ver=release_versions())
@SETTINGS
def test_prefix_match_includes_exact(self, spec_ver: Version) -> None:
"""== V.* matches V itself (exact match is a prefix match)."""
spec = Specifier(f"=={spec_ver}.*")
assert spec_ver in spec
@given(
major=small_ints,
minor=small_ints,
patch=small_ints,
)
@SETTINGS
def test_prefix_match_with_pre_release(
self, major: int, minor: int, patch: int
) -> None:
"""== X.Y.* matches X.Y.Z.aN (pre-releases under the prefix)."""
spec = Specifier(f"=={major}.{minor}.*")
candidate = Version(f"{major}.{minor}.{patch}a1")
assert candidate in spec
@given(
major=small_ints,
minor=small_ints,
post=small_ints,
)
@SETTINGS
def test_prefix_match_with_post_release(
self, major: int, minor: int, post: int
) -> None:
"""== X.Y.* matches X.Y.postN."""
spec = Specifier(f"=={major}.{minor}.*")
candidate = Version(f"{major}.{minor}.post{post}")
assert candidate in spec
class TestVersionMatchingPost1Examples:
r"""For example, given the version ``1.1.post1``, the following clauses would
match or not as shown::
== 1.1 # Not equal, so 1.1.post1 does not match clause
== 1.1.post1 # Equal, so 1.1.post1 matches clause
== 1.1.* # Same prefix, so 1.1.post1 matches clause"""
def test_strict_no_match(self) -> None:
"""== 1.1 does not match 1.1.post1."""
assert Version("1.1.post1") not in Specifier("==1.1")
def test_strict_exact_match(self) -> None:
"""== 1.1.post1 matches 1.1.post1."""
assert Version("1.1.post1") in Specifier("==1.1.post1")
def test_prefix_match(self) -> None:
"""== 1.1.* matches 1.1.post1."""
assert Version("1.1.post1") in Specifier("==1.1.*")
class TestVersionMatchingAlphaExamples:
r"""For purposes of prefix matching, the pre-release segment is considered to
have an implied preceding ``.``, so given the version ``1.1a1``, the
following clauses would match or not as shown::
== 1.1 # Not equal, so 1.1a1 does not match clause
== 1.1a1 # Equal, so 1.1a1 matches clause
== 1.1.* # Same prefix, so 1.1a1 matches clause
# if pre-releases are requested"""
def test_strict_no_match(self) -> None:
"""== 1.1 does not match 1.1a1."""
assert Version("1.1a1") not in Specifier("==1.1")
def test_strict_exact_match(self) -> None:
"""== 1.1a1 matches 1.1a1."""
assert Version("1.1a1") in Specifier("==1.1a1")
def test_prefix_match_with_prereleases(self) -> None:
"""== 1.1.* matches 1.1a1 when pre-releases are requested."""
spec = Specifier("==1.1.*")
assert Version("1.1a1") in spec
class TestVersionMatchingExactExamples:
r"""An exact match is also considered a prefix match (this interpretation is
implied by the usual zero padding rules for the release segment of version
identifiers). Given the version ``1.1``, the following clauses would
match or not as shown::
== 1.1 # Equal, so 1.1 matches clause
== 1.1.0 # Zero padding expands 1.1 to 1.1.0, so it matches clause
== 1.1.dev1 # Not equal (dev-release), so 1.1 does not match clause
== 1.1a1 # Not equal (pre-release), so 1.1 does not match clause
== 1.1.post1 # Not equal (post-release), so 1.1 does not match clause
== 1.1.* # Same prefix, so 1.1 matches clause"""
def test_equal(self) -> None:
"""== 1.1 matches 1.1."""
assert Version("1.1") in Specifier("==1.1")
def test_zero_padding(self) -> None:
"""== 1.1.0 matches 1.1 via zero padding."""
assert Version("1.1") in Specifier("==1.1.0")
def test_dev_not_equal(self) -> None:
"""== 1.1.dev1 does not match 1.1."""
assert Version("1.1") not in Specifier("==1.1.dev1")
def test_pre_not_equal(self) -> None:
"""== 1.1a1 does not match 1.1."""
assert Version("1.1") not in Specifier("==1.1a1")
def test_post_not_equal(self) -> None:
"""== 1.1.post1 does not match 1.1."""
assert Version("1.1") not in Specifier("==1.1.post1")
def test_prefix_match(self) -> None:
"""== 1.1.* matches 1.1."""
assert Version("1.1") in Specifier("==1.1.*")
class TestPrefixMatchInvalidForms:
r"""It is invalid to have a prefix match containing a development or local release
such as ``1.0.dev1.*`` or ``1.0+foo1.*``. If present, the development release
segment is always the final segment in the public version, and the local version
is ignored for comparison purposes, so using either in a prefix match wouldn't
make any sense."""
def test_dev_prefix_match_invalid(self) -> None:
"""== 1.0.dev1.* must be rejected."""
with pytest.raises(InvalidSpecifier):
Specifier("==1.0.dev1.*")
def test_local_prefix_match_invalid(self) -> None:
"""== 1.0+foo1.* must be rejected."""
with pytest.raises(InvalidSpecifier):
Specifier("==1.0+foo1.*")
@given(spec_ver=release_versions(), dev=small_ints)
@SETTINGS
def test_dev_prefix_match_invalid_general(
self, spec_ver: Version, dev: int
) -> None:
"""== V.devN.* must always be rejected."""
with pytest.raises(InvalidSpecifier):
Specifier(f"=={spec_ver}.dev{dev}.*")
class TestLocalVersionMatchingPublic:
r"""If the specified version identifier is a public version identifier (no
local version label), then the local version label of any candidate versions
MUST be ignored when matching versions."""
@given(spec_ver=release_versions(), local=st.integers(min_value=0, max_value=5))
@SETTINGS
def test_public_specifier_ignores_local_label(
self, spec_ver: Version, local: int
) -> None:
"""== V (public) matches V+localN because local label is ignored."""
spec = Specifier(f"=={spec_ver}")
candidate = Version(f"{spec_ver}+local{local}")
assert candidate in spec
@given(spec_ver=release_versions(), local=st.integers(min_value=0, max_value=5))
@SETTINGS
def test_public_wildcard_ignores_local_label(
self, spec_ver: Version, local: int
) -> None:
"""== V.* (public) matches V.0+localN because local label is ignored."""
spec = Specifier(f"=={spec_ver}.*")
candidate = Version(f"{spec_ver}.0+local{local}")
assert candidate in spec
@given(
spec_ver=release_versions(),
local1=st.integers(min_value=0, max_value=5),
local2=st.integers(min_value=0, max_value=5),
)
@SETTINGS
def test_public_specifier_matches_any_local(
self, spec_ver: Version, local1: int, local2: int
) -> None:
"""== V (public) matches V+localX for any local label."""
spec = Specifier(f"=={spec_ver}")
c1 = Version(f"{spec_ver}+local{local1}")
c2 = Version(f"{spec_ver}+local{local2}")
assert c1 in spec
assert c2 in spec
class TestLocalVersionMatchingLocal:
r"""If the specified version identifier is a local version identifier, then the
local version labels of candidate versions MUST be considered when matching
versions, with the public version identifier being matched as described
above, and the local version label being checked for equivalence using a
strict string equality comparison."""
@given(spec_ver=release_versions(), local=st.integers(min_value=0, max_value=5))
@SETTINGS
def test_local_specifier_matches_same_local(
self, spec_ver: Version, local: int
) -> None:
"""== V+localN matches V+localN (same local label)."""
local_str = f"local{local}"
spec = Specifier(f"=={spec_ver}+{local_str}")
candidate = Version(f"{spec_ver}+{local_str}")
assert candidate in spec
@given(spec_ver=release_versions())
@SETTINGS
def test_local_specifier_does_not_match_different_local(
self, spec_ver: Version
) -> None:
"""== V+local0 does not match V+local1 (different local label)."""
spec = Specifier(f"=={spec_ver}+local0")
candidate = Version(f"{spec_ver}+local1")
assert candidate not in spec
@given(spec_ver=release_versions(), local=st.integers(min_value=0, max_value=5))
@SETTINGS
def test_local_specifier_does_not_match_no_local(
self, spec_ver: Version, local: int
) -> None:
"""== V+localN does not match V (no local label on candidate)."""
spec = Specifier(f"=={spec_ver}+local{local}")
assert spec_ver not in spec
class TestVersionExclusion:
r"""A version exclusion clause includes the version exclusion operator ``!=``
and a version identifier.
The allowed version identifiers and comparison semantics are the same as
those of the `Version matching`_ operator, except that the sense of any
match is inverted."""
@given(version=nonlocal_versions())
@SETTINGS
def test_exclusion_is_inverse_of_match(self, version: Version) -> None:
"""!= V is the logical inverse of == V for any version."""
eq_spec = Specifier(f"=={version}")
ne_spec = Specifier(f"!={version}")
assert (version in eq_spec) != (version in ne_spec)
@given(spec_ver=nonlocal_versions(), candidate=pep440_versions())
@SETTINGS
def test_exclusion_inverse_general(
self, spec_ver: Version, candidate: Version
) -> None:
"""For any candidate, != V gives the opposite result of == V."""
eq_spec = Specifier(f"=={spec_ver}")
ne_spec = Specifier(f"!={spec_ver}")
assert (candidate in eq_spec) != (candidate in ne_spec)
@given(spec_ver=release_versions(), candidate=pep440_versions())
@SETTINGS
def test_prefix_exclusion_inverse(
self, spec_ver: Version, candidate: Version
) -> None:
"""!= V.* is the inverse of == V.* for prefix matching."""
eq_spec = Specifier(f"=={spec_ver}.*")
ne_spec = Specifier(f"!={spec_ver}.*")
assert (candidate in eq_spec) != (candidate in ne_spec)
class TestVersionExclusionPost1Examples:
r"""For example, given the version ``1.1.post1``, the following clauses would
match or not as shown::
!= 1.1 # Not equal, so 1.1.post1 matches clause
!= 1.1.post1 # Equal, so 1.1.post1 does not match clause
!= 1.1.* # Same prefix, so 1.1.post1 does not match clause"""
def test_not_equal_matches(self) -> None:
"""!= 1.1 matches 1.1.post1 (they are not equal)."""
assert Version("1.1.post1") in Specifier("!=1.1")
def test_equal_excludes(self) -> None:
"""!= 1.1.post1 does not match 1.1.post1 (they are equal)."""
assert Version("1.1.post1") not in Specifier("!=1.1.post1")
def test_prefix_excludes(self) -> None:
"""!= 1.1.* does not match 1.1.post1 (same prefix)."""
assert Version("1.1.post1") not in Specifier("!=1.1.*")
|