/
cmdtest.go
155 lines (135 loc) · 4.92 KB
/
cmdtest.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
// Copyright 2019 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package cmdtest contains the test suite for the command line behavior of gopls.
package cmdtest
import (
"bytes"
"context"
"fmt"
"io"
"os"
"sync"
"testing"
"github.com/kevinswiber/languageserver-go/jsonrpc2/servertest"
"github.com/kevinswiber/languageserver-go/lsp/cache"
"github.com/kevinswiber/languageserver-go/lsp/cmd"
"github.com/kevinswiber/languageserver-go/lsp/debug"
"github.com/kevinswiber/languageserver-go/lsp/lsprpc"
"github.com/kevinswiber/languageserver-go/lsp/protocol"
"github.com/kevinswiber/languageserver-go/lsp/source"
"github.com/kevinswiber/languageserver-go/lsp/tests"
"github.com/kevinswiber/languageserver-go/span"
"github.com/kevinswiber/languageserver-go/tool"
)
type runner struct {
data *tests.Data
ctx context.Context
options func(*source.Options)
normalizers []tests.Normalizer
remote string
}
func TestCommandLine(t *testing.T, testdata string, options func(*source.Options)) {
// On Android, the testdata directory is not copied to the runner.
if stat, err := os.Stat(testdata); err != nil || !stat.IsDir() {
t.Skip("testdata directory not present")
}
tests.RunTests(t, testdata, false, func(t *testing.T, datum *tests.Data) {
ctx := tests.Context(t)
ts := NewTestServer(ctx, options)
tests.Run(t, NewRunner(datum, ctx, ts.Addr, options), datum)
cmd.CloseTestConnections(ctx)
})
}
func NewTestServer(ctx context.Context, options func(*source.Options)) *servertest.TCPServer {
ctx = debug.WithInstance(ctx, "", "")
cache := cache.New(ctx, options)
ss := lsprpc.NewStreamServer(cache, false)
return servertest.NewTCPServer(ctx, ss, nil)
}
func NewRunner(data *tests.Data, ctx context.Context, remote string, options func(*source.Options)) *runner {
return &runner{
data: data,
ctx: ctx,
options: options,
normalizers: tests.CollectNormalizers(data.Exported),
remote: remote,
}
}
func (r *runner) CodeLens(t *testing.T, uri span.URI, want []protocol.CodeLens) {
//TODO: add command line completions tests when it works
}
func (r *runner) Completion(t *testing.T, src span.Span, test tests.Completion, items tests.CompletionItems) {
//TODO: add command line completions tests when it works
}
func (r *runner) CompletionSnippet(t *testing.T, src span.Span, expected tests.CompletionSnippet, placeholders bool, items tests.CompletionItems) {
//TODO: add command line completions tests when it works
}
func (r *runner) UnimportedCompletion(t *testing.T, src span.Span, test tests.Completion, items tests.CompletionItems) {
//TODO: add command line completions tests when it works
}
func (r *runner) DeepCompletion(t *testing.T, src span.Span, test tests.Completion, items tests.CompletionItems) {
//TODO: add command line completions tests when it works
}
func (r *runner) FuzzyCompletion(t *testing.T, src span.Span, test tests.Completion, items tests.CompletionItems) {
//TODO: add command line completions tests when it works
}
func (r *runner) CaseSensitiveCompletion(t *testing.T, src span.Span, test tests.Completion, items tests.CompletionItems) {
//TODO: add command line completions tests when it works
}
func (r *runner) RankCompletion(t *testing.T, src span.Span, test tests.Completion, items tests.CompletionItems) {
//TODO: add command line completions tests when it works
}
func (r *runner) FunctionExtraction(t *testing.T, start span.Span, end span.Span) {
//TODO: function extraction not supported on command line
}
func (r *runner) runGoplsCmd(t testing.TB, args ...string) (string, string) {
rStdout, wStdout, err := os.Pipe()
if err != nil {
t.Fatal(err)
}
oldStdout := os.Stdout
rStderr, wStderr, err := os.Pipe()
if err != nil {
t.Fatal(err)
}
oldStderr := os.Stderr
stdout, stderr := &bytes.Buffer{}, &bytes.Buffer{}
var wg sync.WaitGroup
wg.Add(2)
go func() {
io.Copy(stdout, rStdout)
wg.Done()
}()
go func() {
io.Copy(stderr, rStderr)
wg.Done()
}()
os.Stdout, os.Stderr = wStdout, wStderr
app := cmd.New("gopls-test", r.data.Config.Dir, r.data.Exported.Config.Env, r.options)
remote := r.remote
err = tool.Run(tests.Context(t),
app,
append([]string{fmt.Sprintf("-remote=internal@%s", remote)}, args...))
if err != nil {
fmt.Fprint(os.Stderr, err)
}
wStdout.Close()
wStderr.Close()
wg.Wait()
os.Stdout, os.Stderr = oldStdout, oldStderr
rStdout.Close()
rStderr.Close()
return stdout.String(), stderr.String()
}
// NormalizeGoplsCmd runs the gopls command and normalizes its output.
func (r *runner) NormalizeGoplsCmd(t testing.TB, args ...string) (string, string) {
stdout, stderr := r.runGoplsCmd(t, args...)
return r.Normalize(stdout), r.Normalize(stderr)
}
func (r *runner) Normalize(s string) string {
return tests.Normalize(s, r.normalizers)
}
func (r *runner) NormalizePrefix(s string) string {
return tests.NormalizePrefix(s, r.normalizers)
}