Skip to content
This repository has been archived by the owner on Mar 16, 2024. It is now read-only.

Commit

Permalink
Adding more coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
rehabaam committed Nov 11, 2023
1 parent 38854cc commit 2ba8632
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 17 deletions.
13 changes: 7 additions & 6 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package config

import (
"fmt"
"os"
"path/filepath"

Expand Down Expand Up @@ -38,19 +39,19 @@ func Load() {
func readFile(cfg interface{}, path string) {
f, err := os.Open(filepath.Clean(path))
if err != nil {
panic(err)
fmt.Println(err.Error())
}
// defer the closing of our jsonFile so that we can parse it later on
// defer the closing of the file so that we can parse it later on
defer func() {
if err := f.Close(); err != nil {
panic(err)
fmt.Println(err.Error())
}
}()

decoder := yaml.NewDecoder(f)
err = decoder.Decode(cfg)
if err != nil {
panic(err)
fmt.Println(decoder)
if errD := decoder.Decode(cfg); errD != nil {
fmt.Println(errD.Error())
}
}

Expand Down
24 changes: 13 additions & 11 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,35 +40,37 @@ func Test_readFile(t *testing.T) {
path string
}
tests := []struct {
name string
args args
wantPanic bool
name string
args args
wantErr bool
}{
{
name: "givenExistPath_success",
args: args{
cfg: &AppConfig,
path: "prod.yml",
},
wantPanic: false,
wantErr: false,
},
{
name: "givenNotExistPath_getPanic_failure",
args: args{
cfg: nil,
path: "dummy-file.yml",
},
wantPanic: true,
wantErr: true,
},
{
name: "givenTestPath_getPanic_failure",
args: args{
cfg: 3,
path: "test.yml",
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
defer func() {
r := recover()
if (r != nil) != tt.wantPanic {
t.Errorf("readFile(), wantPanic = %v", tt.wantPanic)
}
}()
readFile(tt.args.cfg, tt.args.path)
})
}
Expand Down
1 change: 1 addition & 0 deletions config/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"test":"foo"}

0 comments on commit 2ba8632

Please sign in to comment.