autorevert-tests.el 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. ;;; auto-revert-tests.el --- Tests of auto-revert
  2. ;; Copyright (C) 2015-2016 Free Software Foundation, Inc.
  3. ;; Author: Michael Albinus <michael.albinus@gmx.de>
  4. ;; This program is free software: you can redistribute it and/or
  5. ;; modify it under the terms of the GNU General Public License as
  6. ;; published by the Free Software Foundation, either version 3 of the
  7. ;; License, or (at your option) any later version.
  8. ;;
  9. ;; This program is distributed in the hope that it will be useful, but
  10. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. ;; General Public License for more details.
  13. ;;
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with this program. If not, see `http://www.gnu.org/licenses/'.
  16. ;;; Commentary:
  17. ;; A whole test run can be performed calling the command `auto-revert-test-all'.
  18. ;;; Code:
  19. (require 'ert)
  20. (require 'autorevert)
  21. (setq auto-revert-notify-exclude-dir-regexp "nothing-to-be-excluded"
  22. auto-revert-stop-on-user-input nil)
  23. (defconst auto-revert--timeout 10
  24. "Time to wait until a message appears in the *Messages* buffer.")
  25. (defun auto-revert--wait-for-revert (buffer)
  26. "Wait until the *Messages* buffer reports reversion of BUFFER."
  27. (with-timeout (auto-revert--timeout nil)
  28. (with-current-buffer "*Messages*"
  29. (while
  30. (null (string-match
  31. (format-message "Reverting buffer `%s'." (buffer-name buffer))
  32. (buffer-string)))
  33. (if (with-current-buffer buffer auto-revert-use-notify)
  34. (read-event nil nil 0.1)
  35. (sleep-for 0.1))))))
  36. (ert-deftest auto-revert-test00-auto-revert-mode ()
  37. "Check autorevert for a file."
  38. ;; `auto-revert-buffers' runs every 5". And we must wait, until the
  39. ;; file has been reverted.
  40. (let ((tmpfile (make-temp-file "auto-revert-test"))
  41. buf)
  42. (unwind-protect
  43. (progn
  44. (with-current-buffer (get-buffer-create "*Messages*")
  45. (narrow-to-region (point-max) (point-max)))
  46. (write-region "any text" nil tmpfile nil 'no-message)
  47. (setq buf (find-file-noselect tmpfile))
  48. (with-current-buffer buf
  49. (should (string-equal (buffer-string) "any text"))
  50. ;; `buffer-stale--default-function' checks for
  51. ;; `verify-visited-file-modtime'. We must ensure that it
  52. ;; returns nil.
  53. (sleep-for 1)
  54. (auto-revert-mode 1)
  55. (should auto-revert-mode)
  56. ;; Modify file. We wait for a second, in order to have
  57. ;; another timestamp.
  58. (sleep-for 1)
  59. (write-region "another text" nil tmpfile nil 'no-message)
  60. ;; Check, that the buffer has been reverted.
  61. (auto-revert--wait-for-revert buf)
  62. (should (string-match "another text" (buffer-string)))
  63. ;; When the buffer is modified, it shall not be reverted.
  64. (with-current-buffer (get-buffer-create "*Messages*")
  65. (narrow-to-region (point-max) (point-max)))
  66. (set-buffer-modified-p t)
  67. (sleep-for 1)
  68. (write-region "any text" nil tmpfile nil 'no-message)
  69. ;; Check, that the buffer hasn't been reverted.
  70. (auto-revert--wait-for-revert buf)
  71. (should-not (string-match "any text" (buffer-string)))))
  72. ;; Exit.
  73. (with-current-buffer "*Messages*" (widen))
  74. (ignore-errors
  75. (with-current-buffer buf (set-buffer-modified-p nil))
  76. (kill-buffer buf))
  77. (ignore-errors (delete-file tmpfile)))))
  78. ;; This is inspired by Bug#21841.
  79. (ert-deftest auto-revert-test01-auto-revert-several-files ()
  80. "Check autorevert for several files at once."
  81. :tags '(:expensive-test)
  82. (skip-unless (executable-find "cp"))
  83. (let* ((cp (executable-find "cp"))
  84. (tmpdir1 (make-temp-file "auto-revert-test" 'dir))
  85. (tmpdir2 (make-temp-file "auto-revert-test" 'dir))
  86. (tmpfile1
  87. (make-temp-file (expand-file-name "auto-revert-test" tmpdir1)))
  88. (tmpfile2
  89. (make-temp-file (expand-file-name "auto-revert-test" tmpdir1)))
  90. buf1 buf2)
  91. (unwind-protect
  92. (progn
  93. (with-current-buffer (get-buffer-create "*Messages*")
  94. (narrow-to-region (point-max) (point-max)))
  95. (write-region "any text" nil tmpfile1 nil 'no-message)
  96. (setq buf1 (find-file-noselect tmpfile1))
  97. (write-region "any text" nil tmpfile2 nil 'no-message)
  98. (setq buf2 (find-file-noselect tmpfile2))
  99. (dolist (buf (list buf1 buf2))
  100. (with-current-buffer buf
  101. (should (string-equal (buffer-string) "any text"))
  102. ;; `buffer-stale--default-function' checks for
  103. ;; `verify-visited-file-modtime'. We must ensure that
  104. ;; it returns nil.
  105. (sleep-for 1)
  106. (auto-revert-mode 1)
  107. (should auto-revert-mode)))
  108. ;; Modify files. We wait for a second, in order to have
  109. ;; another timestamp.
  110. (sleep-for 1)
  111. (write-region
  112. "another text" nil
  113. (expand-file-name (file-name-nondirectory tmpfile1) tmpdir2)
  114. nil 'no-message)
  115. (write-region
  116. "another text" nil
  117. (expand-file-name (file-name-nondirectory tmpfile2) tmpdir2)
  118. nil 'no-message)
  119. ;;(copy-directory tmpdir2 tmpdir1 nil 'copy-contents)
  120. ;; Strange, that `copy-directory' does not work as expected.
  121. ;; The following shell command is not portable on all
  122. ;; platforms, unfortunately.
  123. (shell-command (format "%s -f %s/* %s" cp tmpdir2 tmpdir1))
  124. ;; Check, that the buffers have been reverted.
  125. (dolist (buf (list buf1 buf2))
  126. (with-current-buffer buf
  127. (auto-revert--wait-for-revert buf)
  128. (should (string-match "another text" (buffer-string))))))
  129. ;; Exit.
  130. (with-current-buffer "*Messages*" (widen))
  131. (ignore-errors
  132. (dolist (buf (list buf1 buf2))
  133. (with-current-buffer buf (set-buffer-modified-p nil))
  134. (kill-buffer buf)))
  135. (ignore-errors (delete-directory tmpdir1 'recursive))
  136. (ignore-errors (delete-directory tmpdir2 'recursive)))))
  137. (ert-deftest auto-revert-test02-auto-revert-tail-mode ()
  138. "Check autorevert tail mode."
  139. ;; `auto-revert-buffers' runs every 5". And we must wait, until the
  140. ;; file has been reverted.
  141. (let ((tmpfile (make-temp-file "auto-revert-test"))
  142. buf)
  143. (unwind-protect
  144. (progn
  145. (with-current-buffer (get-buffer-create "*Messages*")
  146. (narrow-to-region (point-max) (point-max)))
  147. (write-region "any text" nil tmpfile nil 'no-message)
  148. (setq buf (find-file-noselect tmpfile))
  149. (with-current-buffer buf
  150. ;; `buffer-stale--default-function' checks for
  151. ;; `verify-visited-file-modtime'. We must ensure that it
  152. ;; returns nil.
  153. (sleep-for 1)
  154. (auto-revert-tail-mode 1)
  155. (should auto-revert-tail-mode)
  156. (erase-buffer)
  157. (insert "modified text\n")
  158. (set-buffer-modified-p nil)
  159. ;; Modify file. We wait for a second, in order to have
  160. ;; another timestamp.
  161. (sleep-for 1)
  162. (write-region "another text" nil tmpfile 'append 'no-message)
  163. ;; Check, that the buffer has been reverted.
  164. (auto-revert--wait-for-revert buf)
  165. (should
  166. (string-match "modified text\nanother text" (buffer-string)))))
  167. ;; Exit.
  168. (with-current-buffer "*Messages*" (widen))
  169. (ignore-errors (kill-buffer buf))
  170. (ignore-errors (delete-file tmpfile)))))
  171. (ert-deftest auto-revert-test03-auto-revert-mode-dired ()
  172. "Check autorevert for dired."
  173. ;; `auto-revert-buffers' runs every 5". And we must wait, until the
  174. ;; file has been reverted.
  175. (let* ((tmpfile (make-temp-file "auto-revert-test"))
  176. (name (file-name-nondirectory tmpfile))
  177. buf)
  178. (unwind-protect
  179. (progn
  180. (setq buf (dired-noselect temporary-file-directory))
  181. (with-current-buffer buf
  182. ;; `buffer-stale--default-function' checks for
  183. ;; `verify-visited-file-modtime'. We must ensure that it
  184. ;; returns nil.
  185. (sleep-for 1)
  186. (auto-revert-mode 1)
  187. (should auto-revert-mode)
  188. (should
  189. (string-match name (substring-no-properties (buffer-string))))
  190. ;; Delete file. We wait for a second, in order to have
  191. ;; another timestamp.
  192. (with-current-buffer (get-buffer-create "*Messages*")
  193. (narrow-to-region (point-max) (point-max)))
  194. (sleep-for 1)
  195. (delete-file tmpfile)
  196. ;; Check, that the buffer has been reverted.
  197. (auto-revert--wait-for-revert buf)
  198. (should-not
  199. (string-match name (substring-no-properties (buffer-string))))
  200. ;; Make dired buffer modified. Check, that the buffer has
  201. ;; been still reverted.
  202. (with-current-buffer (get-buffer-create "*Messages*")
  203. (narrow-to-region (point-max) (point-max)))
  204. (set-buffer-modified-p t)
  205. (sleep-for 1)
  206. (write-region "any text" nil tmpfile nil 'no-message)
  207. ;; Check, that the buffer has been reverted.
  208. (auto-revert--wait-for-revert buf)
  209. (should
  210. (string-match name (substring-no-properties (buffer-string))))))
  211. ;; Exit.
  212. (with-current-buffer "*Messages*" (widen))
  213. (ignore-errors
  214. (with-current-buffer buf (set-buffer-modified-p nil))
  215. (kill-buffer buf))
  216. (ignore-errors (delete-file tmpfile)))))
  217. (defun auto-revert-test-all (&optional interactive)
  218. "Run all tests for \\[auto-revert]."
  219. (interactive "p")
  220. (if interactive
  221. (ert-run-tests-interactively "^auto-revert-")
  222. (ert-run-tests-batch "^auto-revert-")))
  223. (provide 'auto-revert-tests)
  224. ;;; auto-revert-tests.el ends here