forked from golang/config
Change Get() to have a pointer receiver
This commit is contained in:
@@ -31,7 +31,7 @@ func getConfigDir(n string) string {
|
|||||||
return filepath.Join(base, n)
|
return filepath.Join(base, n)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c Config[T]) Get() (bool, error) {
|
func (c *Config[T]) Get() (bool, error) {
|
||||||
config_dir := getConfigDir(c.Name)
|
config_dir := getConfigDir(c.Name)
|
||||||
c.ConfigDir = &config_dir
|
c.ConfigDir = &config_dir
|
||||||
|
|
||||||
|
|||||||
@@ -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) {
|
func Test_ConfigGetReturnVal_NoConfigFile(t *testing.T) {
|
||||||
os.Setenv("XDG_CONFIG_HOME", "/tmp")
|
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)
|
t.Errorf("Failed to get config: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
config = Config[LocalConfig]{
|
||||||
|
Name: "ohea-config",
|
||||||
|
Filename: "config",
|
||||||
|
Config: LocalConfig{
|
||||||
|
Address: "",
|
||||||
|
Port: 0,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
_, err = config.Get()
|
_, err = config.Get()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Failed to get config: %v", err)
|
t.Errorf("Failed to get config: %v", err)
|
||||||
|
|||||||
Reference in New Issue
Block a user