This repository was archived by the owner on Jul 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathremote_fs_test.go
432 lines (356 loc) · 10.4 KB
/
remote_fs_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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
package main
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net"
"net/url"
"os"
"path"
"path/filepath"
"reflect"
"sort"
"testing"
"github.com/google/uuid"
"github.com/sourcegraph/go-langserver/pkg/lsp"
"github.com/sourcegraph/go-langserver/pkg/lspext"
"github.com/sourcegraph/jsonrpc2"
)
func TestClone(t *testing.T) {
fileList := []batchFile{
{
uri: "file:///a.py",
content: "This is file A.",
},
{
uri: "/b.py",
content: "This is file B.",
},
{
uri: "file:///dir/c.py",
content: "This is file C.",
},
{
uri: "file:///dir/d.go",
content: "This is file D.",
},
}
files := make(map[string]string)
for _, aFile := range fileList {
parsedFileURI, err := url.Parse(string(aFile.uri))
if err != nil {
t.Fatalf("unable to parse uri for batchFile %v: %v", aFile, err)
}
files[parsedFileURI.Path] = aFile.content
}
cases := []struct {
Name string
Globs []string
Want []string
}{
{
Name: "all",
Globs: nil,
Want: []string{"/a.py", "/b.py", "/dir/c.py", "/dir/d.go"},
},
{
Name: "subset",
Globs: []string{"*.py"},
Want: []string{"/a.py", "/b.py", "/dir/c.py"},
},
{
Name: "multi",
Globs: []string{"a*", "b*"},
Want: []string{"/a.py", "/b.py"},
},
{
Name: "none",
Globs: []string{"NOMATCH"},
Want: []string{},
},
}
for _, tt := range cases {
t.Run(tt.Name, func(t *testing.T) {
baseDir, err := ioutil.TempDir("", uuid.New().String()+"testClone")
if err != nil {
t.Fatalf("when creating temp directory for clone test, err: %v", err)
}
defer os.Remove(baseDir)
runTest(t, files, func(ctx context.Context, fs *remoteFS) {
want := make(map[string]string)
for _, k := range tt.Want {
want[k] = files[k]
}
err := fs.Clone(ctx, baseDir, tt.Globs)
if err != nil {
t.Errorf("when calling clone(baseDir=%s): %v", baseDir, err)
}
found, err := findAll(baseDir)
if err != nil {
t.Errorf("when calling Walk for baseDir %s: %v", baseDir, err)
}
if !reflect.DeepEqual(found, want) {
t.Errorf("for clone(baseDir=%s) expected %v, actual %v", baseDir, want, found)
}
})
})
}
}
func findAll(baseDir string) (map[string]string, error) {
found := make(map[string]string)
err := filepath.Walk(baseDir, func(currPath string, info os.FileInfo, err error) error {
if err != nil || info.IsDir() {
return err
}
content, err := ioutil.ReadFile(currPath)
if err != nil {
return err
}
currPath = path.Join("/", filepath.ToSlash(filepathTrimPrefix(currPath, baseDir)))
found[currPath] = string(content)
return nil
})
return found, err
}
func TestBatchOpen(t *testing.T) {
fileList := []batchFile{
{
uri: "file:///a.py",
content: "This is file A.",
},
{
uri: "/b.py",
content: "This is file B.",
},
{
uri: "file:///dir/c.py",
content: "This is file C.",
},
}
sort.Slice(fileList, func(i, j int) bool {
return fileList[i].uri < fileList[j].uri
})
files := make(map[string]string)
for _, aFile := range fileList {
parsedFileURI, err := url.Parse(string(aFile.uri))
if err != nil {
t.Fatalf("unable to parse uri for batchFile %v: %v", aFile, err)
}
files[parsedFileURI.Path] = aFile.content
}
// open single file
for _, aFile := range fileList {
runTest(t, files, func(ctx context.Context, fs *remoteFS) {
results, err := fs.BatchOpen(ctx, []lsp.DocumentURI{aFile.uri})
if err != nil {
t.Errorf("when calling batchOpen on uri %s: %v", aFile.uri, err)
}
if !reflect.DeepEqual(results, []batchFile{aFile}) {
t.Errorf("for batchOpen(paths=%v) expected %v, actual %v", []lsp.DocumentURI{aFile.uri}, []batchFile{aFile}, results)
}
})
}
// open multiple files
runTest(t, files, func(ctx context.Context, fs *remoteFS) {
var allURIs []lsp.DocumentURI
for _, aFile := range fileList {
allURIs = append(allURIs, aFile.uri)
}
results, err := fs.BatchOpen(ctx, allURIs)
if err != nil {
t.Errorf("when calling batchOpen on paths: %v, err: %v", allURIs, err)
}
sort.Slice(results, func(i, j int) bool {
return results[i].uri < results[j].uri
})
if !reflect.DeepEqual(results, fileList) {
t.Errorf("for batchOpen(paths=%v) expected %v, actual %v", allURIs, fileList, results)
}
})
// open single invalid file
runTest(t, files, func(ctx context.Context, fs *remoteFS) {
_, err := fs.BatchOpen(ctx, []lsp.DocumentURI{"/non/existent/file.py"})
if err == nil {
t.Error("expected error when trying to batchOpen non-existent file '/non/existent/file.py'")
}
})
// open multiple valid files and one invalid file
runTest(t, files, func(ctx context.Context, fs *remoteFS) {
allURIs := []lsp.DocumentURI{"non/existent/file.py"}
for _, aFile := range fileList {
allURIs = append(allURIs, aFile.uri)
}
_, err := fs.BatchOpen(ctx, allURIs)
if err == nil {
t.Errorf("expected error when trying to batchOpen(paths=%v) which includes non-existent file '/non/existent/file.py'", allURIs)
}
})
// open zero files
runTest(t, files, func(ctx context.Context, fs *remoteFS) {
results, err := fs.BatchOpen(ctx, []lsp.DocumentURI{})
if err != nil {
t.Errorf("when calling batchOpen on zero paths: %v", err)
}
if len(results) > 0 {
t.Error("expected zero results when trying to batchOpen zero paths")
}
})
}
func TestOpen(t *testing.T) {
fileList := []batchFile{
{
uri: "/a.py",
content: "This is file A.",
},
{
uri: "/b.py",
content: "This is file B.",
},
{
uri: "/dir/c.py",
content: "This is file C.",
},
}
files := make(map[string]string)
for _, aFile := range fileList {
files[string(aFile.uri)] = aFile.content
}
for _, aFile := range fileList {
runTest(t, files, func(ctx context.Context, fs *remoteFS) {
actualFileContent, err := fs.Open(ctx, aFile.uri)
if err != nil {
t.Errorf("when calling open on uri: %s, err: %v", aFile.uri, err)
}
if actualFileContent != aFile.content {
t.Errorf("for open(path=%s) expected %v, actual %v", aFile.uri, aFile.content, actualFileContent)
}
})
}
runTest(t, files, func(ctx context.Context, fs *remoteFS) {
_, err := fs.Open(ctx, "/c.py")
if err == nil {
t.Errorf("expected error when trying to open non-existent file '/c.py'")
}
})
}
func TestWalk(t *testing.T) {
type testCase struct {
fileNames []string
expectedFileURIs []string
}
tests := []testCase{
{
fileNames: []string{"/a.py", "/b.py", "/dir/c.py"},
expectedFileURIs: []string{"file:///a.py", "file:///b.py", "file:///dir/c.py"},
},
}
for _, test := range tests {
files := make(map[string]string)
for _, fileName := range test.fileNames {
files[fileName] = ""
}
runTest(t, files, func(ctx context.Context, fs *remoteFS) {
actualFileURIs, err := fs.Walk(ctx)
if err != nil {
t.Errorf("when calling walk: %v", err)
}
var actualFileNames []string
for _, uri := range actualFileURIs {
actualFileNames = append(actualFileNames, string(uri))
}
sort.Strings(actualFileNames)
sort.Strings(test.expectedFileURIs)
if len(actualFileNames) == 0 && len(test.expectedFileURIs) == 0 {
// special case empty slice versus nil comparison below?
return
}
if !reflect.DeepEqual(actualFileNames, test.expectedFileURIs) {
t.Errorf("for walk expected %v, actual %v", test.expectedFileURIs, actualFileNames)
}
})
}
}
func runTest(t *testing.T, files map[string]string, checkFunc func(ctx context.Context, fs *remoteFS)) {
ctx := context.Background()
a, b := net.Pipe()
defer a.Close()
defer b.Close()
clientConn := jsonrpc2.NewConn(ctx, jsonrpc2.NewBufferedStream(a, jsonrpc2.VSCodeObjectCodec{}), &testFS{
t: t,
files: files,
})
serverConn := jsonrpc2.NewConn(ctx, jsonrpc2.NewBufferedStream(b, jsonrpc2.VSCodeObjectCodec{}), &noopHandler{})
defer clientConn.Close()
defer serverConn.Close()
fs := &remoteFS{
conn: serverConn,
}
checkFunc(ctx, fs)
}
type testFS struct {
t *testing.T
files map[string]string // map of file names to content
}
func (client *testFS) Handle(ctx context.Context, conn *jsonrpc2.Conn, req *jsonrpc2.Request) {
if req.Notif {
return
}
switch req.Method {
case "textDocument/xcontent":
var contentParams lspext.ContentParams
if err := json.Unmarshal(*req.Params, &contentParams); err != nil {
client.t.Fatalf("unable to unmarshal params %v for textdocument/xcontent, err: %v", req.Params, err)
}
filePathRawURI := string(contentParams.TextDocument.URI)
filePathURI, err := url.Parse(filePathRawURI)
if err != nil {
client.t.Fatalf("unable to parse URI %vfor textdocument/xcontent, err: %v", filePathRawURI, err)
}
content, present := client.files[filePathURI.Path]
if !present {
err := &jsonrpc2.Error{
Code: jsonrpc2.CodeInvalidParams,
Message: fmt.Sprintf("requested file path %s does not exist", filePathURI),
Data: nil,
}
if replyErr := conn.ReplyWithError(ctx, req.ID, err); replyErr != nil {
client.t.Fatalf("error when sending back error reply for document %s, err: %v", filePathURI, replyErr)
}
return
}
document := lsp.TextDocumentItem{
URI: contentParams.TextDocument.URI,
Text: content,
}
if replyErr := conn.Reply(ctx, req.ID, document); replyErr != nil {
client.t.Fatalf("error when sending back content reply for document %v, err:%v", document, replyErr)
}
case "workspace/xfiles":
var results []lsp.TextDocumentIdentifier
for filePath := range client.files {
fileURI, err := url.Parse(filePath)
if err != nil {
client.t.Fatalf("unable to parse filePath %s as URI for workspace/xfiles, err: %v", filePath, err)
}
fileURI.Scheme = "file"
results = append(results, lsp.TextDocumentIdentifier{
URI: lsp.DocumentURI(fileURI.String()),
})
}
if replyErr := conn.Reply(ctx, req.ID, results); replyErr != nil {
client.t.Fatalf("error when sending back files reply, err: %v", replyErr)
}
default:
err := &jsonrpc2.Error{
Code: jsonrpc2.CodeMethodNotFound,
Message: fmt.Sprintf("method %s is invalid - only textdocument/xcontent and workspace/xfiles are supported", req.Method),
Data: nil,
}
if replyErr := conn.ReplyWithError(ctx, req.ID, err); replyErr != nil {
client.t.Fatalf("error when sending back error reply for invalid method %s, err: %v", req.Method, replyErr)
}
}
}
type noopHandler struct{}
func (noopHandler) Handle(ctx context.Context, conn *jsonrpc2.Conn, req *jsonrpc2.Request) {}