-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathimporter_test.go
229 lines (190 loc) · 6.88 KB
/
importer_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
217
218
219
220
221
222
223
224
225
226
227
228
229
package jsonnext
import (
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
"github.com/stretchr/testify/require"
)
func TestAppendSearchFromEnv(t *testing.T) {
i := Importer{}
err := os.Setenv("IMPORTER_TEST", "//example.com/path:/local/path")
require.NoError(t, err)
i.AppendSearchFromEnv("IMPORTER_TEST")
require.Equal(t, []string{"//example.com/path", "/local/path"}, i.SearchPath)
}
func TestAppendSearchFromEnvNonExistent(t *testing.T) {
i := Importer{}
err := os.Unsetenv("IMPORTER_TEST")
require.NoError(t, err)
i.AppendSearchFromEnv("IMPORTER_TEST")
require.Empty(t, i.SearchPath)
}
func TestAppendSearchFromEnvEmpty(t *testing.T) {
i := Importer{}
err := os.Setenv("IMPORTER_TEST", "://example.com/path:::/local/path:")
require.NoError(t, err)
i.AppendSearchFromEnv("IMPORTER_TEST")
require.Equal(t, []string{"//example.com/path", "/local/path"}, i.SearchPath)
}
func TestImportLocal(t *testing.T) {
i := Importer{}
contents, foundAt, err := i.Import("", "testdata/importer/hello.txt")
require.NoError(t, err)
require.Equal(t, "hello world\n", contents.String())
require.Equal(t, "testdata/importer/hello.txt", foundAt)
}
func TestImportLocalNotFound(t *testing.T) {
i := Importer{}
_, _, err := i.Import("", "testdata/importer/notfound.txt")
require.Error(t, err)
}
// Make reading file by trying to read a directory. That causes read(2) to
// return EDIR.
func TestImportLocalReadError(t *testing.T) {
i := Importer{}
_, _, err := i.Import("", "testdata/importer")
require.Error(t, err)
}
func TestImportLocalSourceRelative(t *testing.T) {
i := Importer{}
contents, foundAt, err := i.Import("testdata/importer/hello.txt", "mellow.txt")
require.NoError(t, err)
require.Equal(t, "mellow world\n", contents.String())
require.Equal(t, "testdata/importer/mellow.txt", foundAt)
}
func TestImportLocalRelativeSearch(t *testing.T) {
i := Importer{}
i.SearchPath = []string{"testdata"}
contents, foundAt, err := i.Import("", "importer/hello.txt")
require.NoError(t, err)
require.Equal(t, "hello world\n", contents.String())
require.Equal(t, "testdata/importer/hello.txt", foundAt)
}
func TestImportNetpath(t *testing.T) {
s := httptest.NewTLSServer(http.FileServer(http.Dir("testdata")))
defer s.Close()
np := strings.TrimPrefix(s.URL, "https:")
i := Importer{Fetcher: s.Client()}
contents, foundAt, err := i.Import("", np+"/importer/hello.txt")
require.NoError(t, err)
require.Equal(t, "hello world\n", contents.String())
require.Equal(t, np+"/importer/hello.txt", foundAt)
}
func TestImportNetpathNotFound(t *testing.T) {
s := httptest.NewTLSServer(http.FileServer(http.Dir("testdata")))
defer s.Close()
np := strings.TrimPrefix(s.URL, "https:")
i := Importer{Fetcher: s.Client()}
_, _, err := i.Import("", np+"/importer/notfound.txt")
require.Error(t, err)
}
func TestImportNetpathSourceRelative(t *testing.T) {
s := httptest.NewTLSServer(http.FileServer(http.Dir("testdata")))
defer s.Close()
np := strings.TrimPrefix(s.URL, "https:")
i := Importer{Fetcher: s.Client()}
contents, foundAt, err := i.Import(np, "importer/mellow.txt")
require.NoError(t, err)
require.Equal(t, "mellow world\n", contents.String())
require.Equal(t, np+"/importer/mellow.txt", foundAt)
}
// Make the URLFetcher.Get() method return an error by not using the proper
// TLS client for it to work.
func TestImportNetpathFetchError(t *testing.T) {
s := httptest.NewTLSServer(http.FileServer(http.Dir("testdata")))
defer s.Close()
np := strings.TrimPrefix(s.URL, "https:")
i := Importer{} // dont use s.Client(), so we get a TLS error
_, _, err := i.Import("", np+"/importer/hello.txt")
require.Error(t, err)
}
// Make the httptest server return a non-404 error. By making open(2) fail
// with ELOOP (with a symlink that links to itself), we get a 500 error.
func TestImportNetpathHTTPError(t *testing.T) {
s := httptest.NewTLSServer(http.FileServer(http.Dir("testdata")))
defer s.Close()
np := strings.TrimPrefix(s.URL, "https:")
i := Importer{Fetcher: s.Client()}
_, _, err := i.Import("", np+"/importer/ELOOP")
require.Error(t, err)
}
type requestRecorder struct {
requests []*http.Request
next http.Handler
}
func (rr *requestRecorder) ServeHTTP(w http.ResponseWriter, r *http.Request) {
rr.requests = append(rr.requests, r)
rr.next.ServeHTTP(w, r)
}
// Test importing from the cache by importing the same thing twice. We record
// the requests to the http.Handler and count how many times it was called.
func TestImportFromCache(t *testing.T) {
rr := &requestRecorder{next: http.FileServer(http.Dir("testdata"))}
s := httptest.NewTLSServer(rr)
defer s.Close()
np := strings.TrimPrefix(s.URL, "https:")
i := Importer{Fetcher: s.Client()}
require.Equal(t, 0, len(rr.requests))
contents, foundAt, err := i.Import("", np+"/importer/hello.txt")
require.NoError(t, err)
require.Equal(t, "hello world\n", contents.String())
require.Equal(t, np+"/importer/hello.txt", foundAt)
require.Equal(t, 1, len(rr.requests))
contents, foundAt, err = i.Import("", np+"/importer/hello.txt")
require.NoError(t, err)
require.Equal(t, "hello world\n", contents.String())
require.Equal(t, np+"/importer/hello.txt", foundAt)
require.Equal(t, 1, len(rr.requests)) // still 1
}
// Test that an import of an absolute path does not go through the search
// path, by importing non-existent file.
func TestImportAbsolute(t *testing.T) {
rr := &requestRecorder{next: http.FileServer(http.Dir("testdata"))}
s := httptest.NewTLSServer(rr)
defer s.Close()
np := strings.TrimPrefix(s.URL, "https:")
i := Importer{Fetcher: s.Client()}
i.SearchPath = []string{np + "/importer", np + "/importer/https"}
require.Equal(t, 0, len(rr.requests))
_, _, err := i.Import("", np+"/importer/notfound.txt")
require.Error(t, err)
require.Equal(t, 1, len(rr.requests))
}
func TestImportSearchAllPaths(t *testing.T) {
rr := &requestRecorder{next: http.FileServer(http.Dir("testdata"))}
s := httptest.NewTLSServer(rr)
defer s.Close()
np := strings.TrimPrefix(s.URL, "https:")
i := Importer{Fetcher: s.Client()}
i.SearchPath = []string{np + "/importer", np + "/importer/https"}
require.Equal(t, 0, len(rr.requests))
_, _, err := i.Import("", "notfound.txt")
require.Error(t, err)
require.Equal(t, 2, len(rr.requests))
}
func TestPreserveNetRoot(t *testing.T) {
tests := map[string]struct{ inputOrig, inputClean, expected string }{
"single": {"/a/b/c", "/a/b", "/a/b"},
"double": {"//a/b/c", "/a/b", "//a/b"},
"emptyOrig": {"", "/a/b", "/a/b"},
"emptyClean": {"/a/b/c", "", ""},
}
for name, tt := range tests {
tt := tt
t.Run(name, func(t *testing.T) { //nolint:wsl
actual := preserveNetRoot(tt.inputOrig, tt.inputClean)
require.Equal(t, tt.expected, actual)
})
}
}
func TestFetcherNew(t *testing.T) {
i := Importer{}
require.IsType(t, &http.Client{}, i.fetcher())
}
func TestFetcherExisting(t *testing.T) {
expected := &http.Client{}
i := Importer{Fetcher: expected}
require.Equal(t, expected, i.fetcher())
}