Commit | Line | Data |
---|---|---|
60370d40 | 1 | ;;; esh-test.el --- Eshell test suite |
affbf647 | 2 | |
f2e3589a | 3 | ;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, |
8b72699e | 4 | ;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc. |
affbf647 | 5 | |
7de5b421 GM |
6 | ;; Author: John Wiegley <johnw@gnu.org> |
7 | ||
affbf647 GM |
8 | ;; This file is part of GNU Emacs. |
9 | ||
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 | |
e0085d62 | 12 | ;; the Free Software Foundation; either version 3, or (at your option) |
affbf647 GM |
13 | ;; any later version. |
14 | ||
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. | |
19 | ||
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 | |
3a35cf56 LK |
22 | ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
23 | ;; Boston, MA 02110-1301, USA. | |
affbf647 | 24 | |
affbf647 GM |
25 | ;;; Commentary: |
26 | ||
27 | ;; The purpose of this module is to verify that Eshell works as | |
28 | ;; expected. To run it on your system, use the command | |
29 | ;; \\[eshell-test]. | |
30 | ||
31 | ;;; Code: | |
32 | ||
00c3ec76 GM |
33 | (eval-when-compile |
34 | (require 'eshell) | |
35 | (require 'esh-util)) | |
affbf647 GM |
36 | (require 'esh-mode) |
37 | ||
00c3ec76 GM |
38 | (defgroup eshell-test nil |
39 | "This module is meant to ensure that Eshell is working correctly." | |
40 | :tag "Eshell test suite" | |
41 | :group 'eshell) | |
42 | ||
affbf647 GM |
43 | ;;; User Variables: |
44 | ||
958e6876 | 45 | (defface eshell-test-ok |
affbf647 GM |
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." | |
49 | :group 'eshell-test) | |
958e6876 MB |
50 | ;; backward-compatibility alias |
51 | (put 'eshell-test-ok-face 'face-alias 'eshell-test-ok) | |
affbf647 | 52 | |
958e6876 | 53 | (defface eshell-test-failed |
affbf647 GM |
54 | '((((class color) (background light)) (:foreground "OrangeRed" :bold t)) |
55 | (((class color) (background dark)) (:foreground "OrangeRed" :bold t)) | |
56 | (t (:bold t))) | |
57 | "*The face used to highlight FAILED result strings." | |
58 | :group 'eshell-test) | |
958e6876 MB |
59 | ;; backward-compatibility alias |
60 | (put 'eshell-test-failed-face 'face-alias 'eshell-test-failed) | |
affbf647 GM |
61 | |
62 | (defcustom eshell-show-usage-metrics nil | |
63 | "*If non-nil, display different usage metrics for each Eshell command." | |
64 | :set (lambda (symbol value) | |
65 | (if value | |
66 | (add-hook 'eshell-mode-hook 'eshell-show-usage-metrics) | |
67 | (remove-hook 'eshell-mode-hook 'eshell-show-usage-metrics)) | |
68 | (set symbol value)) | |
69 | :type '(choice (const :tag "No metrics" nil) | |
70 | (const :tag "Cons cells consumed" t) | |
71 | (const :tag "Time elapsed" 0)) | |
72 | :group 'eshell-test) | |
73 | ||
74 | ;;; Code: | |
75 | ||
76 | (eval-when-compile | |
77 | (defvar test-buffer)) | |
78 | ||
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))) | |
84 | ||
85 | (defun eshell-match-result (regexp) | |
86 | "Insert a command at the end of the buffer." | |
87 | (goto-char eshell-last-input-end) | |
88 | (looking-at regexp)) | |
89 | ||
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)) | |
94 | ||
95 | (defvar eshell-test-failures nil) | |
96 | ||
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 " ....") | |
106 | (eshell-redisplay)) | |
107 | (let ((truth (eval command))) | |
108 | (with-current-buffer test-buffer | |
109 | (delete-backward-char 6) | |
110 | (insert-before-markers | |
111 | "[" (let (str) | |
112 | (if truth | |
113 | (progn | |
114 | (setq str " OK ") | |
958e6876 | 115 | (put-text-property 0 6 'face 'eshell-test-ok str)) |
affbf647 GM |
116 | (setq str "FAILED") |
117 | (setq eshell-test-failures (1+ eshell-test-failures)) | |
958e6876 | 118 | (put-text-property 0 6 'face 'eshell-test-failed str)) |
affbf647 GM |
119 | str) "]") |
120 | (add-text-properties (line-beginning-position) (point) | |
121 | (list 'test-func funcsym)) | |
122 | (eshell-redisplay))))) | |
123 | ||
124 | (defun eshell-test-goto-func () | |
125 | "Jump to the function that defines a particular test." | |
126 | (interactive) | |
127 | (let ((fsym (get-text-property (point) 'test-func))) | |
128 | (when fsym | |
129 | (let* ((def (symbol-function fsym)) | |
c044263b | 130 | (library (locate-library (symbol-file fsym 'defun))) |
affbf647 GM |
131 | (name (substring (symbol-name fsym) |
132 | (length "eshell-test--"))) | |
133 | (inhibit-redisplay t)) | |
134 | (find-file library) | |
135 | (goto-char (point-min)) | |
136 | (re-search-forward (concat "^(eshell-deftest\\s-+\\w+\\s-+" | |
137 | name)) | |
138 | (beginning-of-line))))) | |
139 | ||
140 | (defun eshell-run-one-test (&optional arg) | |
141 | "Jump to the function that defines a particular test." | |
142 | (interactive "P") | |
143 | (let ((fsym (get-text-property (point) 'test-func))) | |
144 | (when fsym | |
145 | (beginning-of-line) | |
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)))) | |
150 | (funcall fsym) | |
151 | (unless arg | |
152 | (kill-buffer (current-buffer))))))) | |
153 | ||
154 | ;;;###autoload | |
155 | (defun eshell-test (&optional arg) | |
156 | "Test Eshell to verify that it works as expected." | |
157 | (interactive "P") | |
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 | |
163 | (erase-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) | |
171 | ||
97dad9d3 | 172 | (insert "Testing Eshell under " (emacs-version)) |
affbf647 GM |
173 | (switch-to-buffer test-buffer) |
174 | (delete-other-windows)) | |
dace60cf JW |
175 | (eshell-for funcname (sort (all-completions "eshell-test--" |
176 | obarray 'functionp) | |
177 | 'string-lessp) | |
affbf647 GM |
178 | (with-current-buffer test-buffer |
179 | (insert "\n")) | |
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)) | |
185 | begin))) | |
186 | (message "Eshell test suite completed: %s failure%s" | |
187 | (if (> eshell-test-failures 0) | |
188 | (number-to-string eshell-test-failures) | |
189 | "No") | |
190 | (if (= eshell-test-failures 1) "" "s")))) | |
191 | (goto-char eshell-last-output-end) | |
192 | (unless arg | |
193 | (kill-buffer (current-buffer)))) | |
194 | ||
195 | ||
196 | (defvar eshell-metric-before-command 0) | |
197 | (defvar eshell-metric-after-command 0) | |
198 | ||
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) | |
203 | 0 | |
204 | (current-time))) | |
205 | (set (make-local-variable 'eshell-metric-after-command) | |
206 | (if (eq eshell-show-usage-metrics t) | |
207 | 0 | |
208 | (current-time))) | |
209 | ||
affbf647 GM |
210 | (add-hook 'eshell-pre-command-hook |
211 | (function | |
212 | (lambda () | |
213 | (setq eshell-metric-before-command | |
214 | (if (eq eshell-show-usage-metrics t) | |
215 | (car (memory-use-counts)) | |
216 | (current-time))))) nil t) | |
217 | ||
affbf647 GM |
218 | (add-hook 'eshell-post-command-hook |
219 | (function | |
220 | (lambda () | |
221 | (setq eshell-metric-after-command | |
222 | (if (eq eshell-show-usage-metrics t) | |
223 | (car (memory-use-counts)) | |
224 | (current-time))) | |
225 | (eshell-interactive-print | |
226 | (concat | |
227 | (int-to-string | |
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)))) | |
235 | "\n")))) | |
236 | nil t)) | |
237 | ||
00c3ec76 GM |
238 | (provide 'esh-test) |
239 | ||
cbee283d | 240 | ;; arch-tag: 6e32275a-8285-4a4e-b7cf-819aa7c86b8e |
affbf647 | 241 | ;;; esh-test.el ends here |