Skip to content

Commit

Permalink
fix: fixed top level panic from invalid command, uninstantiated logger (
Browse files Browse the repository at this point in the history
  • Loading branch information
madhuravius authored Jul 22, 2022
1 parent 71ac5eb commit 14d2dbc
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func Execute() {
ImageTools: images.GetClingyImageCapture(),
}
if err := RootCmd(rootConfig).Execute(); err != nil {
logger.Println("Error when trying to execute", err)
fmt.Println("Error when trying to execute", err)
rootConfig.ExitTools.Exit(1)
}
}
19 changes: 18 additions & 1 deletion cmd/root_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package cmd

import (
"bytes"
"io/ioutil"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -21,7 +23,22 @@ func TestRootCmdExecuteHelp(t *testing.T) {
mockTools := internal.GenerateMockInterfacesForClingy(t)
defer mockTools.Ctrl.Finish()

cmd := RootCmd(&RootConfig{ImageTools: mockTools.MagickClientImpl})
cmd := RootCmd(&RootConfig{ExitTools: mockTools.ExitClientsImpl, ImageTools: mockTools.MagickClientImpl})
output := internal.ExecCobraCmdAndReturnString(t, cmd, []string{"--help"})
assert.Contains(t, output, "clingy is a tool to test and capture CLI flows")
}

func TestRootCmdExecuteInvalidCommand(t *testing.T) {
mockTools := internal.GenerateMockInterfacesForClingy(t)
defer mockTools.Ctrl.Finish()

cmd := RootCmd(&RootConfig{ExitTools: mockTools.ExitClientsImpl, ImageTools: mockTools.MagickClientImpl})
b := new(bytes.Buffer)
cmd.SetOut(b)
cmd.SetErr(b)
cmd.SetArgs([]string{"will-exit-1-does-not-exist"})
cmdErr := cmd.Execute()
out, _ := ioutil.ReadAll(b)
assert.NotNil(t, cmdErr)
assert.Contains(t, string(out), "Error: unknown command")
}

0 comments on commit 14d2dbc

Please sign in to comment.