Debian Codegraph @

gopls/internal/cmd/capabilities_test.go

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 // Copyright 2019 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 cmd
6
7 import (
8 "context"
9 "fmt"
10 "os"
11 "path/filepath"
12 "testing"
13
14 "golang.org/x/tools/gopls/internal/cache"
15 "golang.org/x/tools/gopls/internal/protocol"
16 "golang.org/x/tools/gopls/internal/server"
17 "golang.org/x/tools/gopls/internal/settings"
18 "golang.org/x/tools/internal/testenv"
19 )
20
21 // TestCapabilities does some minimal validation of the server's adherence to the LSP.
22 // The checks in the test are added as changes are made and errors noticed.
23 func TestCapabilities(t *testing.T) {
24 // server.DidOpen fails to obtain metadata without go command (e.g. on wasm).
25 testenv.NeedsTool(t, "go")
26
27 tmpDir, err := os.MkdirTemp("", "fake")
28 if err != nil {
29 t.Fatal(err)
30 }
31 tmpFile := filepath.Join(tmpDir, "fake.go")
32 if err := os.WriteFile(tmpFile, []byte(""), 0775); err != nil {
33 t.Fatal(err)
34 }
35 if err := os.WriteFile(filepath.Join(tmpDir, "go.mod"), []byte("module fake\n\ngo 1.12\n"), 0775); err != nil {
36 t.Fatal(err)
37 }
38 defer os.RemoveAll(tmpDir)
39
40 app := New()
41 ctx := context.Background()
42
43 // Initialize the client.
44 // (Unlike app.connect, we use minimal Initialize params.)
45 client := newClient(app)
46 options := settings.DefaultOptions(app.options)
47 server := server.New(cache.NewSession(ctx, cache.New(nil)), client, options)
48 params := &protocol.ParamInitialize{}
49 params.RootURI = protocol.URIFromPath(tmpDir)
50 params.Capabilities.Workspace.Configuration = true
51 if err := client.initialize(ctx, server, params); err != nil {
52 t.Fatal(err)
53 }
54 defer client.terminate(ctx)
55
56 if err := validateCapabilities(client.initializeResult); err != nil {
57 t.Error(err)
58 }
59
60 // Open the file on the server side.
61 uri := protocol.URIFromPath(tmpFile)
62 if err := server.DidOpen(ctx, &protocol.DidOpenTextDocumentParams{
63 TextDocument: protocol.TextDocumentItem{
64 URI: uri,
65 LanguageID: "go",
66 Version: 1,
67 Text: `package main; func main() {};`,
68 },
69 }); err != nil {
70 t.Fatal(err)
71 }
72
73 // If we are sending a full text change, the change.Range must be nil.
74 // It is not enough for the Change to be empty, as that is ambiguous.
75 if err := server.DidChange(ctx, &protocol.DidChangeTextDocumentParams{
76 TextDocument: protocol.VersionedTextDocumentIdentifier{
77 TextDocumentIdentifier: protocol.TextDocumentIdentifier{
78 URI: uri,
79 },
80 Version: 2,
81 },
82 ContentChanges: []protocol.TextDocumentContentChangeEvent{
83 {
84 Range: nil,
85 Text: `package main; func main() { fmt.Println("") }`,
86 },
87 },
88 }); err != nil {
89 t.Fatal(err)
90 }
91
92 // Send a code action request to validate expected types.
93 actions, err := server.CodeAction(ctx, &protocol.CodeActionParams{
94 TextDocument: protocol.TextDocumentIdentifier{
95 URI: uri,
96 },
97 Context: protocol.CodeActionContext{
98 Only: []protocol.CodeActionKind{protocol.SourceOrganizeImports},
99 },
100 })
101 if err != nil {
102 t.Fatal(err)
103 }
104 for _, action := range actions {
105 // Validate that an empty command is sent along with import organization responses.
106 if action.Kind == protocol.SourceOrganizeImports && action.Command != nil {
107 t.Errorf("unexpected command for import organization")
108 }
109 }
110
111 if err := server.DidSave(ctx, &protocol.DidSaveTextDocumentParams{
112 TextDocument: protocol.TextDocumentIdentifier{
113 URI: uri,
114 },
115 // LSP specifies that a file can be saved with optional text, so this field must be nil.
116 Text: nil,
117 }); err != nil {
118 t.Fatal(err)
119 }
120
121 // Send a completion request to validate expected types.
122 list, err := server.Completion(ctx, &protocol.CompletionParams{
123 TextDocumentPositionParams: protocol.TextDocumentPositionParams{
124 TextDocument: protocol.TextDocumentIdentifier{
125 URI: uri,
126 },
127 Range: protocol.Range{
128 Start: protocol.Position{
129 Line: 0,
130 Character: 28,
131 },
132 End: protocol.Position{
133 Line: 0,
134 Character: 28,
135 },
136 },
137 },
138 })
139 if err != nil {
140 t.Fatal(err)
141 }
142 for _, item := range list.Items {
143 // All other completion items should have nil commands.
144 // An empty command will be treated as a command with the name '' by VS Code.
145 // This causes VS Code to report errors to users about invalid commands.
146 if item.Command != nil {
147 t.Errorf("unexpected command for completion item")
148 }
149 // The item's TextEdit must be a pointer, as VS Code considers TextEdits
150 // that don't contain the cursor position to be invalid.
151 var textEdit = item.TextEdit.Value
152 switch textEdit.(type) {
153 case protocol.TextEdit, protocol.InsertReplaceEdit:
154 default:
155 t.Errorf("textEdit is not TextEdit nor InsertReplaceEdit, instead it is %T", textEdit)
156 }
157 }
158 if err := server.Shutdown(ctx); err != nil {
159 t.Fatal(err)
160 }
161 if err := server.Exit(ctx); err != nil {
162 t.Fatal(err)
163 }
164 }
165
166 func validateCapabilities(result *protocol.InitializeResult) error {
167 // If the client sends "false" for RenameProvider.PrepareSupport,
168 // the server must respond with a boolean.
169 if v, ok := result.Capabilities.RenameProvider.(bool); !ok {
170 return fmt.Errorf("RenameProvider must be a boolean if PrepareSupport is false (got %T)", v)
171 }
172 // The same goes for CodeActionKind.ValueSet.
173 if v, ok := result.Capabilities.CodeActionProvider.(bool); !ok {
174 return fmt.Errorf("CodeActionSupport must be a boolean if CodeActionKind.ValueSet has length 0 (got %T)", v)
175 }
176 return nil
177 }