| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package debug |
| 6 |
|
| 7 |
import ( |
| 8 |
"context" |
| 9 |
"encoding/json" |
| 10 |
"io" |
| 11 |
"net/http" |
| 12 |
"os" |
| 13 |
"strings" |
| 14 |
"testing" |
| 15 |
|
| 16 |
"golang.org/x/tools/gopls/internal/protocol" |
| 17 |
"golang.org/x/tools/gopls/internal/protocol/command" |
| 18 |
. "golang.org/x/tools/gopls/internal/test/integration" |
| 19 |
"golang.org/x/tools/gopls/internal/util/bug" |
| 20 |
) |
| 21 |
|
| 22 |
func TestMain(m *testing.M) { |
| 23 |
os.Exit(Main(m)) |
| 24 |
} |
| 25 |
|
| 26 |
func TestBugNotification(t *testing.T) { |
| 27 |
|
| 28 |
|
| 29 |
WithOptions( |
| 30 |
Modes(Default), |
| 31 |
Settings{"showBugReports": true}, |
| 32 |
).Run(t, "", func(t *testing.T, env *Env) { |
| 33 |
const desc = "got a bug" |
| 34 |
bug.Report(desc) |
| 35 |
env.Await(ShownMessage(desc)) |
| 36 |
}) |
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
func TestStartDebugging(t *testing.T) { |
| 42 |
WithOptions( |
| 43 |
Modes(Default), |
| 44 |
).Run(t, "", func(t *testing.T, env *Env) { |
| 45 |
|
| 46 |
res, err := startDebugging(env.Ctx, env.Editor.Server, &command.DebuggingArgs{ |
| 47 |
Addr: "", |
| 48 |
}) |
| 49 |
if err != nil { |
| 50 |
t.Fatalf("startDebugging: %v", err) |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
debugURL := res.URLs[0] |
| 56 |
env.Await(ShownDocument(debugURL)) |
| 57 |
|
| 58 |
|
| 59 |
resp, err := http.Get(debugURL) |
| 60 |
if err != nil { |
| 61 |
t.Fatal(err) |
| 62 |
} |
| 63 |
defer resp.Body.Close() |
| 64 |
data, err := io.ReadAll(resp.Body) |
| 65 |
if err != nil { |
| 66 |
t.Fatalf("reading HTTP response body: %v", err) |
| 67 |
} |
| 68 |
const want = "<title>Gopls" |
| 69 |
if !strings.Contains(string(data), want) { |
| 70 |
t.Errorf("GET %s response does not contain %q: <<%s>>", debugURL, want, data) |
| 71 |
} |
| 72 |
}) |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
func startDebugging(ctx context.Context, server protocol.Server, args *command.DebuggingArgs) (*command.DebuggingResult, error) { |
| 78 |
rawArgs, err := command.MarshalArgs(args) |
| 79 |
if err != nil { |
| 80 |
return nil, err |
| 81 |
} |
| 82 |
res0, err := server.ExecuteCommand(ctx, &protocol.ExecuteCommandParams{ |
| 83 |
Command: command.StartDebugging.String(), |
| 84 |
Arguments: rawArgs, |
| 85 |
}) |
| 86 |
if err != nil { |
| 87 |
return nil, err |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
data, err := json.Marshal(res0) |
| 93 |
if err != nil { |
| 94 |
return nil, err |
| 95 |
} |
| 96 |
var res *command.DebuggingResult |
| 97 |
if err := json.Unmarshal(data, &res); err != nil { |
| 98 |
return nil, err |
| 99 |
} |
| 100 |
return res, nil |
| 101 |
} |