forked from project-kessel/inventory-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
RHCLOUD-34576 Moving config reading to PreRun function (project-kesse…
…l#36) * RHCLOUD-34576 Moving config reading to PreRun function - Was initially checking spf13/viper#233 but none of the suggestions there seemed to work. In our case it looked like viper or cobra wasn't loading the value at all. - Moved some code to the PreRun and that seems to work. Some users suggest that the code shouldn't be in the init and use the PreRun instead. * Adding a test to ensure it doesn't happen again
- Loading branch information
Showing
4 changed files
with
73 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package cmd | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/mock" | ||
"testing" | ||
) | ||
|
||
type MockedCommandRun struct { | ||
mock.Mock | ||
} | ||
|
||
func (m *MockedCommandRun) RunE(cmd *cobra.Command, args []string) error { | ||
margs := m.Called(cmd, args) | ||
return margs.Error(0) | ||
} | ||
|
||
func setupMockRunE() map[string]*MockedCommandRun { | ||
mocks := make(map[string]*MockedCommandRun) | ||
|
||
for _, cmd := range rootCmd.Commands() { | ||
mockedCommandRunE := new(MockedCommandRun) | ||
mockedCommandRunE.On("RunE", mock.Anything, mock.Anything).Return(nil) | ||
mocks[cmd.Name()] = mockedCommandRunE | ||
cmd.RunE = mockedCommandRunE.RunE | ||
} | ||
|
||
return mocks | ||
} | ||
|
||
func assertCommandCalled(t *testing.T, command string, mocked map[string]*MockedCommandRun) { | ||
targetRunE := "RunE" | ||
|
||
for name, cmd := range mocked { | ||
if name == command { | ||
cmd.AssertCalled(t, targetRunE, mock.Anything, mock.Anything) | ||
} else { | ||
cmd.AssertNotCalled(t, targetRunE) | ||
} | ||
} | ||
} | ||
|
||
func TestRootCommand(t *testing.T) { | ||
commands := []string{"migrate", "serve"} | ||
for _, command := range commands { | ||
t.Run(command+" by setting storage.database to postgres", func(t *testing.T) { | ||
rootCmd.SetArgs([]string{command, "--storage.database=postgres"}) | ||
|
||
mocked := setupMockRunE() | ||
assert.Nil(t, rootCmd.Execute()) | ||
|
||
assertCommandCalled(t, command, mocked) | ||
assert.Equal(t, "postgres", viper.GetString("storage.database")) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters