From 2377f3c99a21147f94762128c75c60b93ef04a12 Mon Sep 17 00:00:00 2001 From: Jonathan Yoder Date: Mon, 10 Aug 2026 11:42:07 -0400 Subject: [PATCH 1/4] Port test_specifiers.py conformance tables, and fix what they surface Ports 1,649 assertions from pypa/packaging's tests/test_specifiers.py (classes TestSpecifier and TestSpecifierSet) at pinned commit 4eb0753dba8fcaaac8eb75463374e448f0931558. 358 of them failed against the library as it stood. All pass now. Every expectation was cross-checked against TWO independent sources -- the literal in the pinned upstream file, and what the installed packaging 26.2 actually answers -- so a stale literal could not be baked in silently. All of them agreed. Six divergences fixed, in two groups. PARSING: - validConstraintRegexp lacked the (?i) flag specifierRegexp has, so ==1.0a1 parsed while ==1.0A1 was rejected before the parser saw it. 303 of the 358 failures. Pure widening: over the production PyPI corpus (25,457 distinct specifier tokens) 874 are newly accepted and 0 newly rejected. - A vertical tab (0x0B) was not accepted as surrounding whitespace, because Go's \s excludes it and Python's includes it. versionRegex had already been fixed; the specifier patterns had not. - validate() guarded a wildcard against dev, post and local but never against a PRE-release, so ==1.0a1.* was wrongly accepted. This is a narrowing; measured at 0 newly rejected over the production PyPI corpus and 0 over real CRAN data, with both the extraction pattern and the differential harness proved to detect the shapes whose absence is being claimed. MATCHING (all four were base-version comparisons standing in for the rule PEP 440 actually states): - "V must not allow a post-release of V" made the mirror error: for the spec 1.0a1 it excluded 1.0.post0. Now uses _post_base. - ">V must not match a local version of V" needs the PUBLIC version, which keeps pre/post/dev; comparing base versions made >1.0a1 wrongly reject 1.0a2+local. - Prefix matching and ~= both mis-split the operand. versionSplit did not peel the epoch off, so ==0!2.* could not match 2; padding ran between the two sides instead of up to the spec's numeric width, so 2 did not match ==2.0.0.*; and ~= broke its prefix only on post/dev, so ~=1.0a1 derived the wrong prefix. Upstream's _version_split, _version_join, _is_not_suffix, _numeric_prefix_len and _left_pad are now ported directly, replacing padVersion. Over the production corpus, 12 of 25,457 tokens change their match results. All 12, plus 5 synthetic controls, were checked against packaging 26.2 over a 19-version panel: 0 of 17 agreed with the reference before, 17 of 17 agree after. Also audited the 163 rows of the hand-written TestVersion_Check table against packaging 26.2. No row disagrees with the reference where both implementations parse the input; the only flagged rows are this package's four documented deliberate extensions (a bare version, "=", "*" for any, and "||"). Refs rstudio/package-manager#19391 Co-Authored-By: Claude Opus 5 (1M context) --- CHANGELOG.md | 84 ++ NOTICE | 25 + version/specifier.go | 269 +++++-- version/specifier_conformance_test.go | 972 +++++++++++++++++++++++ version/specifier_test.go | 94 ++- version/specifierset_conformance_test.go | 760 ++++++++++++++++++ version/version.go | 49 ++ 7 files changed, 2153 insertions(+), 100 deletions(-) create mode 100644 version/specifier_conformance_test.go create mode 100644 version/specifierset_conformance_test.go diff --git a/CHANGELOG.md b/CHANGELOG.md index b38be99..947a577 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,24 @@ mistaken for a safe patch upgrade. ### Breaking +- **A wildcard may no longer be combined with a pre-release.** `==1.0a1.*`, + `!=2.0rc1.*` and `==1.0alpha3.*` previously parsed and are now rejected, as PEP 440 + requires and as upstream does. + + `validate()` guarded a wildcard against a dev, post or local version but never against + a **pre-release**, so a prefix match on a pre-release was silently accepted. It cannot + mean anything: the wildcard covers the release segment, which is exactly where the + pre-release marker would have to sit. + + **Measured over the production PyPI snapshot** (25,457 distinct specifier tokens + extracted from 932,861 packages): **0 newly rejected**. 1,664 wildcard specifiers occur + in that corpus and not one combines a wildcard with a pre-release. **Measured over real + CRAN data for the R path** (`NewRSpecifiers`): **0 newly rejected** out of 59,139 + constraint occurrences. Both null results were instrumented — the extraction pattern + was proved to find the four synthetic pre-release-wildcard shapes it is looking for, and + the differential harness was proved to report these three specifiers flipping from + accepted to rejected when they are appended to the corpus. + - `WithPreRelease(true)` no longer suppresses the operator-level pre-release guards. It used to set a flag on the `Version` being tested that made @@ -129,6 +147,72 @@ mistaken for a safe patch upgrade. whatever operand they hold. The regression test builds the shape directly and was verified to panic without the fix. +- **Specifier parsing is now case-insensitive, matching version parsing.** + `specifierRegexp` carried the `(?i)` flag and the `validConstraintRegexp` gate did not, + so the two disagreed: `==1.0a1` parsed while `==1.0A1` was rejected before the parser + ever saw it. PEP 440 versions are case-insensitive, and upstream accepts every + upper-case spelling (`==1.0DEV`, `==1.0ALPHA1`, `>=7.9A1`, `~=1.0.POST1`, …). This was + the single largest source of conformance failures — 303 of 358. + + This is a pure widening: **874** of the production corpus's 25,457 distinct specifier + tokens are newly accepted and **0** are newly rejected. + +- **A vertical tab is accepted as surrounding whitespace in a specifier.** Go's `\s` is + `[\t\n\f\r ]` and excludes `\v` (0x0B) where Python's includes it, so `<= \r \f \v + v1.0` was rejected. `versionRegex` had already been fixed for this; the two specifier + patterns had not. + +- **Four PEP 440 matching rules corrected**, all found by the ported tables and all now + agreeing with `pypa/packaging` 26.2: + + - **`V` and a post-release of V.** Same base-version error in the other direction: for + the spec `1.0a1` it excluded `1.0.post0`, which is a post-release of `1.0`, not of + `1.0a1`. Now uses upstream's `_post_base`. + - **`>V` and a local version of V.** "A local version of V" means one whose *public* + part equals V, pre/post/dev included. Comparing base versions made `>1.0a1` wrongly + reject `1.0a2+local`. + - **Prefix matching and `~=`.** `versionSplit` did not split the epoch off as its own + component, so `==0!2.*` could not match `2`; and padding happened between the two + sides rather than up to the spec's numeric width, so `2` did not match `==2.0.0.*`. + `~=` derived its prefix by breaking only on `post`/`dev`, so a pre-release operand + (`~=1.0a1`) produced the wrong prefix. The upstream helpers (`_version_split`, + `_version_join`, `_is_not_suffix`, `_numeric_prefix_len`, `_left_pad`) are now ported + directly. + + **Measured over the production corpus:** 12 of 25,457 distinct specifier tokens change + their match results — all `>`/`<` against a pre-release or post-release operand + (`>1a2`, `>1rc1`, `<1r`, …). Every one of those 12, plus 5 synthetic controls, was + checked against `pypa/packaging` 26.2 over a 19-version panel: **0 of 17 agreed with the + reference before, 17 of 17 agree after.** No rendered form changed. + +### Added + +- **Conformance tables ported from `pypa/packaging`'s `tests/test_specifiers.py`** + (`version/specifier_conformance_test.go`, `version/specifierset_conformance_test.go`), + pinned at `4eb0753dba8fcaaac8eb75463374e448f0931558`: **1,649 assertions**, of which + **358 failed against the pre-fix library**. Attribution is in `NOTICE`. + + Every expectation was cross-checked against two independent sources — the literal in + the pinned upstream file *and* what the installed `packaging` 26.2 actually answers — so + a stale literal cannot be baked in silently. All of them agreed. + + Not ported, with the reason recorded in each file's header: the interval subsystem + behind `is_subset`/`is_superset`/`is_disjoint`/`is_unsatisfiable`, pickling, and the + Python-implementation internals (`__match_args__`, `__hash__`, one-element caches, + construction laziness, `to_range` equivalence). + +### Notes + +- The 163 rows of the hand-written `TestVersion_Check` table were audited against + `packaging` 26.2. **No row disagrees with the reference** where both implementations + parse the input; the only flagged rows are this package's four documented deliberate + extensions (a bare version, the `=` operator, `*` for "any", and `||` for the R path). + ## [0.4.0] - 2026-08-10 ### Breaking diff --git a/NOTICE b/NOTICE index cb5afb9..99446ad 100644 --- a/NOTICE +++ b/NOTICE @@ -106,6 +106,31 @@ TestRequirementParsing, licensed under the Apache License, Version 2.0. Translated from Python/pytest to Go table-driven tests; the parametrize cross-product is expressed as a curated subset with equivalent coverage. +The version/ package's conformance tests port the PEP 440 version-specifier +cases of pypa/packaging (https://github.com/pypa/packaging), Copyright (c) +Donald Stufft and individual contributors, specifically +tests/test_specifiers.py's classes TestSpecifier and TestSpecifierSet, pinned +at commit 4eb0753dba8fcaaac8eb75463374e448f0931558 (release 26.2), licensed +under the Apache License, Version 2.0. Translated from Python/pytest to Go +table-driven tests, in version/specifier_conformance_test.go and +version/specifierset_conformance_test.go. Upstream tests resting on +implementation details with no counterpart here are not ported, and the reason +is recorded in each file's header: the interval subsystem behind is_subset / +is_superset / is_disjoint / is_unsatisfiable, pickling, __match_args__, +__hash__, the one-element specifier-version caches, construction laziness, and +the SpecifierSet.to_range equivalence class. Where a Go expectation differs +from upstream's literal assertion the divergence is annotated inline at its +table. + +The version/ package's specifier-matching helpers (versionSplit, versionJoin, +isNotSuffix, numericPrefixLen, leftPad) and the pre-release, post-release and +local-version boundary rules in its comparison functions are ported from +pypa/packaging's packaging/specifiers.py, specifically _version_split, +_version_join, _is_not_suffix, _numeric_prefix_len, _left_pad, +_earliest_prerelease, _post_base, _public_version, and the bodies of +Specifier._compare_equal, _compare_compatible, _compare_less_than and +_compare_greater_than, licensed under the Apache License, Version 2.0. + The internal/proptest package's property tests are adapted from pypa/packaging's Hypothesis property suite (https://github.com/pypa/packaging), Copyright (c) Donald Stufft and individual contributors, specifically diff --git a/version/specifier.go b/version/specifier.go index a1ba81f..760aefe 100644 --- a/version/specifier.go +++ b/version/specifier.go @@ -5,9 +5,8 @@ package version import ( "errors" "fmt" - "reflect" "regexp" - "strconv" + "slices" "strings" ) @@ -67,9 +66,18 @@ func init() { // "===" must not be decomposed into "==" + "=". const arbitraryOperand = `[^\s,;)]+` + // ⚠️ The whitespace class must be [\s\v], not \s. Go's \s is [\t\n\f\r ] + // and EXCLUDES the vertical tab (0x0B); Python's re \s is [ \t\n\r\f\v] and + // includes it. With a bare \s, the operator/operand separator would not + // match "\v", so `<= \r \f \v v1.0` was rejected while pypa/packaging + // accepts it. versionRegex was already fixed for this; the specifier + // patterns were not. + const wsp = `[\s\v]` + specifierRegexp = regexp.MustCompile( fmt.Sprintf( - `(?i)(?:(?P===)\s*(?P%s)|(?P(%s))\s*(?P%s(\.\*)?))`, + `(?i)(?:(?P===)%[1]s*(?P%[2]s)|(?P(%[3]s))%[1]s*(?P%[4]s(\.\*)?))`, + wsp, arbitraryOperand, strings.Join(ops, "|"), regex, @@ -84,7 +92,8 @@ func init() { // GET /repos/:repo/packages/:key/doc?version= passes a bare version // through NewRSpecifiers. Do not remove it while tightening the comma. constraint := fmt.Sprintf( - `(?:===\s*%s|(%s)\s*(%s(\.\*)?))`, + `(?:===%[1]s*%[2]s|(%[3]s)%[1]s*(%[4]s(\.\*)?))`, + wsp, arbitraryOperand, strings.Join(ops, "|"), regex, @@ -108,8 +117,17 @@ func init() { // Go's regexp permits a capture-group name to repeat, so embedding the // version pattern twice needs no name stripping. Verified rather than // assumed -- it is the opposite of Python's rule. + // ⚠️ The (?i) flag is REQUIRED here and was missing. + // + // specifierRegexp has it, this gate did not, so the two disagreed about + // case: `==1.0a1` passed the gate and parsed, while `==1.0A1` was rejected + // by the gate before specifierRegexp ever saw it. PEP 440 versions are + // case-insensitive, and pypa/packaging accepts every upper-case spelling + // (`==1.0DEV`, `==1.0ALPHA1`, `>=7.9A1`, `~=1.0.POST1`, ...). This was the + // single largest source of conformance failures in + // tests/test_specifiers.py's normalization table. validConstraintRegexp = regexp.MustCompile( - fmt.Sprintf(`^\s*(?:%s\s*(?:,\s*%s\s*)*,?)?\s*$`, constraint, constraint), + fmt.Sprintf(`(?i)^%[1]s*(?:%[2]s%[1]s*(?:,%[1]s*%[2]s%[1]s*)*,?)?%[1]s*$`, wsp, constraint), ) prefixRegexp = regexp.MustCompile(`^([0-9]+)((?:a|b|c|rc)[0-9]+)$`) @@ -327,9 +345,15 @@ func validate(operator, version string) error { switch operator { case "", "=", "==", "!=": - if hasWildcard && (!v.dev.isNull() || !v.post.isNull() || v.local != "") { + // ⚠️ The PRE-release arm was missing. This guard checked dev, post and + // local but never pre, so `==1.0a1.*` and `!=2.0a1.*` were wrongly + // ACCEPTED while pypa/packaging rejects them. PEP 440 does not allow a + // wildcard alongside any of the four, and a prefix match against a + // pre-release is meaningless: the wildcard covers the release segment, + // which is where the pre-release marker would have to sit. + if hasWildcard && (!v.pre.isNull() || !v.dev.isNull() || !v.post.isNull() || v.local != "") { return errors.New( - "the (non)equality operators don't allow to use a wild card and a dev, post, or local version together", + "the (non)equality operators don't allow to use a wild card and a pre, dev, post, or local version together", ) } case "~=": @@ -475,52 +499,103 @@ func andCheck(v Version, specifiers []Specifier) bool { return true } +// versionSplit splits a version string into the components prefix matching and +// the compatible operator compare, ported from pypa/packaging 26.2's +// _version_split. +// +// Two details are load-bearing and were both missing from the earlier +// hand-rolled version: +// +// - The EPOCH is split off on "!" and becomes the first component, defaulting +// to "0". Without it, `2` and `0!2` split to different lengths and +// `==0!2.*` failed to match `2` even though they are the same version. +// - A release-plus-pre-release run in one dot-segment ("2rc1") is split into +// two components ("2", "rc1"), so that a pre-release behaves as its own +// segment for prefix purposes. +// +// The result is for comparison only; joining it back with versionJoin does not +// necessarily reproduce the input. func versionSplit(version string) []string { var result []string - for _, v := range strings.Split(version, ".") { - m := prefixRegexp.FindStringSubmatch(v) - if m != nil { + + // rpartition on "!": everything before the LAST "!" is the epoch. + epoch := "0" + rest := version + if i := strings.LastIndex(version, "!"); i >= 0 { + if version[:i] != "" { + epoch = version[:i] + } + rest = version[i+1:] + } + result = append(result, epoch) + + for _, item := range strings.Split(rest, ".") { + if m := prefixRegexp.FindStringSubmatch(item); m != nil { result = append(result, m[1:]...) } else { - result = append(result, v) + result = append(result, item) } } return result } -func isDigist(s string) bool { - if _, err := strconv.Atoi(s); err == nil { - return true +// versionJoin rebuilds a version string from a versionSplit result, ported from +// pypa/packaging 26.2's _version_join. The first component is the epoch. +func versionJoin(components []string) string { + if len(components) == 0 { + return "" } - return false + return components[0] + "!" + strings.Join(components[1:], ".") } -func padVersion(left, right []string) ([]string, []string) { - var leftRelease, rightRelease []string - for _, l := range left { - if isDigist(l) { - leftRelease = append(leftRelease, l) +// isNotSuffix reports whether a versionSplit component is part of the release +// segment rather than a pre/post/dev suffix. Ported from pypa/packaging 26.2's +// _is_not_suffix; it is the predicate the compatible operator uses to find +// where the release segment ends. +func isNotSuffix(segment string) bool { + for _, prefix := range []string{"dev", "a", "b", "rc", "post"} { + if strings.HasPrefix(segment, prefix) { + return false } } + return true +} - for _, r := range right { - if isDigist(r) { - rightRelease = append(rightRelease, r) +// numericPrefixLen counts the leading all-digit components of a versionSplit +// result. Ported from pypa/packaging 26.2's _numeric_prefix_len. +func numericPrefixLen(split []string) int { + count := 0 + for _, segment := range split { + if !isASCIIDigits(segment) { + break } + count++ } + return count +} - // Get the rest of our versions - leftRest := left[len(leftRelease):] - rightRest := right[len(rightRelease):] - - for i := 0; i < len(leftRelease)-len(rightRelease); i++ { - rightRelease = append(rightRelease, "0") - } - for i := 0; i < len(rightRelease)-len(leftRelease); i++ { - leftRelease = append(leftRelease, "0") - } - - return append(leftRelease, leftRest...), append(rightRelease, rightRest...) +// leftPad pads a versionSplit result with "0" components until it has +// targetNumericLen numeric components, preserving any suffix components. +// Ported from pypa/packaging 26.2's _left_pad. +// +// ⚠️ The padding is inserted AFTER the numeric prefix and BEFORE the suffix, +// which is what the earlier padVersion got wrong: it padded only to the other +// side's length and reassembled the two halves symmetrically, so `2` compared +// against the spec `2.0.0.*` was never widened to three numeric components and +// the prefix match failed. +func leftPad(split []string, targetNumericLen int) []string { + numericLen := numericPrefixLen(split) + padNeeded := targetNumericLen - numericLen + if padNeeded <= 0 { + return split + } + out := make([]string, 0, len(split)+padNeeded) + out = append(out, split[:numericLen]...) + for i := 0; i < padNeeded; i++ { + out = append(out, "0") + } + out = append(out, split[numericLen:]...) + return out } //------------------------------------------------------------------- @@ -557,52 +632,74 @@ func specifierCompatible(prospective Version, spec string) bool { // This allows us to implement this in terms of the other specifiers instead of implementing it ourselves. // The only thing we need to do is construct the other specifiers. + // Everything but the last item of the RELEASE segment. The loop stops at the + // first suffix component, so a pre-release in the operand is not mistaken + // for part of the release. + // + // ⚠️ The earlier version broke only on "post" and "dev", so a pre-release + // operand kept its "a1"/"rc1" component and then had it dropped as "the + // last item" -- which made `~=1.0a1` derive the prefix `1.*` rather than + // dropping only the release's last component. versionSplit now also + // prepends the epoch, so the operand and the prospective version agree on + // component count. var prefixElements []string for _, s := range versionSplit(spec) { - if strings.HasPrefix(s, "post") || strings.HasPrefix(s, "dev") { + if !isNotSuffix(s) { break } prefixElements = append(prefixElements, s) } + if len(prefixElements) == 0 { + // Unreachable for a validated "~=" operand: the grammar requires at + // least two release digits, so there is always an epoch component and + // at least two release components. + return false + } - // We want everything but the last item in the version, but we want to ignore post and dev releases, and - // we want to treat the pre-release as its own separate segment. - prefix := strings.Join(prefixElements[:len(prefixElements)-1], ".") - - // Add the prefix notation to the end of our string - prefix += ".*" + prefix := versionJoin(prefixElements[:len(prefixElements)-1]) + ".*" return specifierGreaterThanEqual(prospective, spec) && specifierEqual(prospective, prefix) } func specifierEqual(prospective Version, spec string) bool { - // https://github.com/pypa/packaging/blob/a6407e3a7e19bd979e93f58cfc7f6641a7378c46/packaging/specifiers.py#L476 - // We need special logic to handle prefix matching + // We need special logic to handle prefix matching. Ported from + // pypa/packaging 26.2's Specifier._compare_equal. if strings.HasSuffix(spec, ".*") { - // In the case of prefix matching we want to ignore local segment. + // In the case of prefix matching we want to ignore the local segment. public, ok := parseOperand(prospective.Public()) if !ok { return false } prospective = public - // Split the spec out by dots, and pretend that there is an implicit - // dot in between a release segment and a pre-release segment. - splitSpec := versionSplit(strings.TrimSuffix(spec, ".*")) + // Split the spec out by bangs and dots, pretending there is an implicit + // dot between a release segment and a pre-release segment. The operand + // goes through Parse first so that its normalized form (leading zeros, + // separator spellings, letter case) is what gets split -- otherwise + // `==1.01.*` and `==1.1.*` would split differently. + operand := strings.TrimSuffix(spec, ".*") + specVersion, ok := parseOperand(operand) + if !ok { + return false + } + splitSpec := versionSplit(specVersion.String()) + specNumericLen := numericPrefixLen(splitSpec) - // Split the prospective version out by dots, and pretend that there is an implicit dot - // in between a release segment and a pre-release segment. splitProspective := versionSplit(prospective.String()) - // Shorten the prospective version to be the same length as the spec - // so that we can determine if the specifier is a prefix of the - // prospective version or not. - if len(splitProspective) > len(splitSpec) { - splitProspective = splitProspective[:len(splitSpec)] + // ⚠️ Pad BEFORE shortening. Padding the prospective version up to the + // spec's numeric width first is what lets `2` match `==2.0.0.*`: it + // becomes ["0","2","0","0"], which then truncates to the spec exactly. + // Truncating first would have thrown away the room the padding needs. + paddedProspective := leftPad(splitProspective, specNumericLen) + + // Shorten the prospective version to the spec's length, so that the + // question becomes whether the spec is a prefix of it. + if len(paddedProspective) > len(splitSpec) { + paddedProspective = paddedProspective[:len(splitSpec)] } - paddedSpec, paddedProspective := padVersion(splitSpec, splitProspective) - return reflect.DeepEqual(paddedSpec, paddedProspective) + return slices.Equal(paddedProspective, splitSpec) } specVersion, ok := parseOperand(spec) @@ -637,35 +734,29 @@ func specifierLessThan(prospective Version, spec string) bool { return false } - // This special case is here so that, unless the specifier itself includes is a pre-release version, - // that we do not accept pre-release versions for the version mentioned in the specifier - // (e.g. <3.1 should not match 3.1.dev0, but should match 3.0.dev0). + // PEP 440: "3.1 should not match 3.0.post0, but should match 3.2.post0). + // PEP 440: ">V MUST NOT allow a post-release of the specified version unless + // the specified version is itself a post-release." // // ⚠️ Matching, not candidate selection. See specifierLessThan. + // + // ⚠️ "a post-release OF the specified version" means the version this one is + // a post-release of IS the spec, exactly. The earlier `sameBaseVersion` test + // was too broad: for the spec `1.0a1` it excluded `1.0.post0`, because both + // have base version 1.0 -- but `1.0.post0` is a post-release of `1.0`, not + // of `1.0a1`, and upstream accepts it. if !s.IsPostRelease() && prospective.IsPostRelease() { - if sameBaseVersion(prospective, s) { + if base, ok := prospective.postBase(); ok && base.Equal(s) { return false } } - // Ensure that we do not allow a local version of the version mentioned - // in the specifier, which is technically greater than, to match. + // PEP 440: ">V MUST NOT match a local version of the specified version." + // + // ⚠️ A "local version of V" is one whose PUBLIC part equals V -- pre, post + // and dev segments included. `sameBaseVersion` discarded those, so `>1.0a1` + // wrongly rejected `1.0a2+local`: same base version 1.0, but a different + // public version, and upstream matches it. if prospective.local != "" { - if sameBaseVersion(prospective, s) { + public, ok := parseOperand(prospective.Public()) + if ok && public.Equal(s) { return false } } diff --git a/version/specifier_conformance_test.go b/version/specifier_conformance_test.go new file mode 100644 index 0000000..94cddd3 --- /dev/null +++ b/version/specifier_conformance_test.go @@ -0,0 +1,972 @@ +// SPDX-License-Identifier: Apache-2.0 OR MIT + +// Conformance tables ported from pypa/packaging's tests/test_specifiers.py, +// one Go table per upstream test function. This file contains ONLY cases +// ported from upstream; hand-written cases belong in specifier_test.go, +// specifier_api_test.go or prerelease_policy_test.go. +// +// Where our expected value differs from upstream's literal assertion, the +// difference is annotated inline with a reason. An UNANNOTATED divergence +// from upstream is a bug in this file, NOT a licence to change the library. +// +// Known intentional divergences, all annotated at their table: +// +// Specifiers.String() - we preserve input order; upstream sorts its +// members. See TestConformance_SpecifierSetStr. +// bare version - we admit an operator-less constraint ("2.0") for +// the R path; upstream rejects it. Tracked as +// rstudio/package-manager#18634. +// +// Every expectation below was cross-checked against the INSTALLED +// pypa/packaging 26.2 as well as the literal in the pinned file, so a stale +// literal cannot be baked in silently. +// +// Upstream pinned at 4eb0753dba8fcaaac8eb75463374e448f0931558. + +package version + +import ( + "fmt" + "strings" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +// Ported from TestSpecifier.test_specifiers_valid (SPECIFIERS). +var conformanceValidSpecifiers = []string{ + "~=2.0", + "==2.1.*", + "==2.1.0.3", + "!=2.2.*", + "!=2.2.0.5", + "<=5", + ">=7.9a1", + "<1.0.dev1", + ">2.0.post1", +} + +func TestConformance_SpecifiersValid(t *testing.T) { + for _, in := range conformanceValidSpecifiers { + t.Run(in, func(t *testing.T) { + _, err := NewSpecifier(in) + assert.NoError(t, err) + _, err = NewSpecifiers(in) + assert.NoError(t, err) + }) + } +} + +// Ported from TestSpecifier.test_specifiers_invalid. +// +// ⚠️ ONE upstream case is deliberately absent: the operator-less specifier +// "2.0". This package admits a bare version as a constraint on purpose, for +// the R path, and PPM depends on it (see the validConstraintRegexp comment in +// init). Narrowing it is tracked as rstudio/package-manager#18634. +var conformanceInvalidSpecifiers = []string{ + // "2.0", // deliberate divergence: bare version, see above + "=>2.0", + "==", + "~=1.0+5", + ">=1.0+deadbeef", + "<=1.0+abc123", + ">1.0+watwat", + "<1.0+1.0", + "~=1.0.*", + ">=1.0.*", + "<=1.0.*", + ">1.0.*", + "<1.0.*", + "==1.0.*+5", + "!=1.0.*+deadbeef", + "==2.0a1.*", + "!=2.0a1.*", + "==2.0.post1.*", + "!=2.0.post1.*", + "==2.0.dev1.*", + "!=2.0.dev1.*", + "==1.0+5.*", + "!=1.0+deadbeef.*", + "==1.0.*.5", + "~=1", + "==1.0.dev1.*", + "!=1.0.dev1.*", + "==1.2+\u0130", + "==1.2+\u0130\u0131\u017fK", +} + +func TestConformance_SpecifiersInvalid(t *testing.T) { + for _, in := range conformanceInvalidSpecifiers { + t.Run(fmt.Sprintf("%q", in), func(t *testing.T) { + _, err := NewSpecifier(in) + assert.Error(t, err, "NewSpecifier must reject %q", in) + _, err = NewSpecifiers(in) + assert.Error(t, err, "NewSpecifiers must reject %q", in) + }) + } +} + +// The bare-version divergence, pinned explicitly so it cannot regress +// silently in either direction. +func TestConformance_BareVersionIsADeliberateDivergence(t *testing.T) { + _, err := NewSpecifiers("2.0") + assert.NoError(t, err, "a bare version stays a valid constraint here") +} + +// Ported from TestSpecifier.test_specifiers_normalized. Upstream prefixes +// each version with every operator that accepts it and asserts the specifier +// parses; the operator set narrows to ==/!= once a local segment is present. +var conformanceNormalizedVersions = []string{ + "1.0dev", + "1.0.dev", + "1.0dev1", + "1.0-dev", + "1.0-dev1", + "1.0DEV", + "1.0.DEV", + "1.0DEV1", + "1.0.DEV1", + "1.0-DEV", + "1.0-DEV1", + "1.0a", + "1.0.a", + "1.0.a1", + "1.0-a", + "1.0-a1", + "1.0alpha", + "1.0.alpha", + "1.0.alpha1", + "1.0-alpha", + "1.0-alpha1", + "1.0A", + "1.0.A", + "1.0.A1", + "1.0-A", + "1.0-A1", + "1.0ALPHA", + "1.0.ALPHA", + "1.0.ALPHA1", + "1.0-ALPHA", + "1.0-ALPHA1", + "1.0b", + "1.0.b", + "1.0.b1", + "1.0-b", + "1.0-b1", + "1.0beta", + "1.0.beta", + "1.0.beta1", + "1.0-beta", + "1.0-beta1", + "1.0B", + "1.0.B", + "1.0.B1", + "1.0-B", + "1.0-B1", + "1.0BETA", + "1.0.BETA", + "1.0.BETA1", + "1.0-BETA", + "1.0-BETA1", + "1.0c", + "1.0.c", + "1.0.c1", + "1.0-c", + "1.0-c1", + "1.0rc", + "1.0.rc", + "1.0.rc1", + "1.0-rc", + "1.0-rc1", + "1.0C", + "1.0.C", + "1.0.C1", + "1.0-C", + "1.0-C1", + "1.0RC", + "1.0.RC", + "1.0.RC1", + "1.0-RC", + "1.0-RC1", + "1.0post", + "1.0.post", + "1.0post1", + "1.0-post", + "1.0-post1", + "1.0POST", + "1.0.POST", + "1.0POST1", + "1.0.POST1", + "1.0-POST", + "1.0-POST1", + "1.0-5", + "1.0+AbC", + "1.01", + "1.0a05", + "1.0b07", + "1.0c056", + "1.0rc09", + "1.0.post000", + "1.1.dev09000", + "00!1.2", + "0100!0.0", + "v1.0", + " \r \f \v v1.0\t\n", +} + +func TestConformance_SpecifiersNormalized(t *testing.T) { + for _, v := range conformanceNormalizedVersions { + ops := []string{"~=", "==", "!=", "<=", ">=", "<", ">"} + if strings.Contains(v, "+") { + // A local version is only legal on the (in)equality operators. + ops = []string{"==", "!="} + } + for _, op := range ops { + in := op + v + t.Run(fmt.Sprintf("%q", in), func(t *testing.T) { + _, err := NewSpecifier(in) + assert.NoError(t, err) + }) + } + } +} + +// Ported from TestSpecifier.test_specifiers_str_and_repr (the str() half; +// Go has no repr). +func TestConformance_SpecifierStr(t *testing.T) { + tests := []struct{ in, want string }{ + {"!=2.0", "!=2.0"}, + {"<2.0", "<2.0"}, + {"<=2.0", "<=2.0"}, + {"==2.0", "==2.0"}, + {">2.0", ">2.0"}, + {">=2.0", ">=2.0"}, + {"~=2.0", "~=2.0"}, + {"< 2", "<2"}, + } + for _, tt := range tests { + t.Run(tt.in, func(t *testing.T) { + s, err := NewSpecifier(tt.in) + require.NoError(t, err) + assert.Equal(t, tt.want, s.String()) + }) + } +} + +// Ported from TestSpecifier.test_specifier_operator_property. +func TestConformance_SpecifierOperatorProperty(t *testing.T) { + tests := []struct{ spec, want string }{ + {"~=2.0", "~="}, + {"==2.1.*", "=="}, + {"==2.1.0.3", "=="}, + {"!=2.2.*", "!="}, + {"!=2.2.0.5", "!="}, + {"<=5", "<="}, + {">=7.9a1", ">="}, + {"<1.0.dev1", "<"}, + {">2.0.post1", ">"}, + {"===lolwat", "==="}, + } + for _, tt := range tests { + t.Run(tt.spec, func(t *testing.T) { + s, err := NewSpecifier(tt.spec) + require.NoError(t, err) + assert.Equal(t, tt.want, s.Operator()) + }) + } +} + +// Ported from TestSpecifier.test_specifier_version_property. +func TestConformance_SpecifierVersionProperty(t *testing.T) { + tests := []struct{ spec, want string }{ + {"~=2.0", "2.0"}, + {"==2.1.*", "2.1.*"}, + {"==2.1.0.3", "2.1.0.3"}, + {"!=2.2.*", "2.2.*"}, + {"!=2.2.0.5", "2.2.0.5"}, + {"<=5", "5"}, + {">=7.9a1", "7.9a1"}, + {"<1.0.dev1", "1.0.dev1"}, + {">2.0.post1", "2.0.post1"}, + {"===lolwat", "lolwat"}, + } + for _, tt := range tests { + t.Run(tt.spec, func(t *testing.T) { + s, err := NewSpecifier(tt.spec) + require.NoError(t, err) + assert.Equal(t, tt.want, s.Version()) + }) + } +} + +// Ported from TestSpecifier.test_specifiers -- the core PEP 440 matching +// table. Upstream builds each Specifier with prereleases=True and asserts +// both the string and the Version form of the operand. +// +// ⚠️ prereleases=True does NOT relax the operator-level guards: entries like +// ("2.0a1", "<2", false) and ("2.0.post1", ">2", false) are expected FALSE +// here even though pre-releases are being included. Selection is not matching. +func TestConformance_SpecifierMatching(t *testing.T) { + tests := []struct { + version string + spec string + want bool + }{ + {"2.0", "==2", true}, + {"2.0", "==2.0", true}, + {"2.0", "==2.0.0", true}, + {"2.0+deadbeef", "==2", true}, + {"2.0+deadbeef", "==2.0", true}, + {"2.0+deadbeef", "==2.0.0", true}, + {"2.0+deadbeef", "==2+deadbeef", true}, + {"2.0+deadbeef", "==2.0+deadbeef", true}, + {"2.0+deadbeef", "==2.0.0+deadbeef", true}, + {"2.0+deadbeef.0", "==2.0.0+deadbeef.00", true}, + {"2.dev1", "==2.*", true}, + {"2a1", "==2.*", true}, + {"2a1.post1", "==2.*", true}, + {"2b1", "==2.*", true}, + {"2b1.dev1", "==2.*", true}, + {"2c1", "==2.*", true}, + {"2c1.post1.dev1", "==2.*", true}, + {"2c1.post1.dev1", "==2.0.*", true}, + {"2rc1", "==2.*", true}, + {"2rc1", "==2.0.*", true}, + {"2", "==2.*", true}, + {"2", "==2.0.*", true}, + {"2", "==2.0.0.*", true}, + {"2", "==0!2.*", true}, + {"0!2", "==2.*", true}, + {"2.0", "==2.*", true}, + {"2.0.0", "==2.*", true}, + {"2.0.0.0", "==2.0.*", true}, + {"2.1+local.version", "==2.1.*", true}, + {"2.1", "!=2", true}, + {"2.1", "!=2.0", true}, + {"2.0.1", "!=2", true}, + {"2.0.1", "!=2.0", true}, + {"2.0.1", "!=2.0.0", true}, + {"2.0", "!=2.0+deadbeef", true}, + {"2.0", "!=3.*", true}, + {"2.1", "!=2.0.*", true}, + {"3", "!=2.0.0.*", true}, + {"2.1.0.0", "!=2.0.*", true}, + {"2.0", ">=2", true}, + {"2.0", ">=2.0", true}, + {"2.0", ">=2.0.0", true}, + {"2.0.post1", ">=2", true}, + {"2.0.post1.dev1", ">=2", true}, + {"3", ">=2", true}, + {"3.0.0a8", ">=3.0.0a7", true}, + {"2.0", "<=2", true}, + {"2.0", "<=2.0", true}, + {"2.0", "<=2.0.0", true}, + {"2.0.dev1", "<=2", true}, + {"2.0a1", "<=2", true}, + {"2.0a1.dev1", "<=2", true}, + {"2.0b1", "<=2", true}, + {"2.0b1.post1", "<=2", true}, + {"2.0c1", "<=2", true}, + {"2.0c1.post1.dev1", "<=2", true}, + {"2.0rc1", "<=2", true}, + {"1", "<=2", true}, + {"3.0.0a7", "<=3.0.0a8", true}, + {"3", ">2", true}, + {"2.1", ">2.0", true}, + {"2.0.1", ">2", true}, + {"2.1.post1", ">2", true}, + {"2.1+local.version", ">2", true}, + {"3.0.0a8", ">3.0.0a7", true}, + {"1", "<2", true}, + {"2.0", "<2.1", true}, + {"2.0.dev0", "<2.1", true}, + {"3.0.0a7", "<3.0.0a8", true}, + {"1", "~=1.0", true}, + {"1.0.1", "~=1.0", true}, + {"1.1", "~=1.0", true}, + {"1.9999999", "~=1.0", true}, + {"1.1", "~=1.0a1", true}, + {"2022.01.01", "~=2022.01.01", true}, + {"2!1.0", "~=2!1.0", true}, + {"2!1.0", "==2!1.*", true}, + {"2!1.0", "==2!1.0", true}, + {"2!1.0", "!=1.0", true}, + {"2!1.0.0", "==2!1.0.0.0.*", true}, + {"2!1.0.0", "==2!1.0.*", true}, + {"2!1.0.0", "==2!1.*", true}, + {"1.0", "!=2!1.0", true}, + {"1.0", "<=2!0.1", true}, + {"2!1.0", ">=2.0", true}, + {"1.0", "<2!0.1", true}, + {"2!1.0", ">2.0", true}, + {"2.0.5", ">2.0dev", true}, + {"1.0+local", ">1.0.dev1", true}, + {"4.1.0a2.dev1235+local", ">4.1.0a2.dev1234", true}, + {"1.0a2+local", ">1.0a1", true}, + {"1.0b2+local", ">1.0b1", true}, + {"1.0rc2+local", ">1.0rc1", true}, + {"1.0.post2+local", ">1.0.post1", true}, + {"1.0.dev2+local", ">1.0.dev1", true}, + {"1.0a1.dev2+local", ">1.0a1.dev1", true}, + {"1.0.post1.dev2+local", ">1.0.post1.dev1", true}, + {"2.1", "==2", false}, + {"2.1", "==2.0", false}, + {"2.1", "==2.0.0", false}, + {"2.0", "==2.0+deadbeef", false}, + {"2.0", "==3.*", false}, + {"2.1", "==2.0.*", false}, + {"3", "==2.0.0.*", false}, + {"2.1.0.0", "==2.0.*", false}, + {"2.0", "!=2", false}, + {"2.0", "!=2.0", false}, + {"2.0", "!=2.0.0", false}, + {"2.0+deadbeef", "!=2", false}, + {"2.0+deadbeef", "!=2.0", false}, + {"2.0+deadbeef", "!=2.0.0", false}, + {"2.0+deadbeef", "!=2+deadbeef", false}, + {"2.0+deadbeef", "!=2.0+deadbeef", false}, + {"2.0+deadbeef", "!=2.0.0+deadbeef", false}, + {"2.0+deadbeef.0", "!=2.0.0+deadbeef.00", false}, + {"2.dev1", "!=2.*", false}, + {"2a1", "!=2.*", false}, + {"2a1.post1", "!=2.*", false}, + {"2b1", "!=2.*", false}, + {"2b1.dev1", "!=2.*", false}, + {"2c1", "!=2.*", false}, + {"2c1.post1.dev1", "!=2.*", false}, + {"2c1.post1.dev1", "!=2.0.*", false}, + {"2rc1", "!=2.*", false}, + {"2rc1", "!=2.0.*", false}, + {"2", "!=2.*", false}, + {"2", "!=2.0.*", false}, + {"2", "!=2.0.0.*", false}, + {"2.0", "!=2.*", false}, + {"2.0.0", "!=2.*", false}, + {"2.0.0.0", "!=2.0.*", false}, + {"2.0.dev1", ">=2", false}, + {"2.0a1", ">=2", false}, + {"2.0a1.dev1", ">=2", false}, + {"2.0b1", ">=2", false}, + {"2.0b1.post1", ">=2", false}, + {"2.0c1", ">=2", false}, + {"2.0c1.post1.dev1", ">=2", false}, + {"2.0rc1", ">=2", false}, + {"1", ">=2", false}, + {"2.0.post1", "<=2", false}, + {"2.0.post1.dev1", "<=2", false}, + {"3", "<=2", false}, + {"1", ">2", false}, + {"2.0.dev1", ">2", false}, + {"2.0a1", ">2", false}, + {"2.0a1.post1", ">2", false}, + {"2.0b1", ">2", false}, + {"2.0b1.dev1", ">2", false}, + {"2.0c1", ">2", false}, + {"2.0c1.post1.dev1", ">2", false}, + {"2.0rc1", ">2", false}, + {"2.0", ">2", false}, + {"2.0.post1", ">2", false}, + {"2.0.post1.dev1", ">2", false}, + {"2.0+local.version", ">2", false}, + {"4.1.0a2.dev1234+local", ">4.1.0a2.dev1234", false}, + {"1.0a1+local", ">1.0a1", false}, + {"1.0b1+local", ">1.0b1", false}, + {"1.0rc1+local", ">1.0rc1", false}, + {"1.0.post1+local", ">1.0.post1", false}, + {"1.0.dev1+local", ">1.0.dev1", false}, + {"1.0a1.dev1+local", ">1.0a1.dev1", false}, + {"1.0.post1.dev1+local", ">1.0.post1.dev1", false}, + {"2.0.dev1", "<2", false}, + {"2.0a1", "<2", false}, + {"2.0a1.post1", "<2", false}, + {"2.0b1", "<2", false}, + {"2.0b2.dev1", "<2", false}, + {"2.0c1", "<2", false}, + {"2.0c1.post1.dev1", "<2", false}, + {"2.0rc1", "<2", false}, + {"2.0", "<2", false}, + {"2.post1", "<2", false}, + {"2.post1.dev1", "<2", false}, + {"3", "<2", false}, + {"2.0", "~=1.0", false}, + {"1.1.0", "~=1.0.0", false}, + {"1.1.post1", "~=1.0.0", false}, + {"1.0", "~=2!1.0", false}, + {"2!1.0", "~=1.0", false}, + {"2!1.0", "==1.0", false}, + {"1.0", "==2!1.0", false}, + {"2!1.0", "==1.0.0.*", false}, + {"1.0", "==2!1.0.0.*", false}, + {"2!1.0", "==1.*", false}, + {"1.0", "==2!1.*", false}, + {"2!1.0", "!=2!1.0", false}, + } + for _, tt := range tests { + t.Run(tt.version+"__"+tt.spec, func(t *testing.T) { + s, err := NewSpecifier(tt.spec, WithPreReleases(PreReleasesInclude)) + require.NoError(t, err) + + assert.Equal(t, tt.want, s.Contains(tt.version), "string operand") + + v, err := Parse(tt.version) + require.NoError(t, err) + assert.Equal(t, tt.want, s.ContainsVersion(v), "Version operand") + + // The same answer through the set, and through pure matching. + ss, err := NewSpecifiers(tt.spec, WithPreReleases(PreReleasesInclude)) + require.NoError(t, err) + assert.Equal(t, tt.want, ss.ContainsVersion(v), "set, Version operand") + assert.Equal(t, tt.want, ss.Check(v), "Check") + }) + } +} + +// Ported from TestSpecifier.test_invalid_version. This table was previously +// INEXPRESSIBLE: Check takes a version.Version, and version.Parse of +// "not a valid version" fails, so there was no way to pass the operand in. +func TestConformance_InvalidVersionOperand(t *testing.T) { + tests := []struct { + spec string + version string + want bool + }{ + {"==1.0", "not a valid version", false}, + {"==1.*", "not a valid version", false}, + {">=1.0", "not a valid version", false}, + {">1.0", "not a valid version", false}, + {"<=1.0", "not a valid version", false}, + {"<1.0", "not a valid version", false}, + {"~=1.0", "not a valid version", false}, + {"!=1.0", "not a valid version", false}, + {"!=1.*", "not a valid version", false}, + {"===invalid", "invalid", true}, + {"===foobar", "invalid", false}, + } + for _, tt := range tests { + t.Run(tt.spec+"__"+tt.version, func(t *testing.T) { + s, err := NewSpecifier(tt.spec, WithPreReleases(PreReleasesInclude)) + require.NoError(t, err) + assert.Equal(t, tt.want, s.Contains(tt.version)) + }) + } +} + +// Ported from TestSpecifier.test_arbitrary_equality. +func TestConformance_ArbitraryEquality(t *testing.T) { + tests := []struct { + version string + spec string + want bool + }{ + {"1.0.0", "===1.0", false}, + {"1.0.dev0", "===1.0", false}, + {"1.0", "===1.0", true}, + {"1.0.dev0", "===1.0.dev0", true}, + {"1.0+downstream1", "===1.0", false}, + {"1.0", "===1.0+downstream1", false}, + {"foobar", "===foobar", true}, + {"foobar", "===baz", false}, + {"1.0a1", "===1.0a1", true}, + {"1.0A1", "===1.0A1", true}, + {"1.0a1", "===1.0A1", true}, + {"1.0A1", "===1.0a1", true}, + {"1.0b1", "===1.0b1", true}, + {"1.0B1", "===1.0B1", true}, + {"1.0b1", "===1.0B1", true}, + {"1.0B1", "===1.0b1", true}, + {"1.0rc1", "===1.0rc1", true}, + {"1.0RC1", "===1.0RC1", true}, + {"1.0rc1", "===1.0RC1", true}, + {"1.0RC1", "===1.0rc1", true}, + {"1.0.post1", "===1.0.post1", true}, + {"1.0.POST1", "===1.0.POST1", true}, + {"1.0.post1", "===1.0.POST1", true}, + {"1.0.POST1", "===1.0.post1", true}, + {"1.0.dev1", "===1.0.dev1", true}, + {"1.0.DEV1", "===1.0.DEV1", true}, + {"1.0.dev1", "===1.0.DEV1", true}, + {"1.0.DEV1", "===1.0.dev1", true}, + {"1.0+local", "===1.0+local", true}, + {"1.0+LOCAL", "===1.0+LOCAL", true}, + {"1.0+local", "===1.0+LOCAL", true}, + {"1.0+LOCAL", "===1.0+local", true}, + {"1.0+abc.def", "===1.0+abc.def", true}, + {"1.0+ABC.DEF", "===1.0+ABC.DEF", true}, + {"1.0+abc.def", "===1.0+ABC.DEF", true}, + {"1.0+ABC.DEF", "===1.0+abc.def", true}, + {"1.0+AbC", "===1.0+AbC", true}, + {"1.0+AbC", "===1.0+abc", true}, + {"1.0+AbC", "===1.0+ABC", true}, + {"1.0a1.post2.dev3", "===1.0a1.post2.dev3", true}, + {"1.0A1.POST2.DEV3", "===1.0A1.POST2.DEV3", true}, + {"1.0a1.post2.dev3", "===1.0A1.POST2.DEV3", true}, + {"1.0A1.POST2.DEV3", "===1.0a1.post2.dev3", true}, + {"lolwat", "===LOLWAT", true}, + {"lolwat", "===LoLWaT", true}, + {"LOLWAT", "===lolwat", true}, + {"LoLWaT", "===lOlwAt", true}, + } + for _, tt := range tests { + t.Run(tt.version+"__"+tt.spec, func(t *testing.T) { + s, err := NewSpecifier(tt.spec) + require.NoError(t, err) + assert.Equal(t, tt.want, s.Contains(tt.version)) + }) + } +} + +// Ported from TestSpecifier.test_arbitrary_equality_normalization. +// +// The whole point of this table is the difference between an operand passed +// as a STRING (compared as written) and one passed as a parsed Version +// (compared normalized). Contains covers the first, ContainsVersion the +// second -- which is why both methods exist. +func TestConformance_ArbitraryEqualityNormalization(t *testing.T) { + tests := []struct { + spec string + item string + asVersion bool + want bool + }{ + {"===1.1", "1.01", false, false}, + {"===1.01", "1.1", false, false}, + {"===1.01", "1.01", false, true}, + {"===1.1", "1.1", false, true}, + {"===1.1", "1.1", true, true}, + {"===1.1", "1.1", true, true}, + {"===1.01", "1.1", true, false}, + {"===1.01", "1.1", true, false}, + {"===1.a1", "1.a1", false, true}, + {"===1a1", "1.a1", false, false}, + {"===1.a1", "1a1", false, false}, + {"===1a1", "1a1", false, true}, + {"===1.a1", "1a1", true, false}, + {"===1a1", "1a1", true, true}, + {"===0!1.0", "0!1.0", false, true}, + {"===0!1.0", "1.0", false, false}, + {"===1.0", "0!1.0", false, false}, + {"===0!1.0", "1.0", true, false}, + {"===1.0", "1.0", true, true}, + {"===01.0", "01.0", false, true}, + {"===01.0", "1.0", false, false}, + {"===1.0", "01.0", false, false}, + {"===01.0", "1.0", true, false}, + {"===1.0", "1.0", true, true}, + {"===1.0.post1", "1.0.post1", false, true}, + {"===1.0-1", "1.0-1", false, true}, + {"===1.0-1", "1.0.post1", false, false}, + {"===1.0.post1", "1.0-1", false, false}, + {"===1.0-1", "1.0.post1", true, false}, + {"===1.0.post1", "1.0.post1", true, true}, + {"===1.0.dev01", "1.0.dev01", false, true}, + {"===1.0.dev01", "1.0.dev1", false, false}, + {"===1.0.dev1", "1.0.dev01", false, false}, + {"===1.0.dev01", "1.0.dev1", true, false}, + {"===1.0.dev1", "1.0.dev1", true, true}, + } + for _, tt := range tests { + t.Run(fmt.Sprintf("%s__%s__v=%t", tt.spec, tt.item, tt.asVersion), func(t *testing.T) { + s, err := NewSpecifier(tt.spec, WithPreReleases(PreReleasesInclude)) + require.NoError(t, err) + if tt.asVersion { + v, err := Parse(tt.item) + require.NoError(t, err) + assert.Equal(t, tt.want, s.ContainsVersion(v)) + return + } + assert.Equal(t, tt.want, s.Contains(tt.item)) + }) + } +} + +// Ported from TestSpecifier.test_specifier_prereleases_detection. +func TestConformance_SpecifierPreReleaseDetection(t *testing.T) { + tests := []struct { + spec string + want PreReleases + }{ + {"==1.0", PreReleasesExclude}, + {">=1.0", PreReleasesExclude}, + {"<=1.0", PreReleasesExclude}, + {"~=1.0", PreReleasesExclude}, + {"<1.0", PreReleasesExclude}, + {">1.0", PreReleasesExclude}, + {"<1.0.dev1", PreReleasesInclude}, + {">1.0.dev1", PreReleasesInclude}, + {"!=1.0.dev1", PreReleasesExclude}, + {"==1.0.*", PreReleasesExclude}, + {"==1.0.dev1", PreReleasesInclude}, + {">=1.0.dev1", PreReleasesInclude}, + {"<=1.0.dev1", PreReleasesInclude}, + {"~=1.0.dev1", PreReleasesInclude}, + } + for _, tt := range tests { + t.Run(tt.spec, func(t *testing.T) { + s, err := NewSpecifier(tt.spec) + require.NoError(t, err) + assert.Equal(t, tt.want, s.PreReleases()) + }) + } +} + +// Ported from TestSpecifier.test_specifiers_prereleases: the constructor +// policy and the per-call policy, in every combination upstream exercises. +func TestConformance_SpecifierPreReleases(t *testing.T) { + tests := []struct { + spec string + version string + specPre PreReleases + callPre PreReleases + want bool + }{ + {">=1.0", "2.0.dev1", PreReleasesAuto, PreReleasesAuto, true}, + {">=2.0.dev1", "2.0a1", PreReleasesAuto, PreReleasesAuto, true}, + {"==2.0.*", "2.0a1.dev1", PreReleasesAuto, PreReleasesAuto, true}, + {"<=2.0", "1.0.dev1", PreReleasesAuto, PreReleasesAuto, true}, + {"<=2.0.dev1", "1.0a1", PreReleasesAuto, PreReleasesAuto, true}, + {"<2.0", "2.0a1", PreReleasesAuto, PreReleasesAuto, false}, + {"<2.0a2", "2.0a1", PreReleasesAuto, PreReleasesAuto, true}, + {">1.0.dev1", "1.0.dev0", PreReleasesAuto, PreReleasesAuto, false}, + {">1.0.dev1", "1.0.dev2", PreReleasesAuto, PreReleasesAuto, true}, + {">1.0.dev1", "1.0.post0", PreReleasesAuto, PreReleasesAuto, true}, + {">1.0.dev1", "1.0.post1", PreReleasesAuto, PreReleasesAuto, true}, + {">1.0.dev0", "1.0.post0", PreReleasesAuto, PreReleasesAuto, true}, + {">1.0a1", "1.0.post0", PreReleasesAuto, PreReleasesAuto, true}, + {">1.0b1", "1.0.post0", PreReleasesAuto, PreReleasesAuto, true}, + {">1.0rc1", "1.0.post0", PreReleasesAuto, PreReleasesAuto, true}, + {">1.0a1", "1.0a1.post0", PreReleasesAuto, PreReleasesAuto, false}, + {">1.0b2", "1.0b2.post0", PreReleasesAuto, PreReleasesAuto, false}, + {">1.0rc1", "1.0rc1.post0", PreReleasesAuto, PreReleasesAuto, false}, + {">1.0a1", "1.0a2.post0", PreReleasesAuto, PreReleasesAuto, true}, + {">1.0b1", "1.0b2.post0", PreReleasesAuto, PreReleasesAuto, true}, + {">1.0.dev1", "1.0", PreReleasesAuto, PreReleasesAuto, true}, + {">1.0.dev1", "1.0a1", PreReleasesAuto, PreReleasesAuto, true}, + {">1.0.dev1", "1.1", PreReleasesAuto, PreReleasesAuto, true}, + {">1.0", "1.0.post0", PreReleasesAuto, PreReleasesAuto, false}, + {">1.0", "1.0.post1", PreReleasesAuto, PreReleasesAuto, false}, + {">1.0", "2.0.post0", PreReleasesAuto, PreReleasesAuto, true}, + {">1.0", "0.9.post0", PreReleasesAuto, PreReleasesAuto, false}, + {">1.0.dev1", "1.1.post0", PreReleasesAuto, PreReleasesAuto, true}, + {">1.0.dev1", "0.9.post0", PreReleasesAuto, PreReleasesAuto, false}, + {"<1.0.post1", "1.0.post1.dev0", PreReleasesAuto, PreReleasesAuto, false}, + {"<1.0.post0", "1.0.post0.dev0", PreReleasesAuto, PreReleasesAuto, false}, + {"<1.0.post1", "1.0.dev0", PreReleasesAuto, PreReleasesAuto, true}, + {"<1.0.post1", "1.0a1", PreReleasesAuto, PreReleasesAuto, true}, + {"<1.0.post1", "1.0rc1", PreReleasesAuto, PreReleasesAuto, true}, + {"<1.0.post0", "1.0.dev0", PreReleasesAuto, PreReleasesAuto, true}, + {"<1.0.post0", "1.0a1", PreReleasesAuto, PreReleasesAuto, true}, + {"<1.0.post0", "1.0b1", PreReleasesAuto, PreReleasesAuto, true}, + {"<1.0.post0", "1.0rc2", PreReleasesAuto, PreReleasesAuto, true}, + {"<1.0.post1", "1.0.post0.dev0", PreReleasesAuto, PreReleasesAuto, true}, + {"<1.0.post2", "1.0.post1.dev0", PreReleasesAuto, PreReleasesAuto, true}, + {"<1.0.post1", "1.0", PreReleasesAuto, PreReleasesAuto, true}, + {"<1.0.post1", "1.0.post0", PreReleasesAuto, PreReleasesAuto, true}, + {"<1.0.post1", "0.9", PreReleasesAuto, PreReleasesAuto, true}, + {"<1.0.post0", "1.0", PreReleasesAuto, PreReleasesAuto, true}, + {"<1.0.post10", "1.0.dev0", PreReleasesAuto, PreReleasesAuto, true}, + {"<1.0.post10", "1.0.post9.dev0", PreReleasesAuto, PreReleasesAuto, true}, + {"<1.0.post10", "1.0.post9", PreReleasesAuto, PreReleasesAuto, true}, + {"<1.0.post1", "1.0+local", PreReleasesAuto, PreReleasesAuto, true}, + {"<1.0.post1", "1.0.post0+local", PreReleasesAuto, PreReleasesAuto, true}, + {"<1.0.post1", "0.9.dev0", PreReleasesAuto, PreReleasesAuto, true}, + {"<=2.0", "1.0.dev1", PreReleasesExclude, PreReleasesAuto, false}, + {"<=2.0a1", "1.0.dev1", PreReleasesExclude, PreReleasesAuto, false}, + {"<=2.0", "1.0.dev1", PreReleasesAuto, PreReleasesExclude, false}, + {"<=2.0a1", "1.0.dev1", PreReleasesAuto, PreReleasesExclude, false}, + {"<=2.0", "1.0.dev1", PreReleasesInclude, PreReleasesExclude, false}, + {"<=2.0a1", "1.0.dev1", PreReleasesInclude, PreReleasesExclude, false}, + {"<=2.0", "1.0.dev1", PreReleasesExclude, PreReleasesInclude, true}, + {"<=2.0a1", "1.0.dev1", PreReleasesExclude, PreReleasesInclude, true}, + } + for _, tt := range tests { + t.Run(fmt.Sprintf("%s__%s__%s__%s", tt.spec, tt.version, tt.specPre, tt.callPre), func(t *testing.T) { + s, err := NewSpecifier(tt.spec, WithPreReleases(tt.specPre)) + require.NoError(t, err) + assert.Equal(t, tt.want, s.Contains(tt.version, WithPreReleases(tt.callPre))) + }) + } +} + +// Ported from TestSpecifier.test_specifier_filter. +func TestConformance_SpecifierFilter(t *testing.T) { + tests := []struct { + spec string + specPre PreReleases + callPre PreReleases + input []string + want []string + }{ + {">=1.0.dev1", PreReleasesAuto, PreReleasesAuto, []string{"1.0", "2.0a1"}, []string{"1.0", "2.0a1"}}, + {">=1.2.3", PreReleasesAuto, PreReleasesAuto, []string{"1.2", "1.5a1"}, []string{"1.5a1"}}, + {">=1.2.3", PreReleasesAuto, PreReleasesAuto, []string{"1.3", "1.5a1"}, []string{"1.3"}}, + {">=1.0", PreReleasesAuto, PreReleasesAuto, []string{"2.0a1"}, []string{"2.0a1"}}, + {"!=2.0a1", PreReleasesAuto, PreReleasesAuto, []string{"1.0a2", "1.0", "2.0a1"}, []string{"1.0"}}, + {"==2.0a1", PreReleasesAuto, PreReleasesAuto, []string{"2.0a1"}, []string{"2.0a1"}}, + {">2.0a1", PreReleasesAuto, PreReleasesAuto, []string{"2.0a1", "3.0a2", "3.0"}, []string{"3.0a2", "3.0"}}, + {"<2.0a1", PreReleasesAuto, PreReleasesAuto, []string{"1.0a2", "1.0", "2.0a1"}, []string{"1.0a2", "1.0"}}, + {"~=2.0a1", PreReleasesAuto, PreReleasesAuto, []string{"1.0", "2.0a1", "3.0a2", "3.0"}, []string{"2.0a1"}}, + {">=1.0.dev1", PreReleasesAuto, PreReleasesExclude, []string{"1.0", "2.0a1"}, []string{"1.0"}}, + {">=1.0.dev1", PreReleasesInclude, PreReleasesAuto, []string{"1.0", "2.0a1"}, []string{"1.0", "2.0a1"}}, + {">=1.0.dev1", PreReleasesExclude, PreReleasesAuto, []string{"1.0", "2.0a1"}, []string{"1.0"}}, + {">=1.0", PreReleasesInclude, PreReleasesExclude, []string{"1.0", "2.0a1"}, []string{"1.0"}}, + {">=1.0", PreReleasesExclude, PreReleasesInclude, []string{"1.0", "2.0a1"}, []string{"1.0", "2.0a1"}}, + {">=1.0", PreReleasesInclude, PreReleasesInclude, []string{"1.0", "2.0a1"}, []string{"1.0", "2.0a1"}}, + {">=1.0", PreReleasesExclude, PreReleasesExclude, []string{"1.0", "2.0a1"}, []string{"1.0"}}, + {">=1.0", PreReleasesAuto, PreReleasesAuto, []string{"not a valid version"}, nil}, + {">=1.0", PreReleasesAuto, PreReleasesAuto, []string{"1.0", "not a valid version"}, []string{"1.0"}}, + {"===foobar", PreReleasesAuto, PreReleasesAuto, []string{"foobar", "foo", "bar"}, []string{"foobar"}}, + {"===foobar", PreReleasesAuto, PreReleasesAuto, []string{"foo", "bar"}, nil}, + {"===1.0", PreReleasesAuto, PreReleasesAuto, []string{"1.0", "1.0.0", "2.0"}, []string{"1.0"}}, + {"===1.0", PreReleasesAuto, PreReleasesAuto, []string{"1.0", "1.0+downstream1"}, []string{"1.0"}}, + {"===foobar", PreReleasesAuto, PreReleasesAuto, []string{"foobar", "1.0", "2.0a1", "invalid"}, []string{"foobar"}}, + {"===1.0", PreReleasesAuto, PreReleasesAuto, []string{"1.0", "foobar", "invalid", "1.0.0"}, []string{"1.0"}}, + {"!=1.0", PreReleasesAuto, PreReleasesAuto, []string{"invalid", "foobar"}, nil}, + {"!=1.0", PreReleasesAuto, PreReleasesAuto, []string{"1.0", "invalid", "2.0"}, []string{"2.0"}}, + {"!=2.0.*", PreReleasesAuto, PreReleasesAuto, []string{"invalid", "foobar", "2.0"}, nil}, + {"!=2.0.*", PreReleasesAuto, PreReleasesAuto, []string{"1.0", "invalid", "2.0.0"}, []string{"1.0"}}, + {"!=1.0", PreReleasesAuto, PreReleasesInclude, []string{"invalid", "foobar"}, nil}, + {"!=1.0", PreReleasesAuto, PreReleasesExclude, []string{"invalid", "foobar"}, nil}, + {"!=1.0", PreReleasesInclude, PreReleasesAuto, []string{"invalid", "foobar"}, nil}, + {"!=1.0", PreReleasesExclude, PreReleasesAuto, []string{"invalid", "foobar"}, nil}, + {"!=1.0", PreReleasesInclude, PreReleasesInclude, []string{"invalid", "foobar"}, nil}, + {"!=1.0", PreReleasesExclude, PreReleasesExclude, []string{"invalid", "foobar"}, nil}, + {"===foobar", PreReleasesAuto, PreReleasesInclude, []string{"foobar", "foo"}, []string{"foobar"}}, + {"===foobar", PreReleasesAuto, PreReleasesExclude, []string{"foobar", "foo"}, []string{"foobar"}}, + {"===foobar", PreReleasesInclude, PreReleasesAuto, []string{"foobar", "foo"}, []string{"foobar"}}, + {"===foobar", PreReleasesExclude, PreReleasesAuto, []string{"foobar", "foo"}, []string{"foobar"}}, + {"===foobar", PreReleasesInclude, PreReleasesInclude, []string{"foobar", "foo"}, []string{"foobar"}}, + {"===foobar", PreReleasesExclude, PreReleasesExclude, []string{"foobar", "foo"}, []string{"foobar"}}, + } + for _, tt := range tests { + t.Run(fmt.Sprintf("%s__%s__%s__%v", tt.spec, tt.specPre, tt.callPre, tt.input), func(t *testing.T) { + s, err := NewSpecifier(tt.spec, WithPreReleases(tt.specPre)) + require.NoError(t, err) + got := s.Filter(tt.input, WithPreReleases(tt.callPre)) + if len(tt.want) == 0 { + assert.Empty(t, got) + return + } + assert.Equal(t, tt.want, got) + }) + } +} + +// Ported from +// TestSpecifier.test_specifier_filter_arbitrary_equality_normalization. +// Upstream mixes plain strings and Version objects in ONE iterable; Go is +// typed, so each case is split into its string half and its Version half. +func TestConformance_SpecifierFilterArbitraryNormalization(t *testing.T) { + type variant struct { + input []string + want []string + } + tests := []struct { + spec string + strings variant + vers variant + }{ + {"===1.01", variant{[]string{"1.01", "1.1", "1.0.1"}, []string{"1.01"}}, variant{nil, nil}}, + {"===1.1", variant{[]string{"1.01", "1.1"}, []string{"1.1"}}, variant{nil, nil}}, + {"===1.1", variant{nil, nil}, variant{[]string{"1.1", "1.1"}, []string{"1.1", "1.1"}}}, + {"===1.01", variant{nil, nil}, variant{[]string{"1.1", "1.1"}, nil}}, + {"===1.1", variant{[]string{"1.01", "1.1"}, []string{"1.1"}}, variant{[]string{"1.1"}, []string{"1.1"}}}, + {"===1.01", variant{[]string{"1.01", "1.1"}, []string{"1.01"}}, variant{[]string{"1.1"}, nil}}, + {"===1.a1", variant{[]string{"1.a1", "1a1"}, []string{"1.a1"}}, variant{nil, nil}}, + {"===1a1", variant{[]string{"1.a1", "1a1"}, []string{"1a1"}}, variant{[]string{"1a1"}, []string{"1a1"}}}, + } + for _, tt := range tests { + t.Run(tt.spec, func(t *testing.T) { + s, err := NewSpecifier(tt.spec, WithPreReleases(PreReleasesInclude)) + require.NoError(t, err) + + gotStrings := s.Filter(tt.strings.input) + if len(tt.strings.want) == 0 { + assert.Empty(t, gotStrings) + } else { + assert.Equal(t, tt.strings.want, gotStrings) + } + + var vs []Version + for _, raw := range tt.vers.input { + v, err := Parse(raw) + require.NoError(t, err) + vs = append(vs, v) + } + var gotVersions []string + for _, v := range s.FilterVersions(vs) { + gotVersions = append(gotVersions, v.String()) + } + assert.Equal(t, tt.vers.want, gotVersions) + }) + } +} + +// Ported from TestSpecifier.test_length and TestSpecifier.test_iteration. +func TestConformance_SetLengthAndIteration(t *testing.T) { + tests := []struct { + spec string + len int + items []string + }{ + {"", 0, nil}, + {"==2.0", 1, []string{"==2.0"}}, + {">=2.0", 1, []string{">=2.0"}}, + {">=2.0,<3", 2, []string{">=2.0", "<3"}}, + {">=2.0,<3,==2.4", 3, []string{">=2.0", "<3", "==2.4"}}, + } + for _, tt := range tests { + t.Run(fmt.Sprintf("%q", tt.spec), func(t *testing.T) { + ss, err := NewSpecifiers(tt.spec) + require.NoError(t, err) + assert.Equal(t, tt.len, ss.Len()) + + var got []string + for _, s := range ss.List() { + got = append(got, s.String()) + } + assert.ElementsMatch(t, tt.items, got) + }) + } +} + +// Ported from TestSpecifier.test_comparison_true / test_comparison_false / +// test_comparison_canonicalizes / test_specifier_equal_for_compatible_operator. +// +// Upstream's tables are the full SPECIFIERS cross-product asserted through +// operator.eq and operator.ne; expressed here as one Equal matrix, which is +// the same 162 assertions without the Python operator plumbing. +func TestConformance_SpecifierEqualityMatrix(t *testing.T) { + for i, left := range conformanceValidSpecifiers { + for j, right := range conformanceValidSpecifiers { + t.Run(left+"__"+right, func(t *testing.T) { + l, err := NewSpecifier(left) + require.NoError(t, err) + r, err := NewSpecifier(right) + require.NoError(t, err) + assert.Equal(t, i == j, l.Equal(r)) + }) + } + } +} + +func TestConformance_SpecifierEqualityCanonicalizes(t *testing.T) { + tests := []struct { + left, right string + want bool + }{ + {"==2.8.0", "==2.8", true}, + // From test_specifier_equal_for_compatible_operator: trailing zeros are + // load-bearing under "~=", so these are NOT equal. + {"~=1.18.0", "~=1.18", false}, + } + for _, tt := range tests { + t.Run(tt.left+"__"+tt.right, func(t *testing.T) { + l, err := NewSpecifier(tt.left) + require.NoError(t, err) + r, err := NewSpecifier(tt.right) + require.NoError(t, err) + assert.Equal(t, tt.want, l.Equal(r)) + }) + } +} diff --git a/version/specifier_test.go b/version/specifier_test.go index f8ee1f4..2d8c820 100644 --- a/version/specifier_test.go +++ b/version/specifier_test.go @@ -423,22 +423,84 @@ func TestVersion_Check(t *testing.T) { } } -// TestPadVersion_RightRestDerivesFromRight is a regression test for a bug where -// padVersion sliced left twice instead of slicing right for the "rest" of the -// right-hand version, so the returned right-hand segments were built from -// leftover pieces of the left-hand version instead of the right-hand version. -func TestPadVersion_RightRestDerivesFromRight(t *testing.T) { - // left's release segment ("2","0") is longer than right's ("3"), and each - // side has a distinct, differently-shaped non-numeric "rest" suffix, so a - // padVersion that mixes up which side the rest comes from is easy to spot. - left := []string{"2", "0", "rc1"} - right := []string{"3", "x"} - - _, gotRight := padVersion(left, right) - - // The right-hand rest must come from right (["x"]), not left (["rc1"]). - want := []string{"3", "0", "x"} - assert.Equal(t, want, gotRight) +// TestLeftPad_InsertsPaddingBeforeTheSuffix replaces the old +// TestPadVersion_RightRestDerivesFromRight. +// +// padVersion is gone: it padded the two sides against EACH OTHER and +// reassembled numeric prefix plus "rest" for both, and the bug it guarded +// against was slicing the left-hand side twice so the right-hand "rest" came +// from the wrong version. leftPad pads ONE side up to a target numeric width, +// with a single slice of a single input, so that class of mix-up is structurally +// impossible. +// +// What still has to hold, and is the substance of the old test, is that the "0" +// padding goes AFTER the numeric prefix and BEFORE any suffix segment, and that +// the suffix survives intact. +func TestLeftPad_InsertsPaddingBeforeTheSuffix(t *testing.T) { + tests := []struct { + name string + split []string + target int + want []string + }{ + { + // Ported from pypa/packaging 26.2's _left_pad docstring. + name: "upstream docstring example", + split: []string{"0", "1", "a1"}, + target: 4, + want: []string{"0", "1", "0", "0", "a1"}, + }, + { + name: "suffix is preserved, not overwritten", + split: []string{"0", "2", "rc1"}, + target: 3, + want: []string{"0", "2", "0", "rc1"}, + }, + { + name: "already wide enough is returned unchanged", + split: []string{"0", "1", "2", "3"}, + target: 3, + want: []string{"0", "1", "2", "3"}, + }, + { + name: "no suffix", + split: []string{"0", "2"}, + target: 4, + want: []string{"0", "2", "0", "0"}, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + assert.Equal(t, tt.want, leftPad(tt.split, tt.target)) + }) + } +} + +// versionSplit must peel the epoch off as its own leading component, and must +// treat a release-plus-pre-release run in one dot-segment as two components. +// Both were missing before, and both are why `==0!2.*` did not match `2`. +func TestVersionSplit_EpochAndPreRelease(t *testing.T) { + tests := []struct { + in string + want []string + }{ + {"2", []string{"0", "2"}}, + {"0!2", []string{"0", "2"}}, + {"2!1.0", []string{"2", "1", "0"}}, + {"2rc1", []string{"0", "2", "rc1"}}, + // The pre-release run is split out of its own dot-segment, so "0a1" + // becomes two components. Verified against pypa/packaging 26.2's + // _version_split. + {"1.0a1", []string{"0", "1", "0", "a1"}}, + {"1.0.post1", []string{"0", "1", "0", "post1"}}, + {"2.0.0", []string{"0", "2", "0", "0"}}, + } + for _, tt := range tests { + t.Run(tt.in, func(t *testing.T) { + assert.Equal(t, tt.want, versionSplit(tt.in)) + }) + } } // TestNewRSpecifiers_UsesProvidedSanitizer is a regression test for a bug where diff --git a/version/specifierset_conformance_test.go b/version/specifierset_conformance_test.go new file mode 100644 index 0000000..f091f3d --- /dev/null +++ b/version/specifierset_conformance_test.go @@ -0,0 +1,760 @@ +// SPDX-License-Identifier: Apache-2.0 OR MIT + +// Conformance tables ported from the TestSpecifierSet class of +// pypa/packaging's tests/test_specifiers.py, one Go table per upstream test +// function. See specifier_conformance_test.go for the porting conventions and +// the list of intentional divergences. +// +// Upstream tests NOT ported from this class, and why: +// +// test_is_subset / test_is_superset / test_is_disjoint / TestIsUnsatisfiable +// - these rest on packaging 26.2's interval subsystem +// (_to_ranges / _intersect_ranges / _LowerBound / +// _UpperBound), which has no counterpart in this module. +// Set relations and unsatisfiability are their own design +// exercise, not accessors; deliberately out of scope. +// test_pickle_* - Go has no pickle. +// test_match_args, test_specifiers_hash, *_construction_is_lazy, +// TestSpecifierInternal.*, TestSpecifierSetToRangeEquivalence.* +// - Python-implementation internals (__match_args__, __hash__, +// one-element caches, laziness, to_range) with no exported +// Go surface to assert against. +// +// Upstream pinned at 4eb0753dba8fcaaac8eb75463374e448f0931558. + +package version + +import ( + "fmt" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +// Ported from TestSpecifierSet.test_empty_specifier_arbitrary_string. +// +// An empty set admits an arbitrary string unconditionally: there is no way to +// tell whether an opaque token is a pre-release, so the pre-release preference +// cannot apply to it. Note the third block -- with prereleases=False the +// arbitrary string survives and the pre-release does not. +func TestConformance_EmptySetArbitraryString(t *testing.T) { + tests := []struct { + pre PreReleases + input []string + want []string + }{ + {PreReleasesAuto, []string{"foobar"}, []string{"foobar"}}, + {PreReleasesExclude, []string{"foobar"}, []string{"foobar"}}, + {PreReleasesInclude, []string{"foobar"}, []string{"foobar"}}, + {PreReleasesAuto, []string{"foobar", "1.0"}, []string{"foobar", "1.0"}}, + {PreReleasesExclude, []string{"foobar", "1.0"}, []string{"foobar", "1.0"}}, + {PreReleasesInclude, []string{"foobar", "1.0"}, []string{"foobar", "1.0"}}, + {PreReleasesAuto, []string{"foobar", "1.0a1"}, []string{"foobar", "1.0a1"}}, + {PreReleasesExclude, []string{"foobar", "1.0a1"}, []string{"foobar"}}, + {PreReleasesInclude, []string{"foobar", "1.0a1"}, []string{"foobar", "1.0a1"}}, + } + for _, tt := range tests { + t.Run(fmt.Sprintf("%s__%v", tt.pre, tt.input), func(t *testing.T) { + ss, err := NewSpecifiers("", WithPreReleases(tt.pre)) + require.NoError(t, err) + assert.True(t, ss.Contains("foobar"), "an empty set admits any string") + assert.Equal(t, tt.want, ss.Filter(tt.input)) + }) + } +} + +// Ported from TestSpecifierSet.test_empty_specifier_ordering_with_arbitrary: +// the preference must not reorder what it keeps. +func TestConformance_EmptySetOrderingWithArbitrary(t *testing.T) { + tests := []struct{ input, want []string }{ + {[]string{"1.0a1", "foobar", "1.0", "2.0a1", "bazqux", "2.0"}, []string{"foobar", "1.0", "bazqux", "2.0"}}, + {[]string{"foobar", "1.0", "bazqux", "2.0", "hello"}, []string{"foobar", "1.0", "bazqux", "2.0", "hello"}}, + {[]string{"1.0a1", "foobar", "2.0a1", "bazqux", "3.0a1"}, []string{"1.0a1", "foobar", "2.0a1", "bazqux", "3.0a1"}}, + } + for _, tt := range tests { + t.Run(fmt.Sprintf("%v", tt.input), func(t *testing.T) { + ss, err := NewSpecifiers("") + require.NoError(t, err) + assert.Equal(t, tt.want, ss.Filter(tt.input)) + }) + } +} + +// Ported from TestSpecifierSet.test_create_from_specifiers. +func TestConformance_CreateFromSpecifiers(t *testing.T) { + specStrs := []string{">=1.0", "!=1.1", "!=1.2", "<2.0"} + var specs []Specifier + for _, s := range specStrs { + parsed, err := NewSpecifier(s) + require.NoError(t, err) + specs = append(specs, parsed) + } + + ss := NewSpecifiersFrom(specs) + assert.Equal(t, len(specStrs), ss.Len()) + + var got []string + for _, s := range ss.List() { + got = append(got, s.String()) + } + assert.ElementsMatch(t, specStrs, got) +} + +// Ported from TestSpecifierSet.test_specifier_prereleases_explicit. +// +// Upstream MUTATES `spec.prereleases` between the two assertions. Specifiers +// is a value type here, so the second half is expressed as a second set built +// with the new policy -- which is the same question, asked without mutation. +func TestConformance_SetPreReleasesExplicit(t *testing.T) { + tests := []struct { + spec string + initialPre PreReleases + finalPre PreReleases + version string + wantInitial bool + wantFinal bool + }{ + {"", PreReleasesAuto, PreReleasesInclude, "1.0.dev1", true, true}, + {"", PreReleasesExclude, PreReleasesInclude, "1.0.dev1", false, true}, + {"", PreReleasesInclude, PreReleasesExclude, "1.0.dev1", true, false}, + {">=1.0", PreReleasesInclude, PreReleasesExclude, "1.0.dev1", false, false}, + {"==1.*", PreReleasesInclude, PreReleasesExclude, "1.0.dev1", true, false}, + {"", PreReleasesExclude, PreReleasesAuto, "1.0.dev1", false, true}, + {">=1.0", PreReleasesExclude, PreReleasesAuto, "2.0.dev1", false, true}, + {"", PreReleasesInclude, PreReleasesAuto, "1.0.dev1", true, true}, + {">=1.0", PreReleasesInclude, PreReleasesAuto, "2.0.dev1", true, true}, + {"", PreReleasesAuto, PreReleasesInclude, "2.0b1", true, true}, + {"", PreReleasesAuto, PreReleasesExclude, "2.0a1", true, false}, + {"", PreReleasesInclude, PreReleasesExclude, "1.0rc1", true, false}, + {"", PreReleasesExclude, PreReleasesInclude, "1.0.post1.dev1", false, true}, + {"==2.0.dev1", PreReleasesAuto, PreReleasesExclude, "2.0.dev1", true, false}, + {"==1.0.*", PreReleasesInclude, PreReleasesExclude, "1.0.dev1", true, false}, + {"!=2.0", PreReleasesExclude, PreReleasesInclude, "1.0.dev1", false, true}, + {">=1.0,<2.0", PreReleasesAuto, PreReleasesInclude, "1.5a1", true, true}, + {">=1.0,<2.0", PreReleasesExclude, PreReleasesInclude, "1.5b1", false, true}, + {">=1.0,<2.0", PreReleasesInclude, PreReleasesExclude, "1.5rc1", true, false}, + {"", PreReleasesAuto, PreReleasesInclude, "1.0a1", true, true}, + {"", PreReleasesAuto, PreReleasesInclude, "1.0b2", true, true}, + {"", PreReleasesAuto, PreReleasesInclude, "1.0rc3", true, true}, + {"", PreReleasesAuto, PreReleasesInclude, "1.0.dev4", true, true}, + {">=1.0a1", PreReleasesAuto, PreReleasesExclude, "1.0a1", true, false}, + {"<1.0.dev1", PreReleasesAuto, PreReleasesExclude, "0.9.dev0", true, false}, + } + for _, tt := range tests { + t.Run(fmt.Sprintf("%q__%s__%s__%s", tt.spec, tt.initialPre, tt.finalPre, tt.version), func(t *testing.T) { + initial, err := NewSpecifiers(tt.spec, WithPreReleases(tt.initialPre)) + require.NoError(t, err) + assert.Equal(t, tt.wantInitial, initial.Contains(tt.version), "initial policy") + + final, err := NewSpecifiers(tt.spec, WithPreReleases(tt.finalPre)) + require.NoError(t, err) + assert.Equal(t, tt.wantFinal, final.Contains(tt.version), "final policy") + + // ⚠️ A per-call EXPLICIT policy reaches the same answer as rebuilding + // the set with it. A per-call PreReleasesAuto does not, and must not: + // Auto means "no opinion, defer to the set", which is upstream's + // `contains(item, prereleases=None)` -- NOT upstream's + // `spec.prereleases = None`, which resets the set's own policy. Those + // are different operations, and only the second one can undo an + // explicit constructor policy. Rebuilding the set is how you do that + // here, which is exactly what `final` above is. + if tt.finalPre != PreReleasesAuto { + assert.Equal(t, tt.wantFinal, initial.Contains(tt.version, WithPreReleases(tt.finalPre)), + "an explicit per-call policy must agree with a rebuilt set") + } + }) + } +} + +// Ported from TestSpecifierSet.test_specifier_contains_prereleases. +func TestConformance_SetContainsPreReleases(t *testing.T) { + auto, err := NewSpecifiers("") + require.NoError(t, err) + assert.Equal(t, PreReleasesAuto, auto.PreReleases()) + assert.True(t, auto.Contains("1.0.dev1")) + assert.True(t, auto.Contains("1.0.dev1", WithPreReleases(PreReleasesInclude))) + + incl, err := NewSpecifiers("", WithPreReleases(PreReleasesInclude)) + require.NoError(t, err) + assert.Equal(t, PreReleasesInclude, incl.PreReleases()) + assert.True(t, incl.Contains("1.0.dev1")) + assert.False(t, incl.Contains("1.0.dev1", WithPreReleases(PreReleasesExclude)), + "a per-call policy must override the constructor policy") +} + +// Ported from TestSpecifierSet.test_specifier_contains_installed_prereleases: +// the `installed=True` carve-out, which accepts an already-installed +// pre-release the set would not offer as a NEW candidate. +func TestConformance_SetContainsInstalledPreReleases(t *testing.T) { + tests := []struct { + spec string + version string + specPre PreReleases + callPre PreReleases + installed bool + want bool + }{ + {"~=1.0", "1.1.0.dev1", PreReleasesAuto, PreReleasesAuto, true, true}, + {"~=1.0", "1.1.0.dev1", PreReleasesExclude, PreReleasesExclude, true, true}, + {"~=1.0", "1.1.0.dev1", PreReleasesInclude, PreReleasesExclude, true, true}, + {"~=1.0", "1.1.0.dev1", PreReleasesAuto, PreReleasesExclude, true, true}, + {"~=1.0", "1.1.0.dev1", PreReleasesAuto, PreReleasesInclude, false, true}, + {"~=1.0", "1.1.0.dev1", PreReleasesExclude, PreReleasesInclude, false, true}, + {"~=1.0", "1.1.0.dev1", PreReleasesExclude, PreReleasesExclude, false, false}, + {"~=1.0", "1.1.0.dev1", PreReleasesAuto, PreReleasesExclude, false, false}, + {"~=1.0", "1.1.0a1", PreReleasesAuto, PreReleasesAuto, true, true}, + {"~=1.0", "1.1.0b1", PreReleasesAuto, PreReleasesAuto, true, true}, + {"~=1.0", "1.1.0rc1", PreReleasesAuto, PreReleasesAuto, true, true}, + {"~=1.0", "1.1.0.post1.dev1", PreReleasesAuto, PreReleasesAuto, true, true}, + {">=1.0", "2.0.dev1", PreReleasesAuto, PreReleasesAuto, true, true}, + {"==1.*", "1.5.0a1", PreReleasesAuto, PreReleasesAuto, true, true}, + {">=1.0,<3.0", "2.0.dev1", PreReleasesAuto, PreReleasesAuto, true, true}, + {"!=2.0", "2.0.dev1", PreReleasesAuto, PreReleasesAuto, true, true}, + {"~=1.0", "3.0.0.dev1", PreReleasesAuto, PreReleasesAuto, true, false}, + {"~=1.0", "3.0.0.dev1", PreReleasesInclude, PreReleasesAuto, true, false}, + {"~=1.0", "3.0.0.dev1", PreReleasesAuto, PreReleasesInclude, true, false}, + {"~=1.0", "3.0.0.dev1", PreReleasesInclude, PreReleasesInclude, true, false}, + {"~=1.0", "3.0.0.dev1", PreReleasesExclude, PreReleasesExclude, true, false}, + {"~=1.0", "3.0.0.dev1", PreReleasesAuto, PreReleasesAuto, false, false}, + {">=2.0", "1.9.0.dev1", PreReleasesInclude, PreReleasesAuto, true, false}, + {">=2.0", "1.9.0.dev1", PreReleasesAuto, PreReleasesInclude, true, false}, + {">=2.0", "1.9.0.dev1", PreReleasesInclude, PreReleasesInclude, true, false}, + {">=2.0", "1.9.0.dev1", PreReleasesAuto, PreReleasesAuto, false, false}, + {">=1.0", "1.0.0.dev1", PreReleasesAuto, PreReleasesAuto, true, false}, + {"<=1.0", "1.0.0.dev1", PreReleasesAuto, PreReleasesAuto, true, true}, + {"<1.0", "1.0.0.dev1", PreReleasesAuto, PreReleasesAuto, true, false}, + {"<1.0", "0.9.0.dev1", PreReleasesAuto, PreReleasesAuto, true, true}, + {">=1.0.dev1", "1.0.0.dev1", PreReleasesAuto, PreReleasesAuto, true, true}, + {">=1.0.dev1", "1.0.0.dev1", PreReleasesExclude, PreReleasesExclude, false, false}, + {"==1.0.0.dev1", "1.0.0.dev1", PreReleasesExclude, PreReleasesExclude, false, false}, + {"~=1.0", "1.1.0", PreReleasesAuto, PreReleasesAuto, true, true}, + {"~=1.0", "1.1.0", PreReleasesExclude, PreReleasesExclude, false, true}, + {"~=1.0", "1.1.0", PreReleasesInclude, PreReleasesExclude, false, true}, + {"~=1.0", "1.1.0.dev1", PreReleasesInclude, PreReleasesAuto, false, true}, + {"~=1.0", "1.1.0.dev1", PreReleasesExclude, PreReleasesAuto, false, false}, + {"~=1.0", "1.1.0.dev1", PreReleasesInclude, PreReleasesExclude, false, false}, + {">=1.0.dev1", "1.0.0.dev1", PreReleasesAuto, PreReleasesExclude, false, false}, + {">=1.0.dev1", "1.0.0.dev1", PreReleasesExclude, PreReleasesAuto, false, false}, + } + for _, tt := range tests { + t.Run(fmt.Sprintf("%s__%s__%s__%s__inst=%t", tt.spec, tt.version, tt.specPre, tt.callPre, tt.installed), func(t *testing.T) { + ss, err := NewSpecifiers(tt.spec, WithPreReleases(tt.specPre)) + require.NoError(t, err) + if tt.installed { + assert.Equal(t, tt.want, ss.ContainsInstalled(tt.version, WithPreReleases(tt.callPre))) + return + } + assert.Equal(t, tt.want, ss.Contains(tt.version, WithPreReleases(tt.callPre))) + }) + } +} + +// Ported from TestSpecifierSet.test_specifier_filter. +func TestConformance_SetFilter(t *testing.T) { + tests := []struct { + spec string + specPre PreReleases + callPre PreReleases + input []string + want []string + }{ + {"", PreReleasesAuto, PreReleasesAuto, []string{"1.0", "2.0a1"}, []string{"1.0"}}, + {">=1.0.dev1", PreReleasesAuto, PreReleasesAuto, []string{"1.0", "2.0a1"}, []string{"1.0", "2.0a1"}}, + {"", PreReleasesAuto, PreReleasesAuto, []string{"1.0a1"}, []string{"1.0a1"}}, + {">=1.2.3", PreReleasesAuto, PreReleasesAuto, []string{"1.2", "1.5a1"}, []string{"1.5a1"}}, + {">=1.2.3", PreReleasesAuto, PreReleasesAuto, []string{"1.3", "1.5a1"}, []string{"1.3"}}, + {"", PreReleasesAuto, PreReleasesAuto, []string{"1.0", "2.0"}, []string{"1.0", "2.0"}}, + {">=1.0", PreReleasesAuto, PreReleasesAuto, []string{"2.0a1"}, []string{"2.0a1"}}, + {"!=2.0a1", PreReleasesAuto, PreReleasesAuto, []string{"1.0a2", "1.0", "2.0a1"}, []string{"1.0"}}, + {"==2.0a1", PreReleasesAuto, PreReleasesAuto, []string{"2.0a1"}, []string{"2.0a1"}}, + {">2.0a1", PreReleasesAuto, PreReleasesAuto, []string{"2.0a1", "3.0a2", "3.0"}, []string{"3.0a2", "3.0"}}, + {"<2.0a1", PreReleasesAuto, PreReleasesAuto, []string{"1.0a2", "1.0", "2.0a1"}, []string{"1.0a2", "1.0"}}, + {"~=2.0a1", PreReleasesAuto, PreReleasesAuto, []string{"1.0", "2.0a1", "3.0a2", "3.0"}, []string{"2.0a1"}}, + {"", PreReleasesAuto, PreReleasesExclude, []string{"1.0a1"}, nil}, + {">=1.0.dev1", PreReleasesAuto, PreReleasesExclude, []string{"1.0", "2.0a1"}, []string{"1.0"}}, + {"", PreReleasesAuto, PreReleasesInclude, []string{"1.0", "2.0a1"}, []string{"1.0", "2.0a1"}}, + {"", PreReleasesInclude, PreReleasesAuto, []string{"1.0", "2.0a1"}, []string{"1.0", "2.0a1"}}, + {"", PreReleasesExclude, PreReleasesAuto, []string{"1.0", "2.0a1"}, []string{"1.0"}}, + {">=1.0.dev1", PreReleasesInclude, PreReleasesAuto, []string{"1.0", "2.0a1"}, []string{"1.0", "2.0a1"}}, + {">=1.0.dev1", PreReleasesExclude, PreReleasesAuto, []string{"1.0", "2.0a1"}, []string{"1.0"}}, + {"", PreReleasesInclude, PreReleasesAuto, []string{"1.0a1"}, []string{"1.0a1"}}, + {"", PreReleasesExclude, PreReleasesAuto, []string{"1.0a1"}, nil}, + {">=1.0", PreReleasesInclude, PreReleasesExclude, []string{"1.0", "2.0a1"}, []string{"1.0"}}, + {">=1.0", PreReleasesExclude, PreReleasesInclude, []string{"1.0", "2.0a1"}, []string{"1.0", "2.0a1"}}, + {">=1.0", PreReleasesInclude, PreReleasesInclude, []string{"1.0", "2.0a1"}, []string{"1.0", "2.0a1"}}, + {">=1.0", PreReleasesExclude, PreReleasesExclude, []string{"1.0", "2.0a1"}, []string{"1.0"}}, + {">=1.0,<=2.0", PreReleasesAuto, PreReleasesAuto, []string{"1.0", "1.5a1"}, []string{"1.0"}}, + {">=1.0,<=2.0dev", PreReleasesAuto, PreReleasesAuto, []string{"1.0", "1.5a1"}, []string{"1.0", "1.5a1"}}, + {">=1.0,<=2.0", PreReleasesInclude, PreReleasesAuto, []string{"1.0", "1.5a1"}, []string{"1.0", "1.5a1"}}, + {">=1.0,<=2.0", PreReleasesExclude, PreReleasesAuto, []string{"1.0", "1.5a1"}, []string{"1.0"}}, + {">=1.0,<=2.0dev", PreReleasesExclude, PreReleasesAuto, []string{"1.0", "1.5a1"}, []string{"1.0"}}, + {">=1.0,<=2.0dev", PreReleasesInclude, PreReleasesAuto, []string{"1.0", "1.5a1"}, []string{"1.0", "1.5a1"}}, + {">=1.0,<=2.0", PreReleasesAuto, PreReleasesExclude, []string{"1.0", "1.5a1"}, []string{"1.0"}}, + {">=1.0,<=2.0", PreReleasesAuto, PreReleasesInclude, []string{"1.0", "1.5a1"}, []string{"1.0", "1.5a1"}}, + {">=1.0,<=2.0dev", PreReleasesAuto, PreReleasesExclude, []string{"1.0", "1.5a1"}, []string{"1.0"}}, + {">=1.0,<=2.0dev", PreReleasesAuto, PreReleasesInclude, []string{"1.0", "1.5a1"}, []string{"1.0", "1.5a1"}}, + {">=1.0,<=2.0", PreReleasesInclude, PreReleasesExclude, []string{"1.0", "1.5a1"}, []string{"1.0"}}, + {">=1.0,<=2.0", PreReleasesExclude, PreReleasesInclude, []string{"1.0", "1.5a1"}, []string{"1.0", "1.5a1"}}, + {">=1.0,<=2.0dev", PreReleasesInclude, PreReleasesExclude, []string{"1.0", "1.5a1"}, []string{"1.0"}}, + {">=1.0,<=2.0dev", PreReleasesExclude, PreReleasesInclude, []string{"1.0", "1.5a1"}, []string{"1.0", "1.5a1"}}, + {"", PreReleasesAuto, PreReleasesAuto, []string{"invalid version"}, []string{"invalid version"}}, + {"", PreReleasesAuto, PreReleasesExclude, []string{"invalid version"}, []string{"invalid version"}}, + {"", PreReleasesExclude, PreReleasesAuto, []string{"invalid version"}, []string{"invalid version"}}, + {"", PreReleasesAuto, PreReleasesAuto, []string{"1.0", "invalid version"}, []string{"1.0", "invalid version"}}, + {"", PreReleasesAuto, PreReleasesExclude, []string{"1.0", "invalid version"}, []string{"1.0", "invalid version"}}, + {"", PreReleasesExclude, PreReleasesAuto, []string{"1.0", "invalid version"}, []string{"1.0", "invalid version"}}, + {"===foobar", PreReleasesAuto, PreReleasesAuto, []string{"foobar", "foo", "bar"}, []string{"foobar"}}, + {"===foobar", PreReleasesAuto, PreReleasesAuto, []string{"foo", "bar"}, nil}, + {"===1.0", PreReleasesAuto, PreReleasesAuto, []string{"1.0", "1.0.0", "2.0"}, []string{"1.0"}}, + {"===1.0", PreReleasesAuto, PreReleasesAuto, []string{"1.0", "1.0+downstream1"}, []string{"1.0"}}, + {">=1.0,===foobar", PreReleasesAuto, PreReleasesAuto, []string{"foobar", "1.0", "2.0"}, nil}, + {"!=2.0,===foobar", PreReleasesAuto, PreReleasesAuto, []string{"foobar", "2.0", "bar"}, nil}, + {">=1.0,===1.5", PreReleasesAuto, PreReleasesAuto, []string{"1.0", "1.5", "2.0"}, []string{"1.5"}}, + {">=2.0,===1.5", PreReleasesAuto, PreReleasesAuto, []string{"1.0", "1.5", "2.0"}, nil}, + {"===foobar", PreReleasesAuto, PreReleasesAuto, []string{"foobar", "1.0", "invalid", "2.0a1"}, []string{"foobar"}}, + {"===1.0", PreReleasesAuto, PreReleasesAuto, []string{"1.0", "foobar", "invalid", "1.0.0"}, []string{"1.0"}}, + {">=1.0,===1.5", PreReleasesAuto, PreReleasesAuto, []string{"1.5", "foobar", "invalid"}, []string{"1.5"}}, + {"!=1.0", PreReleasesAuto, PreReleasesAuto, []string{"invalid", "foobar"}, nil}, + {"!=1.0", PreReleasesAuto, PreReleasesAuto, []string{"1.0", "invalid", "2.0"}, []string{"2.0"}}, + {"!=2.0.*", PreReleasesAuto, PreReleasesAuto, []string{"invalid", "foobar", "2.0"}, nil}, + {"!=2.0.*", PreReleasesAuto, PreReleasesAuto, []string{"1.0", "invalid", "2.0.0"}, []string{"1.0"}}, + {"!=1.0,!=2.0", PreReleasesAuto, PreReleasesAuto, []string{"invalid", "1.0", "2.0", "3.0"}, []string{"3.0"}}, + {">=1.0,!=2.0", PreReleasesAuto, PreReleasesAuto, []string{"invalid", "1.0", "2.0", "3.0"}, []string{"1.0", "3.0"}}, + {"===foobar", PreReleasesAuto, PreReleasesInclude, []string{"foobar", "foo"}, []string{"foobar"}}, + {"===foobar", PreReleasesAuto, PreReleasesExclude, []string{"foobar", "foo"}, []string{"foobar"}}, + {"===foobar", PreReleasesInclude, PreReleasesAuto, []string{"foobar", "foo"}, []string{"foobar"}}, + {"===foobar", PreReleasesExclude, PreReleasesAuto, []string{"foobar", "foo"}, []string{"foobar"}}, + {"===foobar", PreReleasesInclude, PreReleasesInclude, []string{"foobar", "foo"}, []string{"foobar"}}, + {"===foobar", PreReleasesExclude, PreReleasesExclude, []string{"foobar", "foo"}, []string{"foobar"}}, + {"===foobar,===foobar", PreReleasesAuto, PreReleasesAuto, []string{"foobar", "foo", "bar"}, []string{"foobar"}}, + {"==1.*,>=1.0", PreReleasesAuto, PreReleasesAuto, []string{"0.9", "1.0", "1.5", "2.0"}, []string{"1.0", "1.5"}}, + } + for _, tt := range tests { + t.Run(fmt.Sprintf("%q__%s__%s__%v", tt.spec, tt.specPre, tt.callPre, tt.input), func(t *testing.T) { + ss, err := NewSpecifiers(tt.spec, WithPreReleases(tt.specPre)) + require.NoError(t, err) + got := ss.Filter(tt.input, WithPreReleases(tt.callPre)) + if len(tt.want) == 0 { + assert.Empty(t, got) + return + } + assert.Equal(t, tt.want, got) + }) + } +} + +// Ported from TestSpecifierSet.test_specifierset_filter_with_key and +// test_empty_specifierset_filter_with_key -- upstream's `key=` parameter, +// which FilterBy provides. +func TestConformance_SetFilterWithKey(t *testing.T) { + type item struct{ version string } + + nonEmpty := []item{{"1.0"}, {"2.1a1"}} + emptySet := []item{{"2.0a1"}, {"2.1"}} + key := func(i item) string { return i.version } + + nonEmptyCases := []struct { + pre PreReleases + want []int + }{ + {PreReleasesAuto, []int{1}}, + {PreReleasesInclude, []int{1}}, + {PreReleasesExclude, nil}, + } + for _, tt := range nonEmptyCases { + t.Run("ge2__"+tt.pre.String(), func(t *testing.T) { + ss, err := NewSpecifiers(">=2") + require.NoError(t, err) + got := FilterBy(ss, nonEmpty, key, WithPreReleases(tt.pre)) + var want []item + for _, i := range tt.want { + want = append(want, nonEmpty[i]) + } + if want == nil { + assert.Empty(t, got) + return + } + assert.Equal(t, want, got) + }) + } + + emptyCases := []struct { + pre PreReleases + want []int + }{ + {PreReleasesAuto, []int{1}}, + {PreReleasesInclude, []int{0, 1}}, + {PreReleasesExclude, []int{1}}, + } + for _, tt := range emptyCases { + t.Run("empty__"+tt.pre.String(), func(t *testing.T) { + ss, err := NewSpecifiers("", WithPreReleases(tt.pre)) + require.NoError(t, err) + got := FilterBy(ss, emptySet, key) + var want []item + for _, i := range tt.want { + want = append(want, emptySet[i]) + } + assert.Equal(t, want, got) + }) + } +} + +// Ported from TestSpecifierSet.test_contains_exclusionary_bridges. +// +// When a set excludes whole ranges (!=1.*, !=2.*) the surviving region can +// contain only pre-release, dev or post versions. The pre-release preference +// must still offer those, because there is no final release in the gap to +// prefer -- which is the clearest demonstration that the rule is about +// SELECTION among candidates and not about what the operators match. +func TestConformance_SetContainsExclusionaryBridges(t *testing.T) { + tests := []struct { + spec string + pre PreReleases + version string + want bool + }{ + {">=1,!=1.*,!=2.*,!=3.0,<=3.0", PreReleasesAuto, "3.0.dev0", true}, + {">=1,!=1.*,!=2.*,!=3.0,<=3.0", PreReleasesAuto, "3.0a1", true}, + {">=1,!=1.*,!=2.*,!=3.0,<=3.0", PreReleasesInclude, "3.0.dev0", true}, + {">=1,!=1.*,!=2.*,!=3.0,<=3.0", PreReleasesInclude, "3.0a1", true}, + {">=1,!=1.*,!=2.*,!=3.0,<=3.0", PreReleasesExclude, "3.0.dev0", false}, + {">=1,!=1.*,!=2.*,!=3.0,<=3.0", PreReleasesExclude, "3.0a1", false}, + {">=1,!=1.*,!=2.*,!=3.0,<=3.0", PreReleasesAuto, "0.9", false}, + {">=1,!=1.*,!=2.*,!=3.0,<=3.0", PreReleasesAuto, "1.0", false}, + {">=1,!=1.*,!=2.*,!=3.0,<=3.0", PreReleasesAuto, "2.0", false}, + {">=1,!=1.*,!=2.*,!=3.0,<=3.0", PreReleasesAuto, "3.0", false}, + {">=1,!=1.*,!=2.*,!=3.0,<=3.0", PreReleasesAuto, "4.0", false}, + {">=1.0a1,!=1.*,!=2.*,<3.0", PreReleasesAuto, "1.0a1", false}, + {">=1.0a1,!=1.*,!=2.*,<3.0", PreReleasesAuto, "2.0a1", false}, + {">=1.0a1,!=1.*,!=2.*,<3.0", PreReleasesAuto, "3.0a1", false}, + {">=1.0a1,!=1.*,!=2.*,<3.0", PreReleasesInclude, "1.0a1", false}, + {">=1.0a1,!=1.*,!=2.*,<3.0", PreReleasesInclude, "2.0a1", false}, + {">=1.0a1,!=1.*,!=2.*,<3.0", PreReleasesExclude, "1.0a1", false}, + {">=1.0a1,!=1.*,!=2.*,<3.0", PreReleasesExclude, "2.0a1", false}, + {">=1.0.dev0,!=1.*,!=2.*,<3.0.dev0", PreReleasesAuto, "1.0.dev0", false}, + {">=1.0.dev0,!=1.*,!=2.*,<3.0.dev0", PreReleasesAuto, "2.0.dev0", false}, + {">=1.0.dev0,!=1.*,!=2.*,<3.0.dev0", PreReleasesAuto, "3.0.dev0", false}, + {">=1.0.dev0,!=1.*,!=2.*,<3.0.dev0", PreReleasesInclude, "1.0.dev0", false}, + {">=1.0.dev0,!=1.*,!=2.*,<3.0.dev0", PreReleasesInclude, "2.0.dev0", false}, + {">=1.0.dev0,!=1.*,!=2.*,<3.0.dev0", PreReleasesExclude, "1.0.dev0", false}, + {">=1.0,!=1.0,!=1.1,<2.0", PreReleasesAuto, "1.0.post1", true}, + {">=1.0,!=1.0,!=1.1,<2.0", PreReleasesAuto, "1.1.post1", true}, + {">=1.0,!=1.0,!=1.1,<2.0", PreReleasesAuto, "1.0", false}, + {">=1.0,!=1.0,!=1.1,<2.0", PreReleasesAuto, "1.1", false}, + {">=1.0,!=1.0,!=1.1,<2.0", PreReleasesAuto, "2.0", false}, + {">=1.0,!=1.0,!=1.1,<2.0", PreReleasesInclude, "1.0.post1", true}, + {">=1.0,!=1.0,!=1.1,<2.0", PreReleasesInclude, "1.1.post1", true}, + {">=1.0,!=1.0,!=1.1,<2.0", PreReleasesExclude, "1.0.post1", true}, + {">=1.0,!=1.0,!=1.1,<2.0", PreReleasesExclude, "1.1.post1", true}, + {">=1,!=1.*,!=2.*,!=3.0,!=3.1,<4", PreReleasesAuto, "3.0.dev0", true}, + {">=1,!=1.*,!=2.*,!=3.0,!=3.1,<4", PreReleasesAuto, "3.1.dev0", true}, + {">=1,!=1.*,!=2.*,!=3.0,!=3.1,<4", PreReleasesAuto, "0.5", false}, + {">=1,!=1.*,!=2.*,!=3.0,!=3.1,<4", PreReleasesAuto, "3.0", false}, + {">=1,!=1.*,!=2.*,!=3.0,!=3.1,<4", PreReleasesAuto, "3.1", false}, + {">=1,!=1.*,!=2.*,!=3.0,!=3.1,<4", PreReleasesAuto, "5.0", false}, + {">=1,!=1.*,!=2.*,!=3.0,!=3.1,<4", PreReleasesInclude, "3.0.dev0", true}, + {">=1,!=1.*,!=2.*,!=3.0,!=3.1,<4", PreReleasesInclude, "3.1.dev0", true}, + {">=1,!=1.*,!=2.*,!=3.0,!=3.1,<4", PreReleasesExclude, "3.0.dev0", false}, + {">=1,!=1.*,!=2.*,!=3.0,!=3.1,<4", PreReleasesExclude, "3.1.dev0", false}, + {">=1.0a1,!=1.0,<1.1", PreReleasesAuto, "1.0a1", true}, + {">=1.0a1,!=1.0,<1.1", PreReleasesAuto, "1.0b1", true}, + {">=1.0a1,!=1.0,<1.1", PreReleasesAuto, "0.9", false}, + {">=1.0a1,!=1.0,<1.1", PreReleasesAuto, "1.0", false}, + {">=1.0a1,!=1.0,<1.1", PreReleasesAuto, "1.1", false}, + {">=1.0a1,!=1.0,<1.1", PreReleasesAuto, "1.1.dev0", false}, + {">=1.0a1,!=1.0,<1.1", PreReleasesAuto, "1.1a1", false}, + {">=1.0a1,!=1.0,<1.1", PreReleasesInclude, "1.0a1", true}, + {">=1.0a1,!=1.0,<1.1", PreReleasesInclude, "1.0b1", true}, + {">=1.0a1,!=1.0,<1.1", PreReleasesInclude, "1.1.dev0", false}, + {">=1.0a1,!=1.0,<1.1", PreReleasesInclude, "1.1a1", false}, + {">=1.0a1,!=1.0,<1.1", PreReleasesExclude, "1.0a1", false}, + {">=1.0a1,!=1.0,<1.1", PreReleasesExclude, "1.0b1", false}, + {">=0.9,!=0.9,<=1.0", PreReleasesAuto, "0.9.post1", true}, + {">=0.9,!=0.9,<=1.0", PreReleasesAuto, "1.0", true}, + {">=0.9,!=0.9,<=1.0", PreReleasesAuto, "1.0.dev0", true}, + {">=0.9,!=0.9,<=1.0", PreReleasesAuto, "1.0a1", true}, + {">=0.9,!=0.9,<=1.0", PreReleasesAuto, "1.0.post1", false}, + {">=0.9,!=0.9,<=1.0", PreReleasesInclude, "0.9.post1", true}, + {">=0.9,!=0.9,<=1.0", PreReleasesInclude, "1.0.dev0", true}, + {">=0.9,!=0.9,<=1.0", PreReleasesInclude, "1.0a1", true}, + {">=0.9,!=0.9,<=1.0", PreReleasesInclude, "1.0", true}, + {">=0.9,!=0.9,<=1.0", PreReleasesExclude, "0.9.post1", true}, + {">=0.9,!=0.9,<=1.0", PreReleasesExclude, "1.0.dev0", false}, + {">=0.9,!=0.9,<=1.0", PreReleasesExclude, "1.0a1", false}, + {">=0.9,!=0.9,<=1.0", PreReleasesExclude, "1.0", true}, + {">=1!0,!=1!1.*,!=1!2.*,<1!3", PreReleasesAuto, "1!0.5", true}, + {">=1!0,!=1!1.*,!=1!2.*,<1!3", PreReleasesAuto, "1!2.5", false}, + {">=1!0,!=1!1.*,!=1!2.*,<1!3", PreReleasesAuto, "0!5.0", false}, + {">=1!0,!=1!1.*,!=1!2.*,<1!3", PreReleasesAuto, "2!0.0", false}, + {">=1!0,!=1!1.*,!=1!2.*,<1!3", PreReleasesInclude, "1!0.5", true}, + {">=1!0,!=1!1.*,!=1!2.*,<1!3", PreReleasesInclude, "0!5.0", false}, + {">=1!0,!=1!1.*,!=1!2.*,<1!3", PreReleasesExclude, "1!0.5", true}, + {">=1!0,!=1!1.*,!=1!2.*,<1!3", PreReleasesExclude, "0!5.0", false}, + {">1.0.dev1,==1.0.post0", PreReleasesAuto, "1.0.post0", true}, + {">1.0.dev1,==1.0.post1", PreReleasesAuto, "1.0.post1", true}, + {">1.0a1,==1.0.post0", PreReleasesAuto, "1.0.post0", true}, + {">1.0.dev1,<=2.0", PreReleasesAuto, "1.0.post0", true}, + {">1.0.dev1,<=2.0", PreReleasesAuto, "1.0", true}, + {">1.0a1,<=2.0", PreReleasesInclude, "1.0a1.post0", false}, + {">1.0.dev1,<=1.0.post1", PreReleasesAuto, "1.0.post0", true}, + {">1.0.dev1,<=1.0.post1", PreReleasesAuto, "1.0.post1", true}, + {">1.0.dev1,<=1.0.post1", PreReleasesAuto, "1.0", true}, + {">1.0.dev1,!=1.0,<=2.0", PreReleasesAuto, "1.0.post0", true}, + {">1.0.dev1,!=1.0,!=1.0.post0,<=2.0", PreReleasesAuto, "1.0.post1", true}, + {"==1.0.dev0,<1.0.post1", PreReleasesAuto, "1.0.dev0", true}, + {"==1.0a1,<1.0.post0", PreReleasesAuto, "1.0a1", true}, + {"==1.0.post0.dev0,<1.0.post1", PreReleasesAuto, "1.0.post0.dev0", true}, + {">=1.0,<1.0.post1", PreReleasesAuto, "1.0", true}, + {">=1.0,<1.0.post1", PreReleasesAuto, "1.0.post0", true}, + {">=1.0,<1.0.post1", PreReleasesInclude, "1.0.dev0", false}, + {">=1.0.dev0,<1.0.post1", PreReleasesInclude, "1.0.dev0", true}, + {">=1.0.dev0,<1.0.post1", PreReleasesInclude, "1.0.a1", true}, + {">=1.0.dev0,<1.0.post1", PreReleasesInclude, "1.0.post0.dev0", true}, + {">=1.0.dev0,<1.0.post1,!=1.0,!=1.0.post0", PreReleasesInclude, "1.0.dev0", true}, + {">=1.0.dev0,<1.0.post1,!=1.0,!=1.0.post0", PreReleasesInclude, "1.0.post0.dev0", true}, + {">=1.0.dev0,<1.0.post2,!=1.0,!=1.0.post0", PreReleasesInclude, "1.0.post1", true}, + {">=1.0.dev0,<1.0.post2,!=1.0", PreReleasesInclude, "1.0.post0", true}, + } + for _, tt := range tests { + t.Run(fmt.Sprintf("%s__%s__%s", tt.spec, tt.version, tt.pre), func(t *testing.T) { + ss, err := NewSpecifiers(tt.spec) + require.NoError(t, err) + assert.Equal(t, tt.want, ss.Contains(tt.version, WithPreReleases(tt.pre))) + }) + } +} + +// Ported from TestSpecifierSet.test_filter_exclusionary_bridges. +func TestConformance_SetFilterExclusionaryBridges(t *testing.T) { + tests := []struct { + spec string + pre PreReleases + input []string + want []string + }{ + {">=1,!=1.*,!=2.*,!=3.0,<=3.0", PreReleasesAuto, []string{"3.0.dev0", "3.0a1"}, []string{"3.0.dev0", "3.0a1"}}, + {">=1,!=1.*,!=2.*,!=3.0,<=3.0", PreReleasesAuto, []string{"0.9", "3.0.dev0", "3.0a1", "4.0"}, []string{"3.0.dev0", "3.0a1"}}, + {">=1,!=1.*,!=2.*,!=3.0,<=3.0", PreReleasesInclude, []string{"0.9", "3.0.dev0", "3.0a1", "4.0"}, []string{"3.0.dev0", "3.0a1"}}, + {">=1,!=1.*,!=2.*,!=3.0,<=3.0", PreReleasesExclude, []string{"0.9", "3.0.dev0", "3.0a1", "4.0"}, nil}, + {">=1.0a1,!=1.*,!=2.*,<3.0", PreReleasesAuto, []string{"1.0a1", "2.0a1", "3.0a1"}, nil}, + {">=1.0a1,!=1.*,!=2.*,<3.0", PreReleasesInclude, []string{"1.0a1", "2.0a1", "3.0a1"}, nil}, + {">=1.0a1,!=1.*,!=2.*,<3.0", PreReleasesExclude, []string{"1.0a1", "2.0a1", "3.0a1"}, nil}, + {">=1.0.dev0,!=1.*,!=2.*,<3.0.dev0", PreReleasesAuto, []string{"1.0.dev0", "2.0.dev0", "3.0.dev0"}, nil}, + {">=1.0.dev0,!=1.*,!=2.*,<3.0.dev0", PreReleasesInclude, []string{"1.0.dev0", "2.0.dev0", "3.0.dev0"}, nil}, + {">=1.0.dev0,!=1.*,!=2.*,<3.0.dev0", PreReleasesExclude, []string{"1.0.dev0", "2.0.dev0", "3.0.dev0"}, nil}, + {">=1.0,!=1.0,!=1.1,<2.0", PreReleasesAuto, []string{"1.0.post1", "1.1.post1"}, []string{"1.0.post1", "1.1.post1"}}, + {">=1.0,!=1.0,!=1.1,<2.0", PreReleasesAuto, []string{"0.9", "1.0.post1", "1.1.post1", "2.0"}, []string{"1.0.post1", "1.1.post1"}}, + {">=1.0,!=1.0,!=1.1,<2.0", PreReleasesInclude, []string{"0.9", "1.0.post1", "1.1.post1", "2.0"}, []string{"1.0.post1", "1.1.post1"}}, + {">=1.0,!=1.0,!=1.1,<2.0", PreReleasesExclude, []string{"0.9", "1.0.post1", "1.1.post1", "2.0"}, []string{"1.0.post1", "1.1.post1"}}, + {">=1,!=1.*,!=2.*,!=3.0,!=3.1,<4", PreReleasesAuto, []string{"3.0.dev0", "3.1.dev0"}, []string{"3.0.dev0", "3.1.dev0"}}, + {">=1,!=1.*,!=2.*,!=3.0,!=3.1,<4", PreReleasesAuto, []string{"0.5", "3.0.dev0", "3.1.dev0", "5.0"}, []string{"3.0.dev0", "3.1.dev0"}}, + {">=1,!=1.*,!=2.*,!=3.0,!=3.1,<4", PreReleasesInclude, []string{"0.5", "3.0.dev0", "3.1.dev0", "5.0"}, []string{"3.0.dev0", "3.1.dev0"}}, + {">=1,!=1.*,!=2.*,!=3.0,!=3.1,<4", PreReleasesExclude, []string{"0.5", "3.0.dev0", "3.1.dev0", "5.0"}, nil}, + {">=1.0a1,!=1.0,<1.1", PreReleasesAuto, []string{"1.0a1", "1.0b1"}, []string{"1.0a1", "1.0b1"}}, + {">=1.0a1,!=1.0,<1.1", PreReleasesAuto, []string{"0.9", "1.0a1", "1.0b1", "1.1"}, []string{"1.0a1", "1.0b1"}}, + {">=1.0a1,!=1.0,<1.1", PreReleasesAuto, []string{"1.0a1", "1.0b1", "1.1.dev0", "1.1a1"}, []string{"1.0a1", "1.0b1"}}, + {">=1.0a1,!=1.0,<1.1", PreReleasesInclude, []string{"0.9", "1.0a1", "1.0b1", "1.1"}, []string{"1.0a1", "1.0b1"}}, + {">=1.0a1,!=1.0,<1.1", PreReleasesInclude, []string{"1.0a1", "1.0b1", "1.1.dev0", "1.1a1"}, []string{"1.0a1", "1.0b1"}}, + {">=1.0a1,!=1.0,<1.1", PreReleasesExclude, []string{"0.9", "1.0a1", "1.0b1", "1.1"}, nil}, + {">=0.9,!=0.9,<=1.0", PreReleasesAuto, []string{"0.9.post1", "1.0.dev0", "1.0a1", "1.0"}, []string{"0.9.post1", "1.0"}}, + {">=0.9,!=0.9,<=1.0", PreReleasesAuto, []string{"0.9.post1", "1.0.dev0", "1.0a1", "1.0", "1.0.post1"}, []string{"0.9.post1", "1.0"}}, + {">=0.9,!=0.9,<=1.0", PreReleasesInclude, []string{"0.9.post1", "1.0.dev0", "1.0a1", "1.0", "1.1"}, []string{"0.9.post1", "1.0.dev0", "1.0a1", "1.0"}}, + {">=0.9,!=0.9,<=1.0", PreReleasesExclude, []string{"0.9.post1", "1.0.dev0", "1.0a1", "1.0", "1.1"}, []string{"0.9.post1", "1.0"}}, + {">=1!0,!=1!1.*,!=1!2.*,<1!3", PreReleasesAuto, []string{"1!0.5", "1!2.5"}, []string{"1!0.5"}}, + {">=1!0,!=1!1.*,!=1!2.*,<1!3", PreReleasesAuto, []string{"0!5.0", "1!0.5", "1!2.5", "2!0.0"}, []string{"1!0.5"}}, + {">=1!0,!=1!1.*,!=1!2.*,<1!3", PreReleasesInclude, []string{"0!5.0", "1!0.5", "1!2.5", "2!0.0"}, []string{"1!0.5"}}, + {">=1!0,!=1!1.*,!=1!2.*,<1!3", PreReleasesExclude, []string{"0!5.0", "1!0.5", "1!2.5", "2!0.0"}, []string{"1!0.5"}}, + } + for _, tt := range tests { + t.Run(fmt.Sprintf("%s__%s__%v", tt.spec, tt.pre, tt.input), func(t *testing.T) { + ss, err := NewSpecifiers(tt.spec) + require.NoError(t, err) + got := ss.Filter(tt.input, WithPreReleases(tt.pre)) + if len(tt.want) == 0 { + assert.Empty(t, got) + return + } + assert.Equal(t, tt.want, got) + }) + } +} + +// Ported from TestSpecifierSet.test_contains_post_release_boundary_intersection. +// +// ">V" excludes EVERY post-release of V, and intersecting it with +// "!=V.postN" must not re-admit a different post-release of V. Upstream also +// asserts set membership equals the conjunction of its members; that check is +// carried over, because a set that disagreed with its own members would be a +// bug the table alone would not catch. +func TestConformance_SetPostReleaseBoundaryIntersection(t *testing.T) { + tests := []struct { + spec string + version string + want bool + }{ + {">1.0,!=1.0.post1", "1.0.post2", false}, + {">1.0,!=1.0.post1", "1.0.post2.dev3", false}, + {">1.0,!=1.0.post1", "1.0.post1", false}, + {">1.0,!=1.0.post1", "1.1", true}, + {">2.3,!=2.3.post1", "2.3.post2", false}, + {">1!1.0,!=1!1.0.post1", "1!1.0.post2", false}, + } + for _, tt := range tests { + t.Run(tt.spec+"__"+tt.version, func(t *testing.T) { + ss, err := NewSpecifiers(tt.spec) + require.NoError(t, err) + got := ss.Contains(tt.version, WithPreReleases(PreReleasesInclude)) + assert.Equal(t, tt.want, got) + + conjunction := true + for _, s := range ss.List() { + if !s.Contains(tt.version, WithPreReleases(PreReleasesInclude)) { + conjunction = false + } + } + assert.Equal(t, conjunction, got, "the set must agree with its members") + }) + } +} + +// Ported from TestSpecifierSet.test_filter_post_release_boundary_intersection. +func TestConformance_SetFilterPostReleaseBoundaryIntersection(t *testing.T) { + ss, err := NewSpecifiers(">1.0,!=1.0.post1") + require.NoError(t, err) + got := ss.Filter([]string{"1.0.post2", "1.0.post3", "1.1"}, WithPreReleases(PreReleasesInclude)) + assert.Equal(t, []string{"1.1"}, got) +} + +// Ported from TestSpecifierSet.test_contains_arbitrary_equality_contains. +func TestConformance_SetArbitraryEqualityContains(t *testing.T) { + tests := []struct { + spec string + item string + want bool + }{ + {"===foobar", "foobar", true}, + {"===foobar", "foo", false}, + {"===foobar", "bar", false}, + {"===1.0", "1.0", true}, + {"===1.0", "1.0.0", false}, + {"===1.0+downstream1", "1.0", false}, + {"===1.0", "1.0+downstream1", false}, + {"===foobar,!=1.0", "foobar", false}, + {"===foobar,!=1.0", "1.0", false}, + {">=1.0,===foobar", "foobar", false}, + {">=1.0,===1.5", "1.5", true}, + {">=2.0,===1.5", "1.5", false}, + {">=1.0,===2.5", "2.5", true}, + {"!=1.0", "invalid", false}, + {"!=1.0", "foobar", false}, + {"!=2.0.*", "invalid", false}, + {"!=1.0,!=2.0", "invalid", false}, + {">=1.0,!=2.0", "foobar", false}, + {">=1.0,!=2.0", "1.5", true}, + } + for _, tt := range tests { + t.Run(tt.spec+"__"+tt.item, func(t *testing.T) { + ss, err := NewSpecifiers(tt.spec) + require.NoError(t, err) + assert.Equal(t, tt.want, ss.Contains(tt.item)) + }) + } +} + +// Ported from TestSpecifierSet.test_comparison_ignores_local: a local segment +// on the PROSPECTIVE version is ignored by the ordered comparisons, because a +// specifier may not name one. +func TestConformance_SetIgnoresLocalOnProspective(t *testing.T) { + tests := []struct { + version string + spec string + want bool + }{ + {"1.0.0+local", "==1.0.0", true}, + {"1.0.0+local", "!=1.0.0", false}, + {"1.0.0+local", "<=1.0.0", true}, + {"1.0.0+local", ">=1.0.0", true}, + {"1.0.0+local", "<1.0.0", false}, + {"1.0.0+local", ">1.0.0", false}, + } + for _, tt := range tests { + t.Run(tt.version+"__"+tt.spec, func(t *testing.T) { + ss, err := NewSpecifiers(tt.spec) + require.NoError(t, err) + v, err := Parse(tt.version) + require.NoError(t, err) + assert.Equal(t, tt.want, ss.ContainsVersion(v)) + }) + } +} + +// Ported from TestSpecifierSet.test_contains_with_compatible_operator. Both +// members survive the combination precisely because `~=1.18.0` and `~=1.18` +// are NOT equal, so the narrower one still applies. +func TestConformance_SetContainsWithCompatibleOperator(t *testing.T) { + narrow, err := NewSpecifiers("~=1.18.0") + require.NoError(t, err) + wide, err := NewSpecifiers("~=1.18") + require.NoError(t, err) + + combination, err := narrow.And(wide) + require.NoError(t, err) + assert.False(t, combination.Contains("1.19.5")) + assert.True(t, combination.Contains("1.18.0")) +} + +// Ported from TestSpecifierSet.test_contains_rejects_invalid_specifier: a +// string that is not a PEP 440 version cannot satisfy an ordinary specifier. +func TestConformance_SetContainsRejectsInvalidVersion(t *testing.T) { + ss, err := NewSpecifiers(">=1.0", WithPreReleases(PreReleasesInclude)) + require.NoError(t, err) + assert.False(t, ss.Contains("not a valid version")) +} + +// Ported from TestSpecifierSet.test_comparison_true / test_comparison_false: +// the SPECIFIERS cross-product at the SET level. +func TestConformance_SetEqualityMatrix(t *testing.T) { + for i, left := range conformanceValidSpecifiers { + for j, right := range conformanceValidSpecifiers { + t.Run(left+"__"+right, func(t *testing.T) { + l, err := NewSpecifiers(left) + require.NoError(t, err) + r, err := NewSpecifiers(right) + require.NoError(t, err) + assert.Equal(t, i == j, l.Equal(r)) + }) + } + } +} + +// Ported from TestSpecifierSet.test_specifiers_combine, +// test_specifiers_combine_deduplicates and test_specifiers_duplicate_normalization. +func TestConformance_SetCombine(t *testing.T) { + left, err := NewSpecifiers(">=1.0.0,!=1.0.1") + require.NoError(t, err) + right, err := NewSpecifiers("<=2.0.0,!=2.0.1") + require.NoError(t, err) + + got, err := left.And(right) + require.NoError(t, err) + assert.Equal(t, 4, got.Len()) + + // ⚠️ Upstream renders the members SORTED ("!=1.0.1,!=2.0.1,<=2.0.0,>=1.0.0"). + // This package preserves input order, a documented divergence, so the + // membership is asserted rather than the rendering. + expect, err := NewSpecifiers(">=1.0.0,!=1.0.1,<=2.0.0,!=2.0.1") + require.NoError(t, err) + assert.True(t, got.Equal(expect)) + + // Duplicates are kept, exactly as upstream keeps them: in packaging 26.2, + // len(SpecifierSet(">=1.0") & SpecifierSet(">=1.0")) is 2. Equal is the + // operation that is blind to them. + one, err := NewSpecifiers(">=1.0") + require.NoError(t, err) + dup, err := one.And(one) + require.NoError(t, err) + assert.Equal(t, 2, dup.Len()) + assert.True(t, dup.Equal(one)) +} diff --git a/version/version.go b/version/version.go index 4fd5b82..77bd29b 100644 --- a/version/version.go +++ b/version/version.go @@ -486,6 +486,55 @@ func (v Version) IsPreRelease() bool { return !v.pre.isNull() || !v.dev.isNull() } +// postBase returns the version this one is a post-release of: the same version +// with its post, dev and local segments dropped. Ported from pypa/packaging +// 26.2's _post_base. +// +// 1.0.post1 -> 1.0 +// 1.0a1.post0 -> 1.0a1 +// 1.0.post0.dev1 -> 1.0 +// +// It is derived by re-parsing the rendered prefix rather than by editing the +// struct, because the comparison key is precomputed at Parse time and a +// hand-edited copy would carry a key describing the version it used to be. +func (v Version) postBase() (Version, bool) { + var buf bytes.Buffer + if v.epoch.Compare(part.Zero) == 1 { + fmt.Fprintf(&buf, "%s!", v.epoch) + } + writeRelease(&buf, v.release) + if !v.pre.isNull() { + fmt.Fprintf(&buf, "%s%s", v.pre.letter, v.pre.number) + } + return parseOperand(buf.String()) +} + +// earliestPreRelease returns the earliest pre-release of this version: the same +// version with dev set to 0 and the local segment dropped. Ported from +// pypa/packaging 26.2's _earliest_prerelease. +// +// 1.2 -> 1.2.dev0 +// 1.2.post1 -> 1.2.post1.dev0 +// +// It is the lower bound of "a pre-release of V", which is what PEP 440's " Date: Mon, 10 Aug 2026 12:10:35 -0400 Subject: [PATCH 2/4] Drop blank comma items, and record two changes that shipped undocumented MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Two things, both asked for in review. 1. COMMA TOLERANCE (the last listed #19391 divergence) A blank comma-separated item is now dropped rather than rejected, which is upstream's rule: SpecifierSet.__init__ does `[s.strip() for s in specifiers.split(",") if s.strip()]`. So ",>=1", ",,>=1", ">=1,,<2" and ">=1 , , <2" now parse, and a comma-only input (",", ",,", " , ") is the universal set -- exactly where packaging 26.2 lands, verified case by case rather than inferred. Measured over the production PyPI corpus plus real CRAN constraints (28,528 distinct inputs, both entry points): 0 newly rejected and 0 newly accepted. No real constraint in either corpus has a leading or doubled comma, so this is a pure conformance fix with no observable effect on real data. The harness was positive-controlled: all six synthetic comma shapes flip reject->accept on both paths, while ">=1<2" stays rejected. ⚠️ Two safety properties preserved, both tested: - The comma BETWEEN two constraints is still required. Only blank items are dropped, and what survives is re-joined with commas and revalidated, so ">=1<2" and ",,>=1 <2,," stay errors. - The tolerance stops at the "||" boundary, and this is the one place the package deliberately stops short of upstream. SpecifierSet(",") is universal upstream and NewSpecifiers(",") is universal here, but ">=1||," is an ERROR. Upstream has no "||" and so has no opinion; composing "blank items are dropped" with "an empty group is universal" per SEGMENT would let a stray comma disable the whole constraint -- the same hole a stray "||" opened, reached by a different typo. A specifier-less group is universal only when it is the entire input. The "||" control test is rewritten around that interaction rather than around the old rejected-comma behavior, so it still isolates the two concerns. The newly-valid forms are in the valid table with the upstream citation and the pinned SHA. 2. TWO CHANGES THAT SHIPPED WITH NO CHANGELOG ENTRY Both landed in v0.1.0 (commit d097655) and appear nowhere in CHANGELOG.md. Verified: `git tag --contains d097655` starts at v0.1.0, and the file has never mentioned either. - padVersion derived the right-hand version's remainder by slicing the LEFT-hand version, so every ==X.*, !=X.* and ~= comparison with differently-sized release segments could answer wrongly. 349 of 135,204 specifier sets flip over the production PyPI corpus; of 928 flipped pairs, packaging 26.2 agrees with the new behavior 915 times and the old 13 (those 13 are a separate pre-existing epoch divergence). - NewRSpecifiers discarded its sanitizer argument. 0 differences over real CRAN (2,814 constraints), 1,447 of 135,204 adversarially, and ⚠️ invisible in String(), which renders pre-sanitize text. Recorded under [Unreleased] in a labelled subsection naming the version they actually shipped in, NOT backdated into the tagged [0.1.0] section. Also states what the omission implies: this module has shipped unrecorded semantic changes, so an upgrade should be assessed by diffing behavior on real input, which is how these two were found. Refs rstudio/package-manager#19391, rstudio/package-manager#18634 Co-Authored-By: Claude Opus 5 (1M context) --- CHANGELOG.md | 77 +++++++++++++++++++++ version/adjacent_constraint_test.go | 10 +++ version/or_group_structure_test.go | 100 +++++++++++++++++++++++----- version/specifier.go | 63 ++++++++++++++---- 4 files changed, 218 insertions(+), 32 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c38875e..3a69434 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,57 @@ mistaken for a safe patch upgrade. ## [Unreleased] +### Previously undocumented, shipped in 0.1.0 + +> ⚠️ **Recorded late.** The two changes below shipped in **`v0.1.0`** (commit +> `d097655`) and appear nowhere in this file. They are documented here rather than +> in the `[0.1.0]` section because that release is tagged and the Go module proxy +> has served it; **a published section is immutable** and backdating into one would +> claim the release contained a note it did not. See the note at the end of this +> section about what that implies for anyone assessing an upgrade. +> +> They were found by the PPM migration work (rstudio/package-manager#18634) by +> **diffing the two modules on identical input**, not by reading this changelog — +> which is exactly why they were found at all. Measured figures below come from +> that exercise. + +- **Prefix matching and `~=` returned wrong answers, on both the Python and R + paths.** `padVersion` derived the right-hand version's non-numeric remainder by + slicing the **left**-hand version (`rightRest := left[len(rightRelease):]`), so + the padded right-hand side was assembled partly out of the left-hand version's + segments. This affected every `==X.*`, `!=X.*` and `~=` comparison whose two + sides had release segments of different lengths. + + **Measured over the production PyPI corpus: 349 of 135,204 specifier sets change + their answer.** Of 928 flipped (specifier, version) pairs, `pypa/packaging` 26.2 + agrees with the **new** behavior **915** times and with the old **13** times — + so it was a genuine bug fix, not a behavior swap. Those 13 are a separate, + pre-existing epoch divergence (`~=0.0.0
` against `1!1.0`) that this change
+  neither caused nor fixed.
+
+- **`NewRSpecifiers` silently discarded the sanitizer argument it was given** and
+  forwarded an identity function instead, so a caller normalizing a non-standard
+  version token before PEP 440 parsing had that normalization ignored. Four CRAN
+  call sites in PPM pass a sanitizer.
+
+  **Measured: 0 differences over real CRAN data** (2,814 constraints), because a
+  difference requires a character outside `[0-9.]` in the version operand and real
+  CRAN constraints contain none. On an adversarial corpus, 1,447 of 135,204 change.
+
+  ⚠️ This one is **invisible in `String()`**, which renders the pre-sanitize text.
+  A consumer comparing rendered output would have seen no difference at all while
+  the matching answers changed underneath it — so the natural way to check for it
+  does not work.
+
+> ### ⚠️ If you are assessing an upgrade, diff behavior — do not read this file
+>
+> This module has now demonstrably shipped **semantic changes with no changelog
+> entry**, and the two above were the largest observable effects in the whole
+> `v0.1.1` → `v0.4.0` range. One of them was additionally invisible in `String()`.
+> A changelog is a best-effort human artifact and CI does not validate it; the only
+> reliable method is to run both versions over your own inputs and compare, which
+> is how these were found.
+
 ### Breaking
 
 - **A wildcard may no longer be combined with a pre-release.** `==1.0a1.*`,
@@ -149,6 +200,32 @@ mistaken for a safe patch upgrade.
   fix covered only the zero-value/no-groups arm; this is the empty-input arm that
   survived it.
 
+- **A blank comma-separated item is now dropped rather than rejected**, matching
+  upstream. `",>=1"`, `",,>=1"`, `">=1,,<2"` and `">=1 , , <2"` previously errored and
+  now parse; `packaging` 26.2 accepts all of them, because
+  `SpecifierSet.__init__` does `[s.strip() for s in specifiers.split(",") if s.strip()]`.
+  A comma-only input (`","`, `",,"`, `" , "`) is the universal set, which is also what
+  `SpecifierSet(",")` is. A trailing comma was already tolerated and still is.
+
+  This is a widening. Measured over the production PyPI corpus plus real CRAN
+  constraints (28,528 distinct inputs across both paths): **0 newly rejected and 0
+  newly accepted** — no real constraint in either corpus has a leading or doubled
+  comma, so this is a pure conformance fix with no observable effect on real data.
+  The harness was positive-controlled: the six synthetic comma shapes all flip from
+  rejected to accepted on both paths.
+
+  ⚠️ **The comma between two constraints is still required.** `">=1<2"` and
+  `">=1 <2"` remain errors (see 0.4.0). Only *blank* items are dropped, and what
+  survives is re-joined with commas and revalidated, so dropping them cannot smuggle
+  an adjacent pair past the check.
+
+  ⚠️ **The tolerance stops at the `||` boundary**, and this is the one place the
+  package deliberately stops short of upstream. `">=1||,"` is an **error**, not
+  universal. Upstream has no `||` and so has no opinion; but since `||` is an OR and
+  an empty AND-group matches everything, treating a comma-only *segment* as universal
+  would let a stray comma disable the whole constraint — the same failure mode as a
+  stray `||`, described below.
+
   ⚠️ **Only a wholly blank input is universal.** A blank `||` segment is an error:
   `">=1||"`, `"||>=1"` and `">=1||||<2"` are rejected. Since `||` is an OR and an empty
   AND-group matches everything, allowing one would mean a trailing-`||` typo *disabled*
diff --git a/version/adjacent_constraint_test.go b/version/adjacent_constraint_test.go
index 09d0071..7adcdb2 100644
--- a/version/adjacent_constraint_test.go
+++ b/version/adjacent_constraint_test.go
@@ -86,6 +86,16 @@ func TestCommaRequirementDoesNotNarrowValidForms(t *testing.T) {
 		{"!=3.2-2, >3.0.0", "PPM's R multi-constraint test shape"},
 		{"> 2.0.0, < 3.0.0, != 2.4.2", "three comma-separated constraints"},
 		{">= 1.0,", "trailing comma, which upstream also accepts"},
+		// Upstream drops every blank comma-split item, not just a trailing one:
+		// `[s.strip() for s in specifiers.split(",") if s.strip()]` in
+		// SpecifierSet.__init__ (pypa/packaging @ 4eb0753, release 26.2).
+		// Verified: SpecifierSet(",>=1") and SpecifierSet(">=1,,<2") both parse,
+		// to lengths 1 and 2.
+		{",>= 1.0", "leading comma, which upstream also accepts"},
+		{",,>= 1.0", "doubled leading comma"},
+		{">= 1.0,, < 2.0", "doubled comma between constraints"},
+		{">= 1.0 , , < 2.0", "doubled comma with surrounding spaces"},
+		{",", "comma-only input: the universal set, as SpecifierSet(\",\") is"},
 		{"*", "the any-version shorthand"},
 		{"===lolwat", "arbitrary equality takes an opaque operand"},
 		{">=1.0 , <2.0", "spaces around the comma"},
diff --git a/version/or_group_structure_test.go b/version/or_group_structure_test.go
index bba8617..ff690a1 100644
--- a/version/or_group_structure_test.go
+++ b/version/or_group_structure_test.go
@@ -79,16 +79,20 @@ func TestEmptyOrSegmentCannotDisableAConstraint(t *testing.T) {
 	}
 }
 
-// PRESERVED-BEHAVIOR CONTROL. The "||" fix must not disturb the empty-input
-// universal set, nor comma handling inside a group.
+// CONTROL: the "||" rule and the comma rule are separate, and each must hold
+// without weakening the other.
 //
-// ⚠️ Note what the comma rows actually assert. `packaging` 26.2 ACCEPTS ",>=1"
-// and ">=1,,<2" (it drops empty comma-split items) and this package rejects
-// both -- a real, still-open divergence tracked in
-// rstudio/package-manager#19391 and NOT fixed here. The rows pin the current
-// behavior so that the "||" change is demonstrably orthogonal to it; they are
-// not a claim that rejecting them is correct.
-func TestOrFixPreservesEmptyInputAndCommaHandling(t *testing.T) {
+// These interact in a way that is easy to get wrong, which is why they are
+// tested together. Blank comma items are DROPPED (upstream's rule), and a group
+// left with no items at all is the universal set (also upstream's rule --
+// `SpecifierSet(",")` is universal). Compose those two naively and a comma-only
+// "||" SEGMENT becomes an empty AND-group, which matches everything, which
+// disables the whole constraint -- the same hole a trailing "||" opened, reached
+// by a comma instead.
+//
+// So the rules are: commas are tolerated freely WITHIN a group, and a
+// specifier-less group is universal ONLY when it is the entire input.
+func TestCommaToleranceDoesNotCrossTheOrBoundary(t *testing.T) {
 	t.Run("empty input is still the universal set", func(t *testing.T) {
 		for _, in := range []string{"", "   ", "\t"} {
 			ss, err := NewSpecifiers(in)
@@ -105,17 +109,77 @@ func TestOrFixPreservesEmptyInputAndCommaHandling(t *testing.T) {
 		}
 	})
 
-	t.Run("comma handling is unchanged", func(t *testing.T) {
-		// Rejected before this change and rejected after it. See the divergence
-		// note above.
-		for _, in := range []string{",>=1", ">=1,,<2"} {
-			_, err := NewSpecifiers(in)
-			assert.Error(t, err, "%q: still rejected (upstream accepts; #19391)", in)
+	t.Run("blank comma items are dropped within a group", func(t *testing.T) {
+		// Every one of these is accepted by packaging 26.2 and now here.
+		tests := []struct {
+			in      string
+			wantLen int
+		}{
+			{",>=1", 1},
+			{",,>=1", 1},
+			{">=1,", 1},
+			{">=1,,<2", 2},
+			{">=1,,,<2", 2},
+			{">=1 , , <2", 2},
+		}
+		for _, tt := range tests {
+			ss, err := NewSpecifiers(tt.in)
+			require.NoError(t, err, "%q must be accepted, as upstream accepts it", tt.in)
+			assert.Equal(t, tt.wantLen, ss.Len(), "input %q", tt.in)
+			// The dropped commas must not turn into a wider constraint.
+			assert.False(t, ss.Check(MustParse("0.1")), "%q must still enforce >=1", tt.in)
+		}
+	})
+
+	t.Run("a comma-only input is universal, matching SpecifierSet(\",\")", func(t *testing.T) {
+		for _, in := range []string{",", ",,", " , ", " , , "} {
+			ss, err := NewSpecifiers(in)
+			require.NoError(t, err, "input %q", in)
+			assert.Equal(t, 0, ss.Len(), "input %q", in)
+			assert.True(t, ss.Check(MustParse("0.1")), "input %q", in)
 		}
-		// A trailing comma is tolerated, and stays tolerated.
-		ss, err := NewSpecifiers(">=1,")
+	})
+
+	t.Run("but a comma-only || SEGMENT is an error, not universal", func(t *testing.T) {
+		// ⚠️ THE interaction. Each of these is a real constraint plus a segment
+		// that is nothing but punctuation. Treating that segment as universal
+		// would make the OR admit every version -- the same failure the "||"
+		// fix closed, reached by a comma typo.
+		for _, in := range []string{">=1||,", ",||>=1", ">=1||,,", ">=1|| , ||<2", ">=1,<2||,"} {
+			ss, err := NewSpecifiers(in)
+			if err == nil {
+				assert.False(t, ss.Check(MustParse("0.1")),
+					"%q parsed, and then admitted 0.1 -- a comma-only segment disabled the constraint", in)
+				t.Errorf("%q must be rejected", in)
+			}
+
+			// The R path shares this code and is what PPM uses.
+			rs, rerr := NewRSpecifiers(in, identitySanitizer)
+			if rerr == nil {
+				assert.False(t, rs.Check(MustParse("0.1")),
+					"NewRSpecifiers(%q) parsed, and then admitted 0.1", in)
+				t.Errorf("NewRSpecifiers(%q) must be rejected", in)
+			}
+		}
+	})
+
+	t.Run("commas still work inside every branch of a real OR", func(t *testing.T) {
+		ss, err := NewSpecifiers(",>=1.0,<2.0||,>=10.0,<11.0")
 		require.NoError(t, err)
-		assert.Equal(t, 1, ss.Len())
+		assert.Equal(t, 4, ss.Len())
+		assert.True(t, ss.Check(MustParse("1.5")))
+		assert.True(t, ss.Check(MustParse("10.5")))
+		assert.False(t, ss.Check(MustParse("5.0")))
+	})
+
+	t.Run("dropping commas cannot smuggle in an adjacent pair", func(t *testing.T) {
+		// The comma between two constraints is still REQUIRED (see
+		// TestAdjacentConstraintsRejected). Dropping BLANK items must not
+		// weaken that: what survives is re-joined with commas and revalidated.
+		for _, in := range []string{">=1<2", ">=1 <2", ",>=1<2", ">=1<2,", ",,>=1 <2,,"} {
+			_, err := NewSpecifiers(in)
+			assert.Error(t, err, "%q: a missing comma between constraints stays an error", in)
+		}
 	})
 
 	t.Run("well-formed OR groups still parse", func(t *testing.T) {
diff --git a/version/specifier.go b/version/specifier.go
index ecf3780..f1f98d1 100644
--- a/version/specifier.go
+++ b/version/specifier.go
@@ -200,6 +200,10 @@ func newRSpecifiers(v string, sanitizer func(string) string, opts ...SpecifierOp
 		return universalSpecifiers(*c), nil
 	}
 
+	// A specifier-less group may only be the universal set when the input is a
+	// single group. See parseGroup.
+	allowEmpty := len(segments) == 1
+
 	var sss [][]Specifier
 	for _, vv := range segments {
 		if strings.TrimSpace(vv) == "*" {
@@ -207,7 +211,7 @@ func newRSpecifiers(v string, sanitizer func(string) string, opts ...SpecifierOp
 		}
 		vv = strings.ReplaceAll(vv, "-", ".")
 
-		specs, err := parseGroup(vv, sanitizer, *c)
+		specs, err := parseGroup(vv, sanitizer, *c, allowEmpty)
 		if err != nil {
 			return Specifiers{}, err
 		}
@@ -238,13 +242,17 @@ func newSpecifiers(v string, sanitizer func(string) string, opts ...SpecifierOpt
 		return universalSpecifiers(*c), nil
 	}
 
+	// A specifier-less group may only be the universal set when the input is a
+	// single group. See parseGroup.
+	allowEmpty := len(segments) == 1
+
 	var sss [][]Specifier
 	for _, vv := range segments {
 		if strings.TrimSpace(vv) == "*" {
 			vv = ">=0.0.0"
 		}
 
-		specs, err := parseGroup(vv, sanitizer, *c)
+		specs, err := parseGroup(vv, sanitizer, *c, allowEmpty)
 		if err != nil {
 			return Specifiers{}, err
 		}
@@ -354,22 +362,49 @@ func NewSpecifier(s string, opts ...SpecifierOption) (Specifier, error) {
 //
 // A blank group is not the same question as a blank comma-separated ITEM.
 // Upstream drops empty comma-split items (`[s.strip() for s in
-// specifiers.split(",") if s.strip()]`), which is why `packaging` accepts
-// ",>=1" and ">=1,,<2"; this package still rejects both, via
-// validConstraintRegexp, and that remaining divergence is tracked in
-// rstudio/package-manager#19391. Either way it is a question about members
-// within a group, never about whether the group itself may be empty.
-func parseGroup(vv string, sanitizer func(string) string, c conf) ([]Specifier, error) {
-	if strings.TrimSpace(vv) == "" {
-		return nil, fmt.Errorf("improper constraint: empty constraint group")
-	}
-	if !validConstraintRegexp.MatchString(vv) {
+// specifiers.split(",") if s.strip()]`), so `packaging` accepts ",>=1",
+// ">=1,,<2" and ">=1,"; this function does the same, by dropping blank items
+// before validating. Dropping them cannot smuggle in an adjacent pair, because
+// what is left is re-joined with commas and still has to satisfy
+// validConstraintRegexp -- ">=1<2" is a single item with no comma to drop and
+// stays rejected.
+//
+// allowEmpty says whether a group that ends up with NO items may be the
+// universal set. It is true only for a single-group input, and that is the one
+// place this package deliberately stops short of upstream: `SpecifierSet(",")`
+// is universal in packaging 26.2, and `NewSpecifiers(",")` is universal here
+// too, but `">=1||,"` is an ERROR rather than universal. Upstream has no "||"
+// and so has no opinion; letting a comma-only SEGMENT become an empty group
+// would re-open the exact hole described above, just reached by a comma typo
+// instead of a "||" typo.
+func parseGroup(vv string, sanitizer func(string) string, c conf, allowEmpty bool) ([]Specifier, error) {
+	// Drop blank comma-separated items, as upstream does.
+	items := make([]string, 0, strings.Count(vv, ",")+1)
+	for _, item := range strings.Split(vv, ",") {
+		if strings.TrimSpace(item) == "" {
+			continue
+		}
+		items = append(items, item)
+	}
+
+	if len(items) == 0 {
+		if !allowEmpty {
+			return nil, fmt.Errorf(
+				"improper constraint: %q has no constraints in it (an empty || segment is not allowed)", vv)
+		}
+		// Every item was blank, so the whole input is punctuation: the
+		// universal set, matching SpecifierSet(",").
+		return nil, nil
+	}
+
+	normalized := strings.Join(items, ",")
+	if !validConstraintRegexp.MatchString(normalized) {
 		return nil, fmt.Errorf("improper constraint: %s", vv)
 	}
 
-	found := specifierRegexp.FindAllString(vv, -1)
+	found := specifierRegexp.FindAllString(normalized, -1)
 	if found == nil {
-		found = append(found, strings.TrimSpace(vv))
+		found = append(found, strings.TrimSpace(normalized))
 	}
 
 	specs := make([]Specifier, 0, len(found))

From f76fedae172541b7543b2a15d3f54cfc729d2949 Mon Sep 17 00:00:00 2001
From: Jonathan Yoder 
Date: Mon, 10 Aug 2026 13:20:13 -0400
Subject: [PATCH 3/4] Correct the conformance case counts and the size of the
 scope cut
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Both figures in the previous entry were wrong, and both were the kind
that get quoted into an RFD and into status reporting as fact.

The denominator: tests/test_specifiers.py at 4eb0753 collects 2,031 test
cases from 116 functions -- 1,985 from the 70 parametrized ones and 46
from the rest. Reconciled: 1,939 with VERSIONS unresolved, plus the 92
entries of VERSIONS imported from tests/test_version.py, is 2,031.

⚠️ The previous "1,735 parametrized cases" is withdrawn. It came from a
counter that only evaluated MODULE-level assignments, so every
parametrize referring to TestIsUnsatisfiable's UNSATISFIABLE and
SATISFIABLE ClassVars -- declared inside the class -- failed to evaluate
and was silently counted as one case each. It also happened to equal the
sum of three approximate buckets quoted beside it, which is exactly why
an unreconciled number should not have been stated as exact.

⚠️ The scope cut was understated ~10x. The four set-relation and
unsatisfiability APIs are 259 cases, not "~25": TestIsUnsatisfiable
alone is 222. TestSpecifierInternal is a further 18 and
TestSpecifierSetToRangeEquivalence 290. The cut stands on its merits --
those APIs rest on packaging 26.2's interval machinery, which has no
counterpart here -- but it is a large cut, not a rounding error.

What was accurate is now stated as measured rather than as a total:
1,613 Go leaf subtests (1,649 with table parents), 358 of which failed
against the pre-fix library.

Refs rstudio/package-manager#19391

Co-Authored-By: Claude Opus 5 (1M context) 
---
 CHANGELOG.md | 29 +++++++++++++++++++++++++++--
 1 file changed, 27 insertions(+), 2 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 3a69434..5db8295 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -292,18 +292,43 @@ mistaken for a safe patch upgrade.
 
 - **Conformance tables ported from `pypa/packaging`'s `tests/test_specifiers.py`**
   (`version/specifier_conformance_test.go`, `version/specifierset_conformance_test.go`),
-  pinned at `4eb0753dba8fcaaac8eb75463374e448f0931558`: **1,649 assertions**, of which
-  **358 failed against the pre-fix library**. Attribution is in `NOTICE`.
+  pinned at `4eb0753dba8fcaaac8eb75463374e448f0931558`. Attribution is in `NOTICE`.
+
+  **1,613 Go leaf subtests** (1,649 counting the table parents), of which **358 failed
+  against the pre-fix library**.
 
   Every expectation was cross-checked against two independent sources — the literal in
   the pinned upstream file *and* what the installed `packaging` 26.2 actually answers — so
   a stale literal cannot be baked in silently. All of them agreed.
 
+  **Denominator, stated explicitly**, because an earlier draft of this entry got it wrong.
+  `tests/test_specifiers.py` at that commit collects **2,031 test cases** from 116 test
+  functions; **1,985** of those come from the 70 parametrized functions and the remaining
+  46 from unparametrized ones. (Reconciliation: 1,939 cases with `VERSIONS` unresolved,
+  plus the 92 entries of `VERSIONS` imported from `tests/test_version.py`, is 2,031.)
+
+  ⚠️ The earlier figure of "1,735 parametrized cases" was **wrong and is withdrawn**. It
+  came from a counter that only evaluated *module-level* assignments, so every
+  `parametrize` referring to `TestIsUnsatisfiable`'s `UNSATISFIABLE`/`SATISFIABLE`
+  **ClassVars** — declared inside the class — failed to evaluate and was silently counted
+  as one case instead of its real size. It also coincidentally equalled the sum of three
+  approximate buckets quoted alongside it, which is precisely why an unreconciled figure
+  should not have been presented as exact.
+
   Not ported, with the reason recorded in each file's header: the interval subsystem
   behind `is_subset`/`is_superset`/`is_disjoint`/`is_unsatisfiable`, pickling, and the
   Python-implementation internals (`__match_args__`, `__hash__`, one-element caches,
   construction laziness, `to_range` equivalence).
 
+  ⚠️ **The size of that scope cut was also understated.** The four set-relation and
+  unsatisfiability APIs account for **259 cases**, not the "~25" previously claimed — a
+  ~10× understatement, because `TestIsUnsatisfiable` alone is **222** cases (77
+  unsatisfiable + 87 satisfiable + 23 + 17 + assorted). `TestSpecifierInternal` is a
+  further 18 and `TestSpecifierSetToRangeEquivalence` 290. The cut is a considered
+  decision, not a small one: those APIs rest on packaging 26.2's interval machinery
+  (`_to_ranges`, `_intersect_ranges`, `_LowerBound`/`_UpperBound`), which has no
+  counterpart in this module.
+
 ### Notes
 
 - The 163 rows of the hand-written `TestVersion_Check` table were audited against

From 73d681333f14e35ac69fb19e4486f845448f71c5 Mon Sep 17 00:00:00 2001
From: Jonathan Yoder 
Date: Mon, 10 Aug 2026 13:59:54 -0400
Subject: [PATCH 4/4] Correct the mis-framed dismissal of the 13 epoch pairs

They were described as a separate, pre-existing epoch divergence that the
padVersion change neither caused nor fixed. Both halves were wrong: the
retired module answered correctly on them, the change made them wrong, and
the cause was padVersion itself. Corrected in place, pointing at the fix
under [Unreleased].

Refs rstudio/package-manager#19391

Co-Authored-By: Claude Opus 5 (1M context) 
---
 CHANGELOG.md                  | 20 ++++++++++---
 version/specifier.go          |  8 ++++-
 version/specifier_api_test.go | 55 ++++++++++++++++++++++++++++-------
 3 files changed, 67 insertions(+), 16 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 5e4388d..be11c9d 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -32,10 +32,22 @@ mistaken for a safe patch upgrade.
 
   **Measured over the production PyPI corpus: 349 of 135,204 specifier sets change
   their answer.** Of 928 flipped (specifier, version) pairs, `pypa/packaging` 26.2
-  agrees with the **new** behavior **915** times and with the old **13** times —
-  so it was a genuine bug fix, not a behavior swap. Those 13 are a separate,
-  pre-existing epoch divergence (`~=0.0.0
` against `1!1.0`) that this change
-  neither caused nor fixed.
+  agrees with the **new** behavior **915** times and with the old **13** times.
+
+  ⚠️ **Correction.** Those 13 were previously described here as *"a separate,
+  pre-existing epoch divergence that this change neither caused nor fixed"*. **That was
+  wrong on both counts, and the correction matters more than the original claim.** They
+  are neither separate nor pre-existing: the retired module answered *correctly* on them,
+  the change made them *wrong*, and the proximate cause is `padVersion` itself — the very
+  function this entry credits with a fix. `==0.0.0.*` began matching `1!1.0` because
+  `versionSplit` does not prepend the epoch, so `"1!1"` failed the digit test, was dropped
+  by the filter, and the padding loops' under-counting made the two sides coincidentally
+  equal.
+
+  So the honest summary of `padVersion` across `v0.1.0` → `v0.4.0` is: it fixed 915 pairs
+  and **broke 13**, and both the residual under-padding and the epoch regression are fixed
+  under `## [Unreleased]` above, verified at 0 of 82 against an oracle that sweeps
+  multi-segment gaps.
 
 - **`NewRSpecifiers` silently discarded the sanitizer argument it was given** and
   forwarded an identity function instead, so a caller normalizing a non-standard
diff --git a/version/specifier.go b/version/specifier.go
index da49a25..ef5ec28 100644
--- a/version/specifier.go
+++ b/version/specifier.go
@@ -146,8 +146,14 @@ func init() {
 	// One rule, one copy. A singular constructor validating with the plural
 	// grammar is a coupling that breaks silently whenever the plural grammar
 	// moves, which is exactly how this was introduced.
+	// ⚠️ Uses the same (?i) flag and the same [\s\v] whitespace class as
+	// validConstraintRegexp above. The two grammars are deliberately independent
+	// about COMMAS and must stay identical about everything else -- if the
+	// singular one were case-sensitive, `NewSpecifier("==1.0DEV")` would be
+	// rejected while `NewSpecifiers("==1.0DEV")` parsed, which is a fresh version
+	// of the very inconsistency splitting them was meant to remove.
 	singleConstraintRegexp = regexp.MustCompile(
-		fmt.Sprintf(`^\s*%s\s*$`, constraint),
+		fmt.Sprintf(`(?i)^%[1]s*%[2]s%[1]s*$`, wsp, constraint),
 	)
 
 	prefixRegexp = regexp.MustCompile(`^([0-9]+)((?:a|b|c|rc)[0-9]+)$`)
diff --git a/version/specifier_api_test.go b/version/specifier_api_test.go
index 881cc09..57a7d07 100644
--- a/version/specifier_api_test.go
+++ b/version/specifier_api_test.go
@@ -111,19 +111,32 @@ func TestNewSpecifier_RejectsAnyComma(t *testing.T) {
 // stricter alias of the other: the trailing comma the set tolerates on purpose
 // is still tolerated there, and only the singular constructor rejects it.
 //
-// ⚠️ Scope note: on this branch the set grammar accepts a TRAILING comma only.
-// Leading and doubled commas are a separate widening (upstream drops every blank
-// comma item) that lands with the conformance tables in
-// rstudio/package-manager#19391, and the fuller contrast table lives there.
+// On this branch the set grammar also drops leading and doubled blank comma
+// items, so the contrast covers every comma position: each of these is legal for
+// a SET and an error for a SINGLE specifier, exactly as in packaging 26.2.
 func TestNewSpecifiersStillAcceptsTheTrailingCommaTheSingularRejects(t *testing.T) {
-	for _, in := range []string{">=1,", ">=1 , ", ">= 1.0,"} {
-		t.Run(fmt.Sprintf("%q", in), func(t *testing.T) {
-			ss, err := NewSpecifiers(in)
-			require.NoError(t, err, "the SET grammar must still accept %q", in)
-			assert.Equal(t, 1, ss.Len())
+	tests := []struct {
+		in      string
+		wantLen int
+	}{
+		{">=1,", 1},
+		{">=1 , ", 1},
+		{">= 1.0,", 1},
+		{",>=1", 1},
+		{",,>=1", 1},
+		{">=1,,", 1},
+		{" , >=1", 1},
+		{",", 0},
+		{",,", 0},
+	}
+	for _, tt := range tests {
+		t.Run(fmt.Sprintf("%q", tt.in), func(t *testing.T) {
+			ss, err := NewSpecifiers(tt.in)
+			require.NoError(t, err, "the SET grammar must still accept %q", tt.in)
+			assert.Equal(t, tt.wantLen, ss.Len())
 
-			_, err = NewSpecifier(in)
-			assert.Error(t, err, "the SINGULAR grammar must reject %q", in)
+			_, err = NewSpecifier(tt.in)
+			assert.Error(t, err, "the SINGULAR grammar must reject %q", tt.in)
 		})
 	}
 
@@ -133,6 +146,26 @@ func TestNewSpecifiersStillAcceptsTheTrailingCommaTheSingularRejects(t *testing.
 	assert.Equal(t, 2, ss.Len())
 }
 
+// The two grammars are independent about commas and must stay IDENTICAL about
+// everything else. A divergence in case-sensitivity or whitespace handling would
+// be a fresh version of the inconsistency that splitting them removed.
+func TestSingularAndSetGrammarsAgreeOnEverythingButCommas(t *testing.T) {
+	// Every one of these is comma-free, so both grammars must accept all of them.
+	for _, in := range []string{
+		">=1.0", ">= 1.0", ">=  v1.0  ", "==1.0DEV", "!=1.0ALPHA1", ">=7.9A1",
+		"~=1.0.POST1", "==1.0.*", "===lolwat", "2.0", "1!2.0", "1.0+local",
+		"<=  \r \f \v v1.0\t\n",
+	} {
+		t.Run(fmt.Sprintf("%q", in), func(t *testing.T) {
+			_, singularErr := NewSpecifier(in)
+			_, setErr := NewSpecifiers(in)
+			assert.Equal(t, setErr == nil, singularErr == nil,
+				"the two grammars must agree on %q (set err=%v, singular err=%v)", in, setErr, singularErr)
+			assert.NoError(t, singularErr, "and both must accept it")
+		})
+	}
+}
+
 // And the tightening must not narrow any well-formed single specifier. This is
 // the "did I break something real" half: every shape here parsed before and must
 // still parse.