Skip to content

Commit

Permalink
RHCLOUD-34576 Moving config reading to PreRun function (project-kesse…
Browse files Browse the repository at this point in the history
…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
josejulio authored Aug 14, 2024
1 parent c8e21f9 commit a9a0d0c
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 13 deletions.
26 changes: 13 additions & 13 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,19 @@ var (
Use: Name,
Version: Version,
Short: "A simple common inventory system",
PreRunE: func(cmd *cobra.Command, args []string) error {
if err := viper.ReadInConfig(); err != nil {
return err
} else {
msg := fmt.Sprintf("Using config file: %s", viper.ConfigFileUsed())
logger.Debug(msg)
}

// put the values into the options struct.
err := viper.Unmarshal(&options)

return err
},
}

options = struct {
Expand Down Expand Up @@ -122,17 +135,4 @@ func initConfig() {

viper.SetEnvPrefix(Name)
viper.AutomaticEnv()

if err := viper.ReadInConfig(); err != nil {
panic(err)
} else {
msg := fmt.Sprintf("Using config file: %s", viper.ConfigFileUsed())
logger.Debug(msg)
}

// put the values into the options struct.
err := viper.Unmarshal(&options)
if err != nil {
panic(err)
}
}
58 changes: 58 additions & 0 deletions cmd/root_test.go
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"))
})
}
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ require (
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.11.0 // indirect
github.com/spf13/cast v1.6.0 // indirect
github.com/stretchr/objx v0.5.2 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
go.opentelemetry.io/otel v1.28.0 // indirect
go.opentelemetry.io/otel/metric v1.28.0 // indirect
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,7 @@ github.com/spf13/viper v1.19.0/go.mod h1:GQUN9bilAbhU/jgc1bKs99f/suXKeUMct8Adx5+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
Expand Down

0 comments on commit a9a0d0c

Please sign in to comment.