From 731c39a27b9a21754cbee73e7199366a7cc38d82 Mon Sep 17 00:00:00 2001 From: "Basil L. Contovounesios" Date: Sun, 19 Feb 2023 18:09:06 +0000 Subject: [PATCH 2/2] Minor housekeeping in ert-x and eglot-tests * lisp/emacs-lisp/ert-x.el (ert-remote-temporary-file-directory): Don't add trailing slash to HOME. * test/lisp/progmodes/eglot-tests.el: Move footer line to EOF. (eglot--with-fixture): Don't allow fixture elements to be symbol-value pairs. This feature was used in only one test. The same effect can be achieved in a simpler way, and closer to the body, with let-bindings. (eglot--make-file-or-dir): Ensure default-directory ends with a slash. Simplify using with-temp-file. (eglot--call-with-fixture): Ensure default-directory ends with a slash. Remove symbol setting and restoring logic. Modify process-environment only when needed, and simplify using eglot-tests--real-home-env. Leave XDG_CONFIG_HOME alone; it is already unexported in test/Makefile.in. Fix let-binding syntax. (eglot--cleanup-after-test): Remove symbol restoring logic. (eglot--eldoc-on-demand, eglot--tests-force-full-eldoc): Fix commentary typos. (eglot-test-ensure): Adapt c-mode-hook binding to eglot--with-fixture changes. --- lisp/emacs-lisp/ert-x.el | 2 +- test/lisp/progmodes/eglot-tests.el | 97 ++++++++++++------------------ 2 files changed, 39 insertions(+), 60 deletions(-) diff --git a/lisp/emacs-lisp/ert-x.el b/lisp/emacs-lisp/ert-x.el index 98a017c8a8e..3bf9b59f874 100644 --- a/lisp/emacs-lisp/ert-x.el +++ b/lisp/emacs-lisp/ert-x.el @@ -563,7 +563,7 @@ ert-remote-temporary-file-directory ;; Emacs's Makefile sets $HOME to a nonexistent value. Needed ;; in batch mode only, therefore. (when (and noninteractive (not (file-directory-p "~/"))) - (setenv "HOME" temporary-file-directory)) + (setenv "HOME" (directory-file-name temporary-file-directory))) (format "/mock::%s" temporary-file-directory)))) "Temporary directory for remote file tests.") diff --git a/test/lisp/progmodes/eglot-tests.el b/test/lisp/progmodes/eglot-tests.el index 4e1b025e63f..cce5b572521 100644 --- a/test/lisp/progmodes/eglot-tests.el +++ b/test/lisp/progmodes/eglot-tests.el @@ -60,28 +60,25 @@ ;;; Helpers (defmacro eglot--with-fixture (fixture &rest body) - "Setup FIXTURE, call BODY, teardown FIXTURE. + "Set up FIXTURE, call BODY, tear down FIXTURE. FIXTURE is a list. Its elements are of the form (FILE . CONTENT) to create a readable FILE with CONTENT. FILE may be a directory name and CONTENT another (FILE . CONTENT) list to specify a -directory hierarchy. FIXTURE's elements can also be (SYMBOL -VALUE) meaning SYMBOL should be bound to VALUE during BODY and -then restored." +directory hierarchy." (declare (indent 1) (debug t)) - `(eglot--call-with-fixture - ,fixture #'(lambda () ,@body))) + `(eglot--call-with-fixture ,fixture (lambda () ,@body))) (defun eglot--make-file-or-dir (ass) (let ((file-or-dir-name (car ass)) (content (cdr ass))) (cond ((listp content) (make-directory file-or-dir-name 'parents) - (let ((default-directory (concat default-directory "/" file-or-dir-name))) + (let* ((subdir (expand-file-name file-or-dir-name default-directory)) + (default-directory (file-name-as-directory subdir))) (mapcan #'eglot--make-file-or-dir content))) ((stringp content) - (with-temp-buffer - (insert content) - (write-region nil nil file-or-dir-name nil 'nomessage)) + (with-temp-file file-or-dir-name + (insert content)) (list (expand-file-name file-or-dir-name))) (t (eglot--error "Expected a string or a directory spec"))))) @@ -96,43 +93,27 @@ eglot-tests--real-home-env (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)) - (default-directory fixture-directory) - file-specs created-files - syms-to-restore + (let* ((fixture-directory (make-nearby-temp-file "eglot--fixture-" t)) + (default-directory (file-name-as-directory fixture-directory)) + created-files new-servers test-body-successful-p) - (dolist (spec fixture) - (cond ((symbolp spec) - (push (cons spec (symbol-value spec)) syms-to-restore) - (set spec nil)) - ((symbolp (car spec)) - (push (cons (car spec) (symbol-value (car spec))) syms-to-restore) - (set (car spec) (cadr spec))) - ((stringp (car spec)) (push spec file-specs)))) (unwind-protect - (let* ((home (getenv "HOME")) - (process-environment - (append - `(;; Set XDF_CONFIG_HOME to /dev/null to prevent - ;; user-configuration to have an influence on - ;; language servers. (See github#441) - "XDG_CONFIG_HOME=/dev/null" - ;; ... on the flip-side, a similar technique by - ;; Emacs's test makefiles means that HOME is set to - ;; /nonexistent. This breaks some common - ;; installations for LSP servers like pylsp, making - ;; these tests mostly useless, so we hack around it - ;; here with a great big hack. - ,(format "HOME=%s" - (if (file-exists-p home) home - (format "/home/%s" (getenv "USER"))))) - process-environment)) - ;; Prevent "Can't guess python-indent-offset ..." messages. - (python-indent-guess-indent-offset-verbose . nil) - (eglot-server-initialized-hook - (lambda (server) (push server new-servers)))) - (setq created-files (mapcan #'eglot--make-file-or-dir file-specs)) + (let ((process-environment + (if (file-exists-p "~") + process-environment + ;; `test/Makefile' sets `HOME=/nonexistent' (and + ;; `ert-remote-temporary-file-directory' sets it to + ;; `temporary-file-directory'). This breaks some + ;; common installations for LSP servers like pylsp, + ;; making these tests mostly useless, so we hack + ;; around it here with a great big hack. + (eglot-tests--real-home-env))) + ;; Prevent "Can't guess python-indent-offset ..." messages. + (python-indent-guess-indent-offset-verbose nil) + (eglot-server-initialized-hook + (lambda (server) (push server new-servers)))) + (setq created-files (mapcan #'eglot--make-file-or-dir fixture)) (prog1 (funcall fn) (setq test-body-successful-p t))) (eglot--message @@ -165,18 +146,15 @@ eglot--call-with-fixture (t (eglot--message "Preserved for inspection: %s" (mapconcat #'buffer-name buffers ", ")))))))) - (eglot--cleanup-after-test fixture-directory created-files syms-to-restore))))) + (eglot--cleanup-after-test fixture-directory created-files))))) -(defun eglot--cleanup-after-test (fixture-directory created-files syms-to-restore) +(defun eglot--cleanup-after-test (fixture-directory created-files) (let ((buffers-to-delete - (delete nil (mapcar #'find-buffer-visiting created-files)))) - (eglot--message "Killing %s, wiping %s, restoring %s" + (delq nil (mapcar #'find-buffer-visiting created-files)))) + (eglot--message "Killing %s, wiping %s" buffers-to-delete - fixture-directory - (mapcar #'car syms-to-restore)) - (cl-loop for (sym . val) in syms-to-restore - do (set sym val)) - (dolist (buf buffers-to-delete) ;; have to save otherwise will get prompted + fixture-directory) + (dolist (buf buffers-to-delete) ;; Have to save otherwise will get prompted. (with-current-buffer buf (save-buffer) (kill-buffer))) (delete-directory fixture-directory 'recursive) ;; Delete Tramp buffers if needed. @@ -476,11 +454,11 @@ eglot-test-diagnostic-tags-unnecessary-code (should (eq 'eglot-diagnostic-tag-unnecessary-face (face-at-point)))))))) (defun eglot--eldoc-on-demand () - ;; Trick Eldoc 1.1.0 into accepting on-demand calls. + ;; Trick ElDoc 1.1.0 into accepting on-demand calls. (eldoc t)) (defun eglot--tests-force-full-eldoc () - ;; FIXME: This uses some Eldoc implementation defatils. + ;; FIXME: This uses some ElDoc implementation details. (when (buffer-live-p eldoc--doc-buffer) (with-current-buffer eldoc--doc-buffer (let ((inhibit-read-only t)) @@ -898,9 +876,9 @@ eglot-test-ensure (skip-unless (executable-find "clangd")) (eglot--with-fixture `(("project" . (("foo.c" . "int foo() {return 42;}") - ("bar.c" . "int bar() {return 42;}"))) - (c-mode-hook (eglot-ensure))) - (let (server) + ("bar.c" . "int bar() {return 42;}")))) + (let ((c-mode-hook '(eglot-ensure)) + server) ;; need `ert-simulate-command' because `eglot-ensure' ;; relies on `post-command-hook'. (with-current-buffer @@ -1331,8 +1309,9 @@ eglot-test-same-server-multi-mode (should (eq (eglot-current-server) server)))))) (provide 'eglot-tests) -;;; eglot-tests.el ends here ;; Local Variables: ;; checkdoc-force-docstrings-flag: nil ;; End: + +;;; eglot-tests.el ends here -- 2.39.1