diff options
| author | Tassilo Horn | 2015-09-10 17:18:15 +0200 |
|---|---|---|
| committer | Tassilo Horn | 2015-09-10 17:22:58 +0200 |
| commit | a6a1333e7e34a63bce4177ff5100ff92ab51e8a6 (patch) | |
| tree | 3ebcef1481d15f360a1188048871ba4f45da93a0 | |
| parent | f962c5d6472804f788cdd54631bcc46aab5f59ce (diff) | |
| download | emacs-a6a1333e7e34a63bce4177ff5100ff92ab51e8a6.tar.gz emacs-a6a1333e7e34a63bce4177ff5100ff92ab51e8a6.zip | |
Improve file-notify-tests
* test/automated/file-notify-tests.el: Use lexical-binding
(file-notify--test-cleanup): New function.
(file-notify-test00-availability, file-notify-test01-add-watch)
(file-notify-test02-events, file-notify-test03-autorevert): Use it.
(file-notify--test-with-events): New macro.
(file-notify-test02-events): Use it.
| -rw-r--r-- | test/automated/file-notify-tests.el | 124 |
1 files changed, 83 insertions, 41 deletions
diff --git a/test/automated/file-notify-tests.el b/test/automated/file-notify-tests.el index 2c6f9169b4e..eee8ee35b6a 100644 --- a/test/automated/file-notify-tests.el +++ b/test/automated/file-notify-tests.el | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | ;;; file-notify-tests.el --- Tests of file notifications | 1 | ;;; file-notify-tests.el --- Tests of file notifications -*- lexical-binding: t; -*- |
| 2 | 2 | ||
| 3 | ;; Copyright (C) 2013-2015 Free Software Foundation, Inc. | 3 | ;; Copyright (C) 2013-2015 Free Software Foundation, Inc. |
| 4 | 4 | ||
| @@ -62,6 +62,25 @@ | |||
| 62 | (defvar file-notify--test-event nil) | 62 | (defvar file-notify--test-event nil) |
| 63 | (defvar file-notify--test-events nil) | 63 | (defvar file-notify--test-events nil) |
| 64 | 64 | ||
| 65 | (defun file-notify--test-cleanup () | ||
| 66 | "Cleanup after a test." | ||
| 67 | (file-notify-rm-watch file-notify--test-desc) | ||
| 68 | |||
| 69 | (when (and file-notify--test-tmpfile | ||
| 70 | (file-exists-p file-notify--test-tmpfile)) | ||
| 71 | (delete-file file-notify--test-tmpfile)) | ||
| 72 | (when (and file-notify--test-tmpfile1 | ||
| 73 | (file-exists-p file-notify--test-tmpfile1)) | ||
| 74 | (delete-file file-notify--test-tmpfile1)) | ||
| 75 | |||
| 76 | (setq file-notify--test-tmpfile nil) | ||
| 77 | (setq file-notify--test-tmpfile1 nil) | ||
| 78 | (setq file-notify--test-desc nil) | ||
| 79 | (setq file-notify--test-results nil) | ||
| 80 | (setq file-notify--test-events nil) | ||
| 81 | (when file-notify--test-event | ||
| 82 | (error "file-notify--test-event should not be set but bound dynamically"))) | ||
| 83 | |||
| 65 | (setq password-cache-expiry nil | 84 | (setq password-cache-expiry nil |
| 66 | tramp-verbose 0 | 85 | tramp-verbose 0 |
| 67 | tramp-message-show-message nil) | 86 | tramp-message-show-message nil) |
| @@ -120,7 +139,7 @@ being the result.") | |||
| 120 | (should | 139 | (should |
| 121 | (setq file-notify--test-desc | 140 | (setq file-notify--test-desc |
| 122 | (file-notify-add-watch temporary-file-directory '(change) 'ignore))) | 141 | (file-notify-add-watch temporary-file-directory '(change) 'ignore))) |
| 123 | (file-notify-rm-watch file-notify--test-desc)) | 142 | (file-notify--test-cleanup)) |
| 124 | 143 | ||
| 125 | (file-notify--deftest-remote file-notify-test00-availability | 144 | (file-notify--deftest-remote file-notify-test00-availability |
| 126 | "Test availability of `file-notify' for remote files.") | 145 | "Test availability of `file-notify' for remote files.") |
| @@ -158,7 +177,9 @@ being the result.") | |||
| 158 | (should | 177 | (should |
| 159 | (equal (should-error | 178 | (equal (should-error |
| 160 | (file-notify-add-watch temporary-file-directory '(change) 3)) | 179 | (file-notify-add-watch temporary-file-directory '(change) 3)) |
| 161 | '(wrong-type-argument 3)))) | 180 | '(wrong-type-argument 3))) |
| 181 | |||
| 182 | (file-notify--test-cleanup)) | ||
| 162 | 183 | ||
| 163 | (file-notify--deftest-remote file-notify-test01-add-watch | 184 | (file-notify--deftest-remote file-notify-test01-add-watch |
| 164 | "Check `file-notify-add-watch' for remote files.") | 185 | "Check `file-notify-add-watch' for remote files.") |
| @@ -181,13 +202,13 @@ is bound somewhere." | |||
| 181 | (file-notify--event-file1-name file-notify--test-event) | 202 | (file-notify--event-file1-name file-notify--test-event) |
| 182 | file-notify--test-tmpfile1)))) | 203 | file-notify--test-tmpfile1)))) |
| 183 | 204 | ||
| 184 | (defun file-notify--test-event-handler (file-notify--test-event) | 205 | (defun file-notify--test-event-handler (event) |
| 185 | "Run a test over FILE-NOTIFY--TEST-EVENT. | 206 | "Run a test over FILE-NOTIFY--TEST-EVENT. |
| 186 | For later analysis, append the test result to | 207 | For later analysis, append the test result to |
| 187 | `file-notify--test-results' and the event to | 208 | `file-notify--test-results' and the event to |
| 188 | `file-notify--test-events'." | 209 | `file-notify--test-events'." |
| 189 | (let ((result | 210 | (let* ((file-notify--test-event event) |
| 190 | (ert-run-test (make-ert-test :body 'file-notify--test-event-test)))) | 211 | (result (ert-run-test (make-ert-test :body 'file-notify--test-event-test)))) |
| 191 | (setq file-notify--test-events | 212 | (setq file-notify--test-events |
| 192 | (append file-notify--test-events `(,file-notify--test-event))) | 213 | (append file-notify--test-events `(,file-notify--test-event))) |
| 193 | (setq file-notify--test-results | 214 | (setq file-notify--test-results |
| @@ -205,6 +226,18 @@ TIMEOUT is the maximum time to wait for, in seconds." | |||
| 205 | (while (null ,until) | 226 | (while (null ,until) |
| 206 | (read-event nil nil 0.1)))) | 227 | (read-event nil nil 0.1)))) |
| 207 | 228 | ||
| 229 | (defmacro file-notify--test-with-events (n timeout assert-fn &rest body) | ||
| 230 | (declare (indent 3)) | ||
| 231 | (let ((outer (make-symbol "outer"))) | ||
| 232 | `(let ((,outer file-notify--test-events)) | ||
| 233 | (let ((file-notify--test-events nil)) | ||
| 234 | ,@body | ||
| 235 | (file-notify--wait-for-events | ||
| 236 | ,timeout (= ,n (length file-notify--test-events))) | ||
| 237 | (funcall ,assert-fn file-notify--test-events) | ||
| 238 | (setq ,outer (append ,outer file-notify--test-events))) | ||
| 239 | (setq file-notify--test-events ,outer)))) | ||
| 240 | |||
| 208 | (ert-deftest file-notify-test02-events () | 241 | (ert-deftest file-notify-test02-events () |
| 209 | "Check file creation/removal notifications." | 242 | "Check file creation/removal notifications." |
| 210 | (skip-unless (file-notify--test-local-enabled)) | 243 | (skip-unless (file-notify--test-local-enabled)) |
| @@ -221,40 +254,49 @@ TIMEOUT is the maximum time to wait for, in seconds." | |||
| 221 | (should file-notify--test-desc) | 254 | (should file-notify--test-desc) |
| 222 | 255 | ||
| 223 | ;; Check creation, change, and deletion. | 256 | ;; Check creation, change, and deletion. |
| 224 | (write-region | 257 | (file-notify--test-with-events |
| 225 | "any text" nil file-notify--test-tmpfile nil 'no-message) | 258 | 3 3 (lambda (events) |
| 226 | (delete-file file-notify--test-tmpfile) | 259 | (should (equal '(created changed deleted) |
| 227 | (sleep-for 0.1) | 260 | (mapcar #'cadr events)))) |
| 228 | 261 | (write-region | |
| 229 | ;; Check copy and rename. | 262 | "any text" nil file-notify--test-tmpfile nil 'no-message) |
| 230 | (write-region | 263 | (delete-file file-notify--test-tmpfile)) |
| 231 | "any text" nil file-notify--test-tmpfile nil 'no-message) | 264 | |
| 232 | (copy-file file-notify--test-tmpfile file-notify--test-tmpfile1) | 265 | ;; Check copy. |
| 233 | (delete-file file-notify--test-tmpfile) | 266 | (file-notify--test-with-events |
| 234 | (delete-file file-notify--test-tmpfile1) | 267 | 3 3 (lambda (events) |
| 235 | (sleep-for 0.1) | 268 | (should (equal '(created changed deleted) |
| 236 | 269 | (mapcar #'cadr events)))) | |
| 237 | (write-region | 270 | (write-region |
| 238 | "any text" nil file-notify--test-tmpfile nil 'no-message) | 271 | "any text" nil file-notify--test-tmpfile nil 'no-message) |
| 239 | (rename-file file-notify--test-tmpfile file-notify--test-tmpfile1) | 272 | (copy-file file-notify--test-tmpfile file-notify--test-tmpfile1) |
| 240 | (delete-file file-notify--test-tmpfile1) | 273 | (delete-file file-notify--test-tmpfile) |
| 241 | (sleep-for 0.1)) | 274 | (delete-file file-notify--test-tmpfile1)) |
| 242 | 275 | ||
| 243 | ;; Wait for events, and exit. | 276 | ;; Check rename. |
| 244 | (file-notify--wait-for-events 5 file-notify--test-results) | 277 | (file-notify--test-with-events |
| 245 | (should (equal (mapcar #'cadr file-notify--test-events) | 278 | 3 3 (lambda (events) |
| 246 | '(created changed deleted | 279 | (should (equal '(created changed renamed) |
| 247 | created changed deleted | 280 | (mapcar #'cadr events)))) |
| 248 | created changed renamed))) | 281 | (write-region |
| 249 | (file-notify-rm-watch file-notify--test-desc) | 282 | "any text" nil file-notify--test-tmpfile nil 'no-message) |
| 250 | (ignore-errors (delete-file file-notify--test-tmpfile)) | 283 | (rename-file file-notify--test-tmpfile file-notify--test-tmpfile1) |
| 251 | (ignore-errors (delete-file file-notify--test-tmpfile1))) | 284 | ;; After the rename, we won't get events anymore. |
| 252 | 285 | (delete-file file-notify--test-tmpfile1)) | |
| 253 | (should file-notify--test-results) | 286 | |
| 254 | (dolist (result file-notify--test-results) | 287 | ;; Check the global sequence again just to make sure that |
| 255 | ;;(message "%s" (ert-test-result-messages result)) | 288 | ;; `file-notify--test-events' has been set correctly. |
| 256 | (when (ert-test-failed-p result) | 289 | (should (equal (mapcar #'cadr file-notify--test-events) |
| 257 | (ert-fail (cadr (ert-test-result-with-condition-condition result)))))) | 290 | '(created changed deleted |
| 291 | created changed deleted | ||
| 292 | created changed renamed))) | ||
| 293 | |||
| 294 | (should file-notify--test-results) | ||
| 295 | (dolist (result file-notify--test-results) | ||
| 296 | ;;(message "%s" (ert-test-result-messages result)) | ||
| 297 | (when (ert-test-failed-p result) | ||
| 298 | (ert-fail (cadr (ert-test-result-with-condition-condition result)))))) | ||
| 299 | (file-notify--test-cleanup))) | ||
| 258 | 300 | ||
| 259 | (file-notify--deftest-remote file-notify-test02-events | 301 | (file-notify--deftest-remote file-notify-test02-events |
| 260 | "Check file creation/removal notifications for remote files.") | 302 | "Check file creation/removal notifications for remote files.") |
| @@ -313,7 +355,7 @@ This test is skipped in batch mode." | |||
| 313 | 355 | ||
| 314 | ;; Exit. | 356 | ;; Exit. |
| 315 | (ignore-errors (kill-buffer buf)) | 357 | (ignore-errors (kill-buffer buf)) |
| 316 | (ignore-errors (delete-file file-notify--test-tmpfile))))) | 358 | (file-notify--test-cleanup)))) |
| 317 | 359 | ||
| 318 | (file-notify--deftest-remote file-notify-test03-autorevert | 360 | (file-notify--deftest-remote file-notify-test03-autorevert |
| 319 | "Check autorevert via file notification for remote files. | 361 | "Check autorevert via file notification for remote files. |