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 pathuris_test.go
164 lines (134 loc) · 4.84 KB
/
uris_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
package main
import (
"encoding/json"
"net/url"
"path"
"reflect"
"sort"
"strings"
"testing"
"github.com/google/uuid"
"github.com/sourcegraph/go-langserver/pkg/lsp"
)
func TestProbablyFileURI(t *testing.T) {
tests := map[string]bool{
"file:///a.py": true,
"file:///a.py#line=1,char=2": true,
"/a.py": true,
"file:///": true,
// We don't want to rewrite uris with an explicit non-file scheme
"git:///a.py": false,
// We don't want to rewrite uris that have empty paths
"file://": false,
"": false,
}
for rawURIStr, expected := range tests {
parsedURI, err := url.Parse(rawURIStr)
if err != nil {
t.Fatalf("got error %s when parsing test uri %s", err, rawURIStr)
}
actual := probablyFileURI(parsedURI)
if actual != expected {
t.Errorf("for uri %v, expected %v, actual %v", parsedURI, expected, actual)
}
}
}
func TestClientToServerURI(t *testing.T) {
cacheDir := path.Join("/", uuid.New().String(), "TestClientToServerURI")
projectFileLoc := "/a.py"
cacheFileLoc := path.Join(cacheDir, projectFileLoc)
tests := map[string]string{
"file://" + projectFileLoc: "file://" + cacheFileLoc,
projectFileLoc: cacheFileLoc,
// ensure that only the path is modified when rewriting a uri
"file://" + projectFileLoc + "#line=1,char=2": "file://" + cacheFileLoc + "#line=1,char=2",
projectFileLoc + "#line=1,char=2": cacheFileLoc + "#line=1,char=2",
"file:///": "file://" + path.Join(cacheDir, "/"),
"/": path.Join(cacheDir, "/"),
// don't rewrite uris with an explicit non-file scheme
"git:///sourcegraph.com/sourcegraph?SHA": "git:///sourcegraph.com/sourcegraph?SHA",
// don't rewruite uris with empty paths
"file://": "file://",
"": "",
}
for clientURI, rewrittenURI := range tests {
actual := clientToServerURI(lsp.DocumentURI(clientURI), cacheDir)
if actual != lsp.DocumentURI(rewrittenURI) {
t.Errorf("for uri %s, expected %s, actual %s", clientURI, rewrittenURI, actual)
}
}
}
func TestServerToClientURI(t *testing.T) {
cacheDir := path.Join("/", uuid.New().String(), "TestServerToClientURI")
projectFileLoc := "/a.py"
cacheFileLoc := path.Join(cacheDir, projectFileLoc)
tests := map[string]string{
"file://" + cacheFileLoc: "file://" + projectFileLoc,
cacheFileLoc: projectFileLoc,
// ensure that only the path is modified when rewriting a uri
"file://" + cacheFileLoc + "#line=1,char=2": "file://" + projectFileLoc + "#line=1,char=2",
cacheFileLoc + "#line=1,char=2": projectFileLoc + "#line=1,char=2",
// don't rewrite uris with an explicit non-file scheme
"git:///sourcegraph.com/sourcegraph?SHA": "git:///sourcegraph.com/sourcegraph?SHA",
// don't rewruite uris with empty paths
"file://": "file://",
"": "",
// don't rewrite uris that have paths that don't contain cacheDir as the first element
"file://" + projectFileLoc: "file://" + projectFileLoc,
projectFileLoc: projectFileLoc,
"file:///some/other/location": "file:///some/other/location",
"/some/other/location": "/some/other/location",
// no, REALLY don't rewrite uris with an explicit non-file scheme
"git://" + cacheFileLoc + "?SHA": "git://" + cacheFileLoc + "?SHA",
}
for serverURI, rewrittenURI := range tests {
actual := serverToClientURI(lsp.DocumentURI(serverURI), cacheDir)
if actual != lsp.DocumentURI(rewrittenURI) {
t.Errorf("for uri %s, expected %s, actual %s", serverURI, rewrittenURI, actual)
}
}
}
func TestWalkURIFields(t *testing.T) {
tests := map[string][]lsp.DocumentURI{
`{"textDocument":{"uri":"u1"}}`: {"u1"},
`{"uri":"u1"}`: {"u1"},
// `initialize` specific fields
`{"method":"initialize","rootPath":"u1"}`: {"u1"},
`{"method":"initialize","rootUri":"u1"}`: {"u1"},
`{"method":"initialize","rootPath":"u1","rootUri":"u2"}`: {"u1", "u2"},
}
for objStr, wantURIs := range tests {
var obj interface{}
if err := json.Unmarshal([]byte(objStr), &obj); err != nil {
t.Error(err)
continue
}
var collectedURIs []string
update := func(uri lsp.DocumentURI) lsp.DocumentURI {
collectedURIs = append(collectedURIs, string(uri))
return "XXX"
}
WalkURIFields(obj, update)
var wantURIStrs []string
for _, wantURI := range wantURIs {
wantURIStrs = append(wantURIStrs, string(wantURI))
}
sort.Strings(collectedURIs)
sort.Strings(wantURIStrs)
if !reflect.DeepEqual(collectedURIs, wantURIStrs) {
t.Errorf("%s: got URIs %q, want %q", objStr, collectedURIs, wantURIStrs)
}
wantObj := objStr
for _, uri := range collectedURIs {
wantObj = strings.Replace(wantObj, uri, "XXX", -1)
}
gotObj, err := json.Marshal(obj)
if err != nil {
t.Error(err)
continue
}
if string(gotObj) != wantObj {
t.Errorf("%s: got obj %q, want %q after updating URI pointers", objStr, gotObj, wantObj)
}
}
}