Change Get() to have a pointer receiver

This commit is contained in:
2022-09-14 18:08:47 -06:00
parent 03c9ea3081
commit ec118318b5
2 changed files with 61 additions and 1 deletions
+1 -1
View File
@@ -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
+60
View File
@@ -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)