Update copyright year to 2014 by running admin/update-copyright.
[emacs.git] / test / automated / eshell.el
blob82566a14a4e154cf043115724564482457906edc
1 ;;; tests/eshell.el --- Eshell test suite
3 ;; Copyright (C) 1999-2014 Free Software Foundation, Inc.
5 ;; Author: John Wiegley <johnw@gnu.org>
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software: you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
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.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
22 ;;; Commentary:
24 ;; Eshell test suite.
26 ;;; Code:
28 (require 'ert)
29 (require 'eshell)
31 (defmacro with-temp-eshell (&rest body)
32 "Evaluate BODY in a temporary Eshell buffer."
33 `(let* ((eshell-directory-name (make-temp-file "eshell" t))
34 (eshell-history-file-name nil)
35 (eshell-buffer (eshell t)))
36 (unwind-protect
37 (with-current-buffer eshell-buffer
38 ,@body)
39 (let (kill-buffer-query-functions)
40 (kill-buffer eshell-buffer)
41 (delete-directory eshell-directory-name t)))))
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)))
49 (defun eshell-match-result (regexp)
50 "Check that text after `eshell-last-input-end' matches REGEXP."
51 (goto-char eshell-last-input-end)
52 (should (string-match-p regexp (buffer-substring-no-properties
53 (point) (point-max)))))
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))
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))
64 (unwind-protect
65 (eshell-command-result command)
66 (delete-directory eshell-directory-name t))))
68 ;;; Tests:
70 (ert-deftest eshell-test/simple-command-result ()
71 "Test `eshell-command-result' with a simple command."
72 (should (equal (eshell-test-command-result "+ 1 2") 3)))
74 (ert-deftest eshell-test/lisp-command ()
75 "Test `eshell-command-result' with an elisp command."
76 (should (equal (eshell-test-command-result "(+ 1 2)") 3)))
78 (ert-deftest eshell-test/for-loop ()
79 "Test `eshell-command-result' with a for loop.."
80 (let ((process-environment (cons "foo" process-environment)))
81 (should (equal (eshell-test-command-result
82 "for foo in 5 { echo $foo }") 5))))
84 (ert-deftest eshell-test/for-name-loop () ;Bug#15231
85 "Test `eshell-command-result' with a for loop using `name'."
86 (let ((process-environment (cons "name" process-environment)))
87 (should (equal (eshell-test-command-result
88 "for name in 3 { echo $name }") 3))))
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)))
93 (with-temp-eshell
94 (eshell-command-result-p "echo $name; for name in 3 { echo $name }; echo $name"
95 "env-value\n3\nenv-value\n"))))
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"
101 (should (equal (eshell-test-command-result "(+ 1 2) 3") 3)))
103 (ert-deftest eshell-test/subcommand ()
104 "Test `eshell-command-result' with a simple subcommand."
105 (should (equal (eshell-test-command-result "{+ 1 2}") 3)))
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"
111 (should (equal (eshell-test-command-result "{+ 1 2} 3") 3)))
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"
117 (should (equal (eshell-test-command-result "{(+ 1 2)} 3") 3)))
119 (ert-deftest eshell-test/interp-cmd ()
120 "Interpolate command result"
121 (should (equal (eshell-test-command-result "+ ${+ 1 2} 3") 6)))
123 (ert-deftest eshell-test/interp-lisp ()
124 "Interpolate Lisp form evaluation"
125 (should (equal (eshell-test-command-result "+ $(+ 1 2) 3") 6)))
127 (ert-deftest eshell-test/interp-concat ()
128 "Interpolate and concat command"
129 (should (equal (eshell-test-command-result "+ ${+ 1 2}3 3") 36)))
131 (ert-deftest eshell-test/interp-concat-lisp ()
132 "Interpolate and concat Lisp form"
133 (should (equal (eshell-test-command-result "+ $(+ 1 2)3 3") 36)))
135 (ert-deftest eshell-test/interp-concat2 ()
136 "Interpolate and concat two commands"
137 (should (equal (eshell-test-command-result "+ ${+ 1 2}${+ 1 2} 3") 36)))
139 (ert-deftest eshell-test/interp-concat-lisp2 ()
140 "Interpolate and concat two Lisp forms"
141 (should (equal (eshell-test-command-result "+ $(+ 1 2)$(+ 1 2) 3") 36)))
143 (ert-deftest eshell-test/window-height ()
144 "$LINES should equal (window-height)"
145 (should (eshell-test-command-result "= $LINES (window-height)")))
147 (ert-deftest eshell-test/window-width ()
148 "$COLUMNS should equal (window-width)"
149 (should (eshell-test-command-result "= $COLUMNS (window-width)")))
151 (ert-deftest eshell-test/last-result-var ()
152 "Test using the \"last result\" ($$) variable"
153 (with-temp-eshell
154 (eshell-command-result-p "+ 1 2; + $$ 2"
155 "3\n5\n")))
157 (ert-deftest eshell-test/last-result-var2 ()
158 "Test using the \"last result\" ($$) variable twice"
159 (with-temp-eshell
160 (eshell-command-result-p "+ 1 2; + $$ $$"
161 "3\n6\n")))
163 (ert-deftest eshell-test/last-arg-var ()
164 "Test using the \"last arg\" ($_) variable"
165 (with-temp-eshell
166 (eshell-command-result-p "+ 1 2; + $_ 4"
167 "3\n6\n")))
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 "--")))))
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))))))
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))))
201 (eshell-match-result "alpha\n")))
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)
208 (eshell-match-result (regexp-quote "*** output flushed ***\n"))
209 (should (forward-line))
210 (should (= (point) eshell-last-output-start))))
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")))
219 (provide 'esh-test)
221 ;;; tests/eshell.el ends here