1 ;;; esh-test.el --- Eshell test suite
3 ;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008 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 3, 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 ;; The purpose of this module is to verify that Eshell works as
28 ;; expected. To run it on your system, use the command
38 (defgroup eshell-test nil
39 "This module is meant to ensure that Eshell is working correctly."
40 :tag
"Eshell test suite"
45 (defface eshell-test-ok
46 '((((class color
) (background light
)) (:foreground
"Green" :bold t
))
47 (((class color
) (background dark
)) (:foreground
"Green" :bold t
)))
48 "*The face used to highlight OK result strings."
50 ;; backward-compatibility alias
51 (put 'eshell-test-ok-face
'face-alias
'eshell-test-ok
)
53 (defface eshell-test-failed
54 '((((class color
) (background light
)) (:foreground
"OrangeRed" :bold t
))
55 (((class color
) (background dark
)) (:foreground
"OrangeRed" :bold t
))
57 "*The face used to highlight FAILED result strings."
59 ;; backward-compatibility alias
60 (put 'eshell-test-failed-face
'face-alias
'eshell-test-failed
)
62 (defcustom eshell-show-usage-metrics nil
63 "*If non-nil, display different usage metrics for each Eshell command."
64 :set
(lambda (symbol value
)
66 (add-hook 'eshell-mode-hook
'eshell-show-usage-metrics
)
67 (remove-hook 'eshell-mode-hook
'eshell-show-usage-metrics
))
69 :type
'(choice (const :tag
"No metrics" nil
)
70 (const :tag
"Cons cells consumed" t
)
71 (const :tag
"Time elapsed" 0))
79 (defun eshell-insert-command (text &optional func
)
80 "Insert a command at the end of the buffer."
81 (goto-char eshell-last-output-end
)
82 (insert-and-inherit text
)
83 (funcall (or func
'eshell-send-input
)))
85 (defun eshell-match-result (regexp)
86 "Insert a command at the end of the buffer."
87 (goto-char eshell-last-input-end
)
90 (defun eshell-command-result-p (text regexp
&optional func
)
91 "Insert a command at the end of the buffer."
92 (eshell-insert-command text func
)
93 (eshell-match-result regexp
))
95 (defvar eshell-test-failures nil
)
97 (defun eshell-run-test (module funcsym label command
)
98 "Test whether FORM evaluates to a non-nil value."
99 (when (let ((sym (intern-soft (concat "eshell-" (symbol-name module
)))))
100 (or (memq sym
(eshell-subgroups 'eshell
))
101 (eshell-using-module sym
)))
102 (with-current-buffer test-buffer
103 (insert-before-markers
104 (format "%-70s " (substring label
0 (min 70 (length label
)))))
105 (insert-before-markers " ....")
107 (let ((truth (eval command
)))
108 (with-current-buffer test-buffer
109 (delete-backward-char 6)
110 (insert-before-markers
115 (put-text-property 0 6 'face
'eshell-test-ok str
))
117 (setq eshell-test-failures
(1+ eshell-test-failures
))
118 (put-text-property 0 6 'face
'eshell-test-failed str
))
120 (add-text-properties (line-beginning-position) (point)
121 (list 'test-func funcsym
))
122 (eshell-redisplay)))))
124 (defun eshell-test-goto-func ()
125 "Jump to the function that defines a particular test."
127 (let ((fsym (get-text-property (point) 'test-func
)))
129 (let* ((def (symbol-function fsym
))
130 (library (locate-library (symbol-file fsym
'defun
)))
131 (name (substring (symbol-name fsym
)
132 (length "eshell-test--")))
133 (inhibit-redisplay t
))
135 (goto-char (point-min))
136 (re-search-forward (concat "^(eshell-deftest\\s-+\\w+\\s-+"
138 (beginning-of-line)))))
140 (defun eshell-run-one-test (&optional arg
)
141 "Jump to the function that defines a particular test."
143 (let ((fsym (get-text-property (point) 'test-func
)))
146 (delete-region (point) (line-end-position))
147 (let ((test-buffer (current-buffer)))
148 (set-buffer (let ((inhibit-redisplay t
))
149 (save-window-excursion (eshell t
))))
152 (kill-buffer (current-buffer)))))))
155 (defun eshell-test (&optional arg
)
156 "Test Eshell to verify that it works as expected."
158 (let* ((begin (eshell-time-to-seconds (current-time)))
159 (test-buffer (get-buffer-create "*eshell test*")))
160 (set-buffer (let ((inhibit-redisplay t
))
161 (save-window-excursion (eshell t
))))
162 (with-current-buffer test-buffer
164 (setq major-mode
'eshell-test-mode
)
165 (setq mode-name
"EShell Test")
166 (set (make-local-variable 'eshell-test-failures
) 0)
167 (local-set-key [(control ?c
) (control ?c
)] 'eshell-test-goto-func
)
168 (local-set-key [(control ?c
) (control ?r
)] 'eshell-run-one-test
)
169 (local-set-key [(control ?m
)] 'eshell-test-goto-func
)
170 (local-set-key [return] 'eshell-test-goto-func)
172 (insert "Testing Eshell under " (emacs-version))
173 (switch-to-buffer test-buffer)
174 (delete-other-windows))
175 (eshell-for funcname (sort (all-completions "eshell-test--"
178 (with-current-buffer test-buffer
180 (funcall (intern-soft funcname)))
181 (with-current-buffer test-buffer
182 (insert (format "\n\n--- %s --- (completed in %d seconds)\n"
183 (current-time-string)
184 (- (eshell-time-to-seconds (current-time))
186 (message "Eshell test suite completed: %s failure%s"
187 (if (> eshell-test-failures 0)
188 (number-to-string eshell-test-failures)
190 (if (= eshell-test-failures 1) "" "s"))))
191 (goto-char eshell-last-output-end)
193 (kill-buffer (current-buffer))))
196 (defvar eshell-metric-before-command 0)
197 (defvar eshell-metric-after-command 0)
199 (defun eshell-show-usage-metrics ()
200 "If run at Eshell mode startup, metrics are shown after each command."
201 (set (make-local-variable 'eshell-metric-before-command)
202 (if (eq eshell-show-usage-metrics t)
205 (set (make-local-variable 'eshell-metric-after-command)
206 (if (eq eshell-show-usage-metrics t)
210 (add-hook 'eshell-pre-command-hook
213 (setq eshell-metric-before-command
214 (if (eq eshell-show-usage-metrics t)
215 (car (memory-use-counts))
216 (current-time))))) nil t)
218 (add-hook 'eshell-post-command-hook
221 (setq eshell-metric-after-command
222 (if (eq eshell-show-usage-metrics t)
223 (car (memory-use-counts))
225 (eshell-interactive-print
228 (if (eq eshell-show-usage-metrics t)
229 (- eshell-metric-after-command
230 eshell-metric-before-command 7)
231 (- (eshell-time-to-seconds
232 eshell-metric-after-command)
233 (eshell-time-to-seconds
234 eshell-metric-before-command))))
240 ;;; arch-tag: 6e32275a-8285-4a4e-b7cf-819aa7c86b8e
241 ;;; esh-test.el ends here