1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
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)
}
})
}
}
|