From: Stefan Kangas Date: Sun, 23 Feb 2025 02:43:25 +0000 (+0100) Subject: Prefer ert-with-temp-directory in tests X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=4cacdc83e397842a3e6b8d8dd05553f7033dec75;p=emacs.git Prefer ert-with-temp-directory in tests * test/lisp/dired-aux-tests.el (dired-test-bug30624): * test/lisp/emacs-lisp/track-changes-tests.el (track-changes-tests--random): * test/src/buffer-tests.el (test-buffer-chars-modified-ticks): * test/src/fileio-tests.el (fileio-tests--symlink-failure) (fileio-tests--insert-file-interrupt) (fileio-tests--circular-after-insert-file-functions): Prefer ert-with-temp-directory to using make-temp-file directly. (cherry picked from commit 03bc51349f19899812652a8335f5055f561494e2) --- diff --git a/test/lisp/dired-aux-tests.el b/test/lisp/dired-aux-tests.el index e1304be76c0..1a9a5b1b581 100644 --- a/test/lisp/dired-aux-tests.el +++ b/test/lisp/dired-aux-tests.el @@ -89,25 +89,25 @@ (should-error (dired-rename-file from to-mv nil))))) (ert-deftest dired-test-bug30624 () - "test for https://debbugs.gnu.org/30624 ." - (cl-letf* ((target-dir (make-temp-file "target" 'dir)) - ((symbol-function 'dired-mark-read-file-name) - (lambda (&rest _) target-dir)) - (inhibit-message t)) - ;; Delete target-dir: `dired-do-create-files' must recreate it. - (delete-directory target-dir) - (let ((file1 (make-temp-file "bug30624_file1")) - (file2 (make-temp-file "bug30624_file2")) - (dired-create-destination-dirs 'always) - (buf (dired temporary-file-directory))) - (unwind-protect - (progn - (dired-revert) - (dired-mark-files-regexp "bug30624_file") - (should (dired-do-create-files 'copy 'dired-copy-file "Copy" nil))) - (delete-directory target-dir 'recursive) - (mapc #'delete-file `(,file1 ,file2)) - (kill-buffer buf))))) + "Test for ." + (ert-with-temp-directory target-dir + (ert-with-temp-file file1 + :suffix "bug30624_file1" + (ert-with-temp-file _file2 + :suffix "bug30624_file2" + (cl-letf* (((symbol-function 'dired-mark-read-file-name) + (lambda (&rest _) target-dir)) + (inhibit-message t)) + ;; Delete target-dir: `dired-do-create-files' must recreate it. + (delete-directory target-dir) + (let ((dired-create-destination-dirs 'always) + (buf (dired (file-name-directory file1)))) + (unwind-protect + (progn + (dired-revert) + (dired-mark-files-regexp "bug30624_file") + (should (dired-do-create-files 'copy 'dired-copy-file "Copy" nil))) + (kill-buffer buf)))))))) (defun dired-test--check-highlighting (command positions) (let ((start 1)) diff --git a/test/lisp/emacs-lisp/track-changes-tests.el b/test/lisp/emacs-lisp/track-changes-tests.el index 6499deb78d6..4f08c539360 100644 --- a/test/lisp/emacs-lisp/track-changes-tests.el +++ b/test/lisp/emacs-lisp/track-changes-tests.el @@ -24,6 +24,7 @@ (require 'track-changes) (require 'cl-lib) (require 'ert) +(require 'ert-x) (defun track-changes-tests--random-word () (let ((chars ())) @@ -52,104 +53,104 @@ (random track-changes-tests--random-seed) (dotimes (_ 100) (insert (track-changes-tests--random-word) "\n")) - (let* ((buf1 (generate-new-buffer " *tc1*")) - (buf2 (generate-new-buffer " *tc2*")) - (char-counts (make-vector 2 0)) - (sync-counts (make-vector 2 0)) - (print-escape-newlines t) - (file (make-temp-file "tc")) - (id1 (track-changes-register #'ignore)) - (id3 (track-changes-register #'ignore :nobefore t)) - (sync - (lambda (id buf n) - (track-changes-tests--message "!! SYNC %d !!" n) - (track-changes-fetch - id (lambda (beg end before) - (when (eq n 1) - (track-changes-fetch - id3 (lambda (beg3 end3 before3) - (should (eq beg3 beg)) - (should (eq end3 end)) - (should (eq before3 - (if (symbolp before) - before (length before))))))) - (incf (aref sync-counts (1- n))) - (incf (aref char-counts (1- n)) (- end beg)) - (let ((after (buffer-substring beg end))) - (track-changes-tests--message - "Sync:\n %S\n=> %S\nat %d .. %d" - before after beg end) - (with-current-buffer buf - (if (eq before 'error) - (erase-buffer) - (should (equal before - (buffer-substring - beg (+ beg (length before))))) - (delete-region beg (+ beg (length before)))) - (goto-char beg) - (insert after))) - (should (equal (buffer-string) - (with-current-buffer buf - (buffer-string)))))))) - (id2 (track-changes-register - (lambda (id2 &optional distance) - (when distance - (track-changes-tests--message "Disjoint distance: %d" - distance) - (funcall sync id2 buf2 2))) - :disjoint t))) - (write-region (point-min) (point-max) file) - (insert-into-buffer buf1) - (insert-into-buffer buf2) - (should (equal (buffer-hash) (buffer-hash buf1))) - (should (equal (buffer-hash) (buffer-hash buf2))) - (message "seeding with: %S" track-changes-tests--random-seed) - (dotimes (_ 1000) - (pcase (random 15) - (0 - (track-changes-tests--message "Manual sync1") - (funcall sync id1 buf1 1)) - (1 - (track-changes-tests--message "Manual sync2") - (funcall sync id2 buf2 2)) - ((pred (< _ 5)) - (let* ((beg (+ (point-min) (random (1+ (buffer-size))))) - (end (min (+ beg (1+ (random 100))) (point-max)))) - (track-changes-tests--message "Fill %d .. %d" beg end) - (fill-region-as-paragraph beg end))) - ((pred (< _ 8)) - (let* ((beg (+ (point-min) (random (1+ (buffer-size))))) - (end (min (+ beg (1+ (random 12))) (point-max)))) - (track-changes-tests--message "Delete %S at %d .. %d" - (buffer-substring beg end) beg end) - (delete-region beg end))) - ((and 8 (guard (= (random 50) 0))) - (track-changes-tests--message "Silent insertion") - (let ((inhibit-modification-hooks t)) - (insert "a"))) - ((and 8 (guard (= (random 10) 0))) - (track-changes-tests--message "Revert") - (insert-file-contents file nil nil nil 'replace)) - ((and 8 (guard (= (random 3) 0))) - (let* ((beg (+ (point-min) (random (1+ (buffer-size))))) - (end (min (+ beg (1+ (random 12))) (point-max))) - (after (eq (random 2) 0))) - (track-changes-tests--message "Bogus %S %d .. %d" - (if after 'after 'before) beg end) - (if after - (run-hook-with-args 'after-change-functions - beg end (- end beg)) - (run-hook-with-args 'before-change-functions beg end)))) - (_ - (goto-char (+ (point-min) (random (1+ (buffer-size))))) - (let ((word (track-changes-tests--random-word))) - (track-changes-tests--message "insert %S at %d" word (point)) - (insert word "\n"))))) - (message "SCOREs: default: %d/%d=%d disjoint: %d/%d=%d" - (aref char-counts 0) (aref sync-counts 0) - (/ (aref char-counts 0) (aref sync-counts 0)) - (aref char-counts 1) (aref sync-counts 1) - (/ (aref char-counts 1) (aref sync-counts 1)))))) + (ert-with-temp-file file + (let* ((buf1 (generate-new-buffer " *tc1*")) + (buf2 (generate-new-buffer " *tc2*")) + (char-counts (make-vector 2 0)) + (sync-counts (make-vector 2 0)) + (print-escape-newlines t) + (id1 (track-changes-register #'ignore)) + (id3 (track-changes-register #'ignore :nobefore t)) + (sync + (lambda (id buf n) + (track-changes-tests--message "!! SYNC %d !!" n) + (track-changes-fetch + id (lambda (beg end before) + (when (eq n 1) + (track-changes-fetch + id3 (lambda (beg3 end3 before3) + (should (eq beg3 beg)) + (should (eq end3 end)) + (should (eq before3 + (if (symbolp before) + before (length before))))))) + (incf (aref sync-counts (1- n))) + (incf (aref char-counts (1- n)) (- end beg)) + (let ((after (buffer-substring beg end))) + (track-changes-tests--message + "Sync:\n %S\n=> %S\nat %d .. %d" + before after beg end) + (with-current-buffer buf + (if (eq before 'error) + (erase-buffer) + (should (equal before + (buffer-substring + beg (+ beg (length before))))) + (delete-region beg (+ beg (length before)))) + (goto-char beg) + (insert after))) + (should (equal (buffer-string) + (with-current-buffer buf + (buffer-string)))))))) + (id2 (track-changes-register + (lambda (id2 &optional distance) + (when distance + (track-changes-tests--message "Disjoint distance: %d" + distance) + (funcall sync id2 buf2 2))) + :disjoint t))) + (write-region (point-min) (point-max) file) + (insert-into-buffer buf1) + (insert-into-buffer buf2) + (should (equal (buffer-hash) (buffer-hash buf1))) + (should (equal (buffer-hash) (buffer-hash buf2))) + (message "seeding with: %S" track-changes-tests--random-seed) + (dotimes (_ 1000) + (pcase (random 15) + (0 + (track-changes-tests--message "Manual sync1") + (funcall sync id1 buf1 1)) + (1 + (track-changes-tests--message "Manual sync2") + (funcall sync id2 buf2 2)) + ((pred (< _ 5)) + (let* ((beg (+ (point-min) (random (1+ (buffer-size))))) + (end (min (+ beg (1+ (random 100))) (point-max)))) + (track-changes-tests--message "Fill %d .. %d" beg end) + (fill-region-as-paragraph beg end))) + ((pred (< _ 8)) + (let* ((beg (+ (point-min) (random (1+ (buffer-size))))) + (end (min (+ beg (1+ (random 12))) (point-max)))) + (track-changes-tests--message "Delete %S at %d .. %d" + (buffer-substring beg end) beg end) + (delete-region beg end))) + ((and 8 (guard (= (random 50) 0))) + (track-changes-tests--message "Silent insertion") + (let ((inhibit-modification-hooks t)) + (insert "a"))) + ((and 8 (guard (= (random 10) 0))) + (track-changes-tests--message "Revert") + (insert-file-contents file nil nil nil 'replace)) + ((and 8 (guard (= (random 3) 0))) + (let* ((beg (+ (point-min) (random (1+ (buffer-size))))) + (end (min (+ beg (1+ (random 12))) (point-max))) + (after (eq (random 2) 0))) + (track-changes-tests--message "Bogus %S %d .. %d" + (if after 'after 'before) beg end) + (if after + (run-hook-with-args 'after-change-functions + beg end (- end beg)) + (run-hook-with-args 'before-change-functions beg end)))) + (_ + (goto-char (+ (point-min) (random (1+ (buffer-size))))) + (let ((word (track-changes-tests--random-word))) + (track-changes-tests--message "insert %S at %d" word (point)) + (insert word "\n"))))) + (message "SCOREs: default: %d/%d=%d disjoint: %d/%d=%d" + (aref char-counts 0) (aref sync-counts 0) + (/ (aref char-counts 0) (aref sync-counts 0)) + (aref char-counts 1) (aref sync-counts 1) + (/ (aref char-counts 1) (aref sync-counts 1))))))) diff --git a/test/src/buffer-tests.el b/test/src/buffer-tests.el index b4b9e761986..0879b928565 100644 --- a/test/src/buffer-tests.el +++ b/test/src/buffer-tests.el @@ -8523,22 +8523,16 @@ Finally, kill the buffer and its temporary file." (ert-deftest test-buffer-chars-modified-ticks () "Test `buffer-chars-modified-tick'." (setq temporary-file-directory (file-truename temporary-file-directory)) - (let ((text "foobar") - f1 f2) - (unwind-protect - (progn - (setq f1 (make-temp-file "buf-modiff-tests") - f2 (make-temp-file "buf-modiff-tests")) - (with-current-buffer (find-file f1) - (should (= (buffer-chars-modified-tick) 1)) - (should (= (buffer-chars-modified-tick) (buffer-modified-tick))) - (write-region text nil f2 nil 'silent) - (insert-file-contents f2) - (should (= (buffer-chars-modified-tick) (buffer-modified-tick))) - (should (> (buffer-chars-modified-tick) 1)))) - (if f1 (delete-file f1)) - (if f2 (delete-file f2)) - ))) + (ert-with-temp-file f1 + (ert-with-temp-file f2 + (let ((text "foobar")) + (with-current-buffer (find-file f1) + (should (= (buffer-chars-modified-tick) 1)) + (should (= (buffer-chars-modified-tick) (buffer-modified-tick))) + (write-region text nil f2 nil 'silent) + (insert-file-contents f2) + (should (= (buffer-chars-modified-tick) (buffer-modified-tick))) + (should (> (buffer-chars-modified-tick) 1))))))) (ert-deftest test-labeled-narrowing () "Test `with-restriction' and `without-restriction'." diff --git a/test/src/fileio-tests.el b/test/src/fileio-tests.el index 65a21d5950c..c2938c4900e 100644 --- a/test/src/fileio-tests.el +++ b/test/src/fileio-tests.el @@ -20,6 +20,7 @@ ;;; Code: (require 'ert) +(require 'ert-x) (defun try-link (target link) (make-symbolic-link target link) @@ -30,19 +31,17 @@ failure)) (defun fileio-tests--symlink-failure () - (let* ((dir (make-temp-file "fileio" t)) - (link (expand-file-name "link" dir))) - (unwind-protect - (let (failure - (char 0)) - (while (and (not failure) (< char 127)) - (setq char (1+ char)) - (when (and (eq system-type 'cygwin) (eq char 92)) - (setq char (1+ char))) - (setq failure (try-link (string char) link))) - (or failure - (try-link "/:" link))) - (delete-directory dir t)))) + (ert-with-temp-directory dir + (let* ((link (expand-file-name "link" dir))) + (let (failure + (char 0)) + (while (and (not failure) (< char 127)) + (setq char (1+ char)) + (when (and (eq system-type 'cygwin) (eq char 92)) + (setq char (1+ char))) + (setq failure (try-link (string char) link))) + (or failure + (try-link "/:" link)))))) (ert-deftest fileio-tests--odd-symlink-chars () "Check that any non-NULL ASCII character can appear in a symlink. @@ -109,24 +108,20 @@ Also check that an encoding error can appear in a symlink." (should (equal (expand-file-name "~/bar") "x:/foo/bar"))))) (ert-deftest fileio-tests--insert-file-interrupt () - (let ((text "-*- coding: binary -*-\n\xc3\xc3help") - f) - (unwind-protect - (progn - (setq f (make-temp-file "ftifi")) - (write-region text nil f nil 'silent) - (with-temp-buffer - (catch 'toto - (let ((set-auto-coding-function (lambda (&rest _) (throw 'toto nil)))) - (insert-file-contents f))) - (goto-char (point-min)) - (unless (eobp) - (forward-line 1) - (let ((c1 (char-after))) - (forward-char 1) - (should (equal c1 (char-before))) - (should (equal c1 (char-after))))))) - (if f (delete-file f))))) + (ert-with-temp-file f + (let ((text "-*- coding: binary -*-\n\xc3\xc3help")) + (write-region text nil f nil 'silent) + (with-temp-buffer + (catch 'toto + (let ((set-auto-coding-function (lambda (&rest _) (throw 'toto nil)))) + (insert-file-contents f))) + (goto-char (point-min)) + (unless (eobp) + (forward-line 1) + (let ((c1 (char-after))) + (forward-char 1) + (should (equal c1 (char-before))) + (should (equal c1 (char-after))))))))) (ert-deftest fileio-tests--relative-default-directory () "Test `expand-file-name' when `default-directory' is relative." @@ -159,12 +154,12 @@ Also check that an encoding error can appear in a symlink." (ert-deftest fileio-tests--circular-after-insert-file-functions () "Test `after-insert-file-functions' as a circular list." - (let ((f (make-temp-file "fileio")) - (after-insert-file-functions (list 'identity))) - (setcdr after-insert-file-functions after-insert-file-functions) - (write-region "hello\n" nil f nil 'silent) - (should-error (insert-file-contents f) :type 'circular-list) - (delete-file f))) + (ert-with-temp-file f + :suffix "fileio" + (let ((after-insert-file-functions (list 'identity))) + (setcdr after-insert-file-functions after-insert-file-functions) + (write-region "hello\n" nil f nil 'silent) + (should-error (insert-file-contents f) :type 'circular-list)))) (ert-deftest fileio-tests/null-character () (should-error (file-exists-p "/foo\0bar")