summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKyle Meyer <kyle@kyleam.com>2020-08-29 13:22:48 -0400
committerKyle Meyer <kyle@kyleam.com>2020-09-01 00:02:49 -0400
commit7bc18ebbed409ace4665ec9c9910b2837834b03d (patch)
tree2c005dcf688aa3da4f4513717840b2b80b7f6fb7
parente8ebf5d6c93aaa8f343f897f890deb1304ca9d4b (diff)
downloadorg-mode-7bc18ebbed409ace4665ec9c9910b2837834b03d.tar.gz
ob-core: Avoid table conversion warning for empty results
* lisp/ob-core.el (org-babel-import-elisp-from-file): Don't try to convert empty file to a table. * testing/lisp/test-ob.el (test-ob/import-elisp-from-file): Add tests. If org-babel-import-elisp-from-file is called with an empty file (which many ob- libraries do when there are no results), feeding that to org-table-import leads to a beginning-of-buffer error. Before 14878f3f9 (ob-core: Display warning on failure to read results, 2020-05-21), this error was hard to notice because, after catching it, it was reported as a quickly buried message. After that commit, it is displayed as a warning, which is not appropriate for the common and unproblematic case of empty results. Avoid the warning by only doing the table conversion if the file has content. Another option would be to do the table conversion but add a beginning-of-buffer arm to the surrounding condition-case. However, that risks swallowing other sources of that error. Reported-by: Colin Baxter <m43cap@yandex.com> <https://orgmode.org/list/878se3nhbj.fsf@yandex.com>
-rw-r--r--lisp/ob-core.el17
-rw-r--r--testing/lisp/test-ob.el28
2 files changed, 39 insertions, 6 deletions
diff --git a/lisp/ob-core.el b/lisp/ob-core.el
index 5786222..5b79919 100644
--- a/lisp/ob-core.el
+++ b/lisp/ob-core.el
@@ -3003,13 +3003,18 @@ If the table is trivial, then return it as a scalar."
(with-temp-buffer
(condition-case err
(progn
- (org-table-import file-name separator)
+ (insert-file-contents file-name)
(delete-file file-name)
- (delq nil
- (mapcar (lambda (row)
- (and (not (eq row 'hline))
- (mapcar #'org-babel-string-read row)))
- (org-table-to-lisp))))
+ (let ((pmax (point-max)))
+ ;; If the file was empty, don't bother trying to
+ ;; convert the table.
+ (when (> pmax 1)
+ (org-table-convert-region (point-min) pmax separator)
+ (delq nil
+ (mapcar (lambda (row)
+ (and (not (eq row 'hline))
+ (mapcar #'org-babel-string-read row)))
+ (org-table-to-lisp))))))
(error
(display-warning 'org-babel
(format "Error reading results: %S" err)
diff --git a/testing/lisp/test-ob.el b/testing/lisp/test-ob.el
index afaf135..580cd7d 100644
--- a/testing/lisp/test-ob.el
+++ b/testing/lisp/test-ob.el
@@ -2252,6 +2252,34 @@ abc
(should (= 0.1 (org-babel--string-to-number "0.1")))
(should (= 1.0 (org-babel--string-to-number "1.0"))))
+(ert-deftest test-ob/import-elisp-from-file ()
+ "Test `org-babel-import-elisp-from-file'."
+ (should
+ (equal
+ (org-test-with-temp-text-in-file "line 1\nline 2\n"
+ (cl-letf (((symbol-function 'display-warning)
+ (lambda (&rest _) (error "No warnings should occur"))
+ (org-table-convert-region-max-lines 2)))
+ (org-babel-import-elisp-from-file (buffer-file-name))))
+ '(("line" 1)
+ ("line" 2))))
+ ;; If an error occurs during table conversion, it is shown with
+ ;; `display-warning' rather than as a message to make sure the
+ ;; caller sees it.
+ (should-error
+ (org-test-with-temp-text-in-file "line 1\nline 2\n"
+ (cl-letf (((symbol-function 'display-warning)
+ (lambda (&rest _) (error "Warning should be displayed")))
+ (org-table-convert-region-max-lines 1))
+ (org-babel-import-elisp-from-file (buffer-file-name)))))
+ ;; But an empty file (as is the case when there are no execution
+ ;; results) does not trigger a warning.
+ (should-not
+ (org-test-with-temp-text-in-file ""
+ (cl-letf (((symbol-function 'display-warning)
+ (lambda (&rest _) (error "No warnings should occur"))))
+ (org-babel-import-elisp-from-file (buffer-file-name))))))
+
(provide 'test-ob)
;;; test-ob ends here