Skip to content

Commit

Permalink
Merge pull request kubescape#1481 from mohaidoss/unknown-version-fix
Browse files Browse the repository at this point in the history
Fix for Empty Build Number in cmd Version
  • Loading branch information
matthyx authored Nov 21, 2023
2 parents 0191135 + 887f6a0 commit 5676983
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 4 deletions.
8 changes: 4 additions & 4 deletions cmd/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package version
import (
"context"
"fmt"
"os"

"github.com/kubescape/go-logger"
"github.com/kubescape/kubescape/v3/core/cautils"
Expand All @@ -18,10 +17,11 @@ func GetVersionCmd() *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.TODO()
v := cautils.NewIVersionCheckHandler(ctx)
v.CheckLatestVersion(ctx, cautils.NewVersionCheckRequest(cautils.BuildNumber, "", "", "version"))
fmt.Fprintf(os.Stdout,
versionCheckRequest := cautils.NewVersionCheckRequest(cautils.BuildNumber, "", "", "version")
v.CheckLatestVersion(ctx, versionCheckRequest)
fmt.Fprintf(cmd.OutOrStdout(),
"Your current version is: %s\n",
cautils.BuildNumber,
versionCheckRequest.ClientVersion,
)
logger.L().Debug(fmt.Sprintf("git enabled in build: %t", isGitEnabled()))
return nil
Expand Down
45 changes: 45 additions & 0 deletions cmd/version/version_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package version

import (
"bytes"
"io"
"testing"

"github.com/kubescape/kubescape/v3/core/cautils"
"github.com/stretchr/testify/assert"
)

func TestGetVersionCmd(t *testing.T) {
tests := []struct {
name string
buildNumber string
want string
}{
{
name: "Undefined Build Number",
buildNumber: "",
want: "Your current version is: unknown\n",
},
{
name: "Defined Build Number: v3.0.1",
buildNumber: "v3.0.1",
want: "Your current version is: v3.0.1\n",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cautils.BuildNumber = tt.buildNumber

if cmd := GetVersionCmd(); cmd != nil {
buf := bytes.NewBufferString("")
cmd.SetOut(buf)
cmd.Execute()
out, err := io.ReadAll(buf)
if err != nil {
t.Fatal(err)
}
assert.Equal(t, tt.want, string(out))
}
})
}
}

0 comments on commit 5676983

Please sign in to comment.