Skip to content

Commit b02f821

Browse files
committed
fix(test): make sure tests aren't impacted by user config
Modify readConfig to check whether running inside a test, and if so, to not process the user config file (~/.config/cocli/config.yaml) or the environment variables. The config file passed with --config will still be processed in case a test wants provide one. Signed-off-by: setrofim <setrofim@gmail.com>
1 parent c7a63d8 commit b02f821

3 files changed

Lines changed: 12 additions & 3 deletions

File tree

cmd/corimSubmit_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ func Test_CorimSubmitCmd_bad_server_url(t *testing.T) {
1818
ctrl := gomock.NewController(t)
1919
defer ctrl.Finish()
2020

21+
2122
ms := mock_deps.NewMockISubmitter(ctrl)
2223
cmd := NewCorimSubmitCmd(ms)
2324

cmd/output.cbor

717 Bytes
Binary file not shown.

cmd/root.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111

1212
"github.com/spf13/afero"
1313
"github.com/spf13/cobra"
14+
"github.com/spf13/pflag"
1415
"github.com/spf13/viper"
1516
"github.com/veraison/apiclient/auth"
1617
_ "github.com/veraison/corim/profiles/tdx"
@@ -84,10 +85,15 @@ func initConfig() {
8485
}
8586

8687
func readConfig(path string) (*viper.Viper, error) {
88+
// We want to make sure that tests run in a controlled enviroment and
89+
// can't be influenced by user's config file (if it exits) or user-set
90+
// enviroment variables.
91+
insideTest := pflag.Lookup("test.v") != nil
92+
8793
v := viper.GetViper()
8894
if path != "" {
8995
v.SetConfigFile(path)
90-
} else {
96+
} else if !insideTest {
9197
wd, err := os.Getwd()
9298
if err != nil {
9399
return nil, err
@@ -102,8 +108,10 @@ func readConfig(path string) (*viper.Viper, error) {
102108
v.SetConfigName("config")
103109
}
104110

105-
v.SetEnvPrefix("cocli")
106-
v.AutomaticEnv()
111+
if !insideTest {
112+
v.SetEnvPrefix("cocli")
113+
v.AutomaticEnv()
114+
}
107115

108116
err := v.ReadInConfig()
109117
if errors.As(err, &viper.ConfigFileNotFoundError{}) {

0 commit comments

Comments
 (0)