Skip to content

Commit 76171cc

Browse files
committed
test: Add end-to-end test
1 parent 0b0e481 commit 76171cc

File tree

5 files changed

+81
-6
lines changed

5 files changed

+81
-6
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/tmp/

e2e_test.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package main_test
2+
3+
import (
4+
"bytes"
5+
"io"
6+
"os"
7+
"os/exec"
8+
"path/filepath"
9+
"strconv"
10+
"testing"
11+
"time"
12+
13+
. "github.com/onsi/gomega"
14+
15+
"github.com/samber/lo"
16+
)
17+
18+
func TestE2E(t *testing.T) {
19+
g := NewWithT(t)
20+
21+
// Build the server binary
22+
binPath := filepath.Join(t.TempDir(), "gdshader-language-server")
23+
g.Expect(exec.Command("go", "build", "-cover", "-o", binPath, ".").Run()).To(Succeed(), "Failed to build server")
24+
25+
// Configure the server command
26+
const coverDir = "tmp/cover/e2e"
27+
lo.Must0(os.MkdirAll(coverDir, 0o700))
28+
var stdout bytes.Buffer
29+
cmd := exec.Command(binPath, "-debug")
30+
cmd.Env = []string{"GOCOVERDIR=" + coverDir}
31+
cmd.Stderr = os.Stderr
32+
cmd.Stdout = &stdout
33+
stdin := lo.Must(cmd.StdinPipe())
34+
35+
// Start the server
36+
g.Expect(cmd.Start()).To(Succeed(), "Failed to start server")
37+
t.Cleanup(func() { _ = cmd.Process.Kill() })
38+
39+
// Send JSON-RPC requests to the server
40+
41+
send := func(s string) {
42+
var buf bytes.Buffer
43+
buf.WriteString("Content-Length: ")
44+
buf.WriteString(strconv.Itoa(len(s)))
45+
buf.WriteString("\r\n\r\n")
46+
buf.WriteString(s)
47+
lo.Must(io.Copy(stdin, &buf))
48+
}
49+
50+
send(`{"jsonrpc":"2.0","id":1,"method":"initialize","params":{}}`)
51+
send(`{"jsonrpc":"2.0","id":2,"method":"shutdown"}`)
52+
send(`{"jsonrpc":"2.0","method":"exit"}`)
53+
54+
// Wait for the server to exit
55+
select {
56+
case err := <-lo.Async(cmd.Wait):
57+
g.Expect(err).ToNot(HaveOccurred(), "Server exited with error")
58+
case <-time.After(5 * time.Second):
59+
t.Fatal("Server did not exit in time")
60+
}
61+
62+
// Check the output
63+
64+
var expected string
65+
66+
expect := func(s string) {
67+
expected += "Content-Length: " + strconv.Itoa(len(s)) + "\r\nContent-Type: application/vscode-jsonrpc; charset=utf-8\r\n\r\n" + s
68+
}
69+
70+
expect(`{"jsonrpc":"2.0","id":1,"result":{"capabilities":{"textDocumentSync":{"openClose":true,"change":1}},"serverInfo":{"name":"gdshader-language-server","version":"development"}}}`)
71+
expect(`{"jsonrpc":"2.0","id":2,"result":null}`)
72+
73+
g.Expect(stdout.String()).To(BeComparableTo(string(expected)), "Output does not match expected")
74+
}

internal/handler/handler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ type Handler struct {
1111
isUTF8 bool
1212
}
1313

14-
func (h *Handler) Initialize(clientCapabilities *lsp.ClientCapabilities) (*lsp.ServerCapabilities, error) {
14+
func (h *Handler) Initialize(clientCapabilities lsp.ClientCapabilities) (*lsp.ServerCapabilities, error) {
1515
var serverCapabilities lsp.ServerCapabilities
1616
serverCapabilities.TextDocumentSync.OpenClose = true
1717

internal/lsp/server.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
)
1515

1616
type Handler interface {
17-
Initialize(clientCapabilities *ClientCapabilities) (*ServerCapabilities, error)
17+
Initialize(clientCapabilities ClientCapabilities) (*ServerCapabilities, error)
1818
}
1919

2020
type Server struct {
@@ -49,7 +49,7 @@ func (s *Server) Serve() error {
4949

5050
if len(request.ID) == 0 {
5151
logger := slog.With("method", request.Method)
52-
logger.Debug("Received notification")
52+
logger.Debug("Received notification", "params", string(request.Params))
5353

5454
if request.Method == "exit" {
5555
logger.Info("Exiting")
@@ -64,7 +64,7 @@ func (s *Server) Serve() error {
6464
}
6565

6666
logger := slog.With("request_id", request.ID, "method", request.Method)
67-
logger.Debug("Received request")
67+
logger.Debug("Received request", "params", string(request.Params))
6868

6969
response, err := s.handleRequest(request.Method, request.Params)
7070
if err != nil {
@@ -142,7 +142,7 @@ func (s *Server) handleRequest(method string, paramsRaw json.RawMessage) (any, e
142142
Name string `json:"name"`
143143
Version string `json:"version"`
144144
} `json:"clientInfo"`
145-
Capabilities *ClientCapabilities `json:"capabilities"`
145+
Capabilities ClientCapabilities `json:"capabilities"`
146146
}
147147
if err := parseParams(paramsRaw, &params); err != nil {
148148
return nil, err

main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func main() {
2929

3030
server := &lsp.Server{
3131
Info: lsp.ServerInfo{
32-
Name: "gdshader-lsp",
32+
Name: "gdshader-language-server",
3333
Version: Version,
3434
},
3535
Handler: &handler.Handler{},

0 commit comments

Comments
 (0)