1 ;;; esh-test.el --- Eshell test suite
3 ;; Copyright (C) 1999, 2000 Free Software Foundation
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 2, or (at your option)
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; see the file COPYING. If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
26 (eval-when-compile (require 'esh-maint
))
28 (defgroup eshell-test nil
29 "This module is meant to ensure that Eshell is working correctly."
30 :tag
"Eshell test suite"
35 ;; The purpose of this module is to verify that Eshell works as
36 ;; expected. To run it on your system, use the command
45 (defface eshell-test-ok-face
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."
51 (defface eshell-test-failed-face
52 '((((class color
) (background light
)) (:foreground
"OrangeRed" :bold t
))
53 (((class color
) (background dark
)) (:foreground
"OrangeRed" :bold t
))
55 "*The face used to highlight FAILED result strings."
58 (defcustom eshell-show-usage-metrics nil
59 "*If non-nil, display different usage metrics for each Eshell command."
60 :set
(lambda (symbol value
)
62 (add-hook 'eshell-mode-hook
'eshell-show-usage-metrics
)
63 (remove-hook 'eshell-mode-hook
'eshell-show-usage-metrics
))
65 :type
'(choice (const :tag
"No metrics" nil
)
66 (const :tag
"Cons cells consumed" t
)
67 (const :tag
"Time elapsed" 0))
75 (defun eshell-insert-command (text &optional func
)
76 "Insert a command at the end of the buffer."
77 (goto-char eshell-last-output-end
)
78 (insert-and-inherit text
)
79 (funcall (or func
'eshell-send-input
)))
81 (defun eshell-match-result (regexp)
82 "Insert a command at the end of the buffer."
83 (goto-char eshell-last-input-end
)
86 (defun eshell-command-result-p (text regexp
&optional func
)
87 "Insert a command at the end of the buffer."
88 (eshell-insert-command text func
)
89 (eshell-match-result regexp
))
91 (defvar eshell-test-failures nil
)
93 (defun eshell-run-test (module funcsym label command
)
94 "Test whether FORM evaluates to a non-nil value."
95 (when (let ((sym (intern-soft (concat "eshell-" (symbol-name module
)))))
96 (or (memq sym
(eshell-subgroups 'eshell
))
97 (eshell-using-module sym
)))
98 (with-current-buffer test-buffer
99 (insert-before-markers
100 (format "%-70s " (substring label
0 (min 70 (length label
)))))
101 (insert-before-markers " ....")
103 (let ((truth (eval command
)))
104 (with-current-buffer test-buffer
105 (delete-backward-char 6)
106 (insert-before-markers
111 (put-text-property 0 6 'face
112 'eshell-test-ok-face str
))
114 (setq eshell-test-failures
(1+ eshell-test-failures
))
115 (put-text-property 0 6 'face
116 'eshell-test-failed-face str
))
118 (add-text-properties (line-beginning-position) (point)
119 (list 'test-func funcsym
))
120 (eshell-redisplay)))))
122 (defun eshell-test-goto-func ()
123 "Jump to the function that defines a particular test."
125 (let ((fsym (get-text-property (point) 'test-func
)))
127 (let* ((def (symbol-function fsym
))
128 (library (locate-library (symbol-file fsym
)))
129 (name (substring (symbol-name fsym
)
130 (length "eshell-test--")))
131 (inhibit-redisplay t
))
133 (goto-char (point-min))
134 (re-search-forward (concat "^(eshell-deftest\\s-+\\w+\\s-+"
136 (beginning-of-line)))))
138 (defun eshell-run-one-test (&optional arg
)
139 "Jump to the function that defines a particular test."
141 (let ((fsym (get-text-property (point) 'test-func
)))
144 (delete-region (point) (line-end-position))
145 (let ((test-buffer (current-buffer)))
146 (set-buffer (let ((inhibit-redisplay t
))
147 (save-window-excursion (eshell t
))))
150 (kill-buffer (current-buffer)))))))
153 (defun eshell-test (&optional arg
)
154 "Test Eshell to verify that it works as expected."
156 (let* ((begin (eshell-time-to-seconds (current-time)))
157 (test-buffer (get-buffer-create "*eshell test*")))
158 (set-buffer (let ((inhibit-redisplay t
))
159 (save-window-excursion (eshell t
))))
160 (with-current-buffer test-buffer
162 (setq major-mode
'eshell-test-mode
)
163 (setq mode-name
"EShell Test")
164 (set (make-local-variable 'eshell-test-failures
) 0)
165 (local-set-key [(control ?c
) (control ?c
)] 'eshell-test-goto-func
)
166 (local-set-key [(control ?c
) (control ?r
)] 'eshell-run-one-test
)
167 (local-set-key [(control ?m
)] 'eshell-test-goto-func
)
168 (local-set-key [return] 'eshell-test-goto-func)
170 (insert "Testing Eshell under "
171 (format "GNU Emacs %s (%s%s)"
174 (cond ((featurep 'motif) ", Motif")
175 ((featurep 'x-toolkit) ", X toolkit")
177 (switch-to-buffer test-buffer)
178 (delete-other-windows))
179 (eshell-for funcname (sort (all-completions "eshell-test--"
182 (with-current-buffer test-buffer
184 (funcall (intern-soft funcname)))
185 (with-current-buffer test-buffer
186 (insert (format "\n\n--- %s --- (completed in %d seconds)\n"
187 (current-time-string)
188 (- (eshell-time-to-seconds (current-time))
190 (message "Eshell test suite completed: %s failure%s"
191 (if (> eshell-test-failures 0)
192 (number-to-string eshell-test-failures)
194 (if (= eshell-test-failures 1) "" "s"))))
195 (goto-char eshell-last-output-end)
197 (kill-buffer (current-buffer))))
200 (defvar eshell-metric-before-command 0)
201 (defvar eshell-metric-after-command 0)
203 (defun eshell-show-usage-metrics ()
204 "If run at Eshell mode startup, metrics are shown after each command."
205 (set (make-local-variable 'eshell-metric-before-command)
206 (if (eq eshell-show-usage-metrics t)
209 (set (make-local-variable 'eshell-metric-after-command)
210 (if (eq eshell-show-usage-metrics t)
214 (add-hook 'eshell-pre-command-hook
217 (setq eshell-metric-before-command
218 (if (eq eshell-show-usage-metrics t)
219 (car (memory-use-counts))
220 (current-time))))) nil t)
222 (add-hook 'eshell-post-command-hook
225 (setq eshell-metric-after-command
226 (if (eq eshell-show-usage-metrics t)
227 (car (memory-use-counts))
229 (eshell-interactive-print
232 (if (eq eshell-show-usage-metrics t)
233 (- eshell-metric-after-command
234 eshell-metric-before-command 7)
235 (- (eshell-time-to-seconds
236 eshell-metric-after-command)
237 (eshell-time-to-seconds
238 eshell-metric-before-command))))
242 ;;; esh-test.el ends here