1 ;;; esh-test.el --- Eshell test suite
3 ;; Copyright (C) 1999-2011 Free Software Foundation, Inc.
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 3 of the License, or
12 ;; (at your option) any later version.
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. If not, see <http://www.gnu.org/licenses/>.
24 ;; The purpose of this module is to verify that Eshell works as
25 ;; expected. To run it on your system, use the command
35 (defgroup eshell-test nil
36 "This module is meant to ensure that Eshell is working correctly."
37 :tag "Eshell test suite"
42 (defface eshell-test-ok
43 '((((class color) (background light)) (:foreground "Green" :bold t))
44 (((class color) (background dark)) (:foreground "Green" :bold t)))
45 "The face used to highlight OK result strings."
47 (define-obsolete-face-alias 'eshell-test-ok-face 'eshell-test-ok "22.1")
49 (defface eshell-test-failed
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."
55 (define-obsolete-face-alias 'eshell-test-failed-face 'eshell-test-failed "22.1")
57 (defcustom eshell-show-usage-metrics nil
58 "If non-nil, display different usage metrics for each Eshell command."
59 :set (lambda (symbol value)
61 (add-hook 'eshell-mode-hook 'eshell-show-usage-metrics)
62 (remove-hook 'eshell-mode-hook 'eshell-show-usage-metrics))
64 :type '(choice (const :tag "No metrics" nil)
65 (const :tag "Cons cells consumed" t)
66 (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
104 (insert-before-markers
109 (put-text-property 0 6 'face 'eshell-test-ok str))
111 (setq eshell-test-failures (1+ eshell-test-failures))
112 (put-text-property 0 6 'face 'eshell-test-failed str))
114 (add-text-properties (line-beginning-position) (point)
115 (list 'test-func funcsym))
116 (eshell-redisplay)))))
118 (defun eshell-test-goto-func ()
119 "Jump to the function that defines a particular test."
121 (let ((fsym (get-text-property (point) 'test-func)))
123 (let* ((def (symbol-function fsym))
124 (library (locate-library (symbol-file fsym 'defun)))
125 (name (substring (symbol-name fsym)
126 (length "eshell-test--")))
127 (inhibit-redisplay t))
129 (goto-char (point-min))
130 (re-search-forward (concat "^(eshell-deftest\\s-+\\w+\\s-+"
132 (beginning-of-line)))))
134 (defun eshell-run-one-test (&optional arg)
135 "Jump to the function that defines a particular test."
137 (let ((fsym (get-text-property (point) 'test-func)))
140 (delete-region (point) (line-end-position))
141 (let ((test-buffer (current-buffer)))
142 (set-buffer (let ((inhibit-redisplay t))
143 (save-window-excursion (eshell t))))
146 (kill-buffer (current-buffer)))))))
149 (defun eshell-test (&optional arg)
150 "Test Eshell to verify that it works as expected."
152 (let* ((begin (float-time))
153 (test-buffer (get-buffer-create "*eshell test*")))
154 (set-buffer (let ((inhibit-redisplay t))
155 (save-window-excursion (eshell t))))
156 (with-current-buffer test-buffer
158 (setq major-mode 'eshell-test-mode)
159 (setq mode-name "EShell Test")
160 (set (make-local-variable 'eshell-test-failures) 0)
161 (local-set-key [(control ?c) (control ?c)] 'eshell-test-goto-func)
162 (local-set-key [(control ?c) (control ?r)] 'eshell-run-one-test)
163 (local-set-key [(control ?m)] 'eshell-test-goto-func)
164 (local-set-key [return] 'eshell-test-goto-func)
166 (insert "Testing Eshell under " (emacs-version))
167 (switch-to-buffer test-buffer)
168 (delete-other-windows))
169 (eshell-for funcname (sort (all-completions "eshell-test--"
172 (with-current-buffer test-buffer
174 (funcall (intern-soft funcname)))
175 (with-current-buffer test-buffer
176 (insert (format "\n\n--- %s --- (completed in %d seconds)\n"
177 (current-time-string)
178 (- (float-time) begin)))
179 (message "Eshell test suite completed: %s failure%s"
180 (if (> eshell-test-failures 0)
181 (number-to-string eshell-test-failures)
183 (if (= eshell-test-failures 1) "" "s"))))
184 (goto-char eshell-last-output-end)
186 (kill-buffer (current-buffer))))
189 (defvar eshell-metric-before-command 0)
190 (defvar eshell-metric-after-command 0)
192 (defun eshell-show-usage-metrics ()
193 "If run at Eshell mode startup, metrics are shown after each command."
194 (set (make-local-variable 'eshell-metric-before-command)
195 (if (eq eshell-show-usage-metrics t)
198 (set (make-local-variable 'eshell-metric-after-command)
199 (if (eq eshell-show-usage-metrics t)
203 (add-hook 'eshell-pre-command-hook
206 (setq eshell-metric-before-command
207 (if (eq eshell-show-usage-metrics t)
208 (car (memory-use-counts))
209 (current-time))))) nil t)
211 (add-hook 'eshell-post-command-hook
214 (setq eshell-metric-after-command
215 (if (eq eshell-show-usage-metrics t)
216 (car (memory-use-counts))
218 (eshell-interactive-print
221 (if (eq eshell-show-usage-metrics t)
222 (- eshell-metric-after-command
223 eshell-metric-before-command 7)
225 eshell-metric-after-command)
227 eshell-metric-before-command))))
233 ;;; esh-test.el ends here