diff --git a/CHANGELOG.md b/CHANGELOG.md index 5a449d2..685334b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,69 @@ 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. + + ⚠️ **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 + 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 - 🔴 **`v0.4.0` shipped a zero-padding bug that made `==X.*`, `!=X.*` and `~=` answer @@ -79,6 +142,24 @@ mistaken for a safe patch upgrade. multi-segment-gap wildcards barely occur in real constraint data — **not** that the bug was harmless, since it is reachable from arbitrary client input. +- **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 @@ -315,6 +396,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* @@ -334,6 +441,97 @@ 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`. 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 + `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/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 de51f7a..4885ea5 100644 --- a/version/specifier.go +++ b/version/specifier.go @@ -67,9 +67,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 +93,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 +118,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), ) // EXACTLY ONE constraint, with no comma anywhere: the grammar for the @@ -127,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]+)$`) @@ -206,6 +231,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) == "*" { @@ -213,7 +242,7 @@ func newRSpecifiers(v string, sanitizer func(string) string, opts ...SpecifierOp } vv = strings.ReplaceAll(vv, "-", ".") - specs, err := parseGroup(vv, sanitizer) + specs, err := parseGroup(vv, sanitizer, allowEmpty) if err != nil { return Specifiers{}, err } @@ -244,13 +273,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) + specs, err := parseGroup(vv, sanitizer, allowEmpty) if err != nil { return Specifiers{}, err } @@ -378,11 +411,22 @@ 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. +// 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. +// // ⚠️ parseGroup deliberately takes no conf. The pre-release policy passed to // NewSpecifiers belongs to the SET, not to its members, and stamping it onto // each member conflates the two: upstream's SpecifierSet keeps its own @@ -391,17 +435,34 @@ func NewSpecifier(s string, opts ...SpecifierOption) (Specifier, error) { // (the member's own autodetect) while the set's is False. Members are built // with the zero policy and autodetect individually; the set's policy is applied // at query time from ss.conf. -func parseGroup(vv string, sanitizer func(string) string) ([]Specifier, error) { - if strings.TrimSpace(vv) == "" { - return nil, fmt.Errorf("improper constraint: empty constraint group") +func parseGroup(vv string, sanitizer func(string) string, 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 } - if !validConstraintRegexp.MatchString(vv) { + + 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)) @@ -459,9 +520,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 "~=": @@ -901,35 +968,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_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. 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/specifierset_conformance_test.go b/version/specifierset_conformance_test.go new file mode 100644 index 0000000..1c83259 --- /dev/null +++ b/version/specifierset_conformance_test.go @@ -0,0 +1,772 @@ +// 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 COLLAPSE, which is where upstream settles. + // + // ⚠️ An earlier version of this assertion expected 2, citing + // `len(SpecifierSet(">=1.0") & SpecifierSet(">=1.0")) == 2` in packaging + // 26.2 as upstream parity. That reading was wrong: the 2 only holds on a + // FRESH object, because `_canonical_specs()` deduplicates lazily. Measured: + // + // c = SpecifierSet(">=1.0") & SpecifierSet(">=1.0") + // len(c) -> 2 ; str(c) -> '>=1.0' ; len(c) -> 1 + // + // So 2 was a transient artifact of upstream's cache -- the one thing this + // port declares non-portable -- and it made the answer depend on which + // method a caller happened to invoke first. `str()` is ">=1.0" under every + // ordering, which is the settled behavior matched here. + one, err := NewSpecifiers(">=1.0") + require.NoError(t, err) + dup, err := one.And(one) + require.NoError(t, err) + assert.Equal(t, 1, dup.Len()) + assert.Equal(t, ">=1.0", dup.String()) + 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 "