Commit | Line | Data |
---|---|---|
797c9e3d | 1 | ;;; tests/eshell.el --- Eshell test suite |
affbf647 | 2 | |
ba318903 | 3 | ;; Copyright (C) 1999-2014 Free Software Foundation, Inc. |
affbf647 | 4 | |
7de5b421 GM |
5 | ;; Author: John Wiegley <johnw@gnu.org> |
6 | ||
affbf647 GM |
7 | ;; This file is part of GNU Emacs. |
8 | ||
4ee57b2a | 9 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
affbf647 | 10 | ;; it under the terms of the GNU General Public License as published by |
4ee57b2a GM |
11 | ;; the Free Software Foundation, either version 3 of the License, or |
12 | ;; (at your option) any later version. | |
affbf647 GM |
13 | |
14 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
15 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | ;; GNU General Public License for more details. | |
18 | ||
19 | ;; You should have received a copy of the GNU General Public License | |
4ee57b2a | 20 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
affbf647 | 21 | |
affbf647 GM |
22 | ;;; Commentary: |
23 | ||
797c9e3d | 24 | ;; Eshell test suite. |
affbf647 GM |
25 | |
26 | ;;; Code: | |
27 | ||
797c9e3d AG |
28 | (require 'ert) |
29 | (require 'eshell) | |
affbf647 | 30 | |
797c9e3d AG |
31 | (defmacro with-temp-eshell (&rest body) |
32 | "Evaluate BODY in a temporary Eshell buffer." | |
9dc7e8e1 GM |
33 | `(let* ((eshell-directory-name (make-temp-file "eshell" t)) |
34 | (eshell-history-file-name nil) | |
35 | (eshell-buffer (eshell t))) | |
797c9e3d AG |
36 | (unwind-protect |
37 | (with-current-buffer eshell-buffer | |
38 | ,@body) | |
2d5788f4 | 39 | (let (kill-buffer-query-functions) |
9dc7e8e1 GM |
40 | (kill-buffer eshell-buffer) |
41 | (delete-directory eshell-directory-name t))))) | |
affbf647 GM |
42 | |
43 | (defun eshell-insert-command (text &optional func) | |
44 | "Insert a command at the end of the buffer." | |
45 | (goto-char eshell-last-output-end) | |
46 | (insert-and-inherit text) | |
47 | (funcall (or func 'eshell-send-input))) | |
48 | ||
49 | (defun eshell-match-result (regexp) | |
0c2a2b57 | 50 | "Check that text after `eshell-last-input-end' matches REGEXP." |
affbf647 | 51 | (goto-char eshell-last-input-end) |
0c2a2b57 GM |
52 | (should (string-match-p regexp (buffer-substring-no-properties |
53 | (point) (point-max))))) | |
affbf647 GM |
54 | |
55 | (defun eshell-command-result-p (text regexp &optional func) | |
56 | "Insert a command at the end of the buffer." | |
57 | (eshell-insert-command text func) | |
58 | (eshell-match-result regexp)) | |
59 | ||
6ad9cb08 GM |
60 | (defun eshell-test-command-result (command) |
61 | "Like `eshell-command-result', but not using HOME." | |
62 | (let ((eshell-directory-name (make-temp-file "eshell" t)) | |
63 | (eshell-history-file-name nil)) | |
bc2c0769 GM |
64 | (unwind-protect |
65 | (eshell-command-result command) | |
66 | (delete-directory eshell-directory-name t)))) | |
6ad9cb08 | 67 | |
797c9e3d AG |
68 | ;;; Tests: |
69 | ||
70 | (ert-deftest eshell-test/simple-command-result () | |
71 | "Test `eshell-command-result' with a simple command." | |
6ad9cb08 | 72 | (should (equal (eshell-test-command-result "+ 1 2") 3))) |
81fb60b2 | 73 | |
797c9e3d AG |
74 | (ert-deftest eshell-test/lisp-command () |
75 | "Test `eshell-command-result' with an elisp command." | |
6ad9cb08 | 76 | (should (equal (eshell-test-command-result "(+ 1 2)") 3))) |
81fb60b2 | 77 | |
ae5e4c48 | 78 | (ert-deftest eshell-test/for-loop () |
1e53bb4b | 79 | "Test `eshell-command-result' with a for loop.." |
e16582b6 GM |
80 | (let ((process-environment (cons "foo" process-environment))) |
81 | (should (equal (eshell-test-command-result | |
82 | "for foo in 5 { echo $foo }") 5)))) | |
ae5e4c48 SM |
83 | |
84 | (ert-deftest eshell-test/for-name-loop () ;Bug#15231 | |
1e53bb4b | 85 | "Test `eshell-command-result' with a for loop using `name'." |
e16582b6 GM |
86 | (let ((process-environment (cons "name" process-environment))) |
87 | (should (equal (eshell-test-command-result | |
88 | "for name in 3 { echo $name }") 3)))) | |
ae5e4c48 | 89 | |
1e53bb4b GM |
90 | (ert-deftest eshell-test/for-name-shadow-loop () ; bug#15372 |
91 | "Test `eshell-command-result' with a for loop using an env-var." | |
92 | (let ((process-environment (cons "name=env-value" process-environment))) | |
88302cbb | 93 | (with-temp-eshell |
0c2a2b57 GM |
94 | (eshell-command-result-p "echo $name; for name in 3 { echo $name }; echo $name" |
95 | "env-value\n3\nenv-value\n")))) | |
1e53bb4b | 96 | |
797c9e3d AG |
97 | (ert-deftest eshell-test/lisp-command-args () |
98 | "Test `eshell-command-result' with elisp and trailing args. | |
99 | Test that trailing arguments outside the S-expression are | |
100 | ignored. e.g. \"(+ 1 2) 3\" => 3" | |
6ad9cb08 | 101 | (should (equal (eshell-test-command-result "(+ 1 2) 3") 3))) |
81fb60b2 | 102 | |
797c9e3d AG |
103 | (ert-deftest eshell-test/subcommand () |
104 | "Test `eshell-command-result' with a simple subcommand." | |
6ad9cb08 | 105 | (should (equal (eshell-test-command-result "{+ 1 2}") 3))) |
797c9e3d AG |
106 | |
107 | (ert-deftest eshell-test/subcommand-args () | |
108 | "Test `eshell-command-result' with a subcommand and trailing args. | |
109 | Test that trailing arguments outside the subcommand are ignored. | |
110 | e.g. \"{+ 1 2} 3\" => 3" | |
6ad9cb08 | 111 | (should (equal (eshell-test-command-result "{+ 1 2} 3") 3))) |
797c9e3d AG |
112 | |
113 | (ert-deftest eshell-test/subcommand-lisp () | |
114 | "Test `eshell-command-result' with an elisp subcommand and trailing args. | |
115 | Test that trailing arguments outside the subcommand are ignored. | |
116 | e.g. \"{(+ 1 2)} 3\" => 3" | |
6ad9cb08 | 117 | (should (equal (eshell-test-command-result "{(+ 1 2)} 3") 3))) |
797c9e3d AG |
118 | |
119 | (ert-deftest eshell-test/interp-cmd () | |
81fb60b2 | 120 | "Interpolate command result" |
6ad9cb08 | 121 | (should (equal (eshell-test-command-result "+ ${+ 1 2} 3") 6))) |
81fb60b2 | 122 | |
797c9e3d | 123 | (ert-deftest eshell-test/interp-lisp () |
5eba16a3 | 124 | "Interpolate Lisp form evaluation" |
6ad9cb08 | 125 | (should (equal (eshell-test-command-result "+ $(+ 1 2) 3") 6))) |
81fb60b2 | 126 | |
797c9e3d | 127 | (ert-deftest eshell-test/interp-concat () |
81fb60b2 | 128 | "Interpolate and concat command" |
6ad9cb08 | 129 | (should (equal (eshell-test-command-result "+ ${+ 1 2}3 3") 36))) |
81fb60b2 | 130 | |
797c9e3d | 131 | (ert-deftest eshell-test/interp-concat-lisp () |
81fb60b2 | 132 | "Interpolate and concat Lisp form" |
6ad9cb08 | 133 | (should (equal (eshell-test-command-result "+ $(+ 1 2)3 3") 36))) |
81fb60b2 | 134 | |
797c9e3d | 135 | (ert-deftest eshell-test/interp-concat2 () |
81fb60b2 | 136 | "Interpolate and concat two commands" |
6ad9cb08 | 137 | (should (equal (eshell-test-command-result "+ ${+ 1 2}${+ 1 2} 3") 36))) |
81fb60b2 | 138 | |
797c9e3d | 139 | (ert-deftest eshell-test/interp-concat-lisp2 () |
81fb60b2 | 140 | "Interpolate and concat two Lisp forms" |
6ad9cb08 | 141 | (should (equal (eshell-test-command-result "+ $(+ 1 2)$(+ 1 2) 3") 36))) |
797c9e3d AG |
142 | |
143 | (ert-deftest eshell-test/window-height () | |
144 | "$LINES should equal (window-height)" | |
6ad9cb08 | 145 | (should (eshell-test-command-result "= $LINES (window-height)"))) |
797c9e3d AG |
146 | |
147 | (ert-deftest eshell-test/window-width () | |
148 | "$COLUMNS should equal (window-width)" | |
6ad9cb08 | 149 | (should (eshell-test-command-result "= $COLUMNS (window-width)"))) |
797c9e3d AG |
150 | |
151 | (ert-deftest eshell-test/last-result-var () | |
152 | "Test using the \"last result\" ($$) variable" | |
153 | (with-temp-eshell | |
0c2a2b57 GM |
154 | (eshell-command-result-p "+ 1 2; + $$ 2" |
155 | "3\n5\n"))) | |
797c9e3d AG |
156 | |
157 | (ert-deftest eshell-test/last-result-var2 () | |
158 | "Test using the \"last result\" ($$) variable twice" | |
159 | (with-temp-eshell | |
0c2a2b57 GM |
160 | (eshell-command-result-p "+ 1 2; + $$ $$" |
161 | "3\n6\n"))) | |
797c9e3d AG |
162 | |
163 | (ert-deftest eshell-test/last-arg-var () | |
164 | "Test using the \"last arg\" ($_) variable" | |
165 | (with-temp-eshell | |
0c2a2b57 GM |
166 | (eshell-command-result-p "+ 1 2; + $_ 4" |
167 | "3\n6\n"))) | |
797c9e3d AG |
168 | |
169 | (ert-deftest eshell-test/command-running-p () | |
170 | "Modeline should show no command running" | |
171 | (with-temp-eshell | |
172 | (let ((eshell-status-in-mode-line t)) | |
173 | (should (memq 'eshell-command-running-string mode-line-format)) | |
174 | (should (equal eshell-command-running-string "--"))))) | |
175 | ||
176 | (ert-deftest eshell-test/forward-arg () | |
177 | "Test moving across command arguments" | |
178 | (with-temp-eshell | |
179 | (eshell-insert-command "echo $(+ 1 (- 4 3)) \"alpha beta\" file" 'ignore) | |
180 | (let ((here (point)) begin valid) | |
181 | (eshell-bol) | |
182 | (setq begin (point)) | |
183 | (eshell-forward-argument 4) | |
184 | (setq valid (= here (point))) | |
185 | (eshell-backward-argument 4) | |
186 | (prog1 | |
187 | (and valid (= begin (point))) | |
188 | (eshell-bol) | |
189 | (delete-region (point) (point-max)))))) | |
190 | ||
191 | (ert-deftest eshell-test/queue-input () | |
192 | "Test queuing command input" | |
193 | (with-temp-eshell | |
194 | (eshell-insert-command "sleep 2") | |
195 | (eshell-insert-command "echo alpha" 'eshell-queue-input) | |
196 | (let ((count 10)) | |
197 | (while (and eshell-current-command | |
198 | (> count 0)) | |
199 | (sit-for 1) | |
200 | (setq count (1- count)))) | |
0c2a2b57 | 201 | (eshell-match-result "alpha\n"))) |
797c9e3d AG |
202 | |
203 | (ert-deftest eshell-test/flush-output () | |
204 | "Test flushing of previous output" | |
205 | (with-temp-eshell | |
206 | (eshell-insert-command "echo alpha") | |
207 | (eshell-kill-output) | |
0c2a2b57 | 208 | (eshell-match-result (regexp-quote "*** output flushed ***\n")) |
797c9e3d AG |
209 | (should (forward-line)) |
210 | (should (= (point) eshell-last-output-start)))) | |
211 | ||
212 | (ert-deftest eshell-test/run-old-command () | |
213 | "Re-run an old command" | |
214 | (with-temp-eshell | |
215 | (eshell-insert-command "echo alpha") | |
216 | (goto-char eshell-last-input-start) | |
217 | (string= (eshell-get-old-input) "echo alpha"))) | |
81fb60b2 | 218 | |
00c3ec76 GM |
219 | (provide 'esh-test) |
220 | ||
797c9e3d | 221 | ;;; tests/eshell.el ends here |