]> git.eshelyaron.com Git - emacs.git/commitdiff
Eglot: robustify eglot-test-basic-symlink test (bug#70036)
authorJoão Távora <joaotavora@gmail.com>
Tue, 23 Apr 2024 13:43:06 +0000 (08:43 -0500)
committerEshel Yaron <me@eshelyaron.com>
Wed, 24 Apr 2024 17:42:34 +0000 (19:42 +0200)
The previous version of this test was brittle, unstable and
didn't really fail when supposed to (because we need main.cpp to
not be visited when visiting mainlink.cpp).  This new version is
faster and more secure.

* test/lisp/progmodes/eglot-tests.el (eglot--sniffing): Add to the
jsonrpc-event-hook at the end.
(eglot-test-basic-symlink): Robustify test.

(cherry picked from commit 418cade2390e4d6cd7ead1122e62742282e7b2cd)

test/lisp/progmodes/eglot-tests.el

index 914f23bc9e3aca52d2c4a5500bfadbf0db96ea7d..4a75a1c5147c7d84f3fc5a840ba5cd30a68641f1 100644 (file)
@@ -230,7 +230,7 @@ directory hierarchy."
                                          `(push message ,client-replies)))))))))
          (unwind-protect
              (progn
-               (add-hook 'jsonrpc-event-hook #',log-event-hook-sym)
+               (add-hook 'jsonrpc-event-hook #',log-event-hook-sym t)
                ,@body)
            (remove-hook 'jsonrpc-event-hook #',log-event-hook-sym))))))
 
@@ -452,17 +452,42 @@ directory hierarchy."
         (find-file-noselect "symlink-project/main.cpp")
       (make-symbolic-link "main.cpp" "mainlink.cpp")
       (eglot--tests-connect)
-      (find-file-noselect "mainlink.cpp")
+      (eglot--sniffing (:client-notifications c-notifs)
+        (let ((eglot-autoshutdown nil)) (kill-buffer (current-buffer)))
+        (eglot--wait-for (c-notifs 10)
+            (&key method &allow-other-keys)
+          (and (string= method "textDocument/didClose")))))
+    (eglot--sniffing (:client-notifications c-notifs)
       (with-current-buffer
-          (find-file-noselect "foo.h")
-        (goto-char 5)
-        (xref-find-references "foo")
-        (with-current-buffer (get-buffer "*xref*")
-          (goto-char (point-max))
-          ;; Expect xref buffer to not contain duplicate references to
-          ;; main.c and mainlink.c.  If it did, total lines would be 7.
-          ;; FIXME: make less brittle by counting actual references.
-          (should (= (line-number-at-pos (point)) 5)))))))
+          (find-file-noselect "symlink-project/main.cpp")
+        (should (eglot-current-server)))
+      (eglot--wait-for (c-notifs 10)
+          (&rest whole &key params method &allow-other-keys)
+        (and (string= method "textDocument/didOpen")
+             (string-match "main.cpp$"
+                           (plist-get (plist-get params :textDocument)
+                                      :uri)))))
+    ;; This last segment is deactivated, because it's likely not needed.
+    ;; The only way the server would answer with '3' references is if we
+    ;; had erroneously sent a 'didOpen' for anything other than
+    ;; `main.cpp', but if we got this far is because we've just asserted
+    ;; that we didn't.
+    (when nil
+      (with-current-buffer
+          (find-file-noselect "symlink-project/foo.h")
+        ;; Give clangd some time to settle its analysis so it can
+        ;; accurately respond to `textDocument/references'
+        (sleep-for 3)
+        (search-forward "foo")
+        (eglot--sniffing (:server-replies s-replies)
+          (call-interactively 'xref-find-references)
+          (eglot--wait-for (s-replies 10)
+              (&key method result &allow-other-keys)
+            ;; Expect xref buffer to not contain duplicate references to
+            ;; main.cpp and mainlink.cpp.  If it did, 'result's length
+            ;; would be 3.
+            (and (string= method "textDocument/references")
+                 (= (length result) 2))))))))
 
 (ert-deftest eglot-test-diagnostic-tags-unnecessary-code ()
   "Test rendering of diagnostics tagged \"unnecessary\"."