Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/certsuite: refactor main to allow unit testing subcommands #2259

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion cmd/certsuite/info/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ func showInfo(cmd *cobra.Command, _ []string) error { //nolint:funlen
lineMaxWidth := 120
if term.IsTerminal(0) {
width, _, err := term.GetSize(0)
fmt.Println("Term Width: ", width)
if err != nil {
return fmt.Errorf("could not get terminal size, err: %v", err)
}
Expand Down
11 changes: 7 additions & 4 deletions cmd/certsuite/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,24 @@ import (
"github.com/test-network-function/cnf-certification-test/cmd/certsuite/version"
)

var (
rootCmd = &cobra.Command{
func newRootCmd() *cobra.Command {
rootCmd := cobra.Command{
Use: "certsuite",
Short: "A CLI tool for the Red Hat Best Practices Test Suite for Kubernetes.",
}
)

func main() {
rootCmd.AddCommand(claim.NewCommand())
rootCmd.AddCommand(generate.NewCommand())
rootCmd.AddCommand(check.NewCommand())
rootCmd.AddCommand(run.NewCommand())
rootCmd.AddCommand(info.NewCommand())
rootCmd.AddCommand(version.NewCommand())

return &rootCmd
}

func main() {
rootCmd := newRootCmd()
if err := rootCmd.Execute(); err != nil {
log.Error("%v", err)
os.Exit(1)
Expand Down
36 changes: 36 additions & 0 deletions cmd/certsuite/main_test.go
Original file line number Diff line number Diff line change
@@ -1 +1,37 @@
package main

import (
"io"
"os"
"testing"

"github.com/stretchr/testify/assert"
"github.com/test-network-function/cnf-certification-test/pkg/versions"
)

func TestCertsuiteVersionCmd(t *testing.T) {
// Prepare context
versions.GitCommit = "aaabbbccc"
versions.GitRelease = "v0.0.0"
versions.ClaimFormatVersion = "v0.0.0"

// Run the command
savedStdout := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w

cmd := newRootCmd()
cmd.SetArgs([]string{
"version",
})
err := cmd.Execute()
assert.Nil(t, err)

w.Close()
out, _ := io.ReadAll(r)
os.Stdout = savedStdout

// Check the result
const expectedOut = "Certsuite version: v0.0.0 (aaabbbccc)\nClaim file version: v0.0.0\n"
assert.Equal(t, expectedOut, string(out))
}
Loading