;;; -*- lexical-binding:t -*- ;; Run these tests with: ;; emacs -Q --batch -l ~/etc/emacs/connection-local.el \ ;; --eval '(ert-run-tests-batch-and-exit t)' (require 'tramp) (require 'ert) (require 'ert-x) (defvar-local eshell-path-env-list nil) (defsubst eshell-connection-local-criteria-for-default-directory () (or (connection-local-criteria-for-default-directory 'eshell) '(:application eshell :machine local))) (defsubst eshell-connection-local-profile () (intern (concat "eshell-connection-local-profile-" (or (file-remote-p default-directory) "local")))) ;; Initial values. (connection-local-set-profile-variables 'eshell-connection-local-profile '((eshell-path-env-list . nil))) (connection-local-set-profiles '(:application eshell) 'eshell-connection-local-profile) (defun eshell-get-path () "Return $PATH as a list." (let ((enable-connection-local-variables t) connection-local-variables-alist) ;; I'm not sure this is needed. (hack-connection-local-variables-apply (eshell-connection-local-criteria-for-default-directory)) (prog1 (or eshell-path-env-list ;; If not already cached, get the path from `exec-path', ;; removing the last element, which is `exec-directory'. (setq eshell-path-env-list (butlast (exec-path)))) ;; Set connection-local-variable. (connection-local-set-profile-variables (eshell-connection-local-profile) `((eshell-path-env-list . ,eshell-path-env-list))) (connection-local-set-profiles (eshell-connection-local-criteria-for-default-directory) (eshell-connection-local-profile))))) (defun eshell-set-path (path) "Set the Eshell $PATH to PATH. PATH can be either a list of directories or a string of directories separated by `path-separator'." (let ((enable-connection-local-variables t) connection-local-variables-alist) ;; I'm not sure this is needed. (hack-connection-local-variables-apply (eshell-connection-local-criteria-for-default-directory)) (prog1 (setq eshell-path-env-list (if (listp path) path ;; Don't use `parse-colon-path' here, since we don't want ;; the additonal translations it does on each element. (split-string path (path-separator)))) ;; Set connection-local-variable. (connection-local-set-profile-variables (eshell-connection-local-profile) `((eshell-path-env-list . ,eshell-path-env-list))) (connection-local-set-profiles (eshell-connection-local-criteria-for-default-directory) (eshell-connection-local-profile))))) (ert-deftest esh-var-test/path-var/preserve-across-hosts () "Test that $PATH can be set independently on multiple hosts." ;; Test the initial value of the local $PATH. (should (equal (eshell-get-path) (butlast (exec-path)))) ;; Set the local $PATH and make sure it retains the value we set. (should (equal (eshell-set-path "/local/path") '("/local/path"))) (should (equal (eshell-get-path) '("/local/path"))) ; FAIL (let ((default-directory ert-remote-temporary-file-directory)) ;; Test the initial value of the remote $PATH. (should (equal (eshell-get-path) (butlast (exec-path)))) ;; Set the remote $PATH and make sure it retains the value we set. (should (equal (eshell-set-path "/remote/path") '("/remote/path"))) (should (equal (eshell-get-path) '("/remote/path")))) ; FAIL ;; Make sure we get the local $PATH we set above. (should (equal (eshell-get-path) '("/local/path"))) ; FAIL ;; Make sure we get the remote $PATH we set above. (let ((default-directory ert-remote-temporary-file-directory)) (should (equal (eshell-get-path) '("/remote/path"))))) ; FAIL