From 94dade9c43d6be77490e26bdd879a80d20557efb Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Mon, 9 Oct 2023 22:27:17 +0800 Subject: [PATCH] fix --- modules/setting/config_env.go | 3 ++- modules/setting/config_env_test.go | 26 ++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/modules/setting/config_env.go b/modules/setting/config_env.go index b30e44de30a0d..242f40914a159 100644 --- a/modules/setting/config_env.go +++ b/modules/setting/config_env.go @@ -149,8 +149,9 @@ func EnvironmentToConfig(cfg ConfigProvider, envs []string) (changed bool) { continue } } - key := section.Key(keyName) + key := ConfigSectionKey(section, keyName) if key == nil { + changed = true key, err = section.NewKey(keyName, keyValue) if err != nil { log.Error("Error creating key: %s in section: %s with value: %s : %v", keyName, sectionName, keyValue, err) diff --git a/modules/setting/config_env_test.go b/modules/setting/config_env_test.go index e14d5ecb414f1..7d07c479a17df 100644 --- a/modules/setting/config_env_test.go +++ b/modules/setting/config_env_test.go @@ -115,3 +115,29 @@ key = old EnvironmentToConfig(cfg, []string{"GITEA__sec__key__FILE=" + tmpFile}) assert.Equal(t, "value-from-file\n", cfg.Section("sec").Key("key").String()) } + +func TestEnvironmentToConfigSubSecKey(t *testing.T) { + // the INI package has a quirk: by default, the keys are inherited. + // when maintaining the keys, the newly added sub key should not be affected by the parent key. + cfg, err := NewConfigProviderFromData(` +[sec] +key = some +`) + assert.NoError(t, err) + + changed := EnvironmentToConfig(cfg, []string{"GITEA__sec_0X2E_sub__key=some"}) + assert.True(t, changed) + + tmpFile := t.TempDir() + "/test-sub-sec-key.ini" + defer os.Remove(tmpFile) + err = cfg.SaveTo(tmpFile) + assert.NoError(t, err) + bs, err := os.ReadFile(tmpFile) + assert.NoError(t, err) + assert.Equal(t, `[sec] +key = some + +[sec.sub] +key = some +`, string(bs)) +}