Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change config load order so custom.toml is last #1684

Merged
merged 11 commits into from
Sep 15, 2023
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
* Fix for incorrect resource-id type casting on contract specification [#1647](https://github.com/provenance-io/provenance/issues/1647).
* Allow restricted coins to be quarantined [#1626](https://github.com/provenance-io/provenance/issues/1626).
* Prevent marker forced transfers from module accounts [#1626](https://github.com/provenance-io/provenance/issues/1626).
* Change config load order so custom.toml can override other config. [#1262](https://github.com/provenance-io/provenance/issues/1262)

### Client Breaking

Expand Down
14 changes: 8 additions & 6 deletions cmd/provenanced/config/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,14 +385,16 @@ func unquote(str string) string {
}

// LoadConfigFromFiles loads configurations appropriately.
func LoadConfigFromFiles(cmd *cobra.Command) error {
if err := loadUnmanagedConfig(cmd); err != nil {
return err
}
func LoadConfigFromFiles(cmd *cobra.Command) (err error) {
if IsPacked(cmd) {
return loadPackedConfig(cmd)
err = loadPackedConfig(cmd)
} else {
err = loadUnpackedConfig(cmd)
}
if err != nil {
return err
}
return loadUnpackedConfig(cmd)
return loadUnmanagedConfig(cmd)
}

// loadUnmanagedConfig reads the unmanaged config file into viper. It does not apply anything to any contexts though.
Expand Down
27 changes: 24 additions & 3 deletions cmd/provenanced/config/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ func (s *ConfigManagerTestSuite) TestUnmanagedConfig() {
assert.Equal(t, "bananas", actual, "unmanaged field value")
})

s.T().Run("unmanaged config entry does not override other config", func(t *testing.T) {
s.T().Run("unmanaged config entry overrides other config", func(t *testing.T) {
dCmd := s.makeDummyCmd()
configDir := GetFullPathToConfigDir(dCmd)
uFile := GetFullPathToUnmanagedConf(dCmd)
Expand All @@ -180,8 +180,8 @@ func (s *ConfigManagerTestSuite) TestUnmanagedConfig() {
ctx := client.GetClientContextFromCmd(dCmd)
vpr := ctx.Viper
actual := vpr.GetString("db_backend")
assert.NotEqual(t, "still bananas", actual, "unmanaged field value")
assert.Equal(t, DefaultTmConfig().DBBackend, actual, "unmanaged field default value")
assert.Equal(t, "still bananas", actual, "unmanaged field value")
assert.NotEqual(t, DefaultTmConfig().DBBackend, actual, "unmanaged field default value")
})

s.T().Run("unmanaged config is read with unpacked files", func(t *testing.T) {
Expand All @@ -196,6 +196,27 @@ func (s *ConfigManagerTestSuite) TestUnmanagedConfig() {
assert.Equal(t, "stuff", actual, "unmanaged field value")
})

s.T().Run("unmanaged config is read with invalid packed files", func(t *testing.T) {
dCmd := s.makeDummyCmd()
uFile := GetFullPathToUnmanagedConf(dCmd)
pFile := GetFullPathToPackedConf(dCmd)
SaveConfigs(dCmd, DefaultAppConfig(), DefaultTmConfig(), DefaultClientConfig(), false)
require.NoError(t, os.WriteFile(uFile, []byte("my-custom-entry = \"stuff\"\n"), 0o644), "writing unmanaged config")
require.NoError(t, os.WriteFile(pFile, []byte("kl234508923u5jl"), 0o644), "writing invalid data to packed config")
require.EqualError(t, LoadConfigFromFiles(dCmd), "packed config file parse error: invalid character 'k' looking for beginning of value", "should throw error with invalid packed config")
require.NoError(t, os.Remove(pFile), "removing packed config")
})

s.T().Run("unmanaged config is read with invalid unpacked files", func(t *testing.T) {
dCmd := s.makeDummyCmd()
uFile := GetFullPathToUnmanagedConf(dCmd)
pFile := GetFullPathToAppConf(dCmd)
SaveConfigs(dCmd, DefaultAppConfig(), DefaultTmConfig(), DefaultClientConfig(), false)
require.NoError(t, os.WriteFile(uFile, []byte("my-custom-entry = \"stuff\"\n"), 0o644), "writing unmanaged config")
require.NoError(t, os.WriteFile(pFile, []byte("kl234508923u5jl"), 0o644), "writing invalid data to app config")
require.EqualError(t, LoadConfigFromFiles(dCmd), "app config file merge error: While parsing config: toml: expected = after a key, but the document ends there", "should throw error with invalid packed config")
})

s.T().Run("unmanaged config is read with packed config", func(t *testing.T) {
dCmd := s.makeDummyCmd()
uFile := GetFullPathToUnmanagedConf(dCmd)
Expand Down
Loading