Debian Codegraph @

gopls/internal/cmd/capabilities_test.go

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