Skip to content

Commit

Permalink
fix: golang autodiscovery module update (#2247)
Browse files Browse the repository at this point in the history
Signed-off-by: Olblak <me@olblak.com>
  • Loading branch information
olblak authored Jun 17, 2024
1 parent 28e1f8f commit 920ae78
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 3 deletions.
1 change: 1 addition & 0 deletions pkg/plugins/autodiscovery/golang/dependency.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ func (g Golang) discoverDependencyManifests() ([][]byte, error) {

manifests = append(manifests, moduleManifest)
}

// Test if the ignore rule based on path is respected
if len(g.spec.Ignore) > 0 {
if g.spec.Ignore.isMatchingRules(g.rootDir, relativeFoundFile, goVersion, "", "") {
Expand Down
13 changes: 10 additions & 3 deletions pkg/plugins/autodiscovery/golang/matchingRule.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,17 @@ func (m MatchingRules) isGoVersionOnly() bool {
if len(m) == 0 {
return false
}

goOnlyFound := 0
for _, rule := range m {
if rule.GoVersion != "" && len(rule.Modules) > 0 {
return false
if rule.GoVersion != "" && len(rule.Modules) == 0 {
goOnlyFound++
}
}
return true

if goOnlyFound == len(m) && goOnlyFound > 0 {
return true
}

return false
}
71 changes: 71 additions & 0 deletions pkg/plugins/autodiscovery/golang/matchingRule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,3 +157,74 @@ func TestIsMatchingRule(t *testing.T) {
})
}
}

func TestIsGoOnly(t *testing.T) {

dataset := []struct {
name string
rules MatchingRules
expectedResult bool
}{
{
name: "Only path specified",
rules: MatchingRules{
MatchingRule{
Path: "go.mod",
},
},
expectedResult: false,
},
{
name: "Only go version specified",
rules: MatchingRules{
MatchingRule{
GoVersion: "*",
},
},
expectedResult: true,
},
{
name: "Multiple go version specified",
rules: MatchingRules{
MatchingRule{
GoVersion: "1.19.*",
},
MatchingRule{
GoVersion: ">=1.20.0",
},
},
expectedResult: true,
},
{
name: "Go version specified with second go module rule",
rules: MatchingRules{
MatchingRule{
GoVersion: "1.19.*",
},
MatchingRule{
Modules: map[string]string{
"github.com/updatecli/updatecli": "1.0.0",
},
},
},
expectedResult: false,
},
{
name: "Go version specified with go module within the same rule",
rules: MatchingRules{
MatchingRule{
GoVersion: "1.19.*",
Modules: map[string]string{
"github.com/updatecli/updatecli": "1.0.0",
},
},
},
expectedResult: false,
},
}

for _, d := range dataset {
gotReset := d.rules.isGoVersionOnly()
assert.Equal(t, d.expectedResult, gotReset)
}
}

0 comments on commit 920ae78

Please sign in to comment.