aboutsummaryrefslogtreecommitdiffstats
path: root/test/lisp/eshell/esh-cmd-tests.el
diff options
context:
space:
mode:
Diffstat (limited to 'test/lisp/eshell/esh-cmd-tests.el')
-rw-r--r--test/lisp/eshell/esh-cmd-tests.el283
1 files changed, 283 insertions, 0 deletions
diff --git a/test/lisp/eshell/esh-cmd-tests.el b/test/lisp/eshell/esh-cmd-tests.el
new file mode 100644
index 00000000000..e86985ec717
--- /dev/null
+++ b/test/lisp/eshell/esh-cmd-tests.el
@@ -0,0 +1,283 @@
1;;; esh-cmd-tests.el --- esh-cmd test suite -*- lexical-binding:t -*-
2
3;; Copyright (C) 2022 Free Software Foundation, Inc.
4
5;; This file is part of GNU Emacs.
6
7;; GNU Emacs is free software: you can redistribute it and/or modify
8;; it under the terms of the GNU General Public License as published by
9;; the Free Software Foundation, either version 3 of the License, or
10;; (at your option) any later version.
11
12;; GNU Emacs is distributed in the hope that it will be useful,
13;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15;; GNU General Public License for more details.
16
17;; You should have received a copy of the GNU General Public License
18;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
19
20;;; Commentary:
21
22;; Tests for Eshell's command invocation.
23
24;;; Code:
25
26(require 'ert)
27(require 'esh-mode)
28(require 'eshell)
29
30(require 'eshell-tests-helpers
31 (expand-file-name "eshell-tests-helpers"
32 (file-name-directory (or load-file-name
33 default-directory))))
34
35(defvar eshell-test-value nil)
36
37;;; Tests:
38
39
40;; Command invocation
41
42(ert-deftest esh-cmd-test/simple-command-result ()
43 "Test invocation with a simple command."
44 (should (equal (eshell-test-command-result "+ 1 2") 3)))
45
46(ert-deftest esh-cmd-test/lisp-command ()
47 "Test invocation with an elisp command."
48 (should (equal (eshell-test-command-result "(+ 1 2)") 3)))
49
50(ert-deftest esh-cmd-test/lisp-command-with-quote ()
51 "Test invocation with an elisp command containing a quote."
52 (should (equal (eshell-test-command-result "(eq 'foo nil)") nil)))
53
54(ert-deftest esh-cmd-test/lisp-command-args ()
55 "Test invocation with elisp and trailing args.
56Test that trailing arguments outside the S-expression are
57ignored. e.g. \"(+ 1 2) 3\" => 3"
58 (should (equal (eshell-test-command-result "(+ 1 2) 3") 3)))
59
60(ert-deftest esh-cmd-test/subcommand ()
61 "Test invocation with a simple subcommand."
62 (should (equal (eshell-test-command-result "{+ 1 2}") 3)))
63
64(ert-deftest esh-cmd-test/subcommand-args ()
65 "Test invocation with a subcommand and trailing args.
66Test that trailing arguments outside the subcommand are ignored.
67e.g. \"{+ 1 2} 3\" => 3"
68 (should (equal (eshell-test-command-result "{+ 1 2} 3") 3)))
69
70(ert-deftest esh-cmd-test/subcommand-lisp ()
71 "Test invocation with an elisp subcommand and trailing args.
72Test that trailing arguments outside the subcommand are ignored.
73e.g. \"{(+ 1 2)} 3\" => 3"
74 (should (equal (eshell-test-command-result "{(+ 1 2)} 3") 3)))
75
76
77;; Logical operators
78
79(ert-deftest esh-cmd-test/and-operator ()
80 "Test logical && operator."
81 (skip-unless (executable-find "["))
82 (with-temp-eshell
83 (eshell-command-result-p "[ foo = foo ] && echo hi"
84 "hi\n")
85 (eshell-command-result-p "[ foo = bar ] && echo hi"
86 "\\`\\'")))
87
88(ert-deftest esh-cmd-test/or-operator ()
89 "Test logical || operator."
90 (skip-unless (executable-find "["))
91 (with-temp-eshell
92 (eshell-command-result-p "[ foo = foo ] || echo hi"
93 "\\`\\'")
94 (eshell-command-result-p "[ foo = bar ] || echo hi"
95 "hi\n")))
96
97
98;; Control flow statements
99
100(ert-deftest esh-cmd-test/for-loop ()
101 "Test invocation of a for loop."
102 (with-temp-eshell
103 (eshell-command-result-p "for i in 5 { echo $i }"
104 "5\n")))
105
106(ert-deftest esh-cmd-test/for-loop-list ()
107 "Test invocation of a for loop iterating over a list."
108 (with-temp-eshell
109 (eshell-command-result-p "for i in (list 1 2 (list 3 4)) { echo $i }"
110 "1\n2\n(3 4)\n")))
111
112(ert-deftest esh-cmd-test/for-loop-multiple-args ()
113 "Test invocation of a for loop iterating over multiple arguments."
114 (with-temp-eshell
115 (eshell-command-result-p "for i in 1 2 (list 3 4) { echo $i }"
116 "1\n2\n3\n4\n")))
117
118(ert-deftest esh-cmd-test/for-name-loop () ; bug#15231
119 "Test invocation of a for loop using `name'."
120 (let ((process-environment (cons "name" process-environment)))
121 (should (equal (eshell-test-command-result
122 "for name in 3 { echo $name }")
123 3))))
124
125(ert-deftest esh-cmd-test/for-name-shadow-loop () ; bug#15372
126 "Test invocation of a for loop using an env-var."
127 (let ((process-environment (cons "name=env-value" process-environment)))
128 (with-temp-eshell
129 (eshell-command-result-p
130 "echo $name; for name in 3 { echo $name }; echo $name"
131 "env-value\n3\nenv-value\n"))))
132
133(ert-deftest esh-cmd-test/while-loop ()
134 "Test invocation of a while loop."
135 (with-temp-eshell
136 (let ((eshell-test-value '(0 1 2)))
137 (eshell-command-result-p
138 (concat "while $eshell-test-value "
139 "{ setq eshell-test-value (cdr eshell-test-value) }")
140 "(1 2)\n(2)\n"))))
141
142(ert-deftest esh-cmd-test/while-loop-lisp-form ()
143 "Test invocation of a while loop using a Lisp form."
144 (with-temp-eshell
145 (let ((eshell-test-value 0))
146 (eshell-command-result-p
147 (concat "while (/= eshell-test-value 3) "
148 "{ setq eshell-test-value (1+ eshell-test-value) }")
149 "1\n2\n3\n"))))
150
151(ert-deftest esh-cmd-test/while-loop-ext-cmd ()
152 "Test invocation of a while loop using an external command."
153 (skip-unless (executable-find "["))
154 (with-temp-eshell
155 (let ((eshell-test-value 0))
156 (eshell-command-result-p
157 (concat "while {[ $eshell-test-value -ne 3 ]} "
158 "{ setq eshell-test-value (1+ eshell-test-value) }")
159 "1\n2\n3\n"))))
160
161(ert-deftest esh-cmd-test/until-loop ()
162 "Test invocation of an until loop."
163 (with-temp-eshell
164 (let ((eshell-test-value nil))
165 (eshell-command-result-p
166 (concat "until $eshell-test-value "
167 "{ setq eshell-test-value t }")
168 "t\n"))))
169
170(ert-deftest esh-cmd-test/until-loop-lisp-form ()
171 "Test invocation of an until loop using a Lisp form."
172 (skip-unless (executable-find "["))
173 (with-temp-eshell
174 (let ((eshell-test-value 0))
175 (eshell-command-result-p
176 (concat "until (= eshell-test-value 3) "
177 "{ setq eshell-test-value (1+ eshell-test-value) }")
178 "1\n2\n3\n"))))
179
180(ert-deftest esh-cmd-test/until-loop-ext-cmd ()
181 "Test invocation of an until loop using an external command."
182 (skip-unless (executable-find "["))
183 (with-temp-eshell
184 (let ((eshell-test-value 0))
185 (eshell-command-result-p
186 (concat "until {[ $eshell-test-value -eq 3 ]} "
187 "{ setq eshell-test-value (1+ eshell-test-value) }")
188 "1\n2\n3\n"))))
189
190(ert-deftest esh-cmd-test/if-statement ()
191 "Test invocation of an if statement."
192 (with-temp-eshell
193 (let ((eshell-test-value t))
194 (eshell-command-result-p "if $eshell-test-value {echo yes}"
195 "yes\n"))
196 (let ((eshell-test-value nil))
197 (eshell-command-result-p "if $eshell-test-value {echo yes}"
198 "\\`\\'"))))
199
200(ert-deftest esh-cmd-test/if-else-statement ()
201 "Test invocation of an if/else statement."
202 (with-temp-eshell
203 (let ((eshell-test-value t))
204 (eshell-command-result-p "if $eshell-test-value {echo yes} {echo no}"
205 "yes\n"))
206 (let ((eshell-test-value nil))
207 (eshell-command-result-p "if $eshell-test-value {echo yes} {echo no}"
208 "no\n"))))
209
210(ert-deftest esh-cmd-test/if-else-statement-lisp-form ()
211 "Test invocation of an if/else statement using a Lisp form."
212 (with-temp-eshell
213 (eshell-command-result-p "if (zerop 0) {echo yes} {echo no}"
214 "yes\n")
215 (eshell-command-result-p "if (zerop 1) {echo yes} {echo no}"
216 "no\n")
217 (let ((debug-on-error nil))
218 (eshell-command-result-p "if (zerop \"foo\") {echo yes} {echo no}"
219 "no\n"))))
220
221(ert-deftest esh-cmd-test/if-else-statement-lisp-form-2 ()
222 "Test invocation of an if/else statement using a Lisp form.
223This tests when `eshell-lisp-form-nil-is-failure' is nil."
224 (let ((eshell-lisp-form-nil-is-failure nil))
225 (with-temp-eshell
226 (eshell-command-result-p "if (zerop 0) {echo yes} {echo no}"
227 "yes\n")
228 (eshell-command-result-p "if (zerop 1) {echo yes} {echo no}"
229 "yes\n")
230 (let ((debug-on-error nil))
231 (eshell-command-result-p "if (zerop \"foo\") {echo yes} {echo no}"
232 "no\n")))))
233
234(ert-deftest esh-cmd-test/if-else-statement-ext-cmd ()
235 "Test invocation of an if/else statement using an external command."
236 (skip-unless (executable-find "["))
237 (with-temp-eshell
238 (eshell-command-result-p "if {[ foo = foo ]} {echo yes} {echo no}"
239 "yes\n")
240 (eshell-command-result-p "if {[ foo = bar ]} {echo yes} {echo no}"
241 "no\n")))
242
243(ert-deftest esh-cmd-test/unless-statement ()
244 "Test invocation of an unless statement."
245 (with-temp-eshell
246 (let ((eshell-test-value t))
247 (eshell-command-result-p "unless $eshell-test-value {echo no}"
248 "\\`\\'"))
249 (let ((eshell-test-value nil))
250 (eshell-command-result-p "unless $eshell-test-value {echo no}"
251 "no\n"))))
252
253(ert-deftest esh-cmd-test/unless-else-statement ()
254 "Test invocation of an unless/else statement."
255 (with-temp-eshell
256 (let ((eshell-test-value t))
257 (eshell-command-result-p "unless $eshell-test-value {echo no} {echo yes}"
258 "yes\n"))
259 (let ((eshell-test-value nil))
260 (eshell-command-result-p "unless $eshell-test-value {echo no} {echo yes}"
261 "no\n"))))
262
263(ert-deftest esh-cmd-test/unless-else-statement-lisp-form ()
264 "Test invocation of an unless/else statement using a Lisp form."
265 (with-temp-eshell
266 (eshell-command-result-p "unless (zerop 0) {echo no} {echo yes}"
267 "yes\n")
268 (eshell-command-result-p "unless (zerop 1) {echo no} {echo yes}"
269 "no\n")
270 (let ((debug-on-error nil))
271 (eshell-command-result-p "unless (zerop \"foo\") {echo no} {echo yes}"
272 "no\n"))))
273
274(ert-deftest esh-cmd-test/unless-else-statement-ext-cmd ()
275 "Test invocation of an unless/else statement using an external command."
276 (skip-unless (executable-find "["))
277 (with-temp-eshell
278 (eshell-command-result-p "unless {[ foo = foo ]} {echo no} {echo yes}"
279 "yes\n")
280 (eshell-command-result-p "unless {[ foo = bar ]} {echo no} {echo yes}"
281 "no\n")))
282
283;; esh-cmd-tests.el ends here