From 7c0a4bdc0d4ec91623fe2a730d16c79320599f38 Mon Sep 17 00:00:00 2001 From: "A.Unger" Date: Tue, 8 Jun 2021 13:23:14 +0200 Subject: [PATCH] add test coverage for panics --- ocis-pkg/os/os_test.go | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/ocis-pkg/os/os_test.go b/ocis-pkg/os/os_test.go index 29f627b66e..24d56fe9a7 100644 --- a/ocis-pkg/os/os_test.go +++ b/ocis-pkg/os/os_test.go @@ -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) } + }) } }