-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
context_test.go
110 lines (96 loc) · 2.29 KB
/
context_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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package carapace
import (
"os"
"path/filepath"
"strings"
"testing"
)
func wd(s string) string {
if wd, _ := os.Getwd(); s != "" {
return wd + "/" + s
} else {
return wd
}
}
func home(s string) string {
if hd, _ := os.UserHomeDir(); s != "" {
return hd + "/" + s
} else {
return hd
}
}
func parent(s string) string {
if s != "" {
return strings.TrimSuffix(filepath.Dir(wd("")), "/") + "/" + s
}
return strings.TrimSuffix(filepath.Dir(wd("")), "/") + "/"
}
func TestContextAbs(t *testing.T) {
tests := append([]string{},
"/", "file", "/file",
"", "file", wd("file"),
"", "../", parent(""),
"", "../file", parent("file"),
"", "~/file", home("file"),
"/", "~/file", home("file"),
"/", "file", "/file",
"/dir", "file", "/dir/file",
"/dir", "./.file", "/dir/.file",
"", "/dir/", "/dir/",
"/dir/", "", "/dir/",
"~/", "file", home("file"),
"", "/", "/",
"", ".hidden", wd(".hidden"),
"", "./", wd("")+"/",
"", "", wd("")+"/",
"", ".", wd("")+"/"+".",
)
for index := 0; index < len(tests); index += 3 {
actual, err := Context{Dir: tests[index]}.Abs(tests[index+1])
if err != nil {
t.Error(err.Error())
}
if expected := tests[index+2]; expected != actual {
t.Errorf("context: '%v' arg: '%v' expected: '%v' was: '%v'", tests[index], tests[index+1], expected, actual)
}
}
}
func TestEnv(t *testing.T) {
c := Context{}
if c.Getenv("example") != "" {
t.Fail()
}
if v, exist := c.LookupEnv("example"); v != "" || exist {
t.Fail()
}
c.Setenv("example", "value")
if c.Getenv("example") != "value" {
t.Fail()
}
if v, exist := c.LookupEnv("example"); v != "value" || !exist {
t.Fail()
}
c.Setenv("example", "newvalue")
if c.Getenv("example") != "newvalue" {
t.Fail()
}
if v, exist := c.LookupEnv("example"); v != "newvalue" || !exist {
t.Fail()
}
}
func TestEnvsubst(t *testing.T) {
c := Context{}
if s, err := c.Envsubst("start${example}end"); s != "startend" || err != nil {
t.Fail()
}
if s, err := c.Envsubst("start${example:-default}end"); s != "startdefaultend" || err != nil {
t.Fail()
}
c.Setenv("example", "value")
if s, err := c.Envsubst("start${example}end"); s != "startvalueend" || err != nil {
t.Fail()
}
if s, err := c.Envsubst("start${example:-default}end"); s != "startvalueend" || err != nil {
t.Fail()
}
}