diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 973bcad..13be348 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -5,8 +5,6 @@ on: branches: [main] pull_request: branches: [main] - pull_request_target: - branches: [main] workflow_dispatch: jobs: diff --git a/cmd/version.go b/cmd/version.go index a46bfb4..7b39e48 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -1,7 +1,6 @@ package cmd import ( - "context" "encoding/json" "fmt" "runtime/debug" @@ -38,7 +37,6 @@ func versionCommand() *cobra.Command { Short: "Prints the version info", Long: "", RunE: func(cmd *cobra.Command, args []string) error { - info := &Info{ Version: BuildVersionOverride, Date: BuildDate, @@ -46,7 +44,7 @@ func versionCommand() *cobra.Command { License: "Apache-2.0", } - // Where you are using go install, we will get good version information usefully from Go + // When we are using go install, we will get version information from Go // When we're in go-releaser in a Github action, we will have the version passed in explicitly if info.Version == "" { buildInfo, ok := debug.ReadBuildInfo() @@ -54,7 +52,7 @@ func versionCommand() *cobra.Command { } if shortened { - fmt.Println(info.Version) + fmt.Printf("Version: %s", info.Version) } else { var ( bytes []byte @@ -74,7 +72,7 @@ func versionCommand() *cobra.Command { return err } - fmt.Println(string(bytes)) + fmt.Printf("Build info: %s", string(bytes)) } return nil diff --git a/cmd/version_test.go b/cmd/version_test.go new file mode 100644 index 0000000..193831e --- /dev/null +++ b/cmd/version_test.go @@ -0,0 +1,77 @@ +package cmd + +import ( + "strconv" + "testing" + "time" + + "github.com/stretchr/testify/assert" +) + +func TestVersionCommand(t *testing.T) { + testCases := []struct { + name string + buildDate string + buildCommit string + buildVersionOverride string + output string + shortened bool + errMsg string + }{ + { + name: "error", + buildDate: time.Now().String(), + buildCommit: "243413535", + buildVersionOverride: "0.0.1", + output: "unknown", + errMsg: "FF23016: Invalid output type: unknown", + }, + { + name: "yaml output", + buildDate: time.Now().String(), + buildCommit: "243413535", + buildVersionOverride: "0.0.1", + output: "yaml", + }, + { + name: "json output", + buildDate: time.Now().String(), + buildCommit: "243413535", + buildVersionOverride: "0.0.1", + output: "json", + }, + { + name: "shortened", + buildDate: time.Now().String(), + buildCommit: "243413535", + buildVersionOverride: "0.0.1", + shortened: true, + }, + { + name: "version is empty", + buildDate: time.Now().String(), + buildCommit: "243413535", + buildVersionOverride: "", + output: "json", + }, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + command := versionCommand() + BuildVersionOverride = tc.buildVersionOverride + BuildCommit = tc.buildCommit + BuildDate = tc.buildDate + command.Flags().Set("output", tc.output) + command.Flags().Set("short", strconv.FormatBool(tc.shortened)) + err := command.RunE(command, []string{"arg1"}) + + if tc.errMsg != "" { + assert.Error(t, err) + assert.Equal(t, tc.errMsg, err.Error()) + } else { + assert.NoError(t, err) + } + }) + } +}