-
-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathcontext_test.go
More file actions
127 lines (113 loc) · 2.88 KB
/
context_test.go
File metadata and controls
127 lines (113 loc) · 2.88 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
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) {
testCases := []struct {
Dir string
Path string
Expected 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("") + "/" + "."},
{"", "~", home("")},
{"", "~/file", home("file")},
}
for _, tc := range testCases {
actual, err := Context{Dir: tc.Dir}.Abs(tc.Path)
if err != nil {
t.Error(err.Error())
}
if tc.Expected != actual {
t.Errorf("context: '%v' arg: '%v' expected: '%v' was: '%v'", tc.Dir, tc.Path, tc.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{}
c.Setenv("REPLACE", "me")
for s, expected := range map[string]string{
"BEFORE${REPLACE}AFTER": "BEFOREmeAFTER",
"BEFORE${!REPLACE}AFTER": "BEFORE${REPLACE}AFTER",
"BEFORE${REPLACE:-default}AFTER": "BEFOREmeAFTER",
"BEFORE${!REPLACE:-default}AFTER": "BEFORE${REPLACE:-default}AFTER",
"BEFORE${REPLACE/me/you}AFTER": "BEFOREyouAFTER",
"BEFORE${!REPLACE/me/you}AFTER": "BEFORE${REPLACE/me/you}AFTER",
// TODO support curly brackets
// "BEFORE${REPLACE/me/with\\}curly}AFTER": "BEFOREwith}curlyAFTER",
// "BEFORE${!REPLACE/me/with\\}curly}AFTER": "BEFORE${REPLACE/me/with\\}curly}AFTER",
"BEFORE${UNSET:-default}AFTER": "BEFOREdefaultAFTER",
"BEFORE${!UNSET:-default}AFTER": "BEFORE${UNSET:-default}AFTER",
} {
t.Run(s, func(t *testing.T) {
actual, err := c.Envsubst(s)
if err != nil {
t.Fatal(err)
}
if actual != expected {
t.Fatalf("invalid replacement\nexpected: %#v\nactual : %#v", expected, actual)
}
})
}
}