1 ;;; esh-test.el --- Eshell test suite
3 ;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007 Free Software Foundation, Inc.
6 ;; Author: John Wiegley <johnw@gnu.org>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, USA.
27 (eval-when-compile (require 'esh-maint))
29 (defgroup eshell-test nil
30 "This module is meant to ensure that Eshell is working correctly."
31 :tag "Eshell test suite"
36 ;; The purpose of this module is to verify that Eshell works as
37 ;; expected. To run it on your system, use the command
46 (defface eshell-test-ok
47 '((((class color) (background light)) (:foreground "Green" :bold t))
48 (((class color) (background dark)) (:foreground "Green" :bold t)))
49 "*The face used to highlight OK result strings."
51 ;; backward-compatibility alias
52 (put 'eshell-test-ok-face 'face-alias 'eshell-test-ok)
54 (defface eshell-test-failed
55 '((((class color) (background light)) (:foreground "OrangeRed" :bold t))
56 (((class color) (background dark)) (:foreground "OrangeRed" :bold t))
58 "*The face used to highlight FAILED result strings."
60 ;; backward-compatibility alias
61 (put 'eshell-test-failed-face 'face-alias 'eshell-test-failed)
63 (defcustom eshell-show-usage-metrics nil
64 "*If non-nil, display different usage metrics for each Eshell command."
65 :set (lambda (symbol value)
67 (add-hook 'eshell-mode-hook 'eshell-show-usage-metrics)
68 (remove-hook 'eshell-mode-hook 'eshell-show-usage-metrics))
70 :type '(choice (const :tag "No metrics" nil)
71 (const :tag "Cons cells consumed" t)
72 (const :tag "Time elapsed" 0))
80 (defun eshell-insert-command (text &optional func)
81 "Insert a command at the end of the buffer."
82 (goto-char eshell-last-output-end)
83 (insert-and-inherit text)
84 (funcall (or func 'eshell-send-input)))
86 (defun eshell-match-result (regexp)
87 "Insert a command at the end of the buffer."
88 (goto-char eshell-last-input-end)
91 (defun eshell-command-result-p (text regexp &optional func)
92 "Insert a command at the end of the buffer."
93 (eshell-insert-command text func)
94 (eshell-match-result regexp))
96 (defvar eshell-test-failures nil)
98 (defun eshell-run-test (module funcsym label command)
99 "Test whether FORM evaluates to a non-nil value."
100 (when (let ((sym (intern-soft (concat "eshell-" (symbol-name module)))))
101 (or (memq sym (eshell-subgroups 'eshell))
102 (eshell-using-module sym)))
103 (with-current-buffer test-buffer
104 (insert-before-markers
105 (format "%-70s " (substring label 0 (min 70 (length label)))))
106 (insert-before-markers " ....")
108 (let ((truth (eval command)))
109 (with-current-buffer test-buffer
110 (delete-backward-char 6)
111 (insert-before-markers
116 (put-text-property 0 6 'face 'eshell-test-ok str))
118 (setq eshell-test-failures (1+ eshell-test-failures))
119 (put-text-property 0 6 'face 'eshell-test-failed str))
121 (add-text-properties (line-beginning-position) (point)
122 (list 'test-func funcsym))
123 (eshell-redisplay)))))
125 (defun eshell-test-goto-func ()
126 "Jump to the function that defines a particular test."
128 (let ((fsym (get-text-property (point) 'test-func)))
130 (let* ((def (symbol-function fsym))
131 ;;; #### XEmacs; 21.4 doesn't accept the second argument.
132 (library (locate-library (symbol-file fsym 'defun)))
133 (name (substring (symbol-name fsym)
134 (length "eshell-test--")))
135 (inhibit-redisplay t))
137 (goto-char (point-min))
138 (re-search-forward (concat "^(eshell-deftest\\s-+\\w+\\s-+"
140 (beginning-of-line)))))
142 (defun eshell-run-one-test (&optional arg)
143 "Jump to the function that defines a particular test."
145 (let ((fsym (get-text-property (point) 'test-func)))
148 (delete-region (point) (line-end-position))
149 (let ((test-buffer (current-buffer)))
150 (set-buffer (let ((inhibit-redisplay t))
151 (save-window-excursion (eshell t))))
154 (kill-buffer (current-buffer)))))))
157 (defun eshell-test (&optional arg)
158 "Test Eshell to verify that it works as expected."
160 (let* ((begin (eshell-time-to-seconds (current-time)))
161 (test-buffer (get-buffer-create "*eshell test*")))
162 (set-buffer (let ((inhibit-redisplay t))
163 (save-window-excursion (eshell t))))
164 (with-current-buffer test-buffer
166 (setq major-mode 'eshell-test-mode)
167 (setq mode-name "EShell Test")
168 (set (make-local-variable 'eshell-test-failures) 0)
169 (local-set-key [(control ?c) (control ?c)] 'eshell-test-goto-func)
170 (local-set-key [(control ?c) (control ?r)] 'eshell-run-one-test)
171 (local-set-key [(control ?m)] 'eshell-test-goto-func)
172 (local-set-key [return] 'eshell-test-goto-func)
174 (insert "Testing Eshell under " (emacs-version))
175 (switch-to-buffer test-buffer)
176 (delete-other-windows))
177 (eshell-for funcname (sort (all-completions "eshell-test--"
180 (with-current-buffer test-buffer
182 (funcall (intern-soft funcname)))
183 (with-current-buffer test-buffer
184 (insert (format "\n\n--- %s --- (completed in %d seconds)\n"
185 (current-time-string)
186 (- (eshell-time-to-seconds (current-time))
188 (message "Eshell test suite completed: %s failure%s"
189 (if (> eshell-test-failures 0)
190 (number-to-string eshell-test-failures)
192 (if (= eshell-test-failures 1) "" "s"))))
193 (goto-char eshell-last-output-end)
195 (kill-buffer (current-buffer))))
198 (defvar eshell-metric-before-command 0)
199 (defvar eshell-metric-after-command 0)
201 (defun eshell-show-usage-metrics ()
202 "If run at Eshell mode startup, metrics are shown after each command."
203 (set (make-local-variable 'eshell-metric-before-command)
204 (if (eq eshell-show-usage-metrics t)
207 (set (make-local-variable 'eshell-metric-after-command)
208 (if (eq eshell-show-usage-metrics t)
212 (make-local-hook 'eshell-pre-command-hook)
213 (add-hook 'eshell-pre-command-hook
216 (setq eshell-metric-before-command
217 (if (eq eshell-show-usage-metrics t)
218 ;;; #### XEmacs; we don't support this.
219 (car (memory-use-counts))
220 (current-time))))) nil t)
222 (make-local-hook 'eshell-post-command-hook)
223 (add-hook 'eshell-post-command-hook
226 (setq eshell-metric-after-command
227 (if (eq eshell-show-usage-metrics t)
228 (car (memory-use-counts))
230 (eshell-interactive-print
233 (if (eq eshell-show-usage-metrics t)
234 (- eshell-metric-after-command
235 eshell-metric-before-command 7)
236 (- (eshell-time-to-seconds
237 eshell-metric-after-command)
238 (eshell-time-to-seconds
239 eshell-metric-before-command))))
243 ;;; esh-test.el ends here