Debian Codegraph @

gopls/internal/test/integration/web/util_test.go

raw symbols SCIP
1 // Copyright 2025 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package web_test
6
7 // This file defines web server testing utilities.
8
9 import (
10 "io"
11 "net/http"
12 "os"
13 "regexp"
14 "strings"
15 "testing"
16
17 "golang.org/x/tools/gopls/internal/protocol"
18 "golang.org/x/tools/gopls/internal/protocol/command"
19 "golang.org/x/tools/gopls/internal/test/integration"
20 "golang.org/x/tools/gopls/internal/util/bug"
21 )
22
23 func TestMain(m *testing.M) {
24 bug.PanicOnBugs = true
25 os.Exit(integration.Main(m))
26 }
27
28 // shownDocument returns the first shown document matching the URI prefix.
29 // It may be nil.
30 // As a side effect, it clears the list of accumulated shown documents.
31 func shownDocument(t *testing.T, shown []*protocol.ShowDocumentParams, prefix string) *protocol.ShowDocumentParams {
32 t.Helper()
33 var first *protocol.ShowDocumentParams
34 for _, sd := range shown {
35 if strings.HasPrefix(sd.URI, prefix) {
36 if first != nil {
37 t.Errorf("got multiple showDocument requests: %#v", shown)
38 break
39 }
40 first = sd
41 }
42 }
43 return first
44 }
45
46 // get fetches the content of a document over HTTP.
47 func get(t *testing.T, url string) []byte {
48 t.Helper()
49 resp, err := http.Get(url)
50 if err != nil {
51 t.Fatal(err)
52 }
53 defer resp.Body.Close()
54 got, err := io.ReadAll(resp.Body)
55 if err != nil {
56 t.Fatal(err)
57 }
58 return got
59 }
60
61 // checkMatch asserts that got matches (or doesn't match, if !want) the pattern.
62 func checkMatch(t *testing.T, want bool, got []byte, pattern string) {
63 t.Helper()
64 if regexp.MustCompile(pattern).Match(got) != want {
65 if want {
66 t.Errorf("input did not match wanted pattern %q; got:\n%s", pattern, got)
67 } else {
68 t.Errorf("input matched unwanted pattern %q; got:\n%s", pattern, got)
69 }
70 }
71 }
72
73 // codeActionWebPage returns the URL and content of the page opened by the specified code action.
74 func codeActionWebPage(t *testing.T, env *integration.Env, kind protocol.CodeActionKind, loc protocol.Location) (string, []byte) {
75 actions, err := env.Editor.CodeAction(env.Ctx, loc, nil, protocol.CodeActionUnknownTrigger)
76 if err != nil {
77 t.Fatalf("CodeAction: %v", err)
78 }
79 action, err := integration.CodeActionByKind(actions, kind)
80 if err != nil {
81 t.Fatal(err)
82 }
83 action, err = env.Editor.ResolveCodeAction(env.Ctx, action)
84 if err != nil {
85 t.Fatal(err)
86 }
87
88 // Execute the command.
89 // Its side effect should be a single showDocument request.
90 params := &protocol.ExecuteCommandParams{
91 Command: action.Command.Command,
92 Arguments: action.Command.Arguments,
93 }
94 var result command.DebuggingResult
95 collectDocs := env.Awaiter.ListenToShownDocuments()
96 env.ExecuteCommand(params, &result)
97 doc := shownDocument(t, collectDocs(), "http:")
98 if doc == nil {
99 t.Fatalf("no showDocument call had 'file:' prefix")
100 }
101 t.Log("showDocument(package doc) URL:", doc.URI)
102
103 return doc.URI, get(t, doc.URI)
104 }