From 1951e80d786fea151973e7cba69562b35c42e77b Mon Sep 17 00:00:00 2001 From: Ben Foster Date: Tue, 5 Nov 2024 00:45:39 -0500 Subject: [PATCH] Fix parsing and comparing versions with build information (#8) fixes #7 --- pkg/version/constraint_test.go | 6 ++++++ pkg/version/version.go | 3 +-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/pkg/version/constraint_test.go b/pkg/version/constraint_test.go index 3108a7b..94caa67 100644 --- a/pkg/version/constraint_test.go +++ b/pkg/version/constraint_test.go @@ -237,6 +237,12 @@ func TestVersion_Check(t *testing.T) { {"> 1.0 < 1.2 || >3.0, <4.0", "4.2", false}, {"^0.2 || ^1", "1.8.0", true}, {"^0.2, ^1", "1.8.0", false}, + + // Build identifiers + {">= 1.0.0, < 1.2.0+security-01", "1.0.0", true}, + {">= 1.0.0, < 1.2.0+security-01", "1.2.0", false}, + {">= 1.0.0, <= 1.2.0+security-01", "1.2.0", true}, + {">= 1.0.0, < 1.2.0+security-01", "1.3.0", false}, } for _, tt := range tests { t.Run(fmt.Sprintf("%s %s", tt.version, tt.constraint), func(t *testing.T) { diff --git a/pkg/version/version.go b/pkg/version/version.go index 5ffb998..7735d6a 100644 --- a/pkg/version/version.go +++ b/pkg/version/version.go @@ -21,8 +21,7 @@ const ( // The raw regular expression string used for testing the validity of a version. regex = `v?([0-9]+(\.[0-9]+)*)` + `(-([0-9]+[0-9A-Za-z\-~]*(\.[0-9A-Za-z\-~]+)*)|(-?([A-Za-z\-~]+[0-9A-Za-z\-~]*(\.[0-9A-Za-z\-~]+)*)))?` + - `(\+([0-9A-Za-z\-~]+(\.[0-9A-Za-z\-~]+)*))?` + - `?` + `(\+([0-9A-Za-z\-~]+(\.[0-9A-Za-z\-~]+)*))?` ) // Version represents a single version.