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 lsp
import (
"bytes"
"context"
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"testing"
"github.com/grindlemire/go-tui/internal/lsp/gopls"
)
type failAfterWriter struct {
n int
}
func (w *failAfterWriter) Write(p []byte) (int, error) {
if w.n <= 0 {
return 0, errors.New("write failed")
}
w.n--
return len(p), nil
}
func TestServerRun_ContextCancelled(t *testing.T) {
s := NewServer(strings.NewReader(""), new(bytes.Buffer))
ctx, cancel := context.WithCancel(context.Background())
cancel()
err := s.Run(ctx)
if !errors.Is(err, context.Canceled) {
t.Errorf("Run = %v, want context.Canceled", err)
}
}
func TestServerRun_ReadErrors(t *testing.T) {
type tc struct {
input string
wantSub string
}
tests := map[string]tc{
"invalid content length": {
input: "Content-Length: abc\r\n\r\n",
wantSub: "invalid Content-Length",
},
"missing content length": {
input: "Foo: bar\r\n\r\n",
wantSub: "missing Content-Length",
},
"short content": {
input: "Content-Length: 100\r\n\r\n{}",
wantSub: "reading content",
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
s := NewServer(strings.NewReader(tt.input), new(bytes.Buffer))
err := s.Run(context.Background())
if err == nil || !strings.Contains(err.Error(), tt.wantSub) {
t.Errorf("Run = %v, want error containing %q", err, tt.wantSub)
}
})
}
}
func TestServerRun_WriteErrors(t *testing.T) {
type tc struct {
successfulWrites int
}
tests := map[string]tc{
"header write fails": {successfulWrites: 0},
"body write fails": {successfulWrites: 1},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
mock := newMockReadWriter()
if err := mock.writeRequest(1, "initialize", InitializeParams{RootURI: "file:///x"}); err != nil {
t.Fatal(err)
}
s := NewServer(mock.input, &failAfterWriter{n: tt.successfulWrites})
err := s.Run(context.Background())
if err == nil || !strings.Contains(err.Error(), "writing response") {
t.Errorf("Run = %v, want writing response error", err)
}
})
}
}
func TestServerRun_ParseErrorResponse(t *testing.T) {
mock := newMockReadWriter()
body := "this is not json"
fmt.Fprintf(mock.input, "Content-Length: %d\r\n\r\n%s", len(body), body)
s := NewServer(mock.input, mock.output)
if err := s.Run(context.Background()); err != nil {
t.Fatalf("Run: %v", err)
}
resp, err := mock.readResponse()
if err != nil {
t.Fatalf("readResponse: %v", err)
}
if resp.Error == nil || resp.Error.Code != CodeParseError {
t.Errorf("error = %+v, want code %d", resp.Error, CodeParseError)
}
}
func TestServerRun_MethodNotFoundResponse(t *testing.T) {
mock := newMockReadWriter()
if err := mock.writeRequest(7, "no/such/method", nil); err != nil {
t.Fatal(err)
}
s := NewServer(mock.input, mock.output)
if err := s.Run(context.Background()); err != nil {
t.Fatalf("Run: %v", err)
}
resp, err := mock.readResponse()
if err != nil {
t.Fatalf("readResponse: %v", err)
}
if resp.Error == nil || resp.Error.Code != CodeMethodNotFound {
t.Errorf("error = %+v, want code %d", resp.Error, CodeMethodNotFound)
}
if id, ok := resp.ID.(float64); !ok || id != 7 {
t.Errorf("ID = %v, want 7", resp.ID)
}
}
func TestSetLogFile(t *testing.T) {
path := filepath.Join(t.TempDir(), "lsp.log")
f, err := os.Create(path)
if err != nil {
t.Fatal(err)
}
defer f.Close()
s := NewServer(strings.NewReader(""), new(bytes.Buffer))
s.SetLogFile(f)
defer s.SetLogFile(nil)
s.router.Route(Request{JSONRPC: "2.0", Method: "definitely/unknown"})
s.SetLogFile(nil)
data, err := os.ReadFile(path)
if err != nil {
t.Fatal(err)
}
if !strings.Contains(string(data), "[server]") {
t.Errorf("log file %q missing server-prefixed entries", string(data))
}
}
func TestLookupSourceMap(t *testing.T) {
s := NewServer(strings.NewReader(""), new(bytes.Buffer))
uri := "file:///map.gsx"
if fn := s.lookupSourceMap(uri); fn != nil {
t.Error("expected nil lookup without a cached virtual file")
}
s.virtualFiles.Put(uri, gopls.TuiURIToGoURI(uri), "package main", nil, 1)
if fn := s.lookupSourceMap(uri); fn != nil {
t.Error("expected nil lookup for cached file with nil source map")
}
doc := parseTestDoc(routerCoverageSrc)
goContent, sourceMap := gopls.GenerateVirtualGo(doc.AST)
s.virtualFiles.Put(uri, gopls.TuiURIToGoURI(uri), goContent, sourceMap, 2)
fn := s.lookupSourceMap(uri)
if fn == nil {
t.Fatal("expected a translation function for cached source map")
}
}
func TestHandleGoplsDiagnostics(t *testing.T) {
out := new(bytes.Buffer)
s := NewServer(strings.NewReader(""), out)
uri := "file:///diag.gsx"
s.docs.Open(uri, routerCoverageSrc, 1)
diags := []gopls.GoplsDiagnostic{
{
Range: gopls.Range{
Start: gopls.Position{Line: 4, Character: 2},
End: gopls.Position{Line: 4, Character: 7},
},
Severity: 1,
Source: "compiler",
Message: "undefined: title",
},
}
s.handleGoplsDiagnostics(uri, diags)
s.goplsDiagnosticsMu.RLock()
stored := s.goplsDiagnostics[uri]
s.goplsDiagnosticsMu.RUnlock()
if len(stored) != 1 || stored[0].Message != "undefined: title" {
t.Errorf("stored diagnostics = %+v", stored)
}
output := out.String()
if !strings.Contains(output, "textDocument/publishDiagnostics") {
t.Error("no publishDiagnostics notification was written")
}
if !strings.Contains(output, "undefined: title") {
t.Error("gopls diagnostic message missing from published diagnostics")
}
out.Reset()
s.handleGoplsDiagnostics("file:///ghost.gsx", diags)
if out.Len() != 0 {
t.Errorf("unexpected notification for unopened document: %s", out.String())
}
}
|