Skip to content

Commit

Permalink
Fixed loose regex match in go.mod parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
benhalstead committed Jul 6, 2020
1 parent e7c9296 commit 26c204b
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
4 changes: 2 additions & 2 deletions cmd/grnc-bind/binder/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
41 changes: 41 additions & 0 deletions cmd/grnc-bind/binder/env_test.go
Original file line number Diff line number Diff line change
@@ -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()
}

}

0 comments on commit 26c204b

Please sign in to comment.