quick test with emacs batch to ensure that eglot is working with rust
0
test-lsp.el
1;;; test-lsp.el --- Test Rust LSP diagnostics with Eglot -*- lexical-binding: t -*-
2;;; run with: emacs -batch -l test-lsp.el
3
4;;; Commentary:
5;; This test verifies that Eglot (Emacs LSP client) correctly detects
6;; type errors in Rust code using rust-analyzer.
7;;
8;; The test:
9;; 1. Loads the user's Emacs configuration (~/.emacs.d)
10;; 2. Opens src/main.rs (which contains an intentional type error)
11;; 3. Starts Eglot and connects to rust-analyzer
12;; 4. Waits for LSP diagnostics to be reported
13;; 5. Verifies the expected type error is detected
14;;
15;; Exit codes:
16;; - 0: Test passed (type error detected)
17;; - 1: Test failed (no diagnostics or wrong error)
18
19;;; Code:
20
21;; Load user config (external dependency)
22(load "~/.emacs.d/early-init.el" nil t)
23(load "~/.emacs.d/init.el" nil t)
24
25(require 'eglot)
26(require 'flymake)
27
28(defconst test-lsp--expected-error-patterns
29 '("mismatched types"
30 "expected.*i32"
31 "found.*&str"
32 "E0308")
33 "Regex patterns to identify the expected type error.")
34
35(defconst test-lsp--timeout-seconds 30
36 "Maximum time to wait for LSP diagnostics.")
37
38(defun test-lsp--check-diagnostic (diag)
39 "Check if DIAG matches the expected type error patterns.
40Returns t if matched, nil otherwise."
41 (let ((msg (flymake-diagnostic-text diag)))
42 (cl-some (lambda (pattern)
43 (string-match-p pattern msg))
44 test-lsp--expected-error-patterns)))
45
46(defun test-lsp--start-eglot ()
47 "Start Eglot and wait for connection.
48Returns the server object on success, signals error on failure."
49 (message "Starting eglot...")
50 (let ((eglot-autoshutdown t)
51 (eglot-sync-connect 10)
52 (current-prefix-arg nil))
53 (call-interactively 'eglot))
54
55 (let ((server (eglot-current-server)))
56 (unless server
57 (error "Eglot failed to start"))
58 (message "Eglot connected: %s" (eglot--project-nickname server))
59 server))
60
61(defun test-lsp--wait-for-diagnostics (timeout)
62 "Wait up to TIMEOUT seconds for LSP diagnostics.
63Returns the list of diagnostics, or nil if timeout reached."
64 (message "Waiting for diagnostics...")
65 (let ((start-time (float-time))
66 diagnostics)
67 (while (and (< (- (float-time) start-time) timeout)
68 (progn
69 (setq diagnostics (flymake-diagnostics (point-min) (point-max)))
70 (or (null diagnostics)
71 (= (length diagnostics) 0))))
72 (accept-process-output nil 0.1))
73 diagnostics))
74
75(defun test-rust-lsp-diagnostics ()
76 "Open a Rust file, start Eglot, and verify type error detection.
77Exits with status 0 on success, 1 on failure."
78 (let ((file "src/main.rs"))
79
80 ;; Open the test file
81 (find-file file)
82 (message "Opened %s" (buffer-file-name))
83
84 ;; Verify we're in a Rust mode
85 (unless (or (derived-mode-p 'rust-mode)
86 (derived-mode-p 'rust-ts-mode))
87 (error "Not in a Rust mode (current: %s)" major-mode))
88
89 ;; Start Eglot
90 (test-lsp--start-eglot)
91
92 ;; Wait for diagnostics
93 (let ((diagnostics (test-lsp--wait-for-diagnostics test-lsp--timeout-seconds)))
94
95 ;; Check results
96 (if (and diagnostics (> (length diagnostics) 0))
97 (let ((found-error nil))
98 (message "Found %d diagnostic(s)" (length diagnostics))
99
100 ;; Check all diagnostics for the type error
101 (dolist (diag diagnostics)
102 (let ((type (flymake-diagnostic-type diag))
103 (msg (flymake-diagnostic-text diag)))
104 (message " - [%s] %s" type msg)
105 (when (test-lsp--check-diagnostic diag)
106 (setq found-error t))))
107
108 (if found-error
109 (progn
110 (message "TEST PASSED: Found expected type error")
111 (kill-emacs 0))
112 (progn
113 (message "TEST FAILED: Type error not found in diagnostics")
114 (kill-emacs 1))))
115
116 (message "TEST FAILED: No diagnostics received within %d seconds"
117 test-lsp--timeout-seconds)
118 (kill-emacs 1)))))
119
120;; Run the test
121(condition-case err
122 (test-rust-lsp-diagnostics)
123 (error
124 (message "TEST ERROR: %s" (error-message-string err))
125 (kill-emacs 1)))
126
127;;; test-lsp.el ends here