From 54b8ec4e6fb1eeac049e7bd68372e78c180fe8e4 Mon Sep 17 00:00:00 2001 From: Stefan Kangas <stefan@marxist.se> Date: Thu, 21 Oct 2021 19:53:00 +0200 Subject: Remove workaround for fixed Bug#6581 from ert * lisp/emacs-lisp/ert.el (ert-equal-including-properties): Make into obsolete function alias for 'equal-including-properties'. * test/src/editfns-tests.el (format-properties): * test/lisp/emacs-lisp/ert-x-tests.el (ert-propertized-string) (ert-test-run-tests-interactively-2): Don't use above obsolete name. (ert--explain-equal-including-properties-rec): New function. (ert--explain-equal-including-properties): Use as an explainer for 'equal-including-properties' now that Bug#6581 is fixed. * test/lisp/emacs-lisp/ert-tests.el (ert-test-explain-equal-string-properties): Expand test. (ert-test-equal-including-properties): Merge test into above expanded test. --- test/lisp/emacs-lisp/ert-x-tests.el | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'test/lisp/emacs-lisp/ert-x-tests.el') diff --git a/test/lisp/emacs-lisp/ert-x-tests.el b/test/lisp/emacs-lisp/ert-x-tests.el index 9f40a18d343..1784934acb3 100644 --- a/test/lisp/emacs-lisp/ert-x-tests.el +++ b/test/lisp/emacs-lisp/ert-x-tests.el @@ -90,10 +90,10 @@ "foo baz"))) (ert-deftest ert-propertized-string () - (should (ert-equal-including-properties + (should (equal-including-properties (ert-propertized-string "a" '(a b) "b" '(c t) "cd") #("abcd" 1 2 (a b) 2 4 (c t)))) - (should (ert-equal-including-properties + (should (equal-including-properties (ert-propertized-string "foo " '(face italic) "bar" " baz" nil " quux") #("foo bar baz quux" 4 11 (face italic))))) @@ -166,7 +166,7 @@ "1 skipped")))) (with-current-buffer buffer-name (font-lock-mode 0) - (should (ert-equal-including-properties + (should (equal-including-properties (ert-filter-string (buffer-string) '("Started at:\\(.*\\)$" 1) '("Finished at:\\(.*\\)$" 1)) @@ -175,7 +175,7 @@ ;; pretend we are. (let ((noninteractive nil)) (font-lock-mode 1)) - (should (ert-equal-including-properties + (should (equal-including-properties (ert-filter-string (buffer-string) '("Started at:\\(.*\\)$" 1) '("Finished at:\\(.*\\)$" 1)) -- cgit v1.2.3 From bf2e35c5d25a8d1ed5ceacfd6171233bc75eae01 Mon Sep 17 00:00:00 2001 From: Stefan Kangas <stefan@marxist.se> Date: Sat, 6 Nov 2021 13:50:46 +0100 Subject: New convenience macros ert-with-temp-(file|directory) * lisp/emacs-lisp/ert-x.el (ert-with-temp-file) (ert-with-temp-directory): New macros. * test/lisp/emacs-lisp/ert-x-tests.el (ert-x-tests-with-temp-directory) ert-x-tests-with-temp-directory/text-signals-error (ert-x-tests-with-temp-file) (ert-x-tests-with-temp-file/handle-error) (ert-x-tests-with-temp-file/prefix-and-suffix-kwarg) (ert-x-tests-with-temp-file/text-kwarg) (ert-x-tests-with-temp-file/unknown-kwargs-signals-error): New tests. --- lisp/emacs-lisp/ert-x.el | 72 +++++++++++++++++++++++++++++++++++++ test/lisp/emacs-lisp/ert-x-tests.el | 46 ++++++++++++++++++++++++ 2 files changed, 118 insertions(+) (limited to 'test/lisp/emacs-lisp/ert-x-tests.el') diff --git a/lisp/emacs-lisp/ert-x.el b/lisp/emacs-lisp/ert-x.el index a492ef5093f..2af956c8a6d 100644 --- a/lisp/emacs-lisp/ert-x.el +++ b/lisp/emacs-lisp/ert-x.el @@ -386,6 +386,78 @@ A resource file is defined as any file placed in the resource directory as returned by `ert-resource-directory'." `(expand-file-name ,file (ert-resource-directory))) +(defvar ert-temp-file-prefix "emacs-test-" + "Suffix used by `ert-with-temp-file' and `ert-with-temp-directory'.") + +(defvar ert-temp-file-suffix nil + "Prefix used by `ert-with-temp-file' and `ert-with-temp-directory'.") + +(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 +non-locally. NAME will be bound to the file name of the temporary +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'. + +:suffix STRING If non-nil, pass STRING to `make-temp-file' as + the SUFFIX argument. Otherwise, use the value + of `ert-temp-file-suffix'. + +:text STRING If non-nil, pass STRING to `make-temp-file' as + the TEXT argument. + +See also `ert-with-temp-directory'." + (declare (indent 1) (debug (symbolp body))) + (cl-check-type name symbol) + (let (keyw prefix suffix directory text extra-keywords) + (while (keywordp (setq keyw (car body))) + (setq body (cdr body)) + (pcase keyw + (:prefix (setq prefix (pop body))) + (:suffix (setq suffix (pop body))) + (:directory (setq directory (pop body))) + (:text (setq text (pop body))) + (_ (push keyw extra-keywords) (pop body)))) + (when extra-keywords + (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))) + `(let* ((,temp-file (,(if directory 'file-name-as-directory 'identity) + (make-temp-file ,prefix ,directory ,suffix ,text))) + (,name ,temp-file)) + (unwind-protect + (progn ,@body) + (ignore-errors + ,(if directory + `(delete-directory ,temp-file :recursive) + `(delete-file ,temp-file)))))))) + +(defmacro ert-with-temp-directory (name &rest body) + "Bind NAME to the name of a new temporary directory and evaluate BODY. +Delete the temporary directory after BODY exits normally or +non-locally. + +NAME is bound to the directory name, not the directory file +name. (In other words, it will end with the directory delimiter; +on Unix-like systems, it will end with \"/\".) + +The same keyword arguments are supported as in +`ert-with-temp-file' (which see), except for :text." + (declare (indent 1) (debug (symbolp body))) + (let ((tail body) keyw) + (while (keywordp (setq keyw (car tail))) + (setq tail (cddr tail)) + (pcase keyw (:text (error "Invalid keyword for directory: :text"))))) + `(ert-with-temp-file ,name + :directory t + ,@body)) + (provide 'ert-x) ;;; ert-x.el ends here diff --git a/test/lisp/emacs-lisp/ert-x-tests.el b/test/lisp/emacs-lisp/ert-x-tests.el index 1784934acb3..1eed5bb7b94 100644 --- a/test/lisp/emacs-lisp/ert-x-tests.el +++ b/test/lisp/emacs-lisp/ert-x-tests.el @@ -271,6 +271,52 @@ 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 () + (let (saved) + (ert-with-temp-file fil + (setq saved fil) + (should (file-exists-p fil)) + (should (file-regular-p fil))) + (should-not (file-exists-p saved)))) + +(ert-deftest ert-x-tests-with-temp-file/handle-error () + (let (saved) + (ignore-errors + (ert-with-temp-file fil + (setq saved fil) + (error "foo"))) + (should-not (file-exists-p saved)))) + +(ert-deftest ert-x-tests-with-temp-file/prefix-and-suffix-kwarg () + (ert-with-temp-file fil + :prefix "foo" + :suffix "bar" + (should (string-match "foo.*bar" fil)))) + +(ert-deftest ert-x-tests-with-temp-file/text-kwarg () + (ert-with-temp-file fil + :text "foobar3" + (let ((buf (find-file-noselect fil))) + (unwind-protect + (with-current-buffer buf + (should (equal (buffer-string) "foobar3"))) + (kill-buffer buf))))) + +(ert-deftest ert-x-tests-with-temp-file/unknown-kwarg-signals-error () + (should-error + (ert-with-temp-file fil :foo "foo" nil))) + +(ert-deftest ert-x-tests-with-temp-directory () + (let (saved) + (ert-with-temp-directory dir + (setq saved dir) + (should (file-exists-p dir)) + (should (file-directory-p dir))) + (should-not (file-exists-p saved)))) + +(ert-deftest ert-x-tests-with-temp-directory/text-signals-error () + (should-error + (ert-with-temp-directory dir :text "foo" nil))) (provide 'ert-x-tests) -- cgit v1.2.3 From 5dd27fef5885bf0f6ec3b12bad7972276834ccfa Mon Sep 17 00:00:00 2001 From: Stefan Kangas <stefan@marxist.se> Date: Sat, 6 Nov 2021 21:54:02 +0100 Subject: 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(-) (limited to 'test/lisp/emacs-lisp/ert-x-tests.el') diff --git a/lisp/emacs-lisp/ert-x.el b/lisp/emacs-lisp/ert-x.el index 2af956c8a6d..752ac3bfdd4 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 1eed5bb7b94..d7c0985b13e 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 -- cgit v1.2.3 From 0cac4598a77028a64dde9df74c76549cf48a19f2 Mon Sep 17 00:00:00 2001 From: Stefan Kangas <stefan@marxist.se> Date: Sun, 7 Nov 2021 00:41:14 +0100 Subject: Ensure return value of ert-with-temp-directory * lisp/emacs-lisp/ert-x.el (ert-with-temp-file): Ensure return value when :directory is t is the directory name. * test/lisp/emacs-lisp/ert-x-tests.el (ert-x-tests-with-temp-directory): Extend test for the above. --- lisp/emacs-lisp/ert-x.el | 4 +++- test/lisp/emacs-lisp/ert-x-tests.el | 3 ++- 2 files changed, 5 insertions(+), 2 deletions(-) (limited to 'test/lisp/emacs-lisp/ert-x-tests.el') diff --git a/lisp/emacs-lisp/ert-x.el b/lisp/emacs-lisp/ert-x.el index 752ac3bfdd4..ffeead4179d 100644 --- a/lisp/emacs-lisp/ert-x.el +++ b/lisp/emacs-lisp/ert-x.el @@ -445,7 +445,9 @@ See also `ert-with-temp-directory'." (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)) + (,name ,(if directory + `(file-name-as-directory ,temp-file) + temp-file))) (unwind-protect (progn ,@body) (ignore-errors diff --git a/test/lisp/emacs-lisp/ert-x-tests.el b/test/lisp/emacs-lisp/ert-x-tests.el index d7c0985b13e..9baa9941586 100644 --- a/test/lisp/emacs-lisp/ert-x-tests.el +++ b/test/lisp/emacs-lisp/ert-x-tests.el @@ -320,7 +320,8 @@ desired effect." (ert-with-temp-directory dir (setq saved dir) (should (file-exists-p dir)) - (should (file-directory-p dir))) + (should (file-directory-p dir)) + (should (equal dir (file-name-as-directory dir)))) (should-not (file-exists-p saved)))) (ert-deftest ert-x-tests-with-temp-directory/text-signals-error () -- cgit v1.2.3