diff --git a/CHANGELOG.md b/CHANGELOG.md index 95f2fea..af1d1a5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -44,6 +44,15 @@ rather than `0.1.3`. *above* `1.0+ubuntu-10`. It now sorts below, matching upstream. Consumers that persist or compare rendered versions, or that rely on the previous sort order of dash- or underscore-separated local labels, will see a difference. +- A local version segment consisting entirely of ASCII digits normalizes as an integer, + so `1.0+007` renders as `1.0+7` and `1.0+ubuntu-007` as `1.0+ubuntu.7`. PEP 440 says + such a segment "should be considered an integer", and scopes its + no-normalization carve-out to integers inside an *alphanumeric* segment — so + `1.0+foo0100` is still left exactly as it is. **Ordering is unaffected**: those + segments were already compared numerically, so `1.0+007` and `1.0+7` compared equal + before and after. What changes is the rendered string, and therefore arbitrary + equality: `===1.0+7` now matches `1.0+007`, as upstream does, where it previously + did not. ### Added diff --git a/version/local_normalization_test.go b/version/local_normalization_test.go index 126aec8..468fc9a 100644 --- a/version/local_normalization_test.go +++ b/version/local_normalization_test.go @@ -115,3 +115,83 @@ func TestSurroundingWhitespaceIncludesVerticalTab(t *testing.T) { } } } + +// TestAllDigitLocalSegmentsNormalizeAsIntegers covers a third divergence, found +// by a differential run over 634,187 distinct real PyPI version strings — which +// is worth stating plainly, because that corpus could not possibly have found +// it: PEP 440 forbids PyPI from accepting local versions at all, so the corpus +// contained ZERO of them and this came out of the synthetic supplement. +// +// PEP 440: "If a segment consists entirely of ASCII digits then that section +// should be considered an integer", and its integer-normalization rule ("an +// integer version of 00 would normalize to 0") therefore reaches such a +// segment. The rule's carve-out is scoped to integers inside an ALPHANUMERIC +// segment, "such as 1.0+foo0100 which is already in its normalized form". +// +// Expected values below are the measured output of str(packaging.version.Version(s)), +// not a reading of the spec — the spec settles which implementation is right, the +// reference settles what the answer looks like. +func TestAllDigitLocalSegmentsNormalizeAsIntegers(t *testing.T) { + for _, tc := range []struct{ in, want string }{ + {"1.0+007", "1.0+7"}, + {"1.0+7", "1.0+7"}, + {"1.0+00", "1.0+0"}, // All zeros is the integer 0, not the empty string. + {"1.0+0", "1.0+0"}, + {"1.0+ubuntu.007", "1.0+ubuntu.7"}, + {"1.0+ubuntu-007", "1.0+ubuntu.7"}, // Separator and integer normalization compose. + {"1.0+007.ubuntu", "1.0+7.ubuntu"}, + {"1.0+foo0100", "1.0+foo0100"}, // Alphanumeric: the carve-out, left alone. + {"1.0+0foo", "1.0+0foo"}, // Also alphanumeric, despite the leading zero. + {"1.0+09000", "1.0+9000"}, + } { + v, err := Parse(tc.in) + if err != nil { + t.Errorf("Parse(%q): unexpected error %v", tc.in, err) + continue + } + if got := v.String(); got != tc.want { + t.Errorf("Parse(%q).String() = %q, want %q", tc.in, got, tc.want) + } + } +} + +// TestAllDigitLocalNormalizationIsObservableOnlyViaArbitraryEquality pins WHY +// the normalization above matters, so a future reader does not conclude it is +// cosmetic and drop it. +// +// Ordering was already correct without it, because cmpkey compares all-digit +// segments numerically either way — so no comparison test could have caught the +// bug. PEP 440 defines "===" as string equality on the normalized form, which is +// the one place the rendering is load-bearing. +func TestAllDigitLocalNormalizationIsObservableOnlyViaArbitraryEquality(t *testing.T) { + padded, err := Parse("1.0+007") + if err != nil { + t.Fatalf("Parse: %v", err) + } + bare, err := Parse("1.0+7") + if err != nil { + t.Fatalf("Parse: %v", err) + } + + // The half that was always right: they compare equal. + if got := padded.Compare(bare); got != 0 { + t.Errorf("Compare(1.0+007, 1.0+7) = %d, want 0 — all-digit segments compare "+ + "numerically, which was never the defect", got) + } + + // The half that was wrong: "===" is string equality, so the strings must match. + if padded.String() != bare.String() { + t.Errorf("String() forms differ (%q vs %q), so \"===1.0+7\" would fail to match "+ + "\"1.0+007\" while the reference implementation matches it", + padded.String(), bare.String()) + } + + spec, err := NewSpecifiers("===1.0+7") + if err != nil { + t.Fatalf("NewSpecifiers: %v", err) + } + if !spec.Check(padded) { + t.Error("\"===1.0+7\" must match 1.0+007: PEP 440 defines === as string equality " + + "on the normalized form, and 007 normalizes to 7") + } +} diff --git a/version/version.go b/version/version.go index f5ae28e..db77564 100644 --- a/version/version.go +++ b/version/version.go @@ -102,7 +102,8 @@ func init() { var localVersionSeparators = regexp.MustCompile(`[._-]`) // normalizeLocal normalizes a local version label: PEP 440 requires "-" and -// "_" separators to be rewritten as ".", and the label to be lowercased. +// "_" separators to be rewritten as ".", the label to be lowercased, and any +// segment consisting entirely of ASCII digits to be normalized as an integer. // // Normalizing the separator is not cosmetic. cmpkey splits the label on "." // to compare it segment by segment, with numeric segments ordering above @@ -110,8 +111,53 @@ var localVersionSeparators = regexp.MustCompile(`[._-]`) // alphabetic segment and compared lexicographically. That makes // "1.0+ubuntu-10" sort BELOW "1.0+ubuntu-2" -- a silent wrong answer of the // same kind as rstudio/package-manager#19369. +// +// # Why all-digit segments have their leading zeros stripped +// +// PEP 440: "If a segment consists entirely of ASCII digits then that section +// should be considered an integer" — and its integer-normalization rule ("an +// integer version of 00 would normalize to 0") therefore reaches it. The rule's +// one carve-out is scoped to integers inside an ALPHANUMERIC local segment, +// "such as 1.0+foo0100 which is already in its normalized form", so "foo0100" +// is left exactly as it is while "007" becomes "7". +// +// That asymmetry is easy to get backwards, so note what it is NOT about: +// ordering was already correct without this, because cmpkey compares all-digit +// segments numerically either way. What it changes is the normalized STRING — +// and PEP 440 defines the "===" arbitrary-equality operator as string equality +// on that form, so without this "===1.0+7" fails to match "1.0+007" while the +// reference implementation matches it. func normalizeLocal(local string) string { - return localVersionSeparators.ReplaceAllString(strings.ToLower(local), ".") + lowered := localVersionSeparators.ReplaceAllString(strings.ToLower(local), ".") + + segments := strings.Split(lowered, ".") + for i, seg := range segments { + if isASCIIDigits(seg) { + segments[i] = strings.TrimLeft(seg, "0") + if segments[i] == "" { + // The segment was all zeros, which is the integer 0. + segments[i] = "0" + } + } + } + return strings.Join(segments, ".") +} + +// isASCIIDigits reports whether s is non-empty and every byte is an ASCII digit. +// +// Deliberately not unicode.IsDigit: PEP 440 says "entirely of ASCII digits", and +// admitting other Unicode digit forms here would accept labels the grammar +// rejects and hand strconv-shaped input to a numeric comparison. +func isASCIIDigits(s string) bool { + if s == "" { + return false + } + for i := 0; i < len(s); i++ { + if s[i] < '0' || s[i] > '9' { + return false + } + } + return true } // MustParse is like Parse but panics if the version cannot be parsed.