test-ob-shell.el 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. ;;; test-ob-shell.el
  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. (unless (featurep 'ob-shell)
  20. (signal 'missing-test-dependency "Support for Shell code blocks"))
  21. (ert-deftest test-ob-shell/dont-insert-spaces-on-expanded-bodies ()
  22. "Expanded shell bodies should not start with a blank line
  23. unless the body of the tangled block does."
  24. (should-not (string-match "^[\n\r][\t ]*[\n\r]"
  25. (org-babel-expand-body:generic "echo 2" '())))
  26. (should (string-match "^[\n\r][\t ]*[\n\r]"
  27. (org-babel-expand-body:generic "\n\necho 2" '()))))
  28. (ert-deftest test-ob-shell/dont-error-on-empty-results ()
  29. "Was throwing an elisp error when shell blocks threw errors and
  30. returned empty results."
  31. (should (null (org-babel-execute:sh "ls NoSuchFileOrDirectory.txt" nil))))
  32. (ert-deftest test-ob-shell/session ()
  33. "This also tests `org-babel-comint-with-output' in
  34. ob-comint.el, which was not previously tested."
  35. (let ((res (org-babel-execute:sh "echo 1; echo 2" '((:session . "yes")))))
  36. (should res)
  37. (should (listp res))))
  38. ; A list of tests using the samples in ob-shell-test.org
  39. (ert-deftest ob-shell/generic-uses-no-arrays ()
  40. "No arrays for generic"
  41. (org-test-at-id "0ba56632-8dc1-405c-a083-c204bae477cf"
  42. (org-babel-next-src-block)
  43. (should (equal "one two three" (org-trim (org-babel-execute-src-block))))))
  44. (ert-deftest ob-shell/bash-uses-arrays ()
  45. "Bash arrays"
  46. (org-test-at-id "0ba56632-8dc1-405c-a083-c204bae477cf"
  47. (org-babel-next-src-block 2)
  48. (should (equal "one" (org-trim (org-babel-execute-src-block))))))
  49. (ert-deftest ob-shell/generic-uses-no-assoc-arrays ()
  50. "No associative arrays for generic"
  51. (should
  52. (equal "first one second two third three"
  53. (org-test-at-id
  54. "bec1a5b0-4619-4450-a8c0-2a746b44bf8d"
  55. (org-babel-next-src-block)
  56. (org-trim (org-babel-execute-src-block)))))
  57. (should
  58. (equal "bread 2 kg spaghetti 20 cm milk 50 dl"
  59. (org-test-at-id
  60. "82320a48-3409-49d7-85c9-5de1c6d3ff87"
  61. (org-babel-next-src-block)
  62. (org-trim (org-babel-execute-src-block))))))
  63. (ert-deftest ob-shell/bash-uses-assoc-arrays ()
  64. "Bash associative arrays"
  65. (should
  66. (equal "two"
  67. (org-test-at-id
  68. "bec1a5b0-4619-4450-a8c0-2a746b44bf8d"
  69. (org-babel-next-src-block 2)
  70. (org-trim (org-babel-execute-src-block)))))
  71. ;; Bash associative arrays as strings for the row.
  72. (should
  73. (equal "20 cm"
  74. (org-test-at-id
  75. "82320a48-3409-49d7-85c9-5de1c6d3ff87"
  76. (org-babel-next-src-block 2)
  77. (org-trim (org-babel-execute-src-block))))))
  78. (ert-deftest ob-shell/simple-list ()
  79. "Test list variables in shell."
  80. ;; With bash, a list is turned into an array.
  81. (should
  82. (equal "2"
  83. (org-test-with-temp-text
  84. "#+BEGIN_SRC bash :results output :var l='(1 2)\necho ${l[1]}\n#+END_SRC"
  85. (org-trim (org-babel-execute-src-block)))))
  86. ;; On sh, it is a string containing all values.
  87. (should
  88. (equal "1 2"
  89. (org-test-with-temp-text
  90. "#+BEGIN_SRC sh :results output :var l='(1 2)\necho ${l}\n#+END_SRC"
  91. (org-trim (org-babel-execute-src-block))))))
  92. (provide 'test-ob-shell)
  93. ;;; test-ob-shell.el ends here