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
This commit is contained in:
Cuong Manh Le 2021-09-09 18:11:40 +07:00 committed by GitHub
parent bf415467f9
commit b0091d4355
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 1 deletions

View File

@ -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 {

27
client/config_test.go Normal file
View File

@ -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)
}
}