emacs-diffs
[Top][All Lists]
Advanced

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

master 5dd27fe 1/3: Add generated suffix to test temp file names


From: Stefan Kangas
Subject: master 5dd27fe 1/3: Add generated suffix to test temp file names
Date: Sat, 6 Nov 2021 21:20:30 -0400 (EDT)

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

    Add generated suffix to test temp file names
    
    * lisp/emacs-lisp/ert-x.el
    (ert-with-temp-file): Add temp file name suffix based on file name of
    caller.  Reflow docstring.
    (ert--with-temp-file-generate-suffix): New defun.
    * test/lisp/emacs-lisp/ert-x-tests.el
    (ert-x-tests--with-temp-file-generate-suffix): New test.
---
 lisp/emacs-lisp/ert-x.el            | 29 ++++++++++++++++++++++-------
 test/lisp/emacs-lisp/ert-x-tests.el |  9 +++++++++
 2 files changed, 31 insertions(+), 7 deletions(-)

diff --git a/lisp/emacs-lisp/ert-x.el b/lisp/emacs-lisp/ert-x.el
index 2af956c..752ac3b 100644
--- a/lisp/emacs-lisp/ert-x.el
+++ b/lisp/emacs-lisp/ert-x.el
@@ -352,7 +352,6 @@ convert it to a string and pass it to COLLECTOR first."
 (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 (test data) directory.
 
@@ -392,6 +391,17 @@ directory as returned by `ert-resource-directory'."
 (defvar ert-temp-file-suffix nil
   "Prefix used by `ert-with-temp-file' and `ert-with-temp-directory'.")
 
+(defun ert--with-temp-file-generate-suffix (filename)
+  "Generate temp file suffix from FILENAME."
+  (thread-last
+    (file-name-base filename)
+    (replace-regexp-in-string (rx string-start
+                                  (group (+? not-newline))
+                                  (regexp "-?tests?")
+                                  string-end)
+                              "\\1")
+    (concat "-")))
+
 (defmacro ert-with-temp-file (name &rest body)
   "Bind NAME to the name of a new temporary file and evaluate BODY.
 Delete the temporary file after BODY exits normally or
@@ -401,12 +411,15 @@ file.
 The following keyword arguments are supported:
 
 :prefix STRING  If non-nil, pass STRING to `make-temp-file' as
-                the PREFIX argument.  Otherwise, use the value
-                of `ert-temp-file-prefix'.
+                the PREFIX argument.  Otherwise, use the value of
+                `ert-temp-file-prefix'.
 
-:suffix STRING  If non-nil, pass STRING to `make-temp-file' as
-                the SUFFIX argument.  Otherwise, use the value
-                of `ert-temp-file-suffix'.
+:suffix STRING  If non-nil, pass STRING to `make-temp-file' as the
+                SUFFIX argument.  Otherwise, use the value of
+                `ert-temp-file-suffix'; if the value of that
+                variable is nil, generate a suffix based on the
+                name of the file that `ert-with-temp-file' is
+                called from.
 
 :text STRING    If non-nil, pass STRING to `make-temp-file' as
                 the TEXT argument.
@@ -427,7 +440,9 @@ See also `ert-with-temp-directory'."
       (error "Invalid keywords: %s" (mapconcat #'symbol-name extra-keywords " 
")))
     (let ((temp-file (make-symbol "temp-file"))
           (prefix (or prefix ert-temp-file-prefix))
-          (suffix (or suffix ert-temp-file-suffix)))
+          (suffix (or suffix ert-temp-file-suffix
+                      (ert--with-temp-file-generate-suffix
+                       (or (macroexp-file-name) buffer-file-name)))))
       `(let* ((,temp-file (,(if directory 'file-name-as-directory 'identity)
                            (make-temp-file ,prefix ,directory ,suffix ,text)))
               (,name ,temp-file))
diff --git a/test/lisp/emacs-lisp/ert-x-tests.el 
b/test/lisp/emacs-lisp/ert-x-tests.el
index 1eed5bb..d7c0985 100644
--- a/test/lisp/emacs-lisp/ert-x-tests.el
+++ b/test/lisp/emacs-lisp/ert-x-tests.el
@@ -271,6 +271,15 @@ desired effect."
     (cl-loop for x in '(0 1 2 3 4 t) do
              (should (equal (c x) (lisp x))))))
 
+(ert-deftest ert-x-tests--with-temp-file-generate-suffix ()
+  (should (equal (ert--with-temp-file-generate-suffix "foo.el") "-foo"))
+  (should (equal (ert--with-temp-file-generate-suffix "foo-test.el") "-foo"))
+  (should (equal (ert--with-temp-file-generate-suffix "foo-tests.el") "-foo"))
+  (should (equal (ert--with-temp-file-generate-suffix "foo-bar-baz.el")
+                 "-foo-bar-baz"))
+  (should (equal (ert--with-temp-file-generate-suffix "/foo/bar/baz.el")
+                 "-baz")))
+
 (ert-deftest ert-x-tests-with-temp-file ()
   (let (saved)
     (ert-with-temp-file fil



reply via email to

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