add new os pkg on ocis-pkg

This commit is contained in:
A.Unger
2021-06-08 13:08:32 +02:00
parent 24b94dd2f1
commit 83f628052d
5 changed files with 62 additions and 36 deletions
+17
View File
@@ -0,0 +1,17 @@
package os
import (
"os"
"path/filepath"
)
// MustUserConfigDir generates a default config location for a user based on their OS. This location can be used to store
// any artefacts the app needs for its functioning. It is a pure function. Its only side effect is that results vary
// depending on which operative system we're in.
func MustUserConfigDir(prefix, extension string) string {
dir, err := os.UserConfigDir()
if err != nil {
panic(err)
}
return filepath.Join(dir, prefix, extension)
}
+36
View File
@@ -0,0 +1,36 @@
package os
import (
"os"
"path/filepath"
"testing"
)
func Test_mustUserConfigDir(t *testing.T) {
configDir, _ := os.UserConfigDir()
type args struct {
prefix string
extension string
}
tests := []struct {
name string
args args
want string
}{
{
name: "fetch the default config location for the current user",
args: args{
prefix: "ocis",
extension: "testing",
},
want: filepath.Join(configDir, "ocis", "testing"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := MustUserConfigDir(tt.args.prefix, tt.args.extension); got != tt.want {
t.Errorf("MustUserConfigDir() = %v, want %v", got, tt.want)
}
})
}
}