-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgluon_test.go
More file actions
365 lines (317 loc) · 8.14 KB
/
Copy pathgluon_test.go
File metadata and controls
365 lines (317 loc) · 8.14 KB
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
package gluon
import (
"context"
"os"
"os/exec"
"strings"
"testing"
"github.com/accretional/gluon/pb"
"google.golang.org/grpc"
)
func TestNewGoServer(t *testing.T) {
if _, err := exec.LookPath("go"); err != nil {
t.Skip("go binary not found")
}
srv, err := NewGoServer()
if err != nil {
t.Fatal(err)
}
if srv.goBinary == "" {
t.Error("goBinary should not be empty")
}
}
func TestVersion(t *testing.T) {
srv := mustGoServer(t)
ctx := context.Background()
resp, err := srv.Version(ctx, &pb.Nothing{})
if err != nil {
t.Fatal(err)
}
if !strings.HasPrefix(resp.GetText(), "go version") {
t.Errorf("unexpected version output: %q", resp.GetText())
}
}
func TestCommand(t *testing.T) {
srv := mustGoServer(t)
ctx := context.Background()
// Valid command
resp, err := srv.Command(ctx, &pb.Text{Text: "version"})
if err != nil {
t.Fatal(err)
}
if !strings.Contains(resp.GetText(), "go version") {
t.Errorf("expected version output, got: %q", resp.GetText())
}
// Empty command should fail
_, err = srv.Command(ctx, &pb.Text{})
if err == nil {
t.Error("expected error for empty command")
}
}
func TestHelp(t *testing.T) {
srv := mustGoServer(t)
ctx := context.Background()
resp, err := srv.Help(ctx, &pb.Text{Text: "build"})
if err != nil {
t.Fatal(err)
}
if !strings.Contains(resp.GetText(), "build") {
t.Errorf("help output should mention build: %q", resp.GetText())
}
// Empty topic
resp, err = srv.Help(ctx, &pb.Text{})
if err != nil {
t.Fatal(err)
}
if resp.GetText() == "" {
t.Error("help with no topic should still produce output")
}
}
func TestEnv(t *testing.T) {
srv := mustGoServer(t)
ctx := context.Background()
// Query specific vars
resp, err := srv.Env(ctx, &pb.GoEnvRequest{Vars: []string{"GOPATH"}})
if err != nil {
t.Fatal(err)
}
if strings.TrimSpace(resp.GetText()) == "" {
t.Error("GOPATH should not be empty")
}
// JSON output
resp, err = srv.Env(ctx, &pb.GoEnvRequest{Json: true, Vars: []string{"GOPATH"}})
if err != nil {
t.Fatal(err)
}
if !strings.Contains(resp.GetText(), "GOPATH") {
t.Error("JSON env output should contain GOPATH key")
}
}
func TestDoc(t *testing.T) {
srv := mustGoServer(t)
ctx := context.Background()
resp, err := srv.Doc(ctx, &pb.GoDocQuery{Pkg: "fmt", Symbol: "Println"})
if err != nil {
t.Fatal(err)
}
if !strings.Contains(resp.GetText(), "Println") {
t.Errorf("doc should mention Println: %q", resp.GetText())
}
}
func TestDocFlags(t *testing.T) {
srv := mustGoServer(t)
ctx := context.Background()
resp, err := srv.Doc(ctx, &pb.GoDocQuery{
Pkg: "fmt",
Flags: []pb.GoDocQuery_GoDocQueryFlags{pb.GoDocQuery_short},
})
if err != nil {
t.Fatal(err)
}
if resp.GetText() == "" {
t.Error("short doc should produce output")
}
}
func TestBuild(t *testing.T) {
srv := mustGoServer(t)
ctx := context.Background()
dir := makeTempModule(t)
// Build should succeed in the temp module dir
// We need to set the working directory — use Command instead
resp, err := srv.Command(ctx, &pb.Text{Text: "build -C " + dir + " ./..."})
if err != nil {
t.Fatal(err)
}
_ = resp
}
func TestTest(t *testing.T) {
srv := mustGoServer(t)
ctx := context.Background()
dir := makeTempModule(t)
// Run tests in temp module (no test files, should be fine)
resp, err := srv.Command(ctx, &pb.Text{Text: "test -C " + dir + " ./..."})
if err != nil {
t.Fatal(err)
}
_ = resp
}
func TestList(t *testing.T) {
srv := mustGoServer(t)
ctx := context.Background()
// List with modules flag — list std
resp, err := srv.List(ctx, &pb.GoListRequest{
Packages: []string{"fmt"},
})
if err != nil {
t.Fatal(err)
}
if !strings.Contains(resp.GetText(), "fmt") {
t.Errorf("list should contain fmt: %q", resp.GetText())
}
}
func TestVet(t *testing.T) {
srv := mustGoServer(t)
ctx := context.Background()
dir := makeTempModule(t)
resp, err := srv.Command(ctx, &pb.Text{Text: "vet -C " + dir + " ./..."})
if err != nil {
t.Fatal(err)
}
_ = resp
}
func TestTool(t *testing.T) {
srv := mustGoServer(t)
ctx := context.Background()
// List tools (no name = just list)
resp, err := srv.Tool(ctx, &pb.GoToolRequest{})
if err != nil {
t.Fatal(err)
}
if resp.GetText() == "" {
t.Error("tool list should produce output")
}
}
func TestFormat(t *testing.T) {
srv := mustGoServer(t)
ctx := context.Background()
dir := makeTempModule(t)
resp, err := srv.Command(ctx, &pb.Text{Text: "fmt -C " + dir + " ./..."})
if err != nil {
t.Fatal(err)
}
_ = resp
}
func TestGetRequiresPackage(t *testing.T) {
srv := mustGoServer(t)
ctx := context.Background()
_, err := srv.Get(ctx, &pb.GoGetRequest{})
if err == nil {
t.Error("expected error for empty packages")
}
}
func TestInstallRequiresPackage(t *testing.T) {
srv := mustGoServer(t)
ctx := context.Background()
_, err := srv.Install(ctx, &pb.GoInstallRequest{})
if err == nil {
t.Error("expected error for empty packages")
}
}
func TestDescribeFixAnalyzerRequiresName(t *testing.T) {
srv := mustGoServer(t)
ctx := context.Background()
_, err := srv.DescribeFixAnalyzer(ctx, &pb.Text{})
if err == nil {
t.Error("expected error for empty analyzer name")
}
}
func TestTargetEnv(t *testing.T) {
// nil target
if env := targetEnv(nil); env != nil {
t.Errorf("nil target should produce nil env, got %v", env)
}
// Empty target (no OS or arch set)
if env := targetEnv(&pb.GoCompilerTarget{}); env != nil {
t.Errorf("empty target should produce nil env, got %v", env)
}
// Linux amd64
env := targetEnv(&pb.GoCompilerTarget{
Os: pb.GoCompilerTarget_linux,
Arch: pb.GoCompilerTarget_amd64,
})
if env["GOOS"] != "linux" {
t.Errorf("GOOS = %q, want linux", env["GOOS"])
}
if env["GOARCH"] != "amd64" {
t.Errorf("GOARCH = %q, want amd64", env["GOARCH"])
}
// Darwin arm64
env = targetEnv(&pb.GoCompilerTarget{
Os: pb.GoCompilerTarget_darwin,
Arch: pb.GoCompilerTarget_arm64,
})
if env["GOOS"] != "darwin" {
t.Errorf("GOOS = %q, want darwin", env["GOOS"])
}
if env["GOARCH"] != "arm64" {
t.Errorf("GOARCH = %q, want arm64", env["GOARCH"])
}
// Windows 386
env = targetEnv(&pb.GoCompilerTarget{
Os: pb.GoCompilerTarget_windows,
Arch: pb.GoCompilerTarget__386,
})
if env["GOOS"] != "windows" {
t.Errorf("GOOS = %q, want windows", env["GOOS"])
}
if env["GOARCH"] != "386" {
t.Errorf("GOARCH = %q, want 386", env["GOARCH"])
}
// ARM only
env = targetEnv(&pb.GoCompilerTarget{
Arch: pb.GoCompilerTarget_arm,
})
if env["GOARCH"] != "arm" {
t.Errorf("GOARCH = %q, want arm", env["GOARCH"])
}
if _, ok := env["GOOS"]; ok {
t.Error("GOOS should not be set when OS is unspecified")
}
}
func TestAppendTags(t *testing.T) {
// No tags
args := appendTags([]string{"build"}, nil)
if len(args) != 1 {
t.Errorf("no tags should not add args, got %v", args)
}
// With tags
args = appendTags([]string{"build"}, []string{"integration", "e2e"})
if len(args) != 3 {
t.Errorf("expected 3 args, got %v", args)
}
if args[1] != "-tags" {
t.Errorf("expected -tags, got %s", args[1])
}
if args[2] != "integration,e2e" {
t.Errorf("expected joined tags, got %s", args[2])
}
}
func TestRegisterServices(t *testing.T) {
if _, err := exec.LookPath("go"); err != nil {
t.Skip("go binary not found")
}
srv := grpcServer()
if err := RegisterServices(srv); err != nil {
t.Fatal(err)
}
// Server should have services registered — we can't easily introspect,
// but at least it didn't panic or error.
}
// mustGoServer creates a GoServer or skips the test.
func mustGoServer(t *testing.T) *GoServer {
t.Helper()
if _, err := exec.LookPath("go"); err != nil {
t.Skip("go binary not found")
}
srv, err := NewGoServer()
if err != nil {
t.Fatal(err)
}
return srv
}
// makeTempModule creates a temp directory with a valid go.mod and a trivial .go file.
func makeTempModule(t *testing.T) string {
t.Helper()
dir := t.TempDir()
if err := os.WriteFile(dir+"/go.mod", []byte("module test/temp\n\ngo 1.21\n"), 0644); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(dir+"/main.go", []byte("package main\n\nfunc main() {}\n"), 0644); err != nil {
t.Fatal(err)
}
return dir
}
// grpcServer creates a bare gRPC server for registration tests.
func grpcServer() *grpc.Server {
return grpc.NewServer()
}