1 ;;; esh-test --- Eshell test suite
3 ;; Copyright (C) 1999, 2000 Free Software Foundation
5 ;; This file is part of GNU Emacs.
7 ;; GNU Emacs is free software; you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; GNU Emacs is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;; GNU General Public License for more details.
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with GNU Emacs; see the file COPYING. If not, write to the
19 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 ;; Boston, MA 02111-1307, USA.
24 (eval-when-compile (require 'esh-maint))
26 (defgroup eshell-test nil
27 "This module is meant to ensure that Eshell is working correctly."
28 :tag "Eshell test suite"
33 ;; The purpose of this module is to verify that Eshell works as
34 ;; expected. To run it on your system, use the command
43 (defface eshell-test-ok-face
44 '((((class color) (background light)) (:foreground "Green" :bold t))
45 (((class color) (background dark)) (:foreground "Green" :bold t)))
46 "*The face used to highlight OK result strings."
49 (defface eshell-test-failed-face
50 '((((class color) (background light)) (:foreground "OrangeRed" :bold t))
51 (((class color) (background dark)) (:foreground "OrangeRed" :bold t))
53 "*The face used to highlight FAILED result strings."
56 (defcustom eshell-show-usage-metrics nil
57 "*If non-nil, display different usage metrics for each Eshell command."
58 :set (lambda (symbol value)
60 (add-hook 'eshell-mode-hook 'eshell-show-usage-metrics)
61 (remove-hook 'eshell-mode-hook 'eshell-show-usage-metrics))
63 :type '(choice (const :tag "No metrics" nil)
64 (const :tag "Cons cells consumed" t)
65 (const :tag "Time elapsed" 0))
73 (defun eshell-insert-command (text &optional func)
74 "Insert a command at the end of the buffer."
75 (goto-char eshell-last-output-end)
76 (insert-and-inherit text)
77 (funcall (or func 'eshell-send-input)))
79 (defun eshell-match-result (regexp)
80 "Insert a command at the end of the buffer."
81 (goto-char eshell-last-input-end)
84 (defun eshell-command-result-p (text regexp &optional func)
85 "Insert a command at the end of the buffer."
86 (eshell-insert-command text func)
87 (eshell-match-result regexp))
89 (defvar eshell-test-failures nil)
91 (defun eshell-run-test (module funcsym label command)
92 "Test whether FORM evaluates to a non-nil value."
93 (when (let ((sym (intern-soft (concat "eshell-" (symbol-name module)))))
94 (or (memq sym (eshell-subgroups 'eshell))
95 (eshell-using-module sym)))
96 (with-current-buffer test-buffer
97 (insert-before-markers
98 (format "%-70s " (substring label 0 (min 70 (length label)))))
99 (insert-before-markers " ....")
101 (let ((truth (eval command)))
102 (with-current-buffer test-buffer
103 (delete-backward-char 6)
104 (insert-before-markers
109 (put-text-property 0 6 'face
110 'eshell-test-ok-face str))
112 (setq eshell-test-failures (1+ eshell-test-failures))
113 (put-text-property 0 6 'face
114 'eshell-test-failed-face str))
116 (add-text-properties (line-beginning-position) (point)
117 (list 'test-func funcsym))
118 (eshell-redisplay)))))
120 (defun eshell-test-goto-func ()
121 "Jump to the function that defines a particular test."
123 (let ((fsym (get-text-property (point) 'test-func)))
125 (let* ((def (symbol-function fsym))
126 (library (locate-library (symbol-file fsym)))
127 (name (substring (symbol-name fsym)
128 (length "eshell-test--")))
129 (inhibit-redisplay t))
131 (goto-char (point-min))
132 (re-search-forward (concat "^(eshell-deftest\\s-+\\w+\\s-+"
134 (beginning-of-line)))))
136 (defun eshell-run-one-test (&optional arg)
137 "Jump to the function that defines a particular test."
139 (let ((fsym (get-text-property (point) 'test-func)))
142 (delete-region (point) (line-end-position))
143 (let ((test-buffer (current-buffer)))
144 (set-buffer (let ((inhibit-redisplay t))
145 (save-window-excursion (eshell t))))
148 (kill-buffer (current-buffer)))))))
151 (defun eshell-test (&optional arg)
152 "Test Eshell to verify that it works as expected."
154 (let* ((begin (eshell-time-to-seconds (current-time)))
155 (test-buffer (get-buffer-create "*eshell test*")))
156 (set-buffer (let ((inhibit-redisplay t))
157 (save-window-excursion (eshell t))))
158 (with-current-buffer test-buffer
160 (setq major-mode 'eshell-test-mode)
161 (setq mode-name "EShell Test")
162 (set (make-local-variable 'eshell-test-failures) 0)
163 (local-set-key [(control ?c) (control ?c)] 'eshell-test-goto-func)
164 (local-set-key [(control ?c) (control ?r)] 'eshell-run-one-test)
165 (local-set-key [(control ?m)] 'eshell-test-goto-func)
166 (local-set-key [return] 'eshell-test-goto-func)
168 (insert "Testing Eshell under "
169 (format "GNU Emacs %s (%s%s)"
172 (cond ((featurep 'motif) ", Motif")
173 ((featurep 'x-toolkit) ", X toolkit")
175 (switch-to-buffer test-buffer)
176 (delete-other-windows))
178 (sort (all-completions "eshell-test--" obarray 'functionp)
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 (car (memory-use-counts))
219 (current-time))))) nil t)
221 (make-local-hook 'eshell-post-command-hook)
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