summaryrefslogtreecommitdiff
path: root/test/lisp/simple-tests.el
diff options
context:
space:
mode:
authorTino Calancha <tino.calancha@gmail.com>2020-01-19 11:13:02 +0100
committerTino Calancha <tino.calancha@gmail.com>2020-01-19 11:13:02 +0100
commit2eb0b7835d1a9cd4b804436e33c71058cb38f178 (patch)
tree9edfabae5617c7d9113eec228e9b09f0319321ec /test/lisp/simple-tests.el
parentc134978a769a27c10de4a1c3d28c073f3de87a3c (diff)
downloademacs-2eb0b7835d1a9cd4b804436e33c71058cb38f178.tar.gz
Fix shell-command-dont-erase-buffer feature
* lisp/simple.el (shell-command-dont-erase-buffer): The default, nil, is backward compatible, i.e. it erases the buffer only if the output buffer is not the current one; the new value 'erase always erases the output buffer. Update docstring. (shell-command-save-pos-or-erase): Add optional arg output-to-current-buffer. Rename it so that it's not internal. All callers updated. (shell-command-set-point-after-cmd): Rename it so that it's not internal. All callers updated. Adjust it to cover a side case. (shell-command): Adjust logic to match the specification (Bug#39067). Enable the feature when the output buffer is the current one. (shell-command-on-region): Little tweak to follow `shell-command-dont-erase-buffer' specification. * test/lisp/simple-tests.el (with-shell-command-dont-erase-buffer): Add helper macro. (simple-tests-shell-command-39067) (simple-tests-shell-command-dont-erase-buffer): Add tests. * doc/emacs/misc.texi (Single Shell): Update manual. * etc/NEWS (Single shell commands): Announce the change.
Diffstat (limited to 'test/lisp/simple-tests.el')
-rw-r--r--test/lisp/simple-tests.el54
1 files changed, 54 insertions, 0 deletions
diff --git a/test/lisp/simple-tests.el b/test/lisp/simple-tests.el
index 2611519d074..0b12cee5855 100644
--- a/test/lisp/simple-tests.el
+++ b/test/lisp/simple-tests.el
@@ -711,5 +711,59 @@ See Bug#21722."
(when process (delete-process process))
(when buffer (kill-buffer buffer)))))))
+
+;;; Tests for shell-command-dont-erase-buffer
+
+(defmacro with-shell-command-dont-erase-buffer (str output-buffer-is-current &rest body)
+ (declare (debug (form &body)) (indent 2))
+ (let ((expected (make-symbol "expected"))
+ (command (make-symbol "command"))
+ (caller-buf (make-symbol "caller-buf"))
+ (output-buf (make-symbol "output-buf")))
+ `(let* ((,caller-buf (generate-new-buffer "caller-buf"))
+ (,output-buf (if ,output-buffer-is-current ,caller-buf
+ (generate-new-buffer "output-buf")))
+ (,command (format "%s --batch --eval '(princ \"%s\")'" invocation-name ,str))
+ (inhibit-message t))
+ (unwind-protect
+ ;; Feature must work the same regardless how we specify the 2nd arg of `shell-command', ie,
+ ;; as a buffer, buffer name (or t, if the output must go to the current buffer).
+ (dolist (output (append (list ,output-buf (buffer-name ,output-buf))
+ (if ,output-buffer-is-current '(t) nil)))
+ (dolist (save-pos '(erase nil beg-last-out end-last-out save-point))
+ (let ((shell-command-dont-erase-buffer save-pos))
+ (with-current-buffer ,output-buf (erase-buffer))
+ (with-current-buffer ,caller-buf
+ (dotimes (_ 2) (shell-command ,command output)))
+ (with-current-buffer ,output-buf
+ ,@body))))
+ (kill-buffer ,caller-buf)
+ (when (buffer-live-p ,output-buf)
+ (kill-buffer ,output-buf))))))
+
+(ert-deftest simple-tests-shell-command-39067 ()
+ "The output buffer is erased or not according to `shell-command-dont-erase-buffer'."
+ (let ((str "foo\n"))
+ (dolist (output-current '(t nil))
+ (with-shell-command-dont-erase-buffer str output-current
+ (let ((expected (cond ((eq shell-command-dont-erase-buffer 'erase) str)
+ ((null shell-command-dont-erase-buffer)
+ (if output-current (concat str str)
+ str))
+ (t (concat str str)))))
+ (should (string= expected (buffer-string))))))))
+
+(ert-deftest simple-tests-shell-command-dont-erase-buffer ()
+ "The point is set at the expected position after execution of the command."
+ (let* ((str "foo\n")
+ (expected-point `((beg-last-out . ,(1+ (length str)))
+ (end-last-out . ,(1+ (* 2 (length str))))
+ (save-point . 1))))
+ (dolist (output-buffer-is-current '(t ni))
+ (with-shell-command-dont-erase-buffer str output-buffer-is-current
+ (when (memq shell-command-dont-erase-buffer '(beg-last-out end-last-out save-point))
+ (should (= (point) (alist-get shell-command-dont-erase-buffer expected-point))))))))
+
+
(provide 'simple-test)
;;; simple-test.el ends here