ref(version): catch some edge cases

- strip GoVersion from `helm version` test output
- catch some edge cases when GitCommit is unset or less than 7 characters
- add a test case for `helm version --short`

Signed-off-by: Matthew Fisher <matt.fisher@microsoft.com>
pull/5869/head
Matthew Fisher 6 years ago
parent 7d3693bd56
commit a24915a079
No known key found for this signature in database
GPG Key ID: 92AA783CBAAE8E3B

@ -0,0 +1 @@
v3.0+unreleased

@ -1 +1 @@
version.BuildInfo{Version:"v3.0+unreleased", GitCommit:"", GitTreeState:"", GoVersion:"go1.12.5"} version.BuildInfo{Version:"v3.0+unreleased", GitCommit:"", GitTreeState:"", GoVersion:""}

@ -80,7 +80,10 @@ func (o *versionOptions) run(out io.Writer) error {
func formatVersion(short bool) string { func formatVersion(short bool) string {
v := version.Get() v := version.Get()
if short { 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) return fmt.Sprintf("%#v", v)
} }

@ -24,6 +24,10 @@ func TestVersion(t *testing.T) {
name: "default", name: "default",
cmd: "version", cmd: "version",
golden: "output/version.txt", golden: "output/version.txt",
}, {
name: "short",
cmd: "version --short",
golden: "output/version-short.txt",
}, { }, {
name: "template", name: "template",
cmd: "version --template='Version: {{.Version}}'", cmd: "version --template='Version: {{.Version}}'",

@ -17,6 +17,7 @@ limitations under the License.
package version // import "helm.sh/helm/internal/version" package version // import "helm.sh/helm/internal/version"
import ( import (
"flag"
"runtime" "runtime"
hversion "helm.sh/helm/pkg/version" hversion "helm.sh/helm/pkg/version"
@ -50,10 +51,16 @@ func GetVersion() string {
// Get returns build info // Get returns build info
func Get() hversion.BuildInfo { func Get() hversion.BuildInfo {
return hversion.BuildInfo{ v := hversion.BuildInfo{
Version: GetVersion(), Version: GetVersion(),
GitCommit: gitCommit, GitCommit: gitCommit,
GitTreeState: gitTreeState, GitTreeState: gitTreeState,
GoVersion: runtime.Version(), 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
} }

Loading…
Cancel
Save