From b0091d435519149bf21c5bfe245ecb6a178d6dfa Mon Sep 17 00:00:00 2001 From: Cuong Manh Le Date: Thu, 9 Sep 2021 18:11:40 +0700 Subject: [PATCH] client: catch and return unhandled errors in `InitConfig` (#540) Catches and returns unhandled errors after stat-ing for the config file, which is currently assuming that on non-existent error returned. Fixes #539 --- client/config.go | 8 +++++++- client/config_test.go | 27 +++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 client/config_test.go diff --git a/client/config.go b/client/config.go index beb14cb0..6edb08bf 100644 --- a/client/config.go +++ b/client/config.go @@ -23,7 +23,13 @@ func InitConfig(cmd *cobra.Command) error { } configFile := path.Join(home, "config", "config.toml") - if _, err := os.Stat(configFile); err == nil { + _, err = os.Stat(configFile) + if err != nil && !os.IsNotExist(err) { + // Immediately return if the error isn't related to the file not existing. + // See issue https://github.com/tharsis/ethermint/issues/539 + return err + } + if err == nil { viper.SetConfigFile(configFile) if err := viper.ReadInConfig(); err != nil { diff --git a/client/config_test.go b/client/config_test.go new file mode 100644 index 00000000..0f4625dc --- /dev/null +++ b/client/config_test.go @@ -0,0 +1,27 @@ +package client + +import ( + "os" + "path/filepath" + "testing" + + "github.com/cosmos/cosmos-sdk/client/flags" + "github.com/spf13/cobra" +) + +func TestInitConfigNonNotExistError(t *testing.T) { + tempDir := t.TempDir() + subDir := filepath.Join(tempDir, "nonPerms") + if err := os.Mkdir(subDir, 0600); err != nil { + t.Fatalf("Failed to create sub directory: %v", err) + } + cmd := &cobra.Command{} + cmd.PersistentFlags().String(flags.FlagHome, "", "") + if err := cmd.PersistentFlags().Set(flags.FlagHome, subDir); err != nil { + t.Fatalf("Could not set home flag [%T] %v", err, err) + } + + if err := InitConfig(cmd); !os.IsPermission(err) { + t.Fatalf("Failed to catch permissions error, got: [%T] %v", err, err) + } +}