Skip to content

Commit

Permalink
chore: add version package unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: Charles-Edouard Brétéché <[email protected]>
  • Loading branch information
eddycharly committed Nov 19, 2023
1 parent d1e1563 commit 3b2ed9f
Show file tree
Hide file tree
Showing 2 changed files with 149 additions and 11 deletions.
20 changes: 17 additions & 3 deletions pkg/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,15 @@ const (
// BuildVersion is provided at compile-time
var BuildVersion string

type buildInfoReader = func() (*debug.BuildInfo, bool)

func Version() string {
return version(debug.ReadBuildInfo)
}

func version(reader buildInfoReader) string {
if BuildVersion == "" {
bi, ok := debug.ReadBuildInfo()
bi, ok := reader()
if !ok {
return notFound
}
Expand All @@ -25,15 +31,23 @@ func Version() string {
}

func Time() string {
bi, ok := debug.ReadBuildInfo()
return time(debug.ReadBuildInfo)
}

func time(reader buildInfoReader) string {
bi, ok := reader()
if !ok {
return notFound
}
return tryFindSetting(vcsTime, bi.Settings...)
}

func Hash() string {
bi, ok := debug.ReadBuildInfo()
return hash(debug.ReadBuildInfo)
}

func hash(reader buildInfoReader) string {
bi, ok := reader()
if !ok {
return notFound
}
Expand Down
140 changes: 132 additions & 8 deletions pkg/version/version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,149 @@ import (
"runtime/debug"
"testing"

"github.com/stretchr/testify/require"
"github.com/stretchr/testify/assert"
)

func TestVersion(t *testing.T) {
require.NotEqual(t, "---", Version())
assert.NotEqual(t, "---", Version())
}

func Test_version(t *testing.T) {
testCases := []struct {
name string
reader buildInfoReader
expectedVersion string
}{
{
name: "No Build Info",
reader: func() (*debug.BuildInfo, bool) {
return nil, false
},
expectedVersion: notFound,
},
{
name: "Valid Build Info",
reader: func() (*debug.BuildInfo, bool) {
return &debug.BuildInfo{
Main: debug.Module{Version: "v1.0.0"},
}, true
},
expectedVersion: "v1.0.0",
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
BuildVersion = ""
actualVersion := version(tc.reader)
assert.Equal(t, tc.expectedVersion, actualVersion, "Expected version to be '%s'", tc.expectedVersion)
})
}
}

func TestBuildVersion(t *testing.T) {
BuildVersion = "test"
require.Equal(t, "test", Version())
assert.Equal(t, "test", Version())
}

func TestTime(t *testing.T) {
require.Equal(t, "---", Time())
assert.Equal(t, "---", Time())
}

func Test_time(t *testing.T) {
vcsTime := "vcs.time"
testCases := []struct {
name string
reader buildInfoReader
expectedTime string
}{
{
name: "No Build Info",
reader: func() (*debug.BuildInfo, bool) {
return nil, false
},
expectedTime: notFound,
},
{
name: "VCS Time Found",
reader: func() (*debug.BuildInfo, bool) {
return &debug.BuildInfo{
Settings: []debug.BuildSetting{
{Key: vcsTime, Value: "2021-04-01T12:34:56Z"},
},
}, true
},
expectedTime: "2021-04-01T12:34:56Z",
},
{
name: "VCS Time Not Found",
reader: func() (*debug.BuildInfo, bool) {
return &debug.BuildInfo{
Settings: []debug.BuildSetting{
{Key: "some.other.setting", Value: "some-value"},
},
}, true
},
expectedTime: notFound,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
actualTime := time(tc.reader)
assert.Equal(t, tc.expectedTime, actualTime, "Expected time to be '%s'", tc.expectedTime)
})
}
}

func TestHash(t *testing.T) {
require.Equal(t, "---", Hash())
assert.Equal(t, "---", Hash())
}

func Test_hash(t *testing.T) {
vcsRevision := "vcs.revision"
testCases := []struct {
name string
reader buildInfoReader
expectedHash string
}{
{
name: "No Build Info",
reader: func() (*debug.BuildInfo, bool) {
return nil, false
},
expectedHash: notFound,
},
{
name: "VCS Revision Found",
reader: func() (*debug.BuildInfo, bool) {
return &debug.BuildInfo{
Settings: []debug.BuildSetting{
{Key: vcsRevision, Value: "abcdef123456"},
},
}, true
},
expectedHash: "abcdef123456",
},
{
name: "VCS Revision Not Found",
reader: func() (*debug.BuildInfo, bool) {
return &debug.BuildInfo{
Settings: []debug.BuildSetting{
{Key: "some.other.setting", Value: "some-value"},
},
}, true
},
expectedHash: notFound,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
actualHash := hash(tc.reader)
assert.Equal(t, tc.expectedHash, actualHash, "Expected hash to be '%s'", tc.expectedHash)
})
}
}

func Test_tryFindSetting(t *testing.T) {
Expand Down Expand Up @@ -53,9 +178,8 @@ func Test_tryFindSetting(t *testing.T) {
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tryFindSetting(tt.key, tt.settings...); got != tt.want {
t.Errorf("tryeFindSetting() = %v, want %v", got, tt.want)
}
got := tryFindSetting(tt.key, tt.settings...)
assert.Equal(t, tt.want, got)
})
}
}

0 comments on commit 3b2ed9f

Please sign in to comment.