emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* [PATCH] rfc: using ert-deftest with side-effects
@ 2022-11-07 17:03 Leo Butler
  2022-11-08  7:40 ` Ihor Radchenko
  0 siblings, 1 reply; 28+ messages in thread
From: Leo Butler @ 2022-11-07 17:03 UTC (permalink / raw)
  To: Org Mode Mailing List

[-- Attachment #1: Type: text/plain, Size: 977 bytes --]

Hello,

I am patching a bug in ob-octave.el (see attachment) involving the
creation of graphics files. The bug itself is easy to fix: a single line
in ob-octave.el ensures the special variable `ans' is bound, to prevent
Octave from exiting with a non-zero exit code.

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;
2. the general absence of similar tests (except in test-ob.el,
   test-ob/result-graphics-link-type-header-argument).

To address 1., I have wrapped the tests in an `unwind-protect' form to
ensure clean-up code gets run. The ERT manual does not suggest much
beyond this. At the moment, when the test is run, clean-up is being done
whether the test fails or passes.

I am unsure about 2. Is the absence of such tests because there is a
policy against them, or ...

Leo


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-On-ltb-ob-max-ob-octave.patch --]
[-- Type: text/x-diff; name="0001-On-ltb-ob-max-ob-octave.patch", Size: 2198 bytes --]

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..0fc84bc26 100644
--- a/testing/examples/ob-octave-test.org
+++ b/testing/examples/ob-octave-test.org
@@ -46,10 +46,16 @@ ans = s
 
 
 * Graphical tests
-#+begin_src octave :results graphics :file chart.png
+  :PROPERTIES:
+  :ID:       b8b1d6a0-b7f6-49bd-8cb0-0c74f737332f
+  :END:
+
+Graphics file
+#+begin_src octave :results file graphics :file sombrero.png
 sombrero;
 #+end_src
 
+Graphics session
 #+begin_src octave :session
 sombrero;
 #+end_src
diff --git a/testing/lisp/test-ob-octave.el b/testing/lisp/test-ob-octave.el
index 78ce10214..bdc9befef 100644
--- a/testing/lisp/test-ob-octave.el
+++ b/testing/lisp/test-ob-octave.el
@@ -64,4 +64,22 @@
     (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."
+  (org-test-at-id "b8b1d6a0-b7f6-49bd-8cb0-0c74f737332f"
+    (org-babel-next-src-block)
+    (org-babel-execute-src-block)
+    (unwind-protect
+        (prog1
+            (and (should (search-forward "[[file:sombrero.png]]" nil nil))
+                 (should (file-readable-p "sombrero.png"))
+                 (should (let ((size (nth 7 (file-attributes "sombrero.png"))))
+                           (> size 0)))
+                 (should (not (get-buffer "*Org-Babel Error Output*")))))
+      ;; clean-up
+      (delete-file "sombrero.png")
+      (when (get-buffer "*Org-Babel Error Output*")
+        (kill-buffer "*Org-Babel Error Output*"))
+      (revert-buffer t t))))
+
 (provide 'test-ob-octave)

^ permalink raw reply related	[flat|nested] 28+ messages in thread

end of thread, other threads:[~2023-01-24 18:14 UTC | newest]

Thread overview: 28+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-11-07 17:03 [PATCH] rfc: using ert-deftest with side-effects Leo Butler
2022-11-08  7:40 ` Ihor Radchenko
2022-11-08 19:55   ` [PATCH] lisp/ob-octave.el, was " Leo Butler
2022-11-09  5:14     ` Ihor Radchenko
2022-11-09 20:33       ` Leo Butler
2022-11-14  1:56         ` Ihor Radchenko
2022-11-15 19:43           ` Leo Butler
2022-12-17  8:25             ` Ihor Radchenko
2022-12-17 10:06               ` Ihor Radchenko
2022-12-21 11:56                 ` Ihor Radchenko
2022-12-22 13:32                   ` Leo Butler
2022-12-27 14:45                     ` Ihor Radchenko
2022-12-29  9:54                       ` Ihor Radchenko
2023-01-02  8:42                         ` Ihor Radchenko
2023-01-05 20:08                           ` Leo Butler
2023-01-06 15:11                             ` Ihor Radchenko
2023-01-07  3:08                               ` Leo Butler
2023-01-10 20:30                                 ` Leo Butler
2023-01-11 11:15                                   ` Ihor Radchenko
2023-01-11 21:51                                     ` Leo Butler
2023-01-12  8:42                                       ` Ihor Radchenko
2023-01-13 18:00                                     ` Ihor Radchenko
2023-01-14 13:04                                       ` Leo Butler
2023-01-14 13:13                                         ` Ihor Radchenko
2023-01-14 15:16                                         ` Max Nikulin
2023-01-23 10:32                                       ` Ihor Radchenko
2023-01-24 18:08                                         ` Leo Butler
2023-01-07 12:48                               ` Ihor Radchenko

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).