emacs-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

master f52a775 1/2: Add ert macros to get resource file names (Bug#43792


From: Stefan Kangas
Subject: master f52a775 1/2: Add ert macros to get resource file names (Bug#43792)
Date: Fri, 16 Oct 2020 05:42:04 -0400 (EDT)

branch: master
commit f52a775ae247419918404e50137e1cf6078bd865
Author: Stefan Kangas <stefan@marxist.se>
Commit: Stefan Kangas <stefan@marxist.se>

    Add ert macros to get resource file names (Bug#43792)
    
    * lisp/emacs-lisp/ert-x.el (subr-x): Require.
    (ert-resource-dir, ert-resource-file): New macros to get the file name
    of the resource directory belonging to a test.
    (ert-resource-dir-format, ert-resource-dir-trim-left-regexp)
    (ert-resource-dir-trim-right-regexp): New variables.
---
 lisp/emacs-lisp/ert-x.el | 40 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 40 insertions(+)

diff --git a/lisp/emacs-lisp/ert-x.el b/lisp/emacs-lisp/ert-x.el
index 6569b8c..abbff6d 100644
--- a/lisp/emacs-lisp/ert-x.el
+++ b/lisp/emacs-lisp/ert-x.el
@@ -30,6 +30,7 @@
 
 (eval-when-compile (require 'cl-lib))
 (require 'ert)
+(require 'subr-x) ; string-trim
 
 
 ;;; Test buffers.
@@ -353,6 +354,45 @@ convert it to a string and pass it to COLLECTOR first."
                            (funcall func object)))
       (funcall func object printcharfun))))
 
+(defvar ert-resource-directory-format "%s-resources/"
+  "Format for `ert-resource-directory'.")
+(defvar ert-resource-directory-trim-left-regexp ""
+  "Regexp for `string-trim' (left) used by `ert-resource-directory'.")
+(defvar ert-resource-directory-trim-right-regexp "\\(-tests?\\)?\\.el"
+  "Regexp for `string-trim' (right) used by `ert-resource-directory'.")
+
+;; Has to be a macro for `load-file-name'.
+(defmacro ert-resource-directory ()
+  "Return absolute file name of the resource directory for this file.
+
+The path to the resource directory is the \"resources\" directory
+in the same directory as the test file.
+
+If that directory doesn't exist, use the directory named like the
+test file but formatted by `ert-resource-directory-format' and trimmed
+using `string-trim' with arguments
+`ert-resource-directory-trim-left-regexp' and
+`ert-resource-directory-trim-right-regexp'.  The default values mean
+that if called from a test file named \"foo-tests.el\", return
+the absolute file name for \"foo-resources\"."
+  `(let* ((testfile ,(or (bound-and-true-p byte-compile-current-file)
+                         (and load-in-progress load-file-name)
+                         buffer-file-name))
+          (default-directory (file-name-directory testfile)))
+     (file-truename
+      (if (file-accessible-directory-p "resources/")
+          (expand-file-name "resources/")
+        (expand-file-name
+         (format ert-resource-directory-format
+                 (string-trim testfile
+                              ert-resource-directory-trim-left-regexp
+                              ert-resource-directory-trim-right-regexp)))))))
+
+(defmacro ert-resource-file (file)
+  "Return file name of resource file named FILE.
+A resource file is in the resource directory as per
+`ert-resource-directory'."
+  `(expand-file-name ,file (ert-resource-directory)))
 
 (provide 'ert-x)
 



reply via email to

[Prev in Thread] Current Thread [Next in Thread]