diff --git a/cmd/helm/testdata/output/version-short.txt b/cmd/helm/testdata/output/version-short.txt new file mode 100644 index 000000000..5e7749489 --- /dev/null +++ b/cmd/helm/testdata/output/version-short.txt @@ -0,0 +1 @@ +v3.0+unreleased diff --git a/cmd/helm/testdata/output/version.txt b/cmd/helm/testdata/output/version.txt index ce769858b..0d9b536e6 100644 --- a/cmd/helm/testdata/output/version.txt +++ b/cmd/helm/testdata/output/version.txt @@ -1 +1 @@ -version.BuildInfo{Version:"v3.0+unreleased", GitCommit:"", GitTreeState:"", GoVersion:"go1.12.5"} +version.BuildInfo{Version:"v3.0+unreleased", GitCommit:"", GitTreeState:"", GoVersion:""} diff --git a/cmd/helm/version.go b/cmd/helm/version.go index 07c2db324..888dc6094 100644 --- a/cmd/helm/version.go +++ b/cmd/helm/version.go @@ -80,7 +80,10 @@ func (o *versionOptions) run(out io.Writer) error { func formatVersion(short bool) string { v := version.Get() if short { - return fmt.Sprintf("%s+g%s", v.Version, v.GitCommit[:7]) + if len(v.GitCommit) >= 7 { + return fmt.Sprintf("%s+g%s", v.Version, v.GitCommit[:7]) + } + return version.GetVersion() } return fmt.Sprintf("%#v", v) } diff --git a/cmd/helm/version_test.go b/cmd/helm/version_test.go index 7be08e72d..89b89093e 100644 --- a/cmd/helm/version_test.go +++ b/cmd/helm/version_test.go @@ -24,6 +24,10 @@ func TestVersion(t *testing.T) { name: "default", cmd: "version", golden: "output/version.txt", + }, { + name: "short", + cmd: "version --short", + golden: "output/version-short.txt", }, { name: "template", cmd: "version --template='Version: {{.Version}}'", diff --git a/internal/version/version.go b/internal/version/version.go index 0d12d7a1a..23f38500c 100644 --- a/internal/version/version.go +++ b/internal/version/version.go @@ -17,6 +17,7 @@ limitations under the License. package version // import "helm.sh/helm/internal/version" import ( + "flag" "runtime" hversion "helm.sh/helm/pkg/version" @@ -50,10 +51,16 @@ func GetVersion() string { // Get returns build info func Get() hversion.BuildInfo { - return hversion.BuildInfo{ + v := hversion.BuildInfo{ Version: GetVersion(), GitCommit: gitCommit, GitTreeState: gitTreeState, GoVersion: runtime.Version(), } + + // HACK(bacongobbler): strip out GoVersion during a test run for consistent test output + if flag.Lookup("test.v") != nil { + v.GoVersion = "" + } + return v }