Skip to content

Commit

Permalink
Increase code coverage
Browse files Browse the repository at this point in the history
Signed-off-by: Alexey Semenyuk <[email protected]>
  • Loading branch information
alex-semenyuk committed Nov 15, 2023
1 parent 902dcbb commit 5b46980
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 7 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ on:
branches: [main]
pull_request:
branches: [main]
pull_request_target:
branches: [main]
workflow_dispatch:

jobs:
Expand Down
8 changes: 3 additions & 5 deletions cmd/version.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cmd

import (
"context"
"encoding/json"
"fmt"
"runtime/debug"
Expand Down Expand Up @@ -38,23 +37,22 @@ func versionCommand() *cobra.Command {
Short: "Prints the version info",
Long: "",
RunE: func(cmd *cobra.Command, args []string) error {

info := &Info{
Version: BuildVersionOverride,
Date: BuildDate,
Commit: BuildCommit,
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()
setBuildInfo(info, buildInfo, ok)
}

if shortened {
fmt.Println(info.Version)
fmt.Printf("Version: %s", info.Version)
} else {
var (
bytes []byte
Expand All @@ -74,7 +72,7 @@ func versionCommand() *cobra.Command {
return err
}

fmt.Println(string(bytes))
fmt.Printf("Build info: %s", string(bytes))
}

return nil
Expand Down
77 changes: 77 additions & 0 deletions cmd/version_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
}

0 comments on commit 5b46980

Please sign in to comment.