From ec118318b5b82d093704fc124b18f1180a7b4b38 Mon Sep 17 00:00:00 2001 From: restitux Date: Wed, 14 Sep 2022 18:08:47 -0600 Subject: [PATCH] Change Get() to have a pointer receiver --- config.go | 2 +- config_test.go | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 1 deletion(-) diff --git a/config.go b/config.go index 37beefe..934ec36 100644 --- a/config.go +++ b/config.go @@ -31,7 +31,7 @@ func getConfigDir(n string) string { return filepath.Join(base, n) } -func (c Config[T]) Get() (bool, error) { +func (c *Config[T]) Get() (bool, error) { config_dir := getConfigDir(c.Name) c.ConfigDir = &config_dir diff --git a/config_test.go b/config_test.go index 29f359d..8dc0b0c 100644 --- a/config_test.go +++ b/config_test.go @@ -35,6 +35,57 @@ func Test_getConfigDir_XDG_CONFIG_HOME(t *testing.T) { } } +func Test_ConfigGetConfigDir_HOME(t *testing.T) { + os.Unsetenv("XDG_CONFIG_HOME") + os.Setenv("HOME", "/tmp") + + expected_config_dir := "/tmp/.config/test" + + config := Config[LocalConfig]{ + Name: "test", + Filename: "test", + Config: LocalConfig{}, + } + + os.RemoveAll(getConfigDir(config.Name)) + + _, err := config.Get() + if err != nil { + t.Errorf("Failed to get config: %v", err) + } + + if expected_config_dir != *config.ConfigDir { + t.Errorf("config.ConfigDir is %v, %v expected", *config.ConfigDir, expected_config_dir) + } else { + t.Log("Success!") + } +} + +func Test_ConfigGetConfigDir_XDG_CONFIG_HOME(t *testing.T) { + os.Setenv("XDG_CONFIG_HOME", "/tmp/") + + expected_config_dir := "/tmp/test" + + config := Config[LocalConfig]{ + Name: "test", + Filename: "test", + Config: LocalConfig{}, + } + + os.RemoveAll(getConfigDir(config.Name)) + + _, err := config.Get() + if err != nil { + t.Errorf("Failed to get config: %v", err) + } + + if expected_config_dir != *config.ConfigDir { + t.Errorf("config.ConfigDir is %v, %v expected", *config.ConfigDir, expected_config_dir) + } else { + t.Log("Success!") + } +} + func Test_ConfigGetReturnVal_NoConfigFile(t *testing.T) { os.Setenv("XDG_CONFIG_HOME", "/tmp") @@ -109,6 +160,15 @@ func Test_ConfigGetConfig_ConfigFile(t *testing.T) { t.Errorf("Failed to get config: %v", err) } + config = Config[LocalConfig]{ + Name: "ohea-config", + Filename: "config", + Config: LocalConfig{ + Address: "", + Port: 0, + }, + } + _, err = config.Get() if err != nil { t.Errorf("Failed to get config: %v", err)