From 468d96610d5cb1cbc118e922fddcf2824964c6d8 Mon Sep 17 00:00:00 2001 From: Matt Farina Date: Tue, 25 Oct 2016 10:19:10 -0400 Subject: [PATCH 1/2] Fixes #21: pre-releases not API compatible Per SemVer, pre-releases are not API compatible with the releases at the same point. The range comparisons have been updated to: - if the constraint does not have a pre-release then versions with a pre-release are not compatible. - if the constraint has a pre-release than versions with a pre-release are evaluated. --- constraints.go | 8 ++++++++ constraints_test.go | 19 +++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/constraints.go b/constraints.go index b63f5f6..ed5f2c1 100644 --- a/constraints.go +++ b/constraints.go @@ -155,6 +155,14 @@ type constraint struct { // Check if a version meets the constraint func (c *constraint) check(v *Version) bool { + + // If there is a pre-release on the version but the constraint isn't looking + // for them assume that pre-releases are not compatible. See issue 21 for + // more details. + if v.Prerelease() != "" && c.con.Prerelease() == "" { + return false + } + return c.function(v, c) } diff --git a/constraints_test.go b/constraints_test.go index 6dad455..26a28fd 100644 --- a/constraints_test.go +++ b/constraints_test.go @@ -152,6 +152,8 @@ func TestConstraintsCheck(t *testing.T) { {"4.1.x", "4.1.3", true}, {"1.x", "1.4", true}, {"!=4.1", "4.1.0", false}, + {"!=4.1-alpha", "4.1.0-alpha", false}, + {"!=4.1-alpha", "4.1.0", true}, {"!=4.1", "5.1.0", true}, {"!=4.x", "5.1.0", true}, {"!=4.x", "4.1.0", false}, @@ -168,9 +170,12 @@ func TestConstraintsCheck(t *testing.T) { {"<1.1.x", "1.1.500", true}, {"<1.2.x", "1.1.1", true}, {">=1.1", "4.1.0", true}, + {">=1.1", "4.1.0-beta", false}, {">=1.1", "1.1.0", true}, {">=1.1", "0.0.9", false}, {"<=1.1", "0.1.0", true}, + {"<=1.1", "0.1.0-alpha", false}, + {"<=1.1-a", "0.1.0-alpha", true}, {"<=1.1", "1.1.0", true}, {"<=1.x", "1.1.0", true}, {"<=2.x", "3.1.0", false}, @@ -190,11 +195,18 @@ func TestConstraintsCheck(t *testing.T) { {"^1.x", "1.1.1", true}, {"^2.x", "1.1.1", false}, {"^1.x", "2.1.1", false}, + {"^1.x", "1.1.1-beta1", false}, + {"^1.1.2-alpha", "1.2.1-beta1", true}, + {"^1.2.x-alpha", "1.1.1-beta1", false}, {"~*", "2.1.1", true}, {"~1.x", "2.1.1", false}, {"~1.x", "1.3.5", true}, {"~1.x", "1.4", true}, {"~1.1", "1.1.1", true}, + {"~1.1", "1.1.1-alpha", false}, + {"~1.1-alpha", "1.1.1-beta", true}, + {"~1.1.1-beta", "1.1.1-alpha", false}, + {"~1.1.1-beta", "1.1.1", true}, {"~1.2.3", "1.2.5", true}, {"~1.2.3", "1.2.2", false}, {"~1.2.3", "1.3.2", false}, @@ -309,6 +321,9 @@ func TestConstraintsValidate(t *testing.T) { {"1.1 - 2", "1.1.1", true}, {"1.1-3", "4.3.2", false}, {"^1.1", "1.1.1", true}, + {"^1.1", "1.1.1-alpha", false}, + {"^1.1.1-alpha", "1.1.1-beta", true}, + {"^1.1.1-beta", "1.1.1-alpha", false}, {"^1.1", "4.3.2", false}, {"^1.x", "1.1.1", true}, {"^2.x", "1.1.1", false}, @@ -316,6 +331,10 @@ func TestConstraintsValidate(t *testing.T) { {"~*", "2.1.1", true}, {"~1.x", "2.1.1", false}, {"~1.x", "1.3.5", true}, + {"~1.x", "1.3.5-beta", false}, + {"~1.3.6-alpha", "1.3.5-beta", false}, + {"~1.3.5-alpha", "1.3.5-beta", true}, + {"~1.3.5-beta", "1.3.5-alpha", false}, {"~1.x", "1.4", true}, {"~1.1", "1.1.1", true}, {"~1.2.3", "1.2.5", true}, From eb02aaba543473213a2f72f8d9da3af8904321f9 Mon Sep 17 00:00:00 2001 From: Matt Farina Date: Wed, 26 Oct 2016 10:22:08 -0400 Subject: [PATCH 2/2] Updated the changelog and docs for #21 --- CHANGELOG.md | 9 +++++++++ README.md | 9 +++++++++ 2 files changed, 18 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index c3808ea..fc82248 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,12 @@ +# Release 1.1.2 (unreleased) + +## Fixed +- Issue #21: Per the SemVer spec (section 9) a pre-release is unstable and + might not satisfy the intended compatibility. The change here ignores pre-releases + on constraint checks (e.g., ~ or ^) when a pre-release is not part of the + constraint. For example, `^1.2.3` will ignore pre-releases while + `^1.2.3-alpha` will include them. + # Release 1.1.1 (2016-06-30) ## Changed diff --git a/README.md b/README.md index 1edec7a..9a9ed45 100644 --- a/README.md +++ b/README.md @@ -78,6 +78,15 @@ The basic comparisons are: * `>=`: greater than or equal to * `<=`: less than or equal to +_Note, according to the Semantic Version specification pre-releases may not be +API compliant with their release counterpart. It says,_ + +> _A pre-release version indicates that the version is unstable and might not satisfy the intended compatibility requirements as denoted by its associated normal version._ + +_SemVer comparisons without a pre-release value will skip pre-release versions. +For example, `>1.2.3` will skip pre-releases when looking at a list of values +while `>1.2.3-alpha.1` will evaluate pre-releases._ + ## Hyphen Range Comparisons There are multiple methods to handle ranges and the first is hyphens ranges.