Merge branch 'maint'
[org-mode.git] / testing / lisp / test-ob-shell.el
blobf16a56587a3f231a8ea1596347202be7ea26e344
1 ;;; test-ob-shell.el
3 ;; Copyright (c) 2010-2014 Eric Schulte
4 ;; Authors: Eric Schulte
6 ;; This file is not part of GNU Emacs.
8 ;; This program is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation, either version 3 of the License, or
11 ;; (at your option) any later version.
13 ;; This program is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
21 ;;; Comment:
23 ;; Template test file for Org tests
25 ;;; Code:
26 (org-test-for-executable "sh")
27 (unless (featurep 'ob-shell)
28 (signal 'missing-test-dependency "Support for Shell code blocks"))
30 (ert-deftest test-ob-shell/dont-insert-spaces-on-expanded-bodies ()
31 "Expanded shell bodies should not start with a blank line
32 unless the body of the tangled block does."
33 (should-not (string-match "^[\n\r][\t ]*[\n\r]"
34 (org-babel-expand-body:generic "echo 2" '())))
35 (should (string-match "^[\n\r][\t ]*[\n\r]"
36 (org-babel-expand-body:generic "\n\necho 2" '()))))
38 (ert-deftest test-ob-shell/dont-error-on-empty-results ()
39 "Was throwing an elisp error when shell blocks threw errors and
40 returned empty results."
41 (should (null (org-babel-execute:sh "ls NoSuchFileOrDirectory.txt" nil))))
43 (ert-deftest test-ob-shell/session ()
44 "This also tests `org-babel-comint-with-output' in
45 ob-comint.el, which was not previously tested."
46 (let ((res (org-babel-execute:sh "echo 1; echo 2" '((:session . "yes")))))
47 (should res)
48 (should (listp res))))
50 ; A list of tests using the samples in ob-shell-test.org
51 (ert-deftest ob-shell/generic-uses-no-arrays ()
52 "No arrays for generic"
53 (org-test-at-id "0ba56632-8dc1-405c-a083-c204bae477cf"
54 (org-babel-next-src-block)
55 (should (equal "one two three" (org-babel-execute-src-block)))))
57 (ert-deftest ob-shell/bash-uses-arrays ()
58 "Bash arrays"
59 (org-test-at-id "0ba56632-8dc1-405c-a083-c204bae477cf"
60 (org-babel-next-src-block 2)
61 (should (equal "one" (org-babel-execute-src-block)))))
63 (ert-deftest ob-shell/generic-uses-no-assoc-arrays ()
64 "No associative arrays for generic"
65 (should
66 (equal "first one second two third three"
67 (org-test-at-id "bec1a5b0-4619-4450-a8c0-2a746b44bf8d"
68 (org-babel-next-src-block)
69 (org-babel-execute-src-block))))
70 (should
71 (equal "bread 2 kg spaghetti 20 cm milk 50 dl"
72 (org-test-at-id "82320a48-3409-49d7-85c9-5de1c6d3ff87"
73 (org-babel-next-src-block)
74 (org-babel-execute-src-block)))))
76 (ert-deftest ob-shell/bash-uses-assoc-arrays ()
77 "Bash associative arrays"
78 (should
79 (equal "two"
80 (org-test-at-id "bec1a5b0-4619-4450-a8c0-2a746b44bf8d"
81 (org-babel-next-src-block 2)
82 (org-babel-execute-src-block))))
83 ;; Bash associative arrays as strings for the row.
84 (should
85 (equal "20 cm"
86 (org-test-at-id "82320a48-3409-49d7-85c9-5de1c6d3ff87"
87 (org-babel-next-src-block 2)
88 (org-babel-execute-src-block)))))
90 (ert-deftest ob-shell/simple-list ()
91 "Test list variables in shell."
92 ;; With bash, a list is turned into an array.
93 (should
94 (= 2
95 (org-test-with-temp-text
96 "#+BEGIN_SRC bash :var l='(1 2)\necho ${l[1]}\n#+END_SRC"
97 (org-babel-execute-src-block))))
98 ;; On sh, it is a string containing all values.
99 (should
100 (equal "1 2"
101 (org-test-with-temp-text
102 "#+BEGIN_SRC sh :var l='(1 2)\necho ${l}\n#+END_SRC"
103 (org-babel-execute-src-block)))))
105 (provide 'test-ob-shell)
107 ;;; test-ob-shell.el ends here