branch: externals/compat commit 74ec2c1c56ec178ac5b4cd47fb3b7837ebb8f942 Author: Daniel Mendler <m...@daniel-mendler.de> Commit: Daniel Mendler <m...@daniel-mendler.de>
Add test for make-nearby-temp-file --- compat-26.el | 2 +- compat-27.el | 44 +++++++++++++++++++++++++------------------- compat-tests.el | 18 ++++++++++++++++++ 3 files changed, 44 insertions(+), 20 deletions(-) diff --git a/compat-26.el b/compat-26.el index 0670851c07..3502f66db8 100644 --- a/compat-26.el +++ b/compat-26.el @@ -368,7 +368,7 @@ the variable `temporary-file-directory' is returned." default-directory temporary-file-directory)))) -(compat-defun make-nearby-temp-file (prefix &optional dir-flag suffix) ;; <UNTESTED> +(compat-defun make-nearby-temp-file (prefix &optional dir-flag suffix) ;; <compat-tests:make-nearby-temp-file> "Create a temporary file as close as possible to `default-directory'. If PREFIX is a relative file name, and `default-directory' is a remote file name or located on a mounted file systems, the diff --git a/compat-27.el b/compat-27.el index 0dbad6ed57..04c4b5b66d 100644 --- a/compat-27.el +++ b/compat-27.el @@ -419,25 +419,31 @@ The remote host is identified by `default-directory'. For remote hosts that do not support subprocesses, this returns nil. If `default-directory' is a local directory, this function returns the value of the variable `exec-path'." - ;; NOTE: We cannot use the 'exec-path file name handler here since it didn't - ;; exist before Emacs 27.1. - (if (file-remote-p default-directory) - ;; TODO: This is not completely portable, even if "sh" and - ;; "getconf" should be provided on every POSIX system, the chance - ;; of this not working are greater than zero. - ;; - ;; FIXME: This invokes a shell process every time exec-path is - ;; called. It should instead be cached on a host-local basis. - (with-temp-buffer - (if (condition-case nil - (zerop (process-file "sh" nil t nil "-c" "getconf PATH")) - (file-missing t)) - (list "/bin" "/usr/bin") - (let (path) - (while (re-search-forward "\\([^:]+?\\)[\n:]" nil t) - (push (match-string 1) path)) - (nreverse path)))) - exec-path)) + (cond + ((let ((handler (find-file-name-handler default-directory 'exec-path))) + ;; FIXME: The handler was added in 27.1, and this compatibility + ;; function only applies to versions of Emacs before that. + (when handler + (condition-case nil + (funcall handler 'exec-path) + (error nil))))) + ((file-remote-p default-directory) + ;; TODO: This is not completely portable, even if "sh" and + ;; "getconf" should be provided on every POSIX system, the chance + ;; of this not working are greater than zero. + ;; + ;; FIXME: This invokes a shell process every time exec-path is + ;; called. It should instead be cached on a host-local basis. + (with-temp-buffer + (if (condition-case nil + (zerop (process-file "sh" nil t nil "-c" "getconf PATH")) + (file-missing t)) + (list "/bin" "/usr/bin") + (let (path) + (while (re-search-forward "\\([^:]+?\\)[\n:]" nil t) + (push (match-string 1) path)) + (nreverse path))))) + (exec-path))) (compat-defun executable-find (command &optional remote) ;; <compat-tests:executable-find> "Search for COMMAND in `exec-path' and return the absolute file name. diff --git a/compat-tests.el b/compat-tests.el index f1b630f30f..d8aab1c049 100644 --- a/compat-tests.el +++ b/compat-tests.el @@ -1232,15 +1232,33 @@ (should-equal t (always 1)) ;; single argument (should-equal t (always 1 2 3 4))) ;; multiple arguments +(ert-deftest make-nearby-temp-file () + ;; TODO Test tramp remote directory. + (let ((file1 (make-nearby-temp-file "compat-tests")) + (file2 (make-nearby-temp-file "compat-tests" nil "suffix")) + (dir (make-nearby-temp-file "compat-tests" t))) + (should (string-suffix-p "suffix" file2)) + (should (file-regular-p file1)) + (should (file-regular-p file2)) + (should (file-directory-p dir)) + (should-equal (file-name-directory file1) temporary-file-directory) + (should-equal (file-name-directory file2) temporary-file-directory) + (should-equal (file-name-directory dir) temporary-file-directory) + (delete-file file1) + (delete-file file2) + (delete-directory dir))) + (ert-deftest executable-find () (should (member (executable-find "sh") '("/usr/bin/sh" "/bin/sh"))) (should (member (executable-find "ls") '("/usr/bin/ls" "/bin/ls"))) + ;; TODO Test tramp remote directory. (let ((default-directory (format "/sudo:%s@localhost:/" user-login-name))) (should (member (compat-call executable-find "sh" t) '("/usr/bin/sh" "/bin/sh"))) (should (member (compat-call executable-find "ls" t) '("/usr/bin/ls" "/bin/ls"))))) (ert-deftest exec-path () (should-equal (exec-path) exec-path) + ;; TODO Test tramp remote directory. (let ((default-directory (format "/sudo:%s@localhost:/" user-login-name))) (should (file-directory-p (car (exec-path))))))