emacs-elpa-diffs
[Top][All Lists]
Advanced

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

[nongnu] elpa/reformatter 897f3ba503 67/81: Handle slashes in names of c


From: ELPA Syncer
Subject: [nongnu] elpa/reformatter 897f3ba503 67/81: Handle slashes in names of created functions
Date: Tue, 5 Sep 2023 04:03:38 -0400 (EDT)

branch: elpa/reformatter
commit 897f3ba503d4a14abf61304bfef7f2f154e6e941
Author: Johannes Maier <johannes.maier@active-group.de>
Commit: Johannes Maier <johannes.maier@mailbox.org>

    Handle slashes in names of created functions
    
    Previously, if the symbol passed to `reformatter-define' contained a
    slash, then the actual formatting would fail due to `make-temp-file'
    trying to create a directory.  Use a sanitized symbol name as basis
    for the tempfile name instead.
---
 reformatter-tests.el | 14 +++++++++++++-
 reformatter.el       | 15 ++++++++++++---
 2 files changed, 25 insertions(+), 4 deletions(-)

diff --git a/reformatter-tests.el b/reformatter-tests.el
index 7b126fe818..586b12aa75 100644
--- a/reformatter-tests.el
+++ b/reformatter-tests.el
@@ -55,6 +55,19 @@
     (reformatter-tests-shfmt-tempfile-in-stdout)
     (should (equal "[ foo ] && echo yes\n" (buffer-string)))))
 
+;; Same as `reformatter-tests-shfmt-tempfile-in-stdout', but with a
+;; slash in the symbol name.
+(reformatter-define reformatter-tests-tempfile/with-slash-in-symbol-name
+  :program "shfmt"
+  :stdin nil
+  :args (list input-file))
+
+(ert-deftest reformatter-tests-tempfile-with-slash-in-symbol-name ()
+  (with-temp-buffer
+    (insert "[  foo  ] && echo yes\n")
+    (reformatter-tests-tempfile/with-slash-in-symbol-name)
+    (should (equal "[ foo ] && echo yes\n" (buffer-string)))))
+
 ;; Modify a file in place
 (reformatter-define reformatter-tests-shfmt-in-place
   :program "shfmt"
@@ -69,6 +82,5 @@
     (should (equal "[ foo ] && echo yes\n" (buffer-string)))))
 
 
-
 (provide 'reformatter-tests)
 ;;; reformatter-tests.el ends here
diff --git a/reformatter.el b/reformatter.el
index b54dcb5761..1289f7c5f9 100644
--- a/reformatter.el
+++ b/reformatter.el
@@ -76,6 +76,13 @@
   (require 'cl-lib))
 (require 'ansi-color)
 
+(defun reformatter--make-temp-file (sym)
+  "Create a temporary file whose filename is based on SYM, but with
+slashes replaced by underscores.  `make-temp-file' fails
+otherwise as it cannot create intermediate directories."
+  (make-temp-file
+   (replace-regexp-in-string "/" "_" (symbol-name sym))))
+
 (defun reformatter--do-region (name beg end program args stdin stdout 
input-file exit-code-success-p display-errors)
   "Do the work of reformatter called NAME.
 Reformats the current buffer's region from BEG to END using
@@ -89,8 +96,8 @@ the `reformatter-define' macro."
              (string= (file-truename input-file)
                       (file-truename (buffer-file-name))))
     (error "The reformatter must not operate on the current file in-place"))
-  (let* ((stderr-file (make-temp-file (symbol-name name)))
-         (stdout-file (make-temp-file (symbol-name name)))
+  (let* ((stderr-file (reformatter--make-temp-file name))
+         (stdout-file (reformatter--make-temp-file name))
          ;; Setting this coding system might not universally be
          ;; the best default, but was apparently necessary for
          ;; some hand-rolled reformatter functions that this
@@ -268,7 +275,9 @@ might use:
 When called interactively, or with prefix argument
 DISPLAY-ERRORS, shows a buffer if the formatting fails."
          (interactive "rp")
-         (let ((input-file ,(if input-file input-file `(make-temp-file 
,(symbol-name name)))))
+         (let ((input-file ,(if input-file
+                                input-file
+                              (reformatter--make-temp-file name))))
            ;; Evaluate args with input-file bound
            (unwind-protect
                (progn



reply via email to

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