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

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

[nongnu] elpa/reformatter 84cff54b08 72/81: Merge pull request #36 from


From: ELPA Syncer
Subject: [nongnu] elpa/reformatter 84cff54b08 72/81: Merge pull request #36 from kenranunderscore/harden-temp-file-creation
Date: Tue, 5 Sep 2023 04:03:39 -0400 (EDT)

branch: elpa/reformatter
commit 84cff54b0873fcca6fc0314d7584284e86708e8d
Merge: 7c5452bf31 edf1c42de8
Author: Steve Purcell <steve@sanityinc.com>
Commit: GitHub <noreply@github.com>

    Merge pull request #36 from kenranunderscore/harden-temp-file-creation
    
    Handle slashes in names of created functions when creating temporary files
---
 reformatter-tests.el | 14 +++++++++++++-
 reformatter.el       | 23 ++++++++++++++++-------
 2 files changed, 29 insertions(+), 8 deletions(-)

diff --git a/reformatter-tests.el b/reformatter-tests.el
index 8eb99d2380..2878734eb9 100644
--- a/reformatter-tests.el
+++ b/reformatter-tests.el
@@ -55,6 +55,19 @@
     (reformatter-tests-shfmt-tempfile-in-stdout-buffer)
     (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-buffer)
+    (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 6cb4136614..6faad76963 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
@@ -104,10 +111,10 @@ the `reformatter-define' macro."
                  (retcode
                   (condition-case e
                       (apply 'call-process program
-                                  (when stdin input-file)
-                                  (list (list :file stdout-file) stderr-file)
-                                  nil
-                                  args)
+                             (when stdin input-file)
+                             (list (list :file stdout-file) stderr-file)
+                             nil
+                             args)
                     (error e))))
             (with-current-buffer error-buffer
               (let ((inhibit-read-only t))
@@ -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]