Skip to content

Commit

Permalink
Add unit test for crc version
Browse files Browse the repository at this point in the history
  • Loading branch information
guillaumerose committed Aug 6, 2020
1 parent 41208d8 commit f04f892
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 15 deletions.
4 changes: 2 additions & 2 deletions cmd/crc/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cmd

import (
"fmt"

cmdConfig "github.com/code-ready/crc/cmd/crc/cmd/config"
"github.com/code-ready/crc/pkg/crc/config"
"github.com/code-ready/crc/pkg/crc/constants"
Expand Down Expand Up @@ -57,7 +56,8 @@ func runPrerun() {
// Setting up logrus
logging.InitLogrus(logging.LogLevel, constants.LogFilePath)
setProxyDefaults()
for _, str := range GetVersionStrings() {

for _, str := range defaultVersion().lines() {
logging.Debugf(str)
}
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/crc/cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"github.com/code-ready/crc/pkg/crc/output"
"github.com/code-ready/crc/pkg/crc/preflight"
"github.com/code-ready/crc/pkg/crc/validation"
"github.com/code-ready/crc/pkg/crc/version"
crcversion "github.com/code-ready/crc/pkg/crc/version"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)
Expand Down Expand Up @@ -141,7 +141,7 @@ func checkIfNewVersionAvailable(noUpdateCheck bool) {
if noUpdateCheck {
return
}
isNewVersionAvailable, newVersion, err := version.NewVersionAvailable()
isNewVersionAvailable, newVersion, err := crcversion.NewVersionAvailable()
if err != nil {
logging.Debugf("Unable to find out if a new version is available: %v", err)
return
Expand Down
47 changes: 36 additions & 11 deletions cmd/crc/cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package cmd

import (
"fmt"
"strings"
"io"
"os"

"github.com/code-ready/crc/pkg/crc/constants"
"github.com/code-ready/crc/pkg/crc/version"
"github.com/code-ready/crc/pkg/crc/exit"
crcversion "github.com/code-ready/crc/pkg/crc/version"
"github.com/spf13/cobra"
)

Expand All @@ -18,21 +20,44 @@ var versionCmd = &cobra.Command{
Short: "Print version information",
Long: "Print version information",
Run: func(cmd *cobra.Command, args []string) {
runPrintVersion(args)
if err := runPrintVersion(os.Stdout, defaultVersion()); err != nil {
exit.WithMessage(1, err.Error())
}
},
}

func GetVersionStrings() []string {
func runPrintVersion(writer io.Writer, version *version) error {
for _, line := range version.lines() {
if _, err := fmt.Fprintf(writer, line); err != nil {
return err
}
}
return nil
}

type version struct {
Version string `json:"version"`
Commit string `json:"commit"`
OpenshiftVersion string `json:"openshiftVersion"`
Embedded bool `json:"embedded"`
}

func defaultVersion() *version {
return &version{
Version: crcversion.GetCRCVersion(),
Commit: crcversion.GetCommitSha(),
OpenshiftVersion: crcversion.GetBundleVersion(),
Embedded: constants.BundleEmbedded(),
}
}

func (v *version) lines() []string {
var embedded string
if !constants.BundleEmbedded() {
if !v.Embedded {
embedded = "not "
}
return []string{
fmt.Sprintf("CodeReady Containers version: %s+%s", version.GetCRCVersion(), version.GetCommitSha()),
fmt.Sprintf("OpenShift version: %s (%sembedded in binary)", version.GetBundleVersion(), embedded),
fmt.Sprintf("CodeReady Containers version: %s+%s\n", v.Version, v.Commit),
fmt.Sprintf("OpenShift version: %s (%sembedded in binary)\n", v.OpenshiftVersion, embedded),
}
}

func runPrintVersion(arguments []string) {
fmt.Println(strings.Join(GetVersionStrings(), "\n"))
}
21 changes: 21 additions & 0 deletions cmd/crc/cmd/version_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package cmd

import (
"bytes"
"testing"

"github.com/stretchr/testify/assert"
)

func TestPlainVersion(t *testing.T) {
out := new(bytes.Buffer)
assert.NoError(t, runPrintVersion(out, &version{
Version: "1.13",
Commit: "aabbcc",
OpenshiftVersion: "4.5.4",
Embedded: false,
}))
assert.Equal(t, `CodeReady Containers version: 1.13+aabbcc
OpenShift version: 4.5.4 (not embedded in binary)
`, out.String())
}

0 comments on commit f04f892

Please sign in to comment.