test-ob-shell.el 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. ;;; test-ob-shell.el -*- lexical-binding: t; -*-
  2. ;; Copyright (c) 2010-2014, 2019 Eric Schulte
  3. ;; Authors: Eric Schulte
  4. ;; This file is not part of GNU Emacs.
  5. ;; This program is free software; you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation, either version 3 of the License, or
  8. ;; (at your option) any later version.
  9. ;; This program is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with this program. If not, see <https://www.gnu.org/licenses/>.
  15. ;;; Comment:
  16. ;; Template test file for Org tests
  17. ;;; Code:
  18. (org-test-for-executable "sh")
  19. (require 'ob-core)
  20. (unless (featurep 'ob-shell)
  21. (signal 'missing-test-dependency "Support for Shell code blocks"))
  22. (ert-deftest test-ob-shell/dont-insert-spaces-on-expanded-bodies ()
  23. "Expanded shell bodies should not start with a blank line
  24. unless the body of the tangled block does."
  25. (should-not (string-match "^[\n\r][\t ]*[\n\r]"
  26. (org-babel-expand-body:generic "echo 2" '())))
  27. (should (string-match "^[\n\r][\t ]*[\n\r]"
  28. (org-babel-expand-body:generic "\n\necho 2" '()))))
  29. (ert-deftest test-ob-shell/dont-error-on-empty-results ()
  30. "Was throwing an elisp error when shell blocks threw errors and
  31. returned empty results."
  32. (should (null (org-babel-execute:sh "ls NoSuchFileOrDirectory.txt" nil))))
  33. (ert-deftest test-ob-shell/session ()
  34. "This also tests `org-babel-comint-with-output' in
  35. ob-comint.el, which was not previously tested."
  36. (let ((res (org-babel-execute:sh "echo 1; echo 2" '((:session . "yes")))))
  37. (should res)
  38. (should (listp res)))
  39. ;; Test multi-line input.
  40. (let ((result (org-babel-execute:sh
  41. "if true \n then \n echo yes \n fi"
  42. '((:session . "yes")))))
  43. (should result)
  44. (should (string= "yes" result))))
  45. ; A list of tests using the samples in ob-shell-test.org
  46. (ert-deftest ob-shell/generic-uses-no-arrays ()
  47. "No arrays for generic"
  48. (org-test-at-id "0ba56632-8dc1-405c-a083-c204bae477cf"
  49. (org-babel-next-src-block)
  50. (should (equal "one two three" (org-trim (org-babel-execute-src-block))))))
  51. (ert-deftest ob-shell/bash-uses-arrays ()
  52. "Bash arrays"
  53. (org-test-at-id "0ba56632-8dc1-405c-a083-c204bae477cf"
  54. (org-babel-next-src-block 2)
  55. (should (equal "one" (org-trim (org-babel-execute-src-block))))))
  56. (ert-deftest ob-shell/generic-uses-no-assoc-arrays ()
  57. "No associative arrays for generic"
  58. (should
  59. (equal "first one second two third three"
  60. (org-test-at-id
  61. "bec1a5b0-4619-4450-a8c0-2a746b44bf8d"
  62. (org-babel-next-src-block)
  63. (org-trim (org-babel-execute-src-block)))))
  64. (should
  65. (equal "bread 2 kg spaghetti 20 cm milk 50 dl"
  66. (org-test-at-id
  67. "82320a48-3409-49d7-85c9-5de1c6d3ff87"
  68. (org-babel-next-src-block)
  69. (org-trim (org-babel-execute-src-block))))))
  70. (ert-deftest ob-shell/bash-uses-assoc-arrays ()
  71. "Bash associative arrays"
  72. (should
  73. (equal "two"
  74. (org-test-at-id
  75. "bec1a5b0-4619-4450-a8c0-2a746b44bf8d"
  76. (org-babel-next-src-block 2)
  77. (org-trim (org-babel-execute-src-block)))))
  78. ;; Bash associative arrays as strings for the row.
  79. (should
  80. (equal "20 cm"
  81. (org-test-at-id
  82. "82320a48-3409-49d7-85c9-5de1c6d3ff87"
  83. (org-babel-next-src-block 2)
  84. (org-trim (org-babel-execute-src-block))))))
  85. (ert-deftest ob-shell/simple-list ()
  86. "Test list variables in shell."
  87. ;; With bash, a list is turned into an array.
  88. (should
  89. (equal "2"
  90. (org-test-with-temp-text
  91. "#+BEGIN_SRC bash :results output :var l='(1 2)\necho ${l[1]}\n#+END_SRC"
  92. (org-trim (org-babel-execute-src-block)))))
  93. ;; On sh, it is a string containing all values.
  94. (should
  95. (equal "1 2"
  96. (org-test-with-temp-text
  97. "#+BEGIN_SRC sh :results output :var l='(1 2)\necho ${l}\n#+END_SRC"
  98. (org-trim (org-babel-execute-src-block))))))
  99. (ert-deftest ob-shell/remote-with-stdin-or-cmdline ()
  100. "Test :stdin and :cmdline with a remote directory."
  101. ;; We assume `default-directory' is a local directory.
  102. (skip-unless (not (memq system-type '(ms-dos windows-nt))))
  103. (org-test-with-tramp-remote-dir remote-dir
  104. (dolist (spec `( ()
  105. (:dir ,remote-dir)
  106. (:dir ,remote-dir :cmdline t)
  107. (:dir ,remote-dir :stdin t)
  108. (:dir ,remote-dir :cmdline t :shebang t)
  109. (:dir ,remote-dir :stdin t :shebang t)
  110. (:dir ,remote-dir :cmdline t :stdin t :shebang t)
  111. (:cmdline t)
  112. (:stdin t)
  113. (:cmdline t :shebang t)
  114. (:stdin t :shebang t)
  115. (:cmdline t :stdin t :shebang t)))
  116. (let ((default-directory (or (plist-get spec :dir) default-directory))
  117. (org-confirm-babel-evaluate nil)
  118. (params-line "")
  119. (who-line " export who=tramp")
  120. (args-line " echo ARGS: --verbose 23 71"))
  121. (when-let ((dir (plist-get spec :dir)))
  122. (setq params-line (concat params-line " " ":dir " dir)))
  123. (when (plist-get spec :stdin)
  124. (setq who-line " read -r who")
  125. (setq params-line (concat params-line " :stdin input")))
  126. (when (plist-get spec :cmdline)
  127. (setq args-line " echo \"ARGS: $*\"")
  128. (setq params-line (concat params-line " :cmdline \"--verbose 23 71\"")))
  129. (when (plist-get spec :shebang)
  130. (setq params-line (concat params-line " :shebang \"#!/bin/sh\"")))
  131. (let* ((result (org-test-with-temp-text
  132. (mapconcat #'identity
  133. (list "#+name: input"
  134. "tramp"
  135. ""
  136. (concat "<point>"
  137. "#+begin_src sh :results output " params-line)
  138. args-line
  139. who-line
  140. " echo \"hello $who from $(pwd)/\""
  141. "#+end_src")
  142. "\n")
  143. (org-trim (org-babel-execute-src-block))))
  144. (expected (concat "ARGS: --verbose 23 71"
  145. "\nhello tramp from " (file-local-name default-directory))))
  146. (should (equal result expected)))))))
  147. (provide 'test-ob-shell)
  148. ;;; test-ob-shell.el ends here