eshell.el 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. ;;; esh-test.el --- Eshell test suite
  2. ;; Copyright (C) 1999-2012 Free Software Foundation, Inc.
  3. ;; Author: John Wiegley <johnw@gnu.org>
  4. ;; This file is part of GNU Emacs.
  5. ;; GNU Emacs is free software: you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation, either version 3 of the License, or
  8. ;; (at your option) any later version.
  9. ;; GNU Emacs is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  15. ;;; Commentary:
  16. ;; The purpose of this module is to verify that Eshell works as
  17. ;; expected. To run it on your system, use the command
  18. ;; \\[eshell-test].
  19. ;;; Code:
  20. (eval-when-compile
  21. (require 'cl) ; assert
  22. (require 'eshell)
  23. (require 'esh-util))
  24. (require 'esh-mode)
  25. (defgroup eshell-test nil
  26. "This module is meant to ensure that Eshell is working correctly."
  27. :tag "Eshell test suite"
  28. :group 'eshell)
  29. ;;; User Variables:
  30. (defface eshell-test-ok
  31. '((((class color) (background light)) (:foreground "Green" :bold t))
  32. (((class color) (background dark)) (:foreground "Green" :bold t)))
  33. "The face used to highlight OK result strings."
  34. :group 'eshell-test)
  35. (define-obsolete-face-alias 'eshell-test-ok-face 'eshell-test-ok "22.1")
  36. (defface eshell-test-failed
  37. '((((class color) (background light)) (:foreground "OrangeRed" :bold t))
  38. (((class color) (background dark)) (:foreground "OrangeRed" :bold t))
  39. (t (:bold t)))
  40. "The face used to highlight FAILED result strings."
  41. :group 'eshell-test)
  42. (define-obsolete-face-alias 'eshell-test-failed-face 'eshell-test-failed "22.1")
  43. (defcustom eshell-show-usage-metrics nil
  44. "If non-nil, display different usage metrics for each Eshell command."
  45. :set (lambda (symbol value)
  46. (if value
  47. (add-hook 'eshell-mode-hook 'eshell-show-usage-metrics)
  48. (remove-hook 'eshell-mode-hook 'eshell-show-usage-metrics))
  49. (set symbol value))
  50. :type '(choice (const :tag "No metrics" nil)
  51. (const :tag "Cons cells consumed" t)
  52. (const :tag "Time elapsed" 0))
  53. :group 'eshell-test)
  54. ;;; Code:
  55. (defvar test-buffer)
  56. (defun eshell-insert-command (text &optional func)
  57. "Insert a command at the end of the buffer."
  58. (goto-char eshell-last-output-end)
  59. (insert-and-inherit text)
  60. (funcall (or func 'eshell-send-input)))
  61. (defun eshell-match-result (regexp)
  62. "Insert a command at the end of the buffer."
  63. (goto-char eshell-last-input-end)
  64. (looking-at regexp))
  65. (defun eshell-command-result-p (text regexp &optional func)
  66. "Insert a command at the end of the buffer."
  67. (eshell-insert-command text func)
  68. (eshell-match-result regexp))
  69. (defvar eshell-test-failures nil)
  70. (defun eshell-run-test (module funcsym label command)
  71. "Test whether FORM evaluates to a non-nil value."
  72. (when (let ((sym (intern-soft (concat "eshell-" (symbol-name module)))))
  73. (or (memq sym (eshell-subgroups 'eshell))
  74. (eshell-using-module sym)))
  75. (with-current-buffer test-buffer
  76. (insert-before-markers
  77. (format "%-70s " (substring label 0 (min 70 (length label)))))
  78. (insert-before-markers " ....")
  79. (eshell-redisplay))
  80. (let ((truth (eval command)))
  81. (with-current-buffer test-buffer
  82. (delete-char -6)
  83. (insert-before-markers
  84. "[" (let (str)
  85. (if truth
  86. (progn
  87. (setq str " OK ")
  88. (put-text-property 0 6 'face 'eshell-test-ok str))
  89. (setq str "FAILED")
  90. (setq eshell-test-failures (1+ eshell-test-failures))
  91. (put-text-property 0 6 'face 'eshell-test-failed str))
  92. str) "]")
  93. (add-text-properties (line-beginning-position) (point)
  94. (list 'test-func funcsym))
  95. (eshell-redisplay)))))
  96. (defun eshell-test-goto-func ()
  97. "Jump to the function that defines a particular test."
  98. (interactive)
  99. (let ((fsym (get-text-property (point) 'test-func)))
  100. (when fsym
  101. (let* ((def (symbol-function fsym))
  102. (library (locate-library (symbol-file fsym 'defun)))
  103. (name (substring (symbol-name fsym)
  104. (length "eshell-test--")))
  105. (inhibit-redisplay t))
  106. (find-file library)
  107. (goto-char (point-min))
  108. (re-search-forward (concat "^(eshell-deftest\\s-+\\w+\\s-+"
  109. name))
  110. (beginning-of-line)))))
  111. (defun eshell-run-one-test (&optional arg)
  112. "Jump to the function that defines a particular test."
  113. (interactive "P")
  114. (let ((fsym (get-text-property (point) 'test-func)))
  115. (when fsym
  116. (beginning-of-line)
  117. (delete-region (point) (line-end-position))
  118. (let ((test-buffer (current-buffer)))
  119. (set-buffer (let ((inhibit-redisplay t))
  120. (save-window-excursion (eshell t))))
  121. (funcall fsym)
  122. (unless arg
  123. (kill-buffer (current-buffer)))))))
  124. ;;;###autoload
  125. (defun eshell-test (&optional arg)
  126. "Test Eshell to verify that it works as expected."
  127. (interactive "P")
  128. (let* ((begin (float-time))
  129. (test-buffer (get-buffer-create "*eshell test*")))
  130. (set-buffer (let ((inhibit-redisplay t))
  131. (save-window-excursion (eshell t))))
  132. (with-current-buffer test-buffer
  133. (erase-buffer)
  134. (setq major-mode 'eshell-test-mode)
  135. (setq mode-name "EShell Test")
  136. (set (make-local-variable 'eshell-test-failures) 0)
  137. (local-set-key [(control ?c) (control ?c)] 'eshell-test-goto-func)
  138. (local-set-key [(control ?c) (control ?r)] 'eshell-run-one-test)
  139. (local-set-key [(control ?m)] 'eshell-test-goto-func)
  140. (local-set-key [return] 'eshell-test-goto-func)
  141. (insert "Testing Eshell under " (emacs-version))
  142. (switch-to-buffer test-buffer)
  143. (delete-other-windows))
  144. (dolist (funcname (sort (all-completions "eshell-test--"
  145. obarray 'functionp)
  146. 'string-lessp))
  147. (with-current-buffer test-buffer
  148. (insert "\n"))
  149. (funcall (intern-soft funcname)))
  150. (with-current-buffer test-buffer
  151. (insert (format "\n\n--- %s --- (completed in %d seconds)\n"
  152. (current-time-string)
  153. (- (float-time) begin)))
  154. (message "Eshell test suite completed: %s failure%s"
  155. (if (> eshell-test-failures 0)
  156. (number-to-string eshell-test-failures)
  157. "No")
  158. (if (= eshell-test-failures 1) "" "s"))))
  159. (goto-char eshell-last-output-end)
  160. (unless arg
  161. (kill-buffer (current-buffer))))
  162. (defvar eshell-metric-before-command 0)
  163. (defvar eshell-metric-after-command 0)
  164. (defun eshell-show-usage-metrics ()
  165. "If run at Eshell mode startup, metrics are shown after each command."
  166. (set (make-local-variable 'eshell-metric-before-command)
  167. (if (eq eshell-show-usage-metrics t)
  168. 0
  169. (current-time)))
  170. (set (make-local-variable 'eshell-metric-after-command)
  171. (if (eq eshell-show-usage-metrics t)
  172. 0
  173. (current-time)))
  174. (add-hook 'eshell-pre-command-hook
  175. (function
  176. (lambda ()
  177. (setq eshell-metric-before-command
  178. (if (eq eshell-show-usage-metrics t)
  179. (car (memory-use-counts))
  180. (current-time))))) nil t)
  181. (add-hook 'eshell-post-command-hook
  182. (function
  183. (lambda ()
  184. (setq eshell-metric-after-command
  185. (if (eq eshell-show-usage-metrics t)
  186. (car (memory-use-counts))
  187. (current-time)))
  188. (eshell-interactive-print
  189. (concat
  190. (int-to-string
  191. (if (eq eshell-show-usage-metrics t)
  192. (- eshell-metric-after-command
  193. eshell-metric-before-command 7)
  194. (- (float-time
  195. eshell-metric-after-command)
  196. (float-time
  197. eshell-metric-before-command))))
  198. "\n"))))
  199. nil t))
  200. ;;; The tests.
  201. (defmacro eshell-deftest (module name label &rest forms)
  202. (declare (indent 2))
  203. (if (and (fboundp 'cl-compiling-file) (cl-compiling-file))
  204. nil
  205. (let ((fsym (intern (concat "eshell-test--" (symbol-name name)))))
  206. `(eval-when-compile
  207. (ignore
  208. (defun ,fsym () ,label
  209. (eshell-run-test (quote ,module) (quote ,fsym) ,label
  210. (quote (progn ,@forms)))))))))
  211. (eshell-deftest mode same-window-buffer-names
  212. "`eshell-buffer-name' is a member of `same-window-buffer-names'"
  213. (member eshell-buffer-name same-window-buffer-names))
  214. (eshell-deftest mode eshell-directory-exists
  215. "`eshell-directory-name' exists and is writable"
  216. (file-writable-p eshell-directory-name))
  217. (eshell-deftest mode eshell-directory-modes
  218. "`eshell-directory-name' has correct access protections"
  219. (or (eshell-under-windows-p)
  220. (= (file-modes eshell-directory-name)
  221. eshell-private-directory-modes)))
  222. (eshell-deftest mode simple-command-result
  223. "`eshell-command-result' works with a simple command."
  224. (= (eshell-command-result "+ 1 2") 3))
  225. (require 'em-banner)
  226. (eshell-deftest banner banner-displayed
  227. "Startup banner is displayed at point-min"
  228. (assert eshell-banner-message)
  229. (let ((msg (eval eshell-banner-message)))
  230. (assert msg)
  231. (goto-char (point-min))
  232. (looking-at msg)))
  233. (require 'esh-cmd)
  234. (eshell-deftest var last-result-var
  235. "\"last result\" variable"
  236. (eshell-command-result-p "+ 1 2; + $$ 2" "3\n5\n"))
  237. (eshell-deftest var last-result-var2
  238. "\"last result\" variable"
  239. (eshell-command-result-p "+ 1 2; + $$ $$" "3\n6\n"))
  240. (eshell-deftest var last-arg-var
  241. "\"last arg\" variable"
  242. (eshell-command-result-p "+ 1 2; + $_ 4" "3\n6\n"))
  243. (eshell-deftest cmd lisp-command
  244. "Evaluate Lisp command"
  245. (eshell-command-result-p "(+ 1 2)" "3"))
  246. (eshell-deftest cmd lisp-command-args
  247. "Evaluate Lisp command (ignore args)"
  248. (eshell-command-result-p "(+ 1 2) 3" "3"))
  249. (eshell-deftest cmd subcommand
  250. "Run subcommand"
  251. (eshell-command-result-p "{+ 1 2}" "3\n"))
  252. (eshell-deftest cmd subcommand-args
  253. "Run subcommand (ignore args)"
  254. (eshell-command-result-p "{+ 1 2} 3" "3\n"))
  255. (eshell-deftest cmd subcommand-lisp
  256. "Run subcommand + Lisp form"
  257. (eshell-command-result-p "{(+ 1 2)}" "3\n"))
  258. (eshell-deftest cmd named-command
  259. "Execute named command"
  260. (eshell-command-result-p "+ 1 2" "3\n"))
  261. (require 'esh-mode)
  262. (eshell-deftest mode major-mode
  263. "Major mode is correct"
  264. (eq major-mode 'eshell-mode))
  265. (eshell-deftest mode eshell-mode-variable
  266. "`eshell-mode' is true"
  267. (eq eshell-mode t))
  268. (eshell-deftest var window-height
  269. "LINES equals window height"
  270. (let ((eshell-stringify-t t))
  271. (eshell-command-result-p "= $LINES (window-height)" "t\n")))
  272. (eshell-deftest mode command-running-p
  273. "Modeline shows no command running"
  274. (or (featurep 'xemacs)
  275. (not eshell-status-in-modeline)
  276. (and (memq 'eshell-command-running-string mode-line-format)
  277. (equal eshell-command-running-string "--"))))
  278. (eshell-deftest arg forward-arg
  279. "Move across command arguments"
  280. (eshell-insert-command "echo $(+ 1 (- 4 3)) \"alpha beta\" file" 'ignore)
  281. (let ((here (point)) begin valid)
  282. (eshell-bol)
  283. (setq begin (point))
  284. (eshell-forward-argument 4)
  285. (setq valid (= here (point)))
  286. (eshell-backward-argument 4)
  287. (prog1
  288. (and valid (= begin (point)))
  289. (eshell-bol)
  290. (delete-region (point) (point-max)))))
  291. (eshell-deftest mode queue-input
  292. "Queue command input"
  293. (eshell-insert-command "sleep 2")
  294. (eshell-insert-command "echo alpha" 'eshell-queue-input)
  295. (let ((count 10))
  296. (while (and eshell-current-command
  297. (> count 0))
  298. (sit-for 1 0)
  299. (setq count (1- count))))
  300. (eshell-match-result "alpha\n"))
  301. ; (eshell-deftest proc send-to-subprocess
  302. ; "Send input to a subprocess"
  303. ; ;; jww (1999-12-06): what about when bc is unavailable?
  304. ; (if (not (eshell-search-path "bc"))
  305. ; t
  306. ; (eshell-insert-command "bc")
  307. ; (eshell-insert-command "1 + 2")
  308. ; (sit-for 1 0)
  309. ; (forward-line -1)
  310. ; (prog1
  311. ; (looking-at "3\n")
  312. ; (eshell-insert-command "quit")
  313. ; (sit-for 1 0))))
  314. (eshell-deftest io flush-output
  315. "Flush previous output"
  316. (eshell-insert-command "echo alpha")
  317. (eshell-kill-output)
  318. (and (eshell-match-result (regexp-quote "*** output flushed ***\n"))
  319. (forward-line)
  320. (= (point) eshell-last-output-start)))
  321. (eshell-deftest mode run-old-command
  322. "Re-run an old command"
  323. (eshell-insert-command "echo alpha")
  324. (goto-char eshell-last-input-start)
  325. (string= (eshell-get-old-input) "echo alpha"))
  326. (require 'esh-var)
  327. (eshell-deftest var interp-cmd
  328. "Interpolate command result"
  329. (eshell-command-result-p "+ ${+ 1 2} 3" "6\n"))
  330. (eshell-deftest var interp-lisp
  331. "Interpolate Lisp form evaluation"
  332. (eshell-command-result-p "+ $(+ 1 2) 3" "6\n"))
  333. (eshell-deftest var interp-concat
  334. "Interpolate and concat command"
  335. (eshell-command-result-p "+ ${+ 1 2}3 3" "36\n"))
  336. (eshell-deftest var interp-concat-lisp
  337. "Interpolate and concat Lisp form"
  338. (eshell-command-result-p "+ $(+ 1 2)3 3" "36\n"))
  339. (eshell-deftest var interp-concat2
  340. "Interpolate and concat two commands"
  341. (eshell-command-result-p "+ ${+ 1 2}${+ 1 2} 3" "36\n"))
  342. (eshell-deftest var interp-concat-lisp2
  343. "Interpolate and concat two Lisp forms"
  344. (eshell-command-result-p "+ $(+ 1 2)$(+ 1 2) 3" "36\n"))
  345. (provide 'esh-test)
  346. ;;; esh-test.el ends here