On Tue, Nov 08 2022, Ihor Radchenko <yanta...@posteo.net> wrote:

> Leo Butler <leo.but...@umanitoba.ca> writes:
>
>> However, I would like feedback/suggestions on writing such a
>> test. Issues include:
>>
>> 1. how to clean up the side-effects, including changes in the test
>>    buffer, filesystem and potentially creating an error buffer;
>
> As you did, we generally use unwind-protect. Also, we prefer putting
> temporary files into temporary directory.
>
> You can grep for `make-temp-file' and `delete-file' in tests. There are
> plenty of examples.
>
>> 2. the general absence of similar tests (except in test-ob.el,
>>    test-ob/result-graphics-link-type-header-argument).
>> ...
>> I am unsure about 2. Is the absence of such tests because there is a
>> policy against them, or ...
>
> We have no such policy. In fact, many tests are making
> temporary files.

Ihor,
Thanks for your feeback and the pointer. I have revised the tests and
attach the revised patch.

Best regards,
Leo

From 84587bdbd705c2769d0f02398f1e38fe26ac0a98 Mon Sep 17 00:00:00 2001
From: Leo Butler <leo.but...@umanitoba.ca>
Date: Tue, 8 Nov 2022 13:31:47 -0600
Subject: [PATCH] prevent error in Octave process, add tests, update test docs

* lisp/ob-octave.el (org-babel-execute:octave):

  Ensure that the special Octave variable `ans' is bound when GFX-FILE
  is non-nil.  The glue code in ORG-BABEL-OCTAVE-WRAPPER-METHOD causes
  Octave to exit with a non-zero exit code when `ans' is not bound.

* testing/examples/ob-octave-test.org:

  Update the Graphical tests section:
  -put in the correct headers;
  -add a remark about where to find the test.

* testing/lisp/test-ob-octave.el:

  Add the tests ob-octave/graphics-file and
  ob-octave/graphics-file-session. The first test verifies that the
  bug identified above is fixed; it also verifies that graphics file
  creation works correctly for scripting. The second test verifies
  graphics file creation works correctly for sessions.
---
 lisp/ob-octave.el                   |  2 +-
 testing/examples/ob-octave-test.org |  7 +++--
 testing/lisp/test-ob-octave.el      | 47 +++++++++++++++++++++++++++++
 3 files changed, 53 insertions(+), 3 deletions(-)

diff --git a/lisp/ob-octave.el b/lisp/ob-octave.el
index 55926b789..f85b79fa2 100644
--- a/lisp/ob-octave.el
+++ b/lisp/ob-octave.el
@@ -91,7 +91,7 @@ end")
 				 (list
 				  "set (0, \"defaultfigurevisible\", \"off\");"
 				  full-body
-				  (format "print -dpng %s" gfx-file))
+				  (format "print -dpng %s\nans=%S" gfx-file gfx-file))
 				 "\n")
 		    full-body)
 		  result-type matlabp)))
diff --git a/testing/examples/ob-octave-test.org b/testing/examples/ob-octave-test.org
index 9839d637e..c0f04b7b9 100644
--- a/testing/examples/ob-octave-test.org
+++ b/testing/examples/ob-octave-test.org
@@ -46,10 +46,13 @@ ans = s
 
 
 * Graphical tests
-#+begin_src octave :results graphics :file chart.png
+
+Graphics file. This test is performed by =ob-octave/graphics-file= in =testing/lisp/test-ob-octave.el=.
+#+begin_src octave :results file graphics :file sombrero.png
 sombrero;
 #+end_src
 
-#+begin_src octave :session
+Graphics file in a session. This test is performed by =ob-octave/graphics-file-session= in =testing/lisp/test-ob-octave.el=.
+#+begin_src octave :session :results graphics file :file sombrero.png
 sombrero;
 #+end_src
diff --git a/testing/lisp/test-ob-octave.el b/testing/lisp/test-ob-octave.el
index 78ce10214..dc3782fd0 100644
--- a/testing/lisp/test-ob-octave.el
+++ b/testing/lisp/test-ob-octave.el
@@ -64,4 +64,51 @@
     (org-babel-next-src-block 5)
     (should (equal nil (org-babel-execute-src-block)))))
 
+(ert-deftest ob-octave/graphics-file ()
+  "Graphics file. Test that link is correctly inserted and graphics file is created (and not empty). Clean-up side-effects."
+  ;; In case a prior test left the Error Output buffer hanging around.
+  (when (get-buffer "*Org-Babel Error Output*")
+    (kill-buffer "*Org-Babel Error Output*"))
+  (let ((file (make-temp-file "test-ob-octave-" nil ".png")))
+    (unwind-protect
+        (org-test-with-temp-text
+	    (format "#+begin_src octave :results file graphics :file %s
+sombrero;
+#+end_src"
+		    file)
+          (org-babel-execute-src-block)
+          (should (search-forward (format "[[file:%s]]" file) nil nil))
+          (should (file-readable-p file))
+          (should (let ((size (nth 7 (file-attributes file))))
+                    (> size 0)))
+          (should (not (get-buffer "*Org-Babel Error Output*"))))
+      ;; clean-up
+      (delete-file file)
+      (when (get-buffer "*Org-Babel Error Output*")
+        (kill-buffer "*Org-Babel Error Output*")))))
+
+(ert-deftest ob-octave/graphics-file-session ()
+  "Graphics file in a session. Test that session is started in *Inferior Octave* buffer, link is correctly inserted and graphics file is created (and not empty). Clean-up side-effects."
+  (let ((file (make-temp-file "test-ob-octave-" nil ".png")))
+    (unwind-protect
+        (org-test-with-temp-text
+	    (format "#+begin_src octave :session :results file graphics :file %s
+sombrero;
+#+end_src"
+		    file)
+          (org-babel-execute-src-block)
+          (should (get-buffer "*Inferior Octave*"))
+          (should (search-forward (format "[[file:%s]]" file) nil nil))
+          (should (file-readable-p file))
+          (should (let ((size (nth 7 (file-attributes file))))
+                    (> size 0)))
+          (should (not (get-buffer "*Org-Babel Error Output*"))))
+      ;; clean-up
+      (delete-file file)
+      (let (kill-buffer-query-functions kill-buffer-hook)
+        (kill-buffer "*Inferior Octave*"))
+      (when (get-buffer "*Org-Babel Error Output*")
+        (kill-buffer "*Org-Babel Error Output*")))))
+
+
 (provide 'test-ob-octave)
-- 
2.35.1

Reply via email to