From c64354854f6016cb0ad07c90f5e474c97cd790db Mon Sep 17 00:00:00 2001
From: Matt Trzcinski <matt@excalamus.com>
Date: Tue, 20 Dec 2022 15:22:38 -0500
Subject: [PATCH 03/20] test-ob-shell.el: Refactor test-ob-shell/session
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

* testing/lisp/test-ob-shell.el (ob-shell/session): Split
`ob-shell/session' into
`test-ob-shell/session-single-return-returns-string' and
`test-ob-shell/session-multiple-returns-returns-list'.
(ob-shell/session): Rename comint from "yes" to test name[fn:1]. On
pass, kill process and remove process buffer.
* testing/lisp/test-ob-shell.el, lisp/ob-shell.el: Place notes
indicating need for future refactoring.

[fn:1] Session name is a string and not a variable (such as
`session-name').  This is because `org-babel-execute:sh' produces a
type error when the session name is defined with a variable.  The
source of the error appears to be the `params' symbol in
`org-babel-execute:sh'.  `params' does not evaluate as a variable as
expected–it evaluates as a symbol.  However, `org-babel-execute:sh' is
defined within a function factory which makes it difficult to debug.
Hard code the test name for now and refactor it later once
`org-babel-execute:sh` is refactored.
---
 lisp/ob-shell.el              |  2 ++
 testing/lisp/test-ob-shell.el | 36 ++++++++++++++++++++++-------------
 2 files changed, 25 insertions(+), 13 deletions(-)

diff --git a/lisp/ob-shell.el b/lisp/ob-shell.el
index d38d2d335..8da9aa738 100644
--- a/lisp/ob-shell.el
+++ b/lisp/ob-shell.el
@@ -68,6 +68,8 @@ alist element.")
 
 (defvar org-babel-prompt-command)
 
+;; TODO refactor into macro.  Currently violates (elisp) Coding
+;; Conventions and is hard to debug.
 (defun org-babel-shell-initialize ()
   "Define execution functions associated to shell names.
 This function has to be called whenever `org-babel-shell-names'
diff --git a/testing/lisp/test-ob-shell.el b/testing/lisp/test-ob-shell.el
index 625dc64b9..54b6d5a6f 100644
--- a/testing/lisp/test-ob-shell.el
+++ b/testing/lisp/test-ob-shell.el
@@ -29,8 +29,8 @@
   (signal 'missing-test-dependency "Support for Shell code blocks"))
 
 (ert-deftest test-ob-shell/dont-insert-spaces-on-expanded-bodies ()
-  "Expanded shell bodies should not start with a blank line
-unless the body of the tangled block does."
+  "Expanded shell bodies should not start with a blank line unless
+the body of the tangled block does."
   (should-not (string-match "^[\n\r][\t ]*[\n\r]"
                             (org-babel-expand-body:generic "echo 2" '())))
   (should (string-match "^[\n\r][\t ]*[\n\r]"
@@ -43,18 +43,28 @@ unless the body of the tangled block does."
   (if (should (null (org-babel-execute:sh "ls NoSuchFileOrDirectory.txt" nil)))
       (kill-buffer "*Org-Babel Error Output*")))
 
-(ert-deftest test-ob-shell/session ()
-  "This also tests `org-babel-comint-with-output' in
-ob-comint.el, which was not previously tested."
-  (let ((res (org-babel-execute:sh "echo 1; echo 2" '((:session . "yes")))))
-    (should res)
-    (should (listp res)))
-  ;; Test multi-line input.
-  (let ((result (org-babel-execute:sh
-                 "if true \n then \n echo yes \n fi"
-                 '((:session . "yes")))))
+;; TODO refactor session name into variable after refactoring
+;; org-babel-execute:sh.  See comment there.
+(ert-deftest test-ob-shell/session-single-return-returns-string ()
+  (let ((kill-buffer-query-functions nil)
+        (result (org-babel-execute:sh
+                 "echo test-ob-shell/session-evaluation-single-return-returns-string"
+                 '((:session . "test-ob-shell/session-evaluation-single-return-returns-string")))))
     (should result)
-    (should (string= "yes" result))))
+    (if (should (string= "test-ob-shell/session-evaluation-single-return-returns-string" result))
+        (kill-buffer "test-ob-shell/session-evaluation-single-return-returns-string"))))
+
+;; TODO refactor session name into variable after refactoring
+;; org-babel-execute:sh.  See comment there.
+(ert-deftest test-ob-shell/session-multiple-returns-returns-list ()
+  (let ((kill-buffer-query-functions nil)
+        (result (org-babel-execute:sh
+                 "echo 1; echo 2"
+                 '((:session . "test-ob-shell/session-multiple-returns-returns-list")))))
+    (should result)
+    (should (listp result))
+    (if (should (equal '((1) (2)) result))
+        (kill-buffer "test-ob-shell/session-multiple-returns-returns-list"))))
 
 ; A list of tests using the samples in ob-shell-test.org
 (ert-deftest ob-shell/generic-uses-no-arrays ()
-- 
2.38.1

