0

I want to use my org-mode documentation as a sort of quasi-unittest; I want to execute the whole buffer, and be notified if marked cells' new results differ from their previous results. E.g,

#+begin_src python :results verbatim :exports both :wrap example :notify_on_change
x3(10)
#+end_src

#+RESULTS:
#+begin_example
30
#+end_example

Suppose x3 is changed and now x3(10) returns 40. I want to be notified that this change has happened (and get a diff of the two results).

PS: If this should be possible for visual (image) results, it would be really awesome.

PPS: Is there any testing framework that works like this? I.e., it only reports change, and does not need you to manually hardcode values?

1
  • 1
    AFAIK, there is nothing built-in to Org mode that would alow you to do that: you'll have to build it yourself. My inclination would be to use source control as @mankoff suggests, rather than try to use babel directly, although you can use a babel SRC block to run the unit tests and do the comparisons, based on the saved "gold" files that contain the previous output.
    – NickD
    Commented Mar 15, 2021 at 12:22

1 Answer 1

2

One idea might be to save a copy of the current buffer, run the tests, and then run a diff command on them.

(defun run-tests ()
  (interactive)
  (copy-to-buffer "*my-test*" (point-min) (point-max))
  (org-babel-execute-buffer)
  (diff-buffers "*my-test*" (current-buffer)))

This will make a new buffer showing the diff (if any). There are lots of variations I suppose, like writing to files, different ways of diffing.

It would be hard I think to get this to work for images.

[edit]: here is a version that works on a copy of the buffer with ediff.

(defun run-tests ()
  (interactive)
  (let ((contents (buffer-string))
    (buf (current-buffer)))
    (with-current-buffer (get-buffer-create "*my-test*")
      (erase-buffer)
      (insert contents)
      (org-mode)
      (org-babel-execute-buffer)
      (ediff-buffers buf "*my-test*"))))
6
  • I use magit for this.
    – mankoff
    Commented Mar 15, 2021 at 11:38
  • Magic is a clever idea for files in a git repo. That also makes it easy to revert them back to the state you want them. Commented Mar 15, 2021 at 12:35
  • @mankoff Can you add your workflow as an answer?
    – HappyFace
    Commented Mar 15, 2021 at 12:57
  • @JohnKitchin I find ediff-buffers better than diff-buffers. This solution works pretty great. How can I execute the temp buffer instead of the main one?
    – HappyFace
    Commented Mar 15, 2021 at 13:12
  • 1
    @HappyFace I added an example that I think works on a copy and uses ediff. Commented Mar 15, 2021 at 14:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.