Skip to content

Commit

Permalink
Merge pull request #3856 from BenTheElder/cleanup2
Browse files Browse the repository at this point in the history
version test cleanup: don't mutate globals, use pre-computed expected values
  • Loading branch information
k8s-ci-robot authored Feb 1, 2025
2 parents 4ca56e4 + 9e4b00b commit b9be5e5
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 36 deletions.
20 changes: 12 additions & 8 deletions pkg/cmd/kind/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,23 @@ import (

// Version returns the kind CLI Semantic Version
func Version() string {
v := versionCore
return version(versionCore, versionPreRelease, gitCommit, gitCommitCount)
}

func version(core, preRelease, commit, commitCount string) string {
v := core
// add pre-release version info if we have it
if versionPreRelease != "" {
v += "-" + versionPreRelease
// If gitCommitCount was set, add to the pre-release version
if gitCommitCount != "" {
v += "." + gitCommitCount
if preRelease != "" {
v += "-" + preRelease
// If commitCount was set, add to the pre-release version
if commitCount != "" {
v += "." + commitCount
}
// if commit was set, add the + <build>
// we only do this for pre-release versions
if gitCommit != "" {
if commit != "" {
// NOTE: use 14 character short hash, like Kubernetes
v += "+" + truncate(gitCommit, 14)
v += "+" + truncate(commit, 14)
}
}
return v
Expand Down
45 changes: 17 additions & 28 deletions pkg/cmd/kind/version/version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,78 +64,67 @@ func TestTruncate(t *testing.T) {
func TestVersion(t *testing.T) {
tests := []struct {
name string
version string
versionPreRelease string
gitCommit string
gitCommitCount string
want string
}{
{
name: "With git commit count and with commit hash",
version: "v0.27.0",
versionPreRelease: "alpha",
gitCommit: "mocked-hash",
gitCommitCount: "mocked-count",
want: versionCore + "-" + "alpha" + "." + "mocked-count" + "+" + "mocked-hash",
want: "v0.27.0-alpha.mocked-count+mocked-hash",
},
{
name: "Without git commit count and and with hash",
version: "v0.27.0",
versionPreRelease: "beta",
gitCommit: "mocked-hash",
gitCommitCount: "",
want: versionCore + "-" + "beta" + "+" + "mocked-hash",
want: "v0.27.0-beta+mocked-hash",
},
{
name: "Without git commit hash and with commit count",
version: "v0.30.0",
versionPreRelease: "alpha",
gitCommit: "",
gitCommitCount: "mocked-count",
want: versionCore + "-" + "alpha" + "." + "mocked-count",
want: "v0.30.0-alpha.mocked-count",
},
{
name: "Without git commit hash and without commit count",
version: "v0.27.0",
versionPreRelease: "alpha",
gitCommit: "",
gitCommitCount: "",
want: versionCore + "-" + "alpha",
want: "v0.27.0-alpha",
},
{
name: "Without pre release version",
version: "v0.27.0",
versionPreRelease: "",
gitCommit: "",
gitCommitCount: "",
want: versionCore,
want: "v0.27.0",
},
{
name: "Without pre release version and with git commit hash and count",
version: "v0.27.0",
versionPreRelease: "",
gitCommit: "mocked-commit",
gitCommitCount: "mocked-count",
want: versionCore,
want: "v0.27.0",
},
}
for _, tt := range tests {
// TODO: this won't be necessary when we require go 1.22+
tt := tt
t.Run(tt.name, func(t *testing.T) {
if tt.gitCommit != "" {
gitCommitBackup := gitCommit
gitCommit = tt.gitCommit
defer func() {
gitCommit = gitCommitBackup
}()
}

if tt.gitCommitCount != "" {
gitCommitCountBackup := gitCommitCount
gitCommitCount = tt.gitCommitCount
defer func() {
gitCommitCount = gitCommitCountBackup
}()
}

versionPreReleaseBackup := versionPreRelease
versionPreRelease = tt.versionPreRelease
defer func() {
versionPreRelease = versionPreReleaseBackup
}()
if got := Version(); got != tt.want {
t.Parallel()
if got := version(tt.version, tt.versionPreRelease, tt.gitCommit, tt.gitCommitCount); got != tt.want {
t.Errorf("Version() = %v, want %v", got, tt.want)
}
})
Expand Down

0 comments on commit b9be5e5

Please # to comment.