From db89959a41a3516279c48dc02ac799ccb1965160 Mon Sep 17 00:00:00 2001 From: Qi Xiao Date: Mon, 27 Feb 2023 00:12:18 +0000 Subject: [PATCH] pkg/buildinfo: Split devVersionWithVariant into two functions. This makes it a bit simpler to change the code for release branches. --- pkg/buildinfo/buildinfo.go | 7 ++++--- pkg/buildinfo/buildinfo_test.go | 29 ++++++++++++++++++----------- 2 files changed, 22 insertions(+), 14 deletions(-) diff --git a/pkg/buildinfo/buildinfo.go b/pkg/buildinfo/buildinfo.go index 1ca06488..0c337dfd 100644 --- a/pkg/buildinfo/buildinfo.go +++ b/pkg/buildinfo/buildinfo.go @@ -46,12 +46,13 @@ func (Type) IsStructMap() {} // Value contains all the build information. var Value = Type{ - Version: devVersionWithVariant(VersionBase, VCSOverride, BuildVariant), + // On a release branch, change to addVariant(VersionBase, BuildVariant) and + // remove unneeded code. + Version: addVariant(devVersion(VersionBase, VCSOverride), BuildVariant), GoVersion: runtime.Version(), } -func devVersionWithVariant(next, vcsOverride, variant string) string { - version := devVersion(next, vcsOverride) +func addVariant(version, variant string) string { if variant != "" { version += "+" + variant } diff --git a/pkg/buildinfo/buildinfo_test.go b/pkg/buildinfo/buildinfo_test.go index bf1b31a1..27f9841c 100644 --- a/pkg/buildinfo/buildinfo_test.go +++ b/pkg/buildinfo/buildinfo_test.go @@ -27,12 +27,11 @@ func TestProgram(t *testing.T) { ) } -var devVersionWithVariantTests = []struct { +var devVersionTests = []struct { name string next string vcsOverride string buildInfo *debug.BuildInfo - variant string want string }{ { @@ -88,22 +87,16 @@ var devVersionWithVariantTests = []struct { vcsOverride: "20220401235958-123456789012", want: "0.42.0-dev.0.20220401235958-123456789012", }, - { - name: "variant", - next: "0.42.0", - variant: "distro", - want: "0.42.0-dev.unknown+distro", - }, } -func TestDevVersionWithVariant(t *testing.T) { - for _, test := range devVersionWithVariantTests { +func TestDevVersion(t *testing.T) { + for _, test := range devVersionTests { t.Run(test.name, func(t *testing.T) { testutil.Set(t, &readBuildInfo, func() (*debug.BuildInfo, bool) { return test.buildInfo, test.buildInfo != nil }) - got := devVersionWithVariant(test.next, test.vcsOverride, test.variant) + got := devVersion(test.next, test.vcsOverride) if got != test.want { t.Errorf("got %q, want %q", got, test.want) } @@ -111,6 +104,20 @@ func TestDevVersionWithVariant(t *testing.T) { } } +func TestAddVariant(t *testing.T) { + got := addVariant("0.42.0", "") + want := "0.42.0" + if got != want { + t.Errorf("got %q, want %q", got, want) + } + + got = addVariant("0.42.0", "distro") + want = "0.42.0+distro" + if got != want { + t.Errorf("got %q, want %q", got, want) + } +} + func TestValue(t *testing.T) { vals.TestValue(t, Value). Index("version", Value.Version).