Browse Source

fix for required / non existent config

master
Lars Hoogestraat 12 months ago
parent
commit
c99b2380b7
3 changed files with 36 additions and 9 deletions
  1. +1
    -1
      cfg.go
  2. +33
    -6
      cfg_test.go
  3. +2
    -2
      testcfg/config.conf

+ 1
- 1
cfg.go View File

@ -143,7 +143,7 @@ func (c ConfigFiles) MergeConfigsInto(dest interface{}) (map[string]Default, err
f, err := os.Open(filepath.Join(v.Path, v.Name))
if err != nil {
if !v.Required {
if !v.Required && os.IsNotExist(err) {
continue
}
return nil, err


+ 33
- 6
cfg_test.go View File

@ -7,6 +7,33 @@ import (
"git.hoogi.eu/snafu/cfg"
)
func TestFailConfig(t *testing.T) {
type settings struct {
Server struct {
Address string `cfg:"server_address"`
Port int `cfg:"server_port"`
}
}
c := addConfig("./testcfg", "notexisting.conf", true)
s := new(settings)
_, err := c.MergeConfigsInto(s)
if err == nil {
t.Error(err)
}
c = addConfig("./testcfg", "notexisting.conf", false)
_, err = c.MergeConfigsInto(s)
if err != nil {
t.Error(err)
}
}
func TestStandardConfig(t *testing.T) {
type settings struct {
SessionName string `cfg:"session_name"`
@ -22,7 +49,7 @@ func TestStandardConfig(t *testing.T) {
SessionTimeout time.Duration `cfg:"session_timeout"`
}
c := addConfig("./testcfg", "config.conf")
c := addConfig("./testcfg", "config.conf", true)
s := new(settings)
@ -79,7 +106,7 @@ func TestInnerStruct(t *testing.T) {
}
}
c := addConfig("./testcfg", "config.conf")
c := addConfig("./testcfg", "config.conf", true)
s := new(settings)
@ -147,7 +174,7 @@ func TestCustomType(t *testing.T) {
Custom loginMethod `cfg:"login_method"`
}
c := addConfig("./testcfg", "config.conf")
c := addConfig("./testcfg", "config.conf", true)
s := new(settings)
@ -172,7 +199,7 @@ func TestFileSizes(t *testing.T) {
Empty cfg.FileSize `cfg:"file_size_empty"`
}
c := addConfig("./testcfg", "config.conf")
c := addConfig("./testcfg", "config.conf", true)
s := new(settings)
@ -203,12 +230,12 @@ func TestFileSizes(t *testing.T) {
}
func addConfig(path, filename string) cfg.ConfigFiles {
func addConfig(path, filename string, required bool) cfg.ConfigFiles {
cfg := cfg.ConfigFiles{
Files: make([]cfg.File, 0, 1),
}
cfg.AddConfig(path, filename, true)
cfg.AddConfig(path, filename, required)
return cfg
}

+ 2
- 2
testcfg/config.conf View File

@ -20,5 +20,5 @@ file_size_kilobyte = 1 KB
file_size_megabyte = 1 MB
file_size_gigabyte = 1 GB
file_size_empty =
file_size_error = NOT_A_FILE_SIZE
file_size_terabyte = 1 TB
file_size_terabyte = 1 TB
file_size_error = NOT_A_FILE_SIZE

Loading…
Cancel
Save