From 26c204bd8aa09872c322a898c1d133e094469614 Mon Sep 17 00:00:00 2001 From: benhalstead Date: Mon, 6 Jul 2020 14:38:35 +0100 Subject: [PATCH] Fixed loose regex match in go.mod parsing --- cmd/grnc-bind/binder/env.go | 4 ++-- cmd/grnc-bind/binder/env_test.go | 41 ++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 2 deletions(-) create mode 100644 cmd/grnc-bind/binder/env_test.go diff --git a/cmd/grnc-bind/binder/env.go b/cmd/grnc-bind/binder/env.go index cc4adc7..9429dfd 100644 --- a/cmd/grnc-bind/binder/env.go +++ b/cmd/grnc-bind/binder/env.go @@ -15,8 +15,8 @@ import ( const ( graniticHomeEnvVar = "GRANITIC_HOME" goPathEnvVar = "GOPATH" - reqRegex = ".*github.com/graniticio/granitic/(v[\\d]*)[\\s]*(\\S*)" - replaceRegex = ".*github.com/graniticio/granitic/v[\\d]*[\\s]*=>[\\s]*(\\S*)" + reqRegex = ".*github\\.com/graniticio/granitic/(v[\\d]*)[\\s]*(\\S*)" + replaceRegex = ".*github\\.com/graniticio/granitic/v[\\d]*[\\s]*=>[\\s]*(\\S*)" ) // LocateFacilityConfig determines where on your filesystem you have checked out Granitic. This is used when code needs diff --git a/cmd/grnc-bind/binder/env_test.go b/cmd/grnc-bind/binder/env_test.go new file mode 100644 index 0000000..a6108b9 --- /dev/null +++ b/cmd/grnc-bind/binder/env_test.go @@ -0,0 +1,41 @@ +package binder + +import ( + "regexp" + "testing" +) + +func TestModFileRequireShouldMatch(t *testing.T) { + + reqRe := regexp.MustCompile(reqRegex) + + shouldMatch := "github.com/graniticio/granitic/v2 v2.2.0" + + reqMatches := reqRe.FindStringSubmatch(shouldMatch) + + if len(reqMatches) >= 3 { + majorVersion := reqMatches[1] + requiredVersion := reqMatches[2] + + if majorVersion != "v2" || requiredVersion != "v2.2.0" { + t.Fail() + } + } else { + t.Fail() + } + +} + +func TestModFileRequireShouldNotMatch(t *testing.T) { + + reqRe := regexp.MustCompile(reqRegex) + + shouldNotMatch := "githubacom/graniticio/granitic/v2 v2.2.0" + + reqMatches := reqRe.FindStringSubmatch(shouldNotMatch) + + if len(reqMatches) > 0 { + t.Fail() + } + +}