add test coverage for panics

This commit is contained in:
A.Unger
2021-06-08 13:23:14 +02:00
parent 83f628052d
commit 7c0a4bdc0d

View File

@@ -13,9 +13,11 @@ func Test_mustUserConfigDir(t *testing.T) {
extension string
}
tests := []struct {
name string
args args
want string
name string
args args
want string
resetHome bool
panic bool
}{
{
name: "fetch the default config location for the current user",
@@ -25,12 +27,36 @@ func Test_mustUserConfigDir(t *testing.T) {
},
want: filepath.Join(configDir, "ocis", "testing"),
},
{
name: "location cannot be determined becahse $HOME is not set",
args: args{
prefix: "ocis",
extension: "testing",
},
want: filepath.Join(configDir, "ocis", "testing"),
resetHome: true,
panic: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.resetHome {
if err := os.Setenv("HOME", ""); err != nil {
t.Error(err)
}
}
defer func() {
if r := recover(); r != nil && !tt.panic {
t.Errorf("should have panicked!")
}
}()
if got := MustUserConfigDir(tt.args.prefix, tt.args.extension); got != tt.want {
t.Errorf("MustUserConfigDir() = %v, want %v", got, tt.want)
}
})
}
}