Skip to content

Commit

Permalink
Fix go-lint violations in cfg package (#338)
Browse files Browse the repository at this point in the history
  • Loading branch information
orsinium authored Oct 7, 2020
1 parent d7f7d8b commit 173471d
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 19 deletions.
5 changes: 4 additions & 1 deletion cfg/aesbackend.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,10 @@ func (b *AESBackend) Save(config *Config) error {
u := config.URL()
cfgDir := filepath.Dir(u.Path)
if !exist(cfgDir) {
os.MkdirAll(cfgDir, 0755)
err := os.MkdirAll(cfgDir, 0755)
if err != nil {
return err
}
}

j, err := json.MarshalIndent(config, "", " ")
Expand Down
19 changes: 14 additions & 5 deletions cfg/aesbackend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func TestAESBackendLoad(t *testing.T) {
if err != nil {
t.Fatalf("Can't create AES backend: %v", err)
}
conf, err = backend.Load(u)
_, err = backend.Load(u)
if err != nil {
t.Errorf("loading the config file using the environment password should work. %v", err)
}
Expand All @@ -88,7 +88,7 @@ func TestAESBackendLoad(t *testing.T) {
if err != nil {
t.Fatalf("Can't create AES backend: %v", err)
}
conf, err = backend.Load(u)
_, err = backend.Load(u)
if err == nil || err.Error() != "cipher: message authentication failed" {
t.Errorf("loading the config file with an invalid password should fail. %v", err)
}
Expand All @@ -104,7 +104,7 @@ func TestAESBackendLoad(t *testing.T) {
if err != nil {
t.Fatalf("Can't create AES backend: %v", err)
}
conf, err = backend.Load(u)
_, err = backend.Load(u)
if err != nil {
t.Errorf("the password defined in %s should take precedence. %v", PasswordEnvVar, err)
}
Expand All @@ -113,7 +113,7 @@ func TestAESBackendLoad(t *testing.T) {
if err != nil {
t.Fatalf("Can't parse crypto URL: %v", err)
}
conf, err = backend.Load(u)
_, err = backend.Load(u)
if err == nil || err.Error() != "encrypted configuration header not valid" {
t.Errorf("the password defined in %s should take precedence. %v", PasswordEnvVar, err)
}
Expand All @@ -127,6 +127,9 @@ func TestAESBackendSave(t *testing.T) {

cwd, _ := os.Getwd()
u, err := url.Parse(filepath.Join("crypto://"+testPassword+"@", cwd, "testdata", "beehive-crypto.conf"))
if err != nil {
t.Error("cannot parse config url")
}
backend, _ := NewAESBackend(u)
c, err := backend.Load(u)
if err != nil {
Expand All @@ -136,7 +139,13 @@ func TestAESBackendSave(t *testing.T) {
// Save the config file to a new absolute path using a URL
p := filepath.Join(tmpdir, "beehive-crypto.conf")
u, err = url.Parse("crypto://" + testPassword + "@" + p)
c.SetURL(u.String())
if err != nil {
t.Error("cannot parse config url")
}
err = c.SetURL(u.String())
if err != nil {
t.Error("cannot set url")
}
backend, _ = NewAESBackend(u)
err = backend.Save(c)
if err != nil {
Expand Down
6 changes: 1 addition & 5 deletions cfg/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,5 @@ func Lookup() string {

func exist(file string) bool {
_, err := os.Stat(file)
if err == nil {
return true
}

return false
return err == nil
}
15 changes: 9 additions & 6 deletions cfg/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ import (
func TestNew(t *testing.T) {
conf, err := New("/foobar")
if err != nil {
panic(err)
t.Fatal("cannot create config from path")
}
if _, ok := conf.Backend().(*FileBackend); !ok {
t.Error("Backend for '/foobar' should be a FileBackend")
}

conf, err = New("file:///foobar")
if err != nil {
panic(err)
t.Fatal("cannot create config from file:// path")
}
if _, ok := conf.Backend().(*FileBackend); !ok {
t.Error("Backend for 'file:///foobar' should be a FileBackend")
Expand All @@ -26,24 +26,27 @@ func TestNew(t *testing.T) {
cwd, _ := os.Getwd()
p := filepath.Join(cwd, "testdata/beehive-crypto.conf")
conf, err = New(p)
if err != nil {
t.Fatal(err)
}
if _, ok := conf.Backend().(*AESBackend); !ok {
t.Errorf("Backend for '%s' should be an AESBackend", p)
}

conf, err = New("mem:")
if err != nil {
panic(err)
t.Fatal("cannot create config from memory")
}
if _, ok := conf.Backend().(*MemBackend); !ok {
t.Error("Backend for 'mem:' should be a MemoryBackend")
}

conf, err = New("c:\\foobar")
_, err = New("c:\\foobar")
if err == nil {
t.Error("Not a valid URL, should return an error")
}

conf, err = New("")
_, err = New("")
if err == nil {
t.Error("Not a valid URL, should return an error")
}
Expand All @@ -52,7 +55,7 @@ func TestNew(t *testing.T) {
func TestLoad(t *testing.T) {
conf, err := New("mem://")
if err != nil {
panic(err)
t.Fatal(err)
}
err = conf.Load()
if err != nil {
Expand Down
5 changes: 4 additions & 1 deletion cfg/filebackend.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ func (fs *FileBackend) Load(u *url.URL) (*Config, error) {
func (fs *FileBackend) Save(config *Config) error {
cfgDir := filepath.Dir(config.URL().Path)
if !exist(cfgDir) {
os.MkdirAll(cfgDir, 0755)
err := os.MkdirAll(cfgDir, 0755)
if err != nil {
return err
}
}

j, err := json.MarshalIndent(config, "", " ")
Expand Down
24 changes: 23 additions & 1 deletion cfg/filebackend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ func TestFileLoad(t *testing.T) {

// try to load the config from a relative path
u, err = url.Parse(filepath.Join("testdata", "beehive.conf"))
if err != nil {
t.Error("cannot parse config path")
}
backend = NewFileBackend()
conf, err := backend.Load(u)
if err != nil {
Expand All @@ -31,6 +34,9 @@ func TestFileLoad(t *testing.T) {
// try to load the config from an absolute path using a URI
cwd, _ := os.Getwd()
u, err = url.Parse(filepath.Join("file://", cwd, "testdata", "beehive.conf"))
if err != nil {
t.Error("cannot parse config path")
}
backend = NewFileBackend()
conf, err = backend.Load(u)
if err != nil {
Expand All @@ -48,6 +54,9 @@ func TestFileSave(t *testing.T) {
}

u, err := url.Parse(filepath.Join("testdata", "beehive.conf"))
if err != nil {
t.Error("cannot parse config path")
}
backend := NewFileBackend()
c, err := backend.Load(u)
if err != nil {
Expand All @@ -57,7 +66,13 @@ func TestFileSave(t *testing.T) {
// Save the config file to a new absolute path using a URL
p := filepath.Join(tmpdir, "beehive.conf")
u, err = url.Parse("file://" + p)
c.SetURL(u.String())
if err != nil {
t.Error("cannot parse config path")
}
err = c.SetURL(u.String())
if err != nil {
t.Error("cannot set url")
}
backend = NewFileBackend()
err = backend.Save(c)
if err != nil {
Expand All @@ -74,6 +89,13 @@ func TestFileSave(t *testing.T) {
// Save the config file to a new absolute path using a regular path
p = filepath.Join(tmpdir, "beehive.conf")
u, err = url.Parse(p)
if err != nil {
t.Error("cannot parse config path")
}
err = c.SetURL(u.String())
if err != nil {
t.Error("cannot set url")
}
err = backend.Save(c)
if err != nil {
t.Errorf("Failed to save the config to %s", p)
Expand Down

0 comments on commit 173471d

Please sign in to comment.