-
Notifications
You must be signed in to change notification settings - Fork 0
/
root_test.go
76 lines (65 loc) · 1.62 KB
/
root_test.go
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
// Copyright(C) 2020 github.com/hidu All Rights Reserved.
// Author: hidu
// Date: 2020/5/2
package fsenv
import (
"os"
"path/filepath"
"testing"
"github.com/fsgo/fst"
)
func TestAppRoot(t *testing.T) {
wd, _ := os.Getwd()
tests := []struct {
name string
want string
}{
{
name: "case 1",
want: wd,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := AppRootDir()
if got != tt.want {
t.Errorf("AppRootDir() got = %v, want %v", got, tt.want)
}
})
}
}
func Test_chooseDirWithRootEnv(t *testing.T) {
key := "fsenv.k1"
fst.NoError(t, os.Unsetenv(key))
defer fst.NoError(t, os.Unsetenv(key))
fst.Equal(t, "v1", chooseDirWithRootEnv("v1", nil, key, "v2"))
fst.NoError(t, os.Setenv(key, "v3"))
fst.Equal(t, "v3", chooseDirWithRootEnv("", nil, key, "v2"))
fst.NoError(t, os.Unsetenv(key))
fst.NoError(t, os.Setenv(eKeyRoot, "root"))
fst.Equal(t, filepath.Join("root", "v2"), chooseDirWithRootEnv("", nil, key, "v2"))
e1 := NewAppRootEnv("v4")
fst.Equal(t, filepath.Join("v4", "v2"), chooseDirWithRootEnv("", e1, key, "v2"))
}
func TestAppRootEnv(t *testing.T) {
t.Run("init with value", func(t *testing.T) {
e1 := NewAppRootEnv("demo")
fst.Equal(t, "demo", e1.RootDir())
t.Run("twice", func(t *testing.T) {
defer func() {
fst.Nil(t, recover())
}()
e1.SetRootDir("test")
})
})
t.Run("init with empty", func(t *testing.T) {
e1 := NewAppRootEnv("")
fst.NoError(t, os.Setenv(eKeyRoot, "test"))
defer func() {
fst.NoError(t, os.Unsetenv(eKeyRoot))
}()
fst.Equal(t, "test", e1.RootDir())
e1.SetRootDir("v2")
fst.Equal(t, "v2", e1.RootDir())
})
}