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) } + }) } }