Skip to content

Commit

Permalink
Add unit test for GetMostRecentTag
Browse files Browse the repository at this point in the history
  • Loading branch information
chansuke committed Dec 14, 2023
1 parent d7bca53 commit c9c7253
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
4 changes: 2 additions & 2 deletions api/provenance/provenance.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,14 @@ func GetProvenance() Provenance {

for _, dep := range info.Deps {
if dep != nil && dep.Path == "sigs.k8s.io/kustomize/kustomize/v5" {
p.Version = getMostRecentTag(*dep)
p.Version = GetMostRecentTag(*dep)
}
}

return p
}

func getMostRecentTag(m debug.Module) string {
func GetMostRecentTag(m debug.Module) string {
for m.Replace != nil {
m = *m.Replace
}
Expand Down
50 changes: 50 additions & 0 deletions api/provenance/provenance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package provenance_test

import (
"fmt"
"runtime/debug"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -45,3 +46,52 @@ func TestProvenance_Semver(t *testing.T) {
p.Version = "kustomize/v4.11.12"
assert.Equal(t, "v4.11.12", p.Semver())
}

func mockModule(version string) debug.Module {
return debug.Module{
Path: "sigs.k8s.io/kustomize/kustomize/v5",
Version: version,
Replace: nil,
}
}

func TestGetMostRecentTag(t *testing.T) {
tests := []struct {
name string
module debug.Module
expectedTag string
}{
{
name: "Standard version",
module: mockModule("v1.2.3"),
expectedTag: "v1.2.3",
},
{
name: "Pseudo-version with patch increment",
module: mockModule("v0.0.0-20210101010101-abcdefabcdef"),
expectedTag: "v0.0.0",
},
{
name: "Invalid semver string",
module: mockModule("invalid-version"),
expectedTag: "unknown",
},
{
name: "Valid semver with patch increment and pre-release info",
module: mockModule("v1.2.3-0.20210101010101-abcdefabcdef"),
expectedTag: "v1.2.2",
},
{
name: "Valid semver no patch increment",
module: mockModule("v1.2.0"),
expectedTag: "v1.2.0",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tag := provenance.GetMostRecentTag(tt.module)
assert.Equal(t, tt.expectedTag, tag)
})
}
}

0 comments on commit c9c7253

Please sign in to comment.