Skip to content

Commit

Permalink
bugfix: avoid nil and add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aooohan committed Jan 23, 2024
1 parent c4f6d44 commit 5763ac1
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 3 deletions.
12 changes: 10 additions & 2 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ var (
}
)

func NewConfig(path string) (*Config, error) {
p := filepath.Join(path, filename)
func NewConfigWithPath(p string) (*Config, error) {
if !util.FileExists(p) {
content, err := yaml.Marshal(defaultConfig)
if err != nil {
Expand All @@ -53,5 +52,14 @@ func NewConfig(path string) (*Config, error) {
if err != nil {
return nil, err
}
if config.Proxy == nil {
config.Proxy = EmptyProxy
}
return config, nil

}

func NewConfig(path string) (*Config, error) {
p := filepath.Join(path, filename)
return NewConfigWithPath(p)
}
5 changes: 4 additions & 1 deletion internal/config/config.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
proxy:
enable: false
url: http://test
url: http://test

storage:
path: /tmp
12 changes: 12 additions & 0 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,15 @@ func TestConfig_Proxy(t *testing.T) {
t.Fatal("proxy enable is invalid")
}
}
func TestConfigWithEmptyProxy(t *testing.T) {
c, err := config.NewConfigWithPath("empty_test.yaml")
if err != nil {
t.Fatal(err)
}
if c.Proxy.Url != "" {
t.Fatal("proxy url must be empty")
}
if !c.Proxy.Enable == false {
t.Fatal("proxy enable must be false")
}
}
Empty file added internal/config/empty_test.yaml
Empty file.

0 comments on commit 5763ac1

Please sign in to comment.