-
-
Notifications
You must be signed in to change notification settings - Fork 34
/
path_test.go
216 lines (207 loc) · 4.41 KB
/
path_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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package runn
import (
"os"
"path/filepath"
"testing"
)
func TestFp(t *testing.T) {
currentGlobalCacheDir := globalCacheDir
globalCacheDir = t.TempDir()
t.Cleanup(func() {
globalCacheDir = currentGlobalCacheDir
})
root, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
tests := []struct {
name string
p string
readRemote bool
readParent bool
want string
wantErr bool
}{
{
"Join root and path",
"path/to/book.yml",
false,
false,
filepath.Join(root, "path/to/book.yml"),
false,
},
{
"scope `read:parent` error",
"/path/to/book.yml",
false,
false,
"",
true,
},
{
"allow scope `read:parent`",
"/path/to/book.yml",
false,
true,
"/path/to/book.yml",
false,
},
{
"Join root and path with relative path",
"path/../book.yml",
false,
false,
filepath.Join(root, "book.yml"),
false,
},
{
"scope `read:parent` error with relative path",
"../book.yml",
false,
false,
"",
true,
},
{
"allow scope `read:parent` with relative path",
"../book.yml",
false,
true,
filepath.Join(filepath.Dir(root), "book.yml"),
false,
},
{
"scope `read:remote` error",
filepath.Join(globalCacheDir, "path/to/book.yml"),
false,
true,
"",
true,
},
{
"allow scope `read:remote`",
filepath.Join(globalCacheDir, "path/to/book.yml"),
true,
false,
filepath.Join(globalCacheDir, "path/to/book.yml"),
false,
},
{
"Join root and path with file://",
"file://path/to/book.yml",
false,
false,
filepath.Join(root, "path/to/book.yml"),
false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
globalScopes.mu.Lock()
globalScopes.readParent = tt.readParent
globalScopes.readRemote = tt.readRemote
globalScopes.mu.Unlock()
t.Cleanup(func() {
globalScopes.mu.Lock()
globalScopes.readParent = false
globalScopes.readRemote = false
globalScopes.mu.Unlock()
})
got, err := fp(tt.p, root)
if err != nil {
if !tt.wantErr {
t.Errorf("got %v", err)
}
return
}
if tt.wantErr {
t.Errorf("want error")
return
}
if got != tt.want {
t.Errorf("got %v\nwant %v", got, tt.want)
}
})
}
}
func TestFetchPaths(t *testing.T) {
tests := []struct {
pathp string
want int
wantErr bool
}{
{"testdata/book/book.yml", 1, false},
{"testdata/book/notexist.yml", 0, true},
{"testdata/book/runn_*", 4, false},
{"testdata/book/book.yml:testdata/book/http.yml", 2, false},
{"testdata/book/book.yml:testdata/book/runn_*.yml", 5, false},
{"testdata/book/book.yml:testdata/book/book.yml", 1, false},
{"testdata/book/runn_0_success.yml:testdata/book/runn_*.yml", 4, false},
{"github://k1LoW/runn/testdata/book/book.yml", 1, false},
{"github://k1LoW/runn/testdata/book/runn_*", 4, false},
{"https://raw.githubusercontent.com/k1LoW/runn/main/testdata/book/book.yml", 1, false},
{"file://testdata/book/book.yml", 1, false},
}
if os.Getenv("CI") == "" {
// GITHUB_TOKEN for GitHub Actions does not have permission to access Gist
tests = append(tests, []struct {
pathp string
want int
wantErr bool
}{
// Single file
{"gist://b908ae0721300ca45f4e8b81b6be246d", 1, false},
{"gist://b908ae0721300ca45f4e8b81b6be246d/book.yml", 1, false},
// Multiple files
{"gist://def6fa739fba3fcf211b018f41630adc", 0, true},
{"gist://def6fa739fba3fcf211b018f41630adc/book.yml", 1, false},
}...)
}
globalScopes.mu.RLock()
globalScopes.readRemote = true
globalScopes.mu.RUnlock()
t.Cleanup(func() {
if err := RemoveCacheDir(); err != nil {
t.Fatal(err)
}
globalScopes.mu.RLock()
globalScopes.readRemote = false
globalScopes.mu.RUnlock()
})
for _, tt := range tests {
t.Run(tt.pathp, func(t *testing.T) {
paths, err := fetchPaths(tt.pathp)
if err != nil {
if !tt.wantErr {
t.Errorf("got %v", err)
}
return
}
if tt.wantErr {
t.Errorf("want err")
}
got := len(paths)
if got != tt.want {
t.Errorf("got %v\nwant %v", got, tt.want)
}
})
}
}
func TestShortenPath(t *testing.T) {
tests := []struct {
in string
want string
}{
{"path/to/book.yml", "p/t/book.yml"},
{"book.yml", "book.yml"},
{"/path/to/book.yml", "/p/t/book.yml"},
}
for _, tt := range tests {
t.Run(tt.in, func(t *testing.T) {
got := ShortenPath(tt.in)
if got != tt.want {
t.Errorf("got %v\nwant %v", got, tt.want)
}
})
}
}