aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--modules/prog-json.el29
-rw-r--r--modules/prog-webdev.el42
-rw-r--r--modules/prog-yaml.el30
-rw-r--r--tests/test-prog-json--json-format-buffer.el31
-rw-r--r--tests/test-prog-webdev-format.el97
-rw-r--r--tests/test-prog-yaml--yaml-format-buffer.el31
6 files changed, 213 insertions, 47 deletions
diff --git a/modules/prog-json.el b/modules/prog-json.el
index 7b49dff1..953b5f79 100644
--- a/modules/prog-json.el
+++ b/modules/prog-json.el
@@ -41,15 +41,38 @@
;; -------------------------------- Formatting ---------------------------------
;; pretty-print with sorted keys, bound to standard format key
+(defun cj/--json-format-region (program &rest args)
+ "Replace the buffer with PROGRAM ARGS run over its contents, via argv.
+Runs PROGRAM (with ARGS) on the whole buffer through
+`call-process-region' — no shell, so no quoting or word-splitting.
+The buffer is replaced only when PROGRAM exits zero; on a non-zero
+exit the buffer is left untouched and an error is signalled with
+the program's stderr text. Point is preserved as closely as the
+reformatted size allows. Returns t on success."
+ (let* ((point (point))
+ (src (current-buffer))
+ (out (generate-new-buffer " *json-format-out*"))
+ (status (apply #'call-process-region
+ (point-min) (point-max) program
+ nil out nil args)))
+ (unwind-protect
+ (if (and (integerp status) (zerop status))
+ (progn
+ (with-current-buffer src
+ (replace-buffer-contents out)
+ (goto-char (min point (point-max))))
+ t)
+ (user-error "%s failed: %s" program
+ (string-trim (with-current-buffer out (buffer-string)))))
+ (kill-buffer out))))
+
(defun cj/json-format-buffer ()
"Format the current JSON buffer with sorted keys.
Uses jq if available for reliable formatting, otherwise falls
back to the built-in `json-pretty-print-buffer-ordered'."
(interactive)
(if (executable-find "jq")
- (let ((point (point)))
- (shell-command-on-region (point-min) (point-max) "jq --sort-keys ." nil t)
- (goto-char (min point (point-max))))
+ (cj/--json-format-region "jq" "--sort-keys" ".")
(json-pretty-print-buffer-ordered)))
(defun cj/json-setup ()
diff --git a/modules/prog-webdev.el b/modules/prog-webdev.el
index 74cff732..8832446a 100644
--- a/modules/prog-webdev.el
+++ b/modules/prog-webdev.el
@@ -75,21 +75,45 @@ Install with: sudo pacman -S prettier")
(executable-find ts-language-server-path))
(lsp-deferred)))
-(defun cj/--webdev-format-command (file)
- "Return the prettier command that formats FILE's contents on stdin."
- (format "prettier --stdin-filepath %s" (shell-quote-argument file)))
+(defun cj/--webdev-format-args (file)
+ "Return the prettier argv list that formats FILE's contents on stdin.
+No shell quoting is needed: the args are passed to prettier directly
+via `call-process-region', so FILE can contain spaces or shell
+metacharacters without risk."
+ (list "--stdin-filepath" file))
+
+(defun cj/--webdev-format-region (program &rest args)
+ "Replace the buffer with PROGRAM ARGS run over its contents, via argv.
+Runs PROGRAM (with ARGS) on the whole buffer through
+`call-process-region' — no shell, so no quoting or word-splitting.
+The buffer is replaced only when PROGRAM exits zero; on a non-zero
+exit the buffer is left untouched and an error is signalled with
+the program's stderr text. Point is preserved as closely as the
+reformatted size allows. Returns t on success."
+ (let* ((point (point))
+ (src (current-buffer))
+ (out (generate-new-buffer " *webdev-format-out*"))
+ (status (apply #'call-process-region
+ (point-min) (point-max) program
+ nil out nil args)))
+ (unwind-protect
+ (if (and (integerp status) (zerop status))
+ (progn
+ (with-current-buffer src
+ (replace-buffer-contents out)
+ (goto-char (min point (point-max))))
+ t)
+ (user-error "%s failed: %s" program
+ (string-trim (with-current-buffer out (buffer-string)))))
+ (kill-buffer out))))
(defun cj/webdev-format-buffer ()
"Format the current buffer with prettier.
Detects the file type automatically from the filename."
(interactive)
(if (executable-find prettier-path)
- (let ((point (point)))
- (shell-command-on-region (point-min) (point-max)
- (cj/--webdev-format-command
- (or buffer-file-name "file.ts"))
- nil t)
- (goto-char (min point (point-max))))
+ (apply #'cj/--webdev-format-region prettier-path
+ (cj/--webdev-format-args (or buffer-file-name "file.ts")))
(user-error "prettier not found; install with: sudo pacman -S prettier")))
(defun cj/webdev-keybindings ()
diff --git a/modules/prog-yaml.el b/modules/prog-yaml.el
index 6bb11a62..c2bb559b 100644
--- a/modules/prog-yaml.el
+++ b/modules/prog-yaml.el
@@ -36,15 +36,37 @@
;; -------------------------------- Formatting ---------------------------------
;; normalize indentation and style, bound to standard format key
+(defun cj/--yaml-format-region (program &rest args)
+ "Replace the buffer with PROGRAM ARGS run over its contents, via argv.
+Runs PROGRAM (with ARGS) on the whole buffer through
+`call-process-region' — no shell, so no quoting or word-splitting.
+The buffer is replaced only when PROGRAM exits zero; on a non-zero
+exit the buffer is left untouched and an error is signalled with
+the program's stderr text. Point is preserved as closely as the
+reformatted size allows. Returns t on success."
+ (let* ((point (point))
+ (src (current-buffer))
+ (out (generate-new-buffer " *yaml-format-out*"))
+ (status (apply #'call-process-region
+ (point-min) (point-max) program
+ nil out nil args)))
+ (unwind-protect
+ (if (and (integerp status) (zerop status))
+ (progn
+ (with-current-buffer src
+ (replace-buffer-contents out)
+ (goto-char (min point (point-max))))
+ t)
+ (user-error "%s failed: %s" program
+ (string-trim (with-current-buffer out (buffer-string)))))
+ (kill-buffer out))))
+
(defun cj/yaml-format-buffer ()
"Format the current YAML buffer with prettier.
Preserves point position as closely as possible."
(interactive)
(if (executable-find "prettier")
- (let ((point (point)))
- (shell-command-on-region (point-min) (point-max)
- "prettier --parser yaml" nil t)
- (goto-char (min point (point-max))))
+ (cj/--yaml-format-region "prettier" "--parser" "yaml")
(user-error "prettier not found; install with: npm install -g prettier")))
(defun cj/yaml-setup ()
diff --git a/tests/test-prog-json--json-format-buffer.el b/tests/test-prog-json--json-format-buffer.el
index 227eafb9..70d7e98b 100644
--- a/tests/test-prog-json--json-format-buffer.el
+++ b/tests/test-prog-json--json-format-buffer.el
@@ -7,9 +7,40 @@
;;; Code:
(require 'ert)
+(require 'cl-lib)
(require 'json)
(require 'prog-json)
+;;; argv path — process invocation (stubbed, no shell)
+
+(ert-deftest test-prog-json--json-format-buffer-invokes-jq-argv ()
+ "Normal: with jq present, the formatter calls jq via argv, no shell."
+ (let (program args)
+ (cl-letf (((symbol-function 'executable-find) (lambda (_p) "/usr/bin/jq"))
+ ((symbol-function 'call-process-region)
+ (lambda (_start _end prog &rest rest)
+ (setq program prog
+ args (nthcdr 3 rest))
+ (with-current-buffer (nth 1 rest) (insert "{}"))
+ 0)))
+ (with-temp-buffer
+ (insert "{\"a\":1}")
+ (cj/json-format-buffer)))
+ (should (equal "jq" program))
+ (should (equal '("--sort-keys" ".") args))))
+
+(ert-deftest test-prog-json--json-format-buffer-no-clobber-on-failure ()
+ "Error: a non-zero jq exit leaves the buffer untouched and signals an error."
+ (cl-letf (((symbol-function 'executable-find) (lambda (_p) "/usr/bin/jq"))
+ ((symbol-function 'call-process-region)
+ (lambda (_start _end _prog _delete buffer &rest _)
+ (with-current-buffer buffer (insert "jq: parse error"))
+ 1)))
+ (with-temp-buffer
+ (insert "{not valid json}")
+ (should-error (cj/json-format-buffer) :type 'user-error)
+ (should (string= (buffer-string) "{not valid json}")))))
+
;;; Normal Cases — jq path
(ert-deftest test-prog-json--json-format-buffer-normal-formats-object ()
diff --git a/tests/test-prog-webdev-format.el b/tests/test-prog-webdev-format.el
index 5abb1f52..694f9e96 100644
--- a/tests/test-prog-webdev-format.el
+++ b/tests/test-prog-webdev-format.el
@@ -2,9 +2,12 @@
;;; Commentary:
;; Covers the prettier-formatting path in `modules/prog-webdev.el':
-;; - `cj/--webdev-format-command' (pure: file -> "prettier --stdin-filepath ...")
-;; - `cj/webdev-format-buffer' (interactive wrapper; `executable-find' and
-;; `shell-command-on-region' stubbed)
+;; - `cj/--webdev-format-args' (pure: file -> ("--stdin-filepath" file))
+;; - `cj/webdev-format-buffer' (interactive wrapper; `executable-find' and
+;; `call-process-region' stubbed)
+;;
+;; The formatter now runs prettier via `call-process-region' with an
+;; explicit argv list (no shell), so there is no command string to quote.
;;
;; `cj/webdev-keybindings' (the C-; f mount) is already covered by
;; `test-prog-webdev--format-wiring.el'.
@@ -21,70 +24,102 @@
(format-test--ensure-packages-init)
(require 'prog-webdev)
-;; --------------------------- cj/--webdev-format-command ----------------------
+;; --------------------------- cj/--webdev-format-args -------------------------
-(ert-deftest test-prog-webdev--format-command-normal ()
- "Normal: a plain .ts path yields the stdin-filepath prettier command."
- (should (equal "prettier --stdin-filepath /home/me/app.ts"
- (cj/--webdev-format-command "/home/me/app.ts"))))
+(ert-deftest test-prog-webdev--format-args-normal ()
+ "Normal: a plain .ts path yields the stdin-filepath argv list."
+ (should (equal '("--stdin-filepath" "/home/me/app.ts")
+ (cj/--webdev-format-args "/home/me/app.ts"))))
-(ert-deftest test-prog-webdev--format-command-spaces-quoted ()
- "Boundary: a path with spaces is shell-quoted."
- (should (equal (format "prettier --stdin-filepath %s"
- (shell-quote-argument "/home/me/my app/index.tsx"))
- (cj/--webdev-format-command "/home/me/my app/index.tsx"))))
+(ert-deftest test-prog-webdev--format-args-spaces-not-quoted ()
+ "Boundary: a path with spaces is passed verbatim — argv needs no quoting."
+ (should (equal '("--stdin-filepath" "/home/me/my app/index.tsx")
+ (cj/--webdev-format-args "/home/me/my app/index.tsx"))))
-(ert-deftest test-prog-webdev--format-command-tsx-extension ()
+(ert-deftest test-prog-webdev--format-args-tsx-extension ()
"Boundary: a .tsx path is passed through (prettier infers the parser)."
- (should (equal "prettier --stdin-filepath /x/Component.tsx"
- (cj/--webdev-format-command "/x/Component.tsx"))))
+ (should (equal '("--stdin-filepath" "/x/Component.tsx")
+ (cj/--webdev-format-args "/x/Component.tsx"))))
;; ---------------------------- cj/webdev-format-buffer ------------------------
(ert-deftest test-prog-webdev-format-buffer-runs-prettier-on-the-file ()
- "Normal: with prettier on PATH, the region command targets `buffer-file-name'."
- (let (cmd)
+ "Normal: with prettier on PATH, the argv targets `buffer-file-name'."
+ (let (program args)
(cl-letf (((symbol-function 'executable-find) (lambda (_p) "/usr/bin/prettier"))
- ((symbol-function 'shell-command-on-region)
- (lambda (_start _end command &rest _) (setq cmd command))))
+ ((symbol-function 'call-process-region)
+ (lambda (_start _end prog &rest rest)
+ ;; rest = (DELETE BUFFER DISPLAY &rest ARGS)
+ (setq program prog
+ args (nthcdr 3 rest))
+ 0)))
(with-temp-buffer
(insert "const x=1\n")
(setq buffer-file-name "/home/me/app.ts")
(cj/webdev-format-buffer)
(setq buffer-file-name nil)))
- (should (equal (cj/--webdev-format-command "/home/me/app.ts") cmd))))
+ (should (equal "prettier" program))
+ (should (equal '("--stdin-filepath" "/home/me/app.ts") args))))
(ert-deftest test-prog-webdev-format-buffer-falls-back-to-file-ts ()
"Boundary: a buffer with no file uses the \"file.ts\" filename hint."
- (let (cmd)
+ (let (args)
(cl-letf (((symbol-function 'executable-find) (lambda (_p) "/usr/bin/prettier"))
- ((symbol-function 'shell-command-on-region)
- (lambda (_start _end command &rest _) (setq cmd command))))
+ ((symbol-function 'call-process-region)
+ (lambda (_start _end _prog &rest rest)
+ (setq args (nthcdr 3 rest))
+ 0)))
(with-temp-buffer
(insert "const x=1\n")
(should-not buffer-file-name)
(cj/webdev-format-buffer)))
- (should (equal (cj/--webdev-format-command "file.ts") cmd))))
+ (should (equal '("--stdin-filepath" "file.ts") args))))
(ert-deftest test-prog-webdev-format-buffer-clamps-point-to-point-max ()
"Boundary: after a format that shrinks the buffer, point clamps to point-max."
(cl-letf (((symbol-function 'executable-find) (lambda (_p) "/usr/bin/prettier"))
- ((symbol-function 'shell-command-on-region)
- (lambda (_start _end _command &rest _)
- (delete-region (point-min) (point-max))
- (insert "x"))))
+ ((symbol-function 'call-process-region)
+ (lambda (_start _end _prog _delete buffer &rest _)
+ ;; Simulate prettier writing a shorter result to the output buffer.
+ (with-current-buffer buffer (insert "x"))
+ 0)))
(with-temp-buffer
(insert "a really long original buffer body that prettier will shorten\n")
(goto-char 40)
(cj/webdev-format-buffer)
(should (= (point) (point-max))))))
+(ert-deftest test-prog-webdev-format-buffer-replaces-on-success ()
+ "Normal: a zero exit replaces the buffer with the formatter's output."
+ (cl-letf (((symbol-function 'executable-find) (lambda (_p) "/usr/bin/prettier"))
+ ((symbol-function 'call-process-region)
+ (lambda (_start _end _prog _delete buffer &rest _)
+ (with-current-buffer buffer (insert "const x = 1;\n"))
+ 0)))
+ (with-temp-buffer
+ (insert "const x=1\n")
+ (cj/webdev-format-buffer)
+ (should (string= (buffer-string) "const x = 1;\n")))))
+
+(ert-deftest test-prog-webdev-format-buffer-no-clobber-on-failure ()
+ "Error: a non-zero exit leaves the buffer untouched and signals an error."
+ (cl-letf (((symbol-function 'executable-find) (lambda (_p) "/usr/bin/prettier"))
+ ((symbol-function 'call-process-region)
+ (lambda (_start _end _prog _delete buffer &rest _)
+ (with-current-buffer buffer (insert "[error] syntax error"))
+ 1)))
+ (with-temp-buffer
+ (insert "const x=1\n")
+ (should-error (cj/webdev-format-buffer) :type 'user-error)
+ ;; Buffer must still hold the original source, not the error text.
+ (should (string= (buffer-string) "const x=1\n")))))
+
(ert-deftest test-prog-webdev-format-buffer-errors-without-prettier ()
"Error: prettier missing -> `user-error', nothing shells out."
(let ((ran nil))
(cl-letf (((symbol-function 'executable-find) (lambda (_p) nil))
- ((symbol-function 'shell-command-on-region)
- (lambda (&rest _) (setq ran t))))
+ ((symbol-function 'call-process-region)
+ (lambda (&rest _) (setq ran t) 0)))
(with-temp-buffer
(should-error (cj/webdev-format-buffer) :type 'user-error)))
(should-not ran)))
diff --git a/tests/test-prog-yaml--yaml-format-buffer.el b/tests/test-prog-yaml--yaml-format-buffer.el
index 4e928a2c..28ad351f 100644
--- a/tests/test-prog-yaml--yaml-format-buffer.el
+++ b/tests/test-prog-yaml--yaml-format-buffer.el
@@ -6,8 +6,39 @@
;;; Code:
(require 'ert)
+(require 'cl-lib)
(require 'prog-yaml)
+;;; argv path — process invocation (stubbed, no shell)
+
+(ert-deftest test-prog-yaml--yaml-format-buffer-invokes-prettier-argv ()
+ "Normal: with prettier present, the formatter calls it via argv, no shell."
+ (let (program args)
+ (cl-letf (((symbol-function 'executable-find) (lambda (_p) "/usr/bin/prettier"))
+ ((symbol-function 'call-process-region)
+ (lambda (_start _end prog &rest rest)
+ (setq program prog
+ args (nthcdr 3 rest))
+ (with-current-buffer (nth 1 rest) (insert "key: value\n"))
+ 0)))
+ (with-temp-buffer
+ (insert "key: value\n")
+ (cj/yaml-format-buffer)))
+ (should (equal "prettier" program))
+ (should (equal '("--parser" "yaml") args))))
+
+(ert-deftest test-prog-yaml--yaml-format-buffer-no-clobber-on-failure ()
+ "Error: a non-zero prettier exit leaves the buffer untouched and errors."
+ (cl-letf (((symbol-function 'executable-find) (lambda (_p) "/usr/bin/prettier"))
+ ((symbol-function 'call-process-region)
+ (lambda (_start _end _prog _delete buffer &rest _)
+ (with-current-buffer buffer (insert "[error] bad yaml"))
+ 1)))
+ (with-temp-buffer
+ (insert "key: : bad\n")
+ (should-error (cj/yaml-format-buffer) :type 'user-error)
+ (should (string= (buffer-string) "key: : bad\n")))))
+
;;; Normal Cases
(ert-deftest test-prog-yaml--yaml-format-buffer-normal-fixes-indentation ()