autorevert-tests.el 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. ;;; auto-revert-tests.el --- Tests of auto-revert
  2. ;; Copyright (C) 2015-2017 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. ;; This is inspired by Bug#23276.
  138. (ert-deftest auto-revert-test02-auto-revert-deleted-file ()
  139. "Check autorevert for a deleted file."
  140. :tags '(:expensive-test)
  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. (should (string-equal (buffer-string) "any text"))
  151. ;; `buffer-stale--default-function' checks for
  152. ;; `verify-visited-file-modtime'. We must ensure that
  153. ;; it returns nil.
  154. (sleep-for 1)
  155. (auto-revert-mode 1)
  156. (should auto-revert-mode)
  157. ;; Remove file while reverting. We simulate this by
  158. ;; modifying `before-revert-hook'.
  159. (add-hook
  160. 'before-revert-hook
  161. (lambda () (delete-file buffer-file-name))
  162. nil t)
  163. (with-current-buffer (get-buffer-create "*Messages*")
  164. (narrow-to-region (point-max) (point-max)))
  165. (sleep-for 1)
  166. (write-region "another text" nil tmpfile nil 'no-message)
  167. ;; Check, that the buffer hasn't been reverted. File
  168. ;; notification should be disabled, falling back to
  169. ;; polling.
  170. (auto-revert--wait-for-revert buf)
  171. (should (string-match "any text" (buffer-string)))
  172. (should-not auto-revert-use-notify)
  173. ;; Once the file has been recreated, the buffer shall be
  174. ;; reverted.
  175. (kill-local-variable 'before-revert-hook)
  176. (with-current-buffer (get-buffer-create "*Messages*")
  177. (narrow-to-region (point-max) (point-max)))
  178. (sleep-for 1)
  179. (write-region "another text" nil tmpfile nil 'no-message)
  180. ;; Check, that the buffer has been reverted.
  181. (auto-revert--wait-for-revert buf)
  182. (should (string-match "another text" (buffer-string)))
  183. ;; An empty file shall still be reverted.
  184. (with-current-buffer (get-buffer-create "*Messages*")
  185. (narrow-to-region (point-max) (point-max)))
  186. (sleep-for 1)
  187. (write-region "" nil tmpfile nil 'no-message)
  188. ;; Check, that the buffer has been reverted.
  189. (auto-revert--wait-for-revert buf)
  190. (should (string-equal "" (buffer-string)))))
  191. ;; Exit.
  192. (with-current-buffer "*Messages*" (widen))
  193. (ignore-errors
  194. (with-current-buffer buf (set-buffer-modified-p nil))
  195. (kill-buffer buf))
  196. (ignore-errors (delete-file tmpfile)))))
  197. (ert-deftest auto-revert-test03-auto-revert-tail-mode ()
  198. "Check autorevert tail mode."
  199. ;; `auto-revert-buffers' runs every 5". And we must wait, until the
  200. ;; file has been reverted.
  201. (let ((tmpfile (make-temp-file "auto-revert-test"))
  202. buf)
  203. (unwind-protect
  204. (progn
  205. (with-current-buffer (get-buffer-create "*Messages*")
  206. (narrow-to-region (point-max) (point-max)))
  207. (write-region "any text" nil tmpfile nil 'no-message)
  208. (setq buf (find-file-noselect tmpfile))
  209. (with-current-buffer buf
  210. ;; `buffer-stale--default-function' checks for
  211. ;; `verify-visited-file-modtime'. We must ensure that it
  212. ;; returns nil.
  213. (sleep-for 1)
  214. (auto-revert-tail-mode 1)
  215. (should auto-revert-tail-mode)
  216. (erase-buffer)
  217. (insert "modified text\n")
  218. (set-buffer-modified-p nil)
  219. ;; Modify file. We wait for a second, in order to have
  220. ;; another timestamp.
  221. (sleep-for 1)
  222. (write-region "another text" nil tmpfile 'append 'no-message)
  223. ;; Check, that the buffer has been reverted.
  224. (auto-revert--wait-for-revert buf)
  225. (should
  226. (string-match "modified text\nanother text" (buffer-string)))))
  227. ;; Exit.
  228. (with-current-buffer "*Messages*" (widen))
  229. (ignore-errors (kill-buffer buf))
  230. (ignore-errors (delete-file tmpfile)))))
  231. (ert-deftest auto-revert-test04-auto-revert-mode-dired ()
  232. "Check autorevert for dired."
  233. ;; `auto-revert-buffers' runs every 5". And we must wait, until the
  234. ;; file has been reverted.
  235. (let* ((tmpfile (make-temp-file "auto-revert-test"))
  236. (name (file-name-nondirectory tmpfile))
  237. buf)
  238. (unwind-protect
  239. (progn
  240. (setq buf (dired-noselect temporary-file-directory))
  241. (with-current-buffer buf
  242. ;; `buffer-stale--default-function' checks for
  243. ;; `verify-visited-file-modtime'. We must ensure that it
  244. ;; returns nil.
  245. (sleep-for 1)
  246. (auto-revert-mode 1)
  247. (should auto-revert-mode)
  248. (should
  249. (string-match name (substring-no-properties (buffer-string))))
  250. ;; Delete file. We wait for a second, in order to have
  251. ;; another timestamp.
  252. (with-current-buffer (get-buffer-create "*Messages*")
  253. (narrow-to-region (point-max) (point-max)))
  254. (sleep-for 1)
  255. (delete-file tmpfile)
  256. ;; Check, that the buffer has been reverted.
  257. (auto-revert--wait-for-revert buf)
  258. (should-not
  259. (string-match name (substring-no-properties (buffer-string))))
  260. ;; Make dired buffer modified. Check, that the buffer has
  261. ;; been still reverted.
  262. (with-current-buffer (get-buffer-create "*Messages*")
  263. (narrow-to-region (point-max) (point-max)))
  264. (set-buffer-modified-p t)
  265. (sleep-for 1)
  266. (write-region "any text" nil tmpfile nil 'no-message)
  267. ;; Check, that the buffer has been reverted.
  268. (auto-revert--wait-for-revert buf)
  269. (should
  270. (string-match name (substring-no-properties (buffer-string))))))
  271. ;; Exit.
  272. (with-current-buffer "*Messages*" (widen))
  273. (ignore-errors
  274. (with-current-buffer buf (set-buffer-modified-p nil))
  275. (kill-buffer buf))
  276. (ignore-errors (delete-file tmpfile)))))
  277. (defun auto-revert-test-all (&optional interactive)
  278. "Run all tests for \\[auto-revert]."
  279. (interactive "p")
  280. (if interactive
  281. (ert-run-tests-interactively "^auto-revert-")
  282. (ert-run-tests-batch "^auto-revert-")))
  283. (provide 'auto-revert-tests)
  284. ;;; auto-revert-tests.el ends here