Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions hpath.el
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
;; Author: Bob Weiner
;;
;; Orig-Date: 1-Nov-91 at 00:44:23
;; Last-Mod: 28-Jun-26 at 16:52:07 by Bob Weiner
;; Last-Mod: 4-Jul-26 at 23:29:41 by Mats Lidell
;;
;; SPDX-License-Identifier: GPL-3.0-or-later
;;
Expand Down Expand Up @@ -2283,13 +2283,13 @@ variable reference like ${variable}."
(setq val (symbol-value var))
(cond ((stringp val)
(if (setq result
(hpath:substitute-var-name var val path))
(hpath:substitute-var-name var val (expand-file-name path)))
(setq new-path result)))
((null val))
((listp val)
(while (and val (null new-path))
(when (setq result
(hpath:substitute-var-name var (car val) path))
(hpath:substitute-var-name var (car val) (expand-file-name path)))
(setq new-path result))
(setq val (cdr val))))
(t (error "(hpath:substitute-var): `%s' has invalid value for hpath:variables" var)))))
Expand Down Expand Up @@ -2890,8 +2890,7 @@ resolved without attaching the variable name.
If PATH is modified, return PATH, otherwise return nil."
(when (and (stringp var-dir-val) (file-name-absolute-p var-dir-val))
(let ((new-path (replace-regexp-in-string
(regexp-quote (file-name-as-directory
(or var-dir-val default-directory)))
(concat "^" (regexp-quote (file-name-as-directory var-dir-val)))
;; Remove matching path rather than adding the
;; variable to the path when the variable is one
;; for Elisp file paths and path is to an Elisp
Expand Down
42 changes: 41 additions & 1 deletion test/hpath-tests.el
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
;; Author: Mats Lidell <matsl@gnu.org>
;;
;; Orig-Date: 28-Feb-21 at 23:26:00
;; Last-Mod: 20-May-26 at 16:01:46 by Bob Weiner
;; Last-Mod: 5-Jul-26 at 00:10:53 by Mats Lidell
;;
;; SPDX-License-Identifier: GPL-3.0-or-later
;;
Expand Down Expand Up @@ -270,6 +270,46 @@
(should (string= (hpath:substitute-value "$UNDEFINED_IS_NOT_SUBSTITUTED") "$UNDEFINED_IS_NOT_SUBSTITUTED"))
(should (string= (hpath:substitute-value "${UNDEFINED_IS_NOT_SUBSTITUTED}") "${UNDEFINED_IS_NOT_SUBSTITUTED}")))

(ert-deftest hpath:substitute-var-test ()
"Verify `hpath:substitute-var' inserts vars."
(let* ((hyperb:dir (make-temp-file "hypb_dir" t))
(file (expand-file-name "file" hyperb:dir))
(el-file (expand-file-name "file.el" hyperb:dir))
(non-existing-file (expand-file-name "foo.el" hyperb:dir))
(other-dir (make-temp-file "other_dir" t))
(other-el-file (expand-file-name "other.el" other-dir)))
(unwind-protect
(progn
(make-empty-file file)
(make-empty-file el-file)
(make-empty-file other-el-file)
(should (string= "${hyperb:dir}/file"
(hpath:substitute-var file)))
(let ((default-directory other-dir))
(should (string= "${hyperb:dir}/file"
(hpath:substitute-var (file-relative-name file default-directory)))))
(let ((default-directory hyperb:dir))
(should (string= (concat "../" (file-name-base other-dir) "/other.el")
(hpath:substitute-var (file-relative-name other-el-file default-directory)))))
(should (string= "file.el"
(hpath:substitute-var el-file)))
(should (string= non-existing-file
(hpath:substitute-var non-existing-file))))
(hy-delete-files-and-buffers (list file el-file other-el-file))
(hy-delete-dir-and-buffer hyperb:dir)
(hy-delete-dir-and-buffer other-dir))))

(ert-deftest hpath:substitute-var-name-test ()
"Verify variable name is substituted for the variable value in path."
(should (string= "${hyperb:dir}/file"
(hpath:substitute-var-name 'hyperb:dir "/home/user/folder" "/home/user/folder/file")))
(should-not (hpath:substitute-var-name 'hyperb:dir "/home/user/folder" "../home/user/folder/file"))
(should (string= "${hyperb:dir}/file"
(hpath:substitute-var-name 'hyperb:dir "/home/user/folder/" "/home/user/folder/file")))
(should (string= "file.el"
(hpath:substitute-var-name 'hyperb:dir "/home/user/folder/" "/home/user/folder/file.el")))
(should-not (hpath:substitute-var-name 'hyperb:dir "/home/user/folder2/" "/home/user/folder/file.el")))

(defun hypb-run-shell-test-command (command buffer)
"Run a shell COMMAND with output to BUFFER and select it."
(switch-to-buffer buffer)
Expand Down