mirror of
https://git.savannah.gnu.org/git/emacs/org-mode.git
synced 2024-09-12 21:40:03 +00:00
org-babel: Never throw away standard error
* lisp/ob-eval.el (org-babel-eval-error-notify): Do not insert superfluous whitespace. * lisp/ob-eval.el (org-babel-eval): Show standard error even if the command exits with a zero code. * testing/lisp/test-ob-shell.el( ob-shell/standard-output-after-success, ob-shell/standard-output-after-failure, ob-shell/error-output-after-success, ob-shell/error-output-after-failure, ob-shell/error-output-after-failure-multiple, ob-shell/exit-code, ob-shell/exit-code-multiple ): Add tests to avoid regressions.
This commit is contained in:
parent
2924c77848
commit
f7b16402e6
|
@ -40,39 +40,44 @@
|
||||||
(with-current-buffer buf
|
(with-current-buffer buf
|
||||||
(goto-char (point-max))
|
(goto-char (point-max))
|
||||||
(save-excursion
|
(save-excursion
|
||||||
(insert stderr)
|
|
||||||
(unless (bolp) (insert "\n"))
|
(unless (bolp) (insert "\n"))
|
||||||
(insert (format "[ Babel evaluation exited with code %S ]\n" exit-code))))
|
(insert stderr)
|
||||||
|
(insert (format "[ Babel evaluation exited with code %S ]" exit-code))))
|
||||||
(display-buffer buf))
|
(display-buffer buf))
|
||||||
(message "Babel evaluation exited with code %S" exit-code))
|
(message "Babel evaluation exited with code %S" exit-code))
|
||||||
|
|
||||||
(defun org-babel-eval (command query)
|
(defun org-babel-eval (command query)
|
||||||
"Run COMMAND on QUERY.
|
"Run COMMAND on QUERY.
|
||||||
|
Return standard output produced by COMMAND. If COMMAND exits
|
||||||
|
with a non-zero code or produces error output, show it with
|
||||||
|
`org-babel-eval-error-notify'.
|
||||||
|
|
||||||
Writes QUERY into a temp-buffer that is processed with
|
Writes QUERY into a temp-buffer that is processed with
|
||||||
`org-babel--shell-command-on-region'. If COMMAND succeeds then return
|
`org-babel--shell-command-on-region'."
|
||||||
its results, otherwise display STDERR with
|
|
||||||
`org-babel-eval-error-notify'."
|
|
||||||
(let ((error-buffer (get-buffer-create " *Org-Babel Error*")) exit-code)
|
(let ((error-buffer (get-buffer-create " *Org-Babel Error*")) exit-code)
|
||||||
(with-current-buffer error-buffer (erase-buffer))
|
(with-current-buffer error-buffer (erase-buffer))
|
||||||
(with-temp-buffer
|
(with-temp-buffer
|
||||||
(insert query)
|
(insert query)
|
||||||
(setq exit-code
|
(setq exit-code
|
||||||
(org-babel--shell-command-on-region
|
(org-babel--shell-command-on-region
|
||||||
command error-buffer))
|
command error-buffer))
|
||||||
(if (or (not (numberp exit-code)) (> exit-code 0))
|
(let ((stderr (with-current-buffer error-buffer (buffer-string))))
|
||||||
(progn
|
(if (or (not (numberp exit-code))
|
||||||
(with-current-buffer error-buffer
|
(> exit-code 0)
|
||||||
(org-babel-eval-error-notify exit-code (buffer-string)))
|
(not (string-empty-p stderr)))
|
||||||
(save-excursion
|
(progn
|
||||||
(when (get-buffer org-babel-error-buffer-name)
|
(org-babel-eval-error-notify exit-code stderr)
|
||||||
(with-current-buffer org-babel-error-buffer-name
|
(save-excursion
|
||||||
(unless (derived-mode-p 'compilation-mode)
|
(when (get-buffer org-babel-error-buffer-name)
|
||||||
(compilation-mode))
|
(with-current-buffer org-babel-error-buffer-name
|
||||||
;; Compilation-mode enforces read-only, but Babel expects the buffer modifiable.
|
(unless (derived-mode-p 'compilation-mode)
|
||||||
(setq buffer-read-only nil))))
|
(compilation-mode))
|
||||||
;; Return output, if any.
|
;; Compilation-mode enforces read-only, but
|
||||||
(buffer-string))
|
;; Babel expects the buffer modifiable.
|
||||||
(buffer-string)))))
|
(setq buffer-read-only nil))))
|
||||||
|
;; Return output, if any.
|
||||||
|
(buffer-string))
|
||||||
|
(buffer-string))))))
|
||||||
|
|
||||||
(defun org-babel-eval-read-file (file)
|
(defun org-babel-eval-read-file (file)
|
||||||
"Return the contents of FILE as a string."
|
"Return the contents of FILE as a string."
|
||||||
|
|
|
@ -170,6 +170,88 @@ ob-comint.el, which was not previously tested."
|
||||||
"#+BEGIN_SRC sh :results table\necho 'I \"want\" it all'\n#+END_SRC"
|
"#+BEGIN_SRC sh :results table\necho 'I \"want\" it all'\n#+END_SRC"
|
||||||
(org-babel-execute-src-block)))))
|
(org-babel-execute-src-block)))))
|
||||||
|
|
||||||
|
;;; Standard output
|
||||||
|
|
||||||
|
(ert-deftest ob-shell/standard-output-after-success ()
|
||||||
|
"Test standard output after exiting with a zero code."
|
||||||
|
(should (= 1
|
||||||
|
(org-babel-execute:sh
|
||||||
|
"echo 1" nil))))
|
||||||
|
|
||||||
|
(ert-deftest ob-shell/standard-output-after-failure ()
|
||||||
|
"Test standard output after exiting with a non-zero code."
|
||||||
|
(should (= 1
|
||||||
|
(org-babel-execute:sh
|
||||||
|
"echo 1; exit 2" nil))))
|
||||||
|
|
||||||
|
;;; Standard error
|
||||||
|
|
||||||
|
(ert-deftest ob-shell/error-output-after-success ()
|
||||||
|
"Test that standard error shows in the error buffer, alongside the
|
||||||
|
exit code, after exiting with a zero code."
|
||||||
|
(should
|
||||||
|
(string= "1
|
||||||
|
[ Babel evaluation exited with code 0 ]"
|
||||||
|
(progn (org-babel-eval-wipe-error-buffer)
|
||||||
|
(org-babel-execute:sh
|
||||||
|
"echo 1 >&2" nil)
|
||||||
|
(with-current-buffer org-babel-error-buffer-name
|
||||||
|
(buffer-string))))))
|
||||||
|
|
||||||
|
(ert-deftest ob-shell/error-output-after-failure ()
|
||||||
|
"Test that standard error shows in the error buffer, alongside the
|
||||||
|
exit code, after exiting with a non-zero code."
|
||||||
|
(should
|
||||||
|
(string= "1
|
||||||
|
[ Babel evaluation exited with code 2 ]"
|
||||||
|
(progn (org-babel-eval-wipe-error-buffer)
|
||||||
|
(org-babel-execute:sh
|
||||||
|
"echo 1 >&2; exit 2" nil)
|
||||||
|
(with-current-buffer org-babel-error-buffer-name
|
||||||
|
(buffer-string))))))
|
||||||
|
|
||||||
|
(ert-deftest ob-shell/error-output-after-failure-multiple ()
|
||||||
|
"Test that multiple standard error strings show in the error
|
||||||
|
buffer, alongside multiple exit codes."
|
||||||
|
(should
|
||||||
|
(string= "1
|
||||||
|
[ Babel evaluation exited with code 2 ]
|
||||||
|
3
|
||||||
|
[ Babel evaluation exited with code 4 ]"
|
||||||
|
(progn (org-babel-eval-wipe-error-buffer)
|
||||||
|
(org-babel-execute:sh
|
||||||
|
"echo 1 >&2; exit 2" nil)
|
||||||
|
(org-babel-execute:sh
|
||||||
|
"echo 3 >&2; exit 4" nil)
|
||||||
|
(with-current-buffer org-babel-error-buffer-name
|
||||||
|
(buffer-string))))))
|
||||||
|
|
||||||
|
;;; Exit codes
|
||||||
|
|
||||||
|
(ert-deftest ob-shell/exit-code ()
|
||||||
|
"Test that the exit code shows in the error buffer after exiting
|
||||||
|
with a non-zero return code."
|
||||||
|
(should
|
||||||
|
(string= "[ Babel evaluation exited with code 1 ]"
|
||||||
|
(progn (org-babel-eval-wipe-error-buffer)
|
||||||
|
(org-babel-execute:sh
|
||||||
|
"exit 1" nil)
|
||||||
|
(with-current-buffer org-babel-error-buffer-name
|
||||||
|
(buffer-string))))))
|
||||||
|
|
||||||
|
(ert-deftest ob-shell/exit-code-multiple ()
|
||||||
|
"Test that multiple exit codes show in the error buffer after
|
||||||
|
exiting with a non-zero return code multiple times."
|
||||||
|
(should
|
||||||
|
(string= "[ Babel evaluation exited with code 1 ]
|
||||||
|
[ Babel evaluation exited with code 2 ]"
|
||||||
|
(progn (org-babel-eval-wipe-error-buffer)
|
||||||
|
(org-babel-execute:sh
|
||||||
|
"exit 1" nil)
|
||||||
|
(org-babel-execute:sh
|
||||||
|
"exit 2" nil)
|
||||||
|
(with-current-buffer org-babel-error-buffer-name
|
||||||
|
(buffer-string))))))
|
||||||
|
|
||||||
(provide 'test-ob-shell)
|
(provide 'test-ob-shell)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue