Skip to content

Commit

Permalink
feat(main): adding main test to check version
Browse files Browse the repository at this point in the history
  • Loading branch information
atbore-phx committed Mar 5, 2024
1 parent a52baca commit 35934ab
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
22 changes: 22 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package main

import (
"os"
"sbam/pkg/cmd"
"testing"

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

func TestMain(t *testing.T) {
err := cmd.SetVersionInfo("1.0", "abc123", "2022-01-01")
assert.NoError(t, err)

old := os.Args
defer func() { os.Args = old }()

os.Args = []string{"cmd", "--version"}

err = cmd.Execute()
assert.NoError(t, err)
}
6 changes: 4 additions & 2 deletions pkg/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var rootCmd = &cobra.Command{
Initiate parameters from command line, env variables or config.yaml file.`,
}

func Execute() {
func Execute() error {
err := rootCmd.Execute()
if err != nil {
os.Exit(1)
Expand All @@ -28,6 +28,7 @@ func Execute() {
os.Exit(0)
}
}
return nil
}

func init() {
Expand All @@ -43,6 +44,7 @@ func init() {
}
}

func SetVersionInfo(version, commit, date string) {
func SetVersionInfo(version, commit, date string) error {
rootCmd.Version = fmt.Sprintf("%s (Built on %s from Git SHA %s)", version, date, commit)
return nil
}

0 comments on commit 35934ab

Please sign in to comment.