* test/automated/eshell.el (with-temp-eshell): Use a temp directory
[bpt/emacs.git] / test / automated / eshell.el
CommitLineData
797c9e3d 1;;; tests/eshell.el --- Eshell test suite
affbf647 2
ab422c4d 3;; Copyright (C) 1999-2013 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)
50 "Insert a command at the end of the buffer."
51 (goto-char eshell-last-input-end)
52 (looking-at regexp))
53
54(defun eshell-command-result-p (text regexp &optional func)
55 "Insert a command at the end of the buffer."
56 (eshell-insert-command text func)
57 (eshell-match-result regexp))
58
797c9e3d
AG
59;;; Tests:
60
61(ert-deftest eshell-test/simple-command-result ()
62 "Test `eshell-command-result' with a simple command."
63 (should (equal (eshell-command-result "+ 1 2") 3)))
81fb60b2 64
797c9e3d
AG
65(ert-deftest eshell-test/lisp-command ()
66 "Test `eshell-command-result' with an elisp command."
67 (should (equal (eshell-command-result "(+ 1 2)") 3)))
81fb60b2 68
ae5e4c48
SM
69(ert-deftest eshell-test/for-loop ()
70 "Test `eshell-command-result' with an elisp command."
71 (should (equal (eshell-command-result "for foo in 5 { echo $foo }") 5)))
72
73(ert-deftest eshell-test/for-name-loop () ;Bug#15231
74 "Test `eshell-command-result' with an elisp command."
75 (should (equal (eshell-command-result "for name in 3 { echo $name }") 3)))
76
797c9e3d
AG
77(ert-deftest eshell-test/lisp-command-args ()
78 "Test `eshell-command-result' with elisp and trailing args.
79Test that trailing arguments outside the S-expression are
80ignored. e.g. \"(+ 1 2) 3\" => 3"
81 (should (equal (eshell-command-result "(+ 1 2) 3") 3)))
81fb60b2 82
797c9e3d
AG
83(ert-deftest eshell-test/subcommand ()
84 "Test `eshell-command-result' with a simple subcommand."
85 (should (equal (eshell-command-result "{+ 1 2}") 3)))
86
87(ert-deftest eshell-test/subcommand-args ()
88 "Test `eshell-command-result' with a subcommand and trailing args.
89Test that trailing arguments outside the subcommand are ignored.
90e.g. \"{+ 1 2} 3\" => 3"
91 (should (equal (eshell-command-result "{+ 1 2} 3") 3)))
92
93(ert-deftest eshell-test/subcommand-lisp ()
94 "Test `eshell-command-result' with an elisp subcommand and trailing args.
95Test that trailing arguments outside the subcommand are ignored.
96e.g. \"{(+ 1 2)} 3\" => 3"
97 (should (equal (eshell-command-result "{(+ 1 2)} 3") 3)))
98
99(ert-deftest eshell-test/interp-cmd ()
81fb60b2 100 "Interpolate command result"
797c9e3d 101 (should (equal (eshell-command-result "+ ${+ 1 2} 3") 6)))
81fb60b2 102
797c9e3d 103(ert-deftest eshell-test/interp-lisp ()
5eba16a3 104 "Interpolate Lisp form evaluation"
797c9e3d 105 (should (equal (eshell-command-result "+ $(+ 1 2) 3") 6)))
81fb60b2 106
797c9e3d 107(ert-deftest eshell-test/interp-concat ()
81fb60b2 108 "Interpolate and concat command"
797c9e3d 109 (should (equal (eshell-command-result "+ ${+ 1 2}3 3") 36)))
81fb60b2 110
797c9e3d 111(ert-deftest eshell-test/interp-concat-lisp ()
81fb60b2 112 "Interpolate and concat Lisp form"
797c9e3d 113 (should (equal (eshell-command-result "+ $(+ 1 2)3 3") 36)))
81fb60b2 114
797c9e3d 115(ert-deftest eshell-test/interp-concat2 ()
81fb60b2 116 "Interpolate and concat two commands"
797c9e3d 117 (should (equal (eshell-command-result "+ ${+ 1 2}${+ 1 2} 3") 36)))
81fb60b2 118
797c9e3d 119(ert-deftest eshell-test/interp-concat-lisp2 ()
81fb60b2 120 "Interpolate and concat two Lisp forms"
797c9e3d
AG
121 (should (equal (eshell-command-result "+ $(+ 1 2)$(+ 1 2) 3") 36)))
122
123(ert-deftest eshell-test/window-height ()
124 "$LINES should equal (window-height)"
125 (should (eshell-command-result "= $LINES (window-height)")))
126
127(ert-deftest eshell-test/window-width ()
128 "$COLUMNS should equal (window-width)"
129 (should (eshell-command-result "= $COLUMNS (window-width)")))
130
131(ert-deftest eshell-test/last-result-var ()
132 "Test using the \"last result\" ($$) variable"
133 (with-temp-eshell
134 (should
135 (eshell-command-result-p "+ 1 2; + $$ 2"
136 "3\n5\n"))))
137
138(ert-deftest eshell-test/last-result-var2 ()
139 "Test using the \"last result\" ($$) variable twice"
140 (with-temp-eshell
141 (should
142 (eshell-command-result-p "+ 1 2; + $$ $$"
143 "3\n6\n"))))
144
145(ert-deftest eshell-test/last-arg-var ()
146 "Test using the \"last arg\" ($_) variable"
147 (with-temp-eshell
148 (should
149 (eshell-command-result-p "+ 1 2; + $_ 4"
150 "3\n6\n"))))
151
152(ert-deftest eshell-test/command-running-p ()
153 "Modeline should show no command running"
154 (with-temp-eshell
155 (let ((eshell-status-in-mode-line t))
156 (should (memq 'eshell-command-running-string mode-line-format))
157 (should (equal eshell-command-running-string "--")))))
158
159(ert-deftest eshell-test/forward-arg ()
160 "Test moving across command arguments"
161 (with-temp-eshell
162 (eshell-insert-command "echo $(+ 1 (- 4 3)) \"alpha beta\" file" 'ignore)
163 (let ((here (point)) begin valid)
164 (eshell-bol)
165 (setq begin (point))
166 (eshell-forward-argument 4)
167 (setq valid (= here (point)))
168 (eshell-backward-argument 4)
169 (prog1
170 (and valid (= begin (point)))
171 (eshell-bol)
172 (delete-region (point) (point-max))))))
173
174(ert-deftest eshell-test/queue-input ()
175 "Test queuing command input"
176 (with-temp-eshell
177 (eshell-insert-command "sleep 2")
178 (eshell-insert-command "echo alpha" 'eshell-queue-input)
179 (let ((count 10))
180 (while (and eshell-current-command
181 (> count 0))
182 (sit-for 1)
183 (setq count (1- count))))
184 (should (eshell-match-result "alpha\n"))))
185
186(ert-deftest eshell-test/flush-output ()
187 "Test flushing of previous output"
188 (with-temp-eshell
189 (eshell-insert-command "echo alpha")
190 (eshell-kill-output)
191 (should (eshell-match-result (regexp-quote "*** output flushed ***\n")))
192 (should (forward-line))
193 (should (= (point) eshell-last-output-start))))
194
195(ert-deftest eshell-test/run-old-command ()
196 "Re-run an old command"
197 (with-temp-eshell
198 (eshell-insert-command "echo alpha")
199 (goto-char eshell-last-input-start)
200 (string= (eshell-get-old-input) "echo alpha")))
81fb60b2 201
00c3ec76
GM
202(provide 'esh-test)
203
797c9e3d 204;;; tests/eshell.el ends here