|
| 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 | +} |
0 commit comments