>From 825d0781522f87c6a2cc954a85b2c48a499d4857 Mon Sep 17 00:00:00 2001 From: "Basil L. Contovounesios" Date: Sun, 19 Feb 2023 17:41:48 +0000 Subject: [PATCH 1/2] Fix eglot-tests.el that need HOME=~USER * test/lisp/progmodes/eglot-tests.el (eglot-tests--real-home-env): New helper function. (eglot-test-rust-analyzer-watches-files) (eglot-test-diagnostic-tags-unnecessary-code) (eglot-test-rust-analyzer-hover-after-edit) (eglot-test-rust-on-type-formatting) (eglot-test-project-wide-diagnostics-rust-analyzer): Use it to run cargo and rust-analyzer under HOME=~USER. (eglot-test-python-autopep-formatting): Update expected formats for latest autopep8. (eglot-test-json-basic): Skip when YASnippet is missing. --- test/lisp/progmodes/eglot-tests.el | 108 +++++++++++++++++------------ 1 file changed, 63 insertions(+), 45 deletions(-) diff --git a/test/lisp/progmodes/eglot-tests.el b/test/lisp/progmodes/eglot-tests.el index 4b6528351b2..4e1b025e63f 100644 --- a/test/lisp/progmodes/eglot-tests.el +++ b/test/lisp/progmodes/eglot-tests.el @@ -86,6 +86,14 @@ eglot--make-file-or-dir (t (eglot--error "Expected a string or a directory spec"))))) +(defun eglot-tests--real-home-env () + "Return a `process-environment' with user's real HOME directory. +This uses `user-login-name' to work around cases where the HOME +environment variable has been set to something like +\"/nonexistent\" or `temporary-file-directory'." + (cons (concat "HOME=" (expand-file-name (concat "~" (user-login-name)))) + process-environment)) + (defun eglot--call-with-fixture (fixture fn) "Helper for `eglot--with-fixture'. Run FN under FIXTURE." (let* ((fixture-directory (make-nearby-temp-file "eglot--fixture" t)) @@ -394,27 +402,27 @@ eglot-test-rust-analyzer-watches-files "Start rust-analyzer. Notify it when a critical file changes." (skip-unless (executable-find "rust-analyzer")) (skip-unless (executable-find "cargo")) - (let ((eglot-autoreconnect 1)) - (eglot--with-fixture - '(("watch-project" . (("coiso.rs" . "bla") - ("merdix.rs" . "bla")))) - (with-current-buffer - (eglot--find-file-noselect "watch-project/coiso.rs") + (eglot--with-fixture + '(("watch-project" . (("coiso.rs" . "bla") + ("merdix.rs" . "bla")))) + (with-current-buffer + (eglot--find-file-noselect "watch-project/coiso.rs") + (let ((eglot-autoreconnect 1) + ;; Cargo doesn't work under `temporary-file-directory'. + (process-environment (eglot-tests--real-home-env)) + register-id) (should (zerop (shell-command "cargo init"))) - (eglot--sniffing ( - :server-requests s-requests - :client-notifications c-notifs - :client-replies c-replies - ) + (eglot--sniffing ( :server-requests s-requests + :client-notifications c-notifs + :client-replies c-replies) (should (eglot--tests-connect)) - (let (register-id) - (eglot--wait-for (s-requests 1) - (&key id method &allow-other-keys) - (setq register-id id) - (string= method "client/registerCapability")) - (eglot--wait-for (c-replies 1) - (&key id error &allow-other-keys) - (and (eq id register-id) (null error)))) + (eglot--wait-for (s-requests 3) + (&key id method &allow-other-keys) + (setq register-id id) + (string= method "client/registerCapability")) + (eglot--wait-for (c-replies 1) + (&key id error &allow-other-keys) + (and (eq id register-id) (null error))) (delete-file "Cargo.toml") (eglot--wait-for (c-notifs 3 "waiting for didChangeWatchedFiles notification") @@ -453,7 +461,9 @@ eglot-test-diagnostic-tags-unnecessary-code "fn main() -> () { let test=3; }")))) (with-current-buffer (eglot--find-file-noselect "diagnostic-tag-project/main.rs") - (let ((eglot-server-programs '((rust-mode . ("rust-analyzer"))))) + (let ((eglot-server-programs '((rust-mode . ("rust-analyzer")))) + ;; Cargo doesn't work under `temporary-file-directory'. + (process-environment (eglot-tests--real-home-env))) (should (zerop (shell-command "cargo init"))) (eglot--sniffing (:server-notifications s-notifs) (eglot--tests-connect) @@ -496,21 +506,20 @@ eglot-test-rust-analyzer-hover-after-edit "fn test() -> i32 { let test=3; return te; }")))) (with-current-buffer (eglot--find-file-noselect "hover-project/main.rs") - (should (zerop (shell-command "cargo init"))) - (eglot--sniffing ( - :server-replies s-replies - :client-requests c-reqs - ) - (eglot--tests-connect) - (goto-char (point-min)) - (search-forward "return te") - (insert "st") - (progn - ;; simulate these two which don't happen when buffer isn't + (let (;; Cargo doesn't work under `temporary-file-directory'. + (process-environment (eglot-tests--real-home-env)) + pending-id) + (should (zerop (shell-command "cargo init"))) + (eglot--sniffing ( :server-replies s-replies + :client-requests c-reqs) + (eglot--tests-connect) + (goto-char (point-min)) + (search-forward "return te") + (insert "st") + ;; Simulate these two which don't happen when buffer isn't ;; visible in a window. (eglot--signal-textDocument/didChange) - (eglot--eldoc-on-demand)) - (let (pending-id) + (eglot--eldoc-on-demand) (eglot--wait-for (c-reqs 2) (&key id method &allow-other-keys) (setq pending-id id) @@ -683,18 +692,22 @@ eglot-test-python-autopep-formatting (with-current-buffer (eglot--find-file-noselect "project/something.py") (should (eglot--tests-connect)) - ;; Try to format just the second line + ;; Try to format just the second line. (search-forward "b():pa") (eglot-format (line-beginning-position) (line-end-position)) - (should (looking-at "ss")) - (should - (or (string= (buffer-string) "def a():pass\n\n\ndef b(): pass\n") - ;; autopep8 2.0.0 (pycodestyle: 2.9.1) - (string= (buffer-string) "def a():pass\n\ndef b(): pass"))) - ;; now format the whole buffer + (should (looking-at-p "ss")) + (should (member (buffer-string) + '(;; autopep8 2.0.1 (pycodestyle: 2.10.0) + "def a():pass\n\ndef b():\n pass\n" + ;; autopep8 2.0.0 (pycodestyle: 2.9.1) + "def a():pass\n\ndef b(): pass" + "def a():pass\n\n\ndef b(): pass\n"))) + ;; Now format the whole buffer. (eglot-format-buffer) - (should - (string= (buffer-string) "def a(): pass\n\n\ndef b(): pass\n"))))) + (should (member (buffer-string) + '(;; autopep8 2.0.1 (pycodestyle: 2.10.0) + "def a():\n pass\n\n\ndef b():\n pass\n" + "def a(): pass\n\n\ndef b(): pass\n")))))) (ert-deftest eglot-test-python-yapf-formatting () "Test formatting in the pylsp python LSP." @@ -728,12 +741,14 @@ eglot-test-rust-on-type-formatting "fn main() -> () {\n foo\n .bar()\n ")))) (with-current-buffer (eglot--find-file-noselect "on-type-formatting-project/main.rs") - (let ((eglot-server-programs '((rust-mode . ("rust-analyzer"))))) + (let ((eglot-server-programs '((rust-mode . ("rust-analyzer")))) + ;; Cargo doesn't work under `temporary-file-directory'. + (process-environment (eglot-tests--real-home-env))) (should (zerop (shell-command "cargo init"))) (eglot--sniffing (:server-notifications s-notifs) (should (eglot--tests-connect)) (eglot--wait-for (s-notifs 10) (&key method &allow-other-keys) - (string= method "textDocument/publishDiagnostics"))) + (string= method "textDocument/publishDiagnostics"))) (goto-char (point-max)) (eglot--simulate-key-event ?.) (should (looking-back "^ \\.")))))) @@ -808,7 +823,9 @@ eglot-test-project-wide-diagnostics-rust-analyzer ("other-file.rs" . "fn foo() -> () { let hi=3; }")))) (eglot--make-file-or-dir '(".git")) - (let ((eglot-server-programs '((rust-mode . ("rust-analyzer"))))) + (let ((eglot-server-programs '((rust-mode . ("rust-analyzer")))) + ;; Cargo doesn't work under `temporary-file-directory'. + (process-environment (eglot-tests--real-home-env))) ;; Open other-file, and see diagnostics arrive for main.rs (with-current-buffer (eglot--find-file-noselect "project/other-file.rs") (should (zerop (shell-command "cargo init"))) @@ -829,6 +846,7 @@ eglot-test-project-wide-diagnostics-rust-analyzer (ert-deftest eglot-test-json-basic () "Test basic autocompletion in vscode-json-languageserver." (skip-unless (executable-find "vscode-json-languageserver")) + (skip-unless (fboundp 'yas-minor-mode)) (eglot--with-fixture '(("project" . (("p.json" . "{\"foo.b") -- 2.39.1