-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinitialize_test.go
137 lines (123 loc) · 3.49 KB
/
initialize_test.go
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
package server_test
import (
"bytes"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"testing"
"time"
"github.com/docker/docker-language-server/internal/pkg/document"
"github.com/docker/docker-language-server/internal/pkg/server"
"github.com/docker/docker-language-server/internal/tliron/glsp"
"github.com/docker/docker-language-server/internal/tliron/glsp/protocol"
"github.com/docker/docker-language-server/internal/types"
"github.com/stretchr/testify/require"
)
func init() {
os.Setenv("DOCKER_LANGUAGE_SERVER_TELEMETRY", "false")
}
func createDidOpenTextDocumentParams(homedir, testName, text string, languageID protocol.LanguageIdentifier) protocol.DidOpenTextDocumentParams {
return protocol.DidOpenTextDocumentParams{
TextDocument: protocol.TextDocumentItem{
URI: protocol.URI(fmt.Sprintf("file:///%v", strings.TrimPrefix(filepath.ToSlash(filepath.Join(homedir, testName)), "/"))),
Text: text,
LanguageID: languageID,
Version: 1,
},
}
}
func createDidChangeTextDocumentParams(homedir, testName, text string) protocol.DidChangeTextDocumentParams {
return protocol.DidChangeTextDocumentParams{
TextDocument: protocol.VersionedTextDocumentIdentifier{
TextDocumentIdentifier: protocol.TextDocumentIdentifier{
URI: protocol.URI(fmt.Sprintf("file:///%v", strings.TrimPrefix(filepath.ToSlash(filepath.Join(homedir, testName)), "/"))),
},
Version: 1,
},
ContentChanges: []any{
protocol.TextDocumentContentChangeEvent{
Text: text,
},
},
}
}
func TestInitializeFunctionIsolatedCall(t *testing.T) {
testCases := []struct {
name string
params protocol.InitializeParams
workspaceFolders []string
}{
{
name: "one workspace folder",
params: protocol.InitializeParams{
WorkspaceFolders: []protocol.WorkspaceFolder{{Name: "abc", URI: "uri:///a/b/c"}},
},
workspaceFolders: []string{"/a/b/c"},
},
{
name: "two workspace folders",
params: protocol.InitializeParams{
WorkspaceFolders: []protocol.WorkspaceFolder{
{Name: "abc", URI: "uri:///a/b/c"},
{Name: "def", URI: "uri:///d/e/f"},
},
},
workspaceFolders: []string{"/a/b/c", "/d/e/f"},
},
{
name: "rootUri",
params: protocol.InitializeParams{RootURI: types.CreateStringPointer("uri:///a/b/c")},
workspaceFolders: []string{"/a/b/c"},
},
{
name: "rootPath",
params: protocol.InitializeParams{RootPath: types.CreateStringPointer("/a/b/c")},
workspaceFolders: []string{"/a/b/c"},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
s := testInitialize(t, tc.params)
require.Equal(t, tc.workspaceFolders, s.WorkspaceFolders())
})
}
}
func testInitialize(t *testing.T, initializeParams protocol.InitializeParams) *server.Server {
s := startServer()
_, err := s.Initialize(&glsp.Context{}, &initializeParams)
require.NoError(t, err)
return s
}
type TestStream struct {
incoming *bytes.Buffer
outgoing *bytes.Buffer
closed bool
}
func (ts *TestStream) Read(b []byte) (int, error) {
for {
if ts.closed {
return 0, io.EOF
}
r, err := ts.incoming.Read(b)
if r > 0 {
return r, err
} else if err != io.EOF {
return r, err
}
time.Sleep(1 * time.Second)
}
}
func (ts *TestStream) Write(n []byte) (int, error) {
return ts.outgoing.Write(n)
}
func (ts *TestStream) Close() error {
ts.closed = true
return nil
}
func startServer() *server.Server {
docManager := document.NewDocumentManager()
s := server.NewServer(docManager)
return s
}