diff options
author | Robin Jarry <robin@jarry.cc> | 2023-08-23 19:51:54 +0200 |
---|---|---|
committer | Robin Jarry <robin@jarry.cc> | 2023-08-27 18:44:06 +0200 |
commit | fff16640ad7cd8c4b73187fbce10f2aa558701be (patch) | |
tree | 8112c2d2414231d78d05dc255a430512c26fc4e1 /lib/xdg/home_test.go | |
parent | bc8fdbbe8479f9f604f429931e7c90e396ea6f00 (diff) | |
download | aerc-fff16640ad7cd8c4b73187fbce10f2aa558701be.tar.gz |
xdg: add functions to deal with user home paths
These are intended to replace the following deprecated libraries:
github.com/kyoh86/xdg
github.com/mitchellh/go-homedir
The feature set should be roughly equivalent with some tweaks to make
our life easier in aerc.
Signed-off-by: Robin Jarry <robin@jarry.cc>
Reviewed-by: Moritz Poldrack <moritz@poldrack.dev>
Diffstat (limited to 'lib/xdg/home_test.go')
-rw-r--r-- | lib/xdg/home_test.go | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/lib/xdg/home_test.go b/lib/xdg/home_test.go new file mode 100644 index 00000000..673e35b5 --- /dev/null +++ b/lib/xdg/home_test.go @@ -0,0 +1,88 @@ +package xdg + +import ( + "errors" + "os/user" + "testing" +) + +func TestHomeDir(t *testing.T) { + t.Run("from env", func(t *testing.T) { + t.Setenv("HOME", "/home/user") + home := HomeDir() + if home != "/home/user" { + t.Errorf(`got %q expected "/home/user"`, home) + } + }) + t.Run("from getpwuid_r", func(t *testing.T) { + t.Setenv("HOME", "") + orig := currentUser + currentUser = func() (*user.User, error) { + return &user.User{HomeDir: "/home/user"}, nil + } + home := HomeDir() + currentUser = orig + if home != "/home/user" { + t.Errorf(`got %q expected "/home/user"`, home) + } + }) + t.Run("failure", func(t *testing.T) { + t.Setenv("HOME", "") + orig := currentUser + currentUser = func() (*user.User, error) { + return nil, errors.New("no such user") + } + home := HomeDir() + currentUser = orig + if home != "" { + t.Errorf(`got %q expected ""`, home) + } + }) +} + +func TestExpandHome(t *testing.T) { + t.Setenv("HOME", "/home/user") + vectors := []struct { + args []string + expected string + }{ + {args: []string{"foo"}, expected: "foo"}, + {args: []string{"foo", "bar"}, expected: "foo/bar"}, + {args: []string{"/foobar/baz"}, expected: "/foobar/baz"}, + {args: []string{"~/foobar/baz"}, expected: "/home/user/foobar/baz"}, + {args: []string{}, expected: ""}, + {args: []string{"~"}, expected: "/home/user"}, + } + for _, vec := range vectors { + t.Run(vec.expected, func(t *testing.T) { + res := ExpandHome(vec.args...) + if res != vec.expected { + t.Errorf("got %q expected %q", res, vec.expected) + } + }) + } +} + +func TestTildeHome(t *testing.T) { + t.Setenv("HOME", "/home/user") + vectors := []struct { + arg string + expected string + }{ + {arg: "foo", expected: "foo"}, + {arg: "foo/bar", expected: "foo/bar"}, + {arg: "/foobar/baz", expected: "/foobar/baz"}, + {arg: "/home/user/foobar/baz", expected: "~/foobar/baz"}, + {arg: "", expected: ""}, + {arg: "/home/user", expected: "~"}, + {arg: "/home/user2/foobar/baz", expected: "/home/user2/foobar/baz"}, + } + for _, vec := range vectors { + t.Run(vec.expected, func(t *testing.T) { + res := TildeHome(vec.arg) + if res != vec.expected { + t.Errorf("got %q expected %q", res, vec.expected) + } + }) + } +} |