diff options
| author | Tassilo Horn | 2022-01-15 23:59:20 +0100 |
|---|---|---|
| committer | Tassilo Horn | 2022-01-16 00:21:17 +0100 |
| commit | 77d823794eacd73469d32fe780b7dd4eae213b34 (patch) | |
| tree | c51f002dd142cc59635eed8268612784901e658a | |
| parent | 791694c5fe86b8c9ab5cb593bd5e050aa185aa3a (diff) | |
| download | emacs-77d823794eacd73469d32fe780b7dd4eae213b34.tar.gz emacs-77d823794eacd73469d32fe780b7dd4eae213b34.zip | |
Add some more mailcap tests.
* test/lisp/net/mailcap-resources/mailcap: New file.
* test/lisp/net/mailcap-resources/test.test: New file.
* test/lisp/net/mailcap-tests.el (with-pristine-mailcap): New macro.
(mailcap-parsing-and-mailcap-mime-info): New test for parsing mailcap
files and selecting the right viewer.
(mailcap--test-result): New variable.
(mailcap--test-viewer): New function.
(mailcap-view-file): New test for mailcap-view-file.
| -rw-r--r-- | test/lisp/net/mailcap-resources/mailcap | 9 | ||||
| -rw-r--r-- | test/lisp/net/mailcap-resources/test.test | 1 | ||||
| -rw-r--r-- | test/lisp/net/mailcap-tests.el | 70 |
3 files changed, 80 insertions, 0 deletions
diff --git a/test/lisp/net/mailcap-resources/mailcap b/test/lisp/net/mailcap-resources/mailcap new file mode 100644 index 00000000000..ad3f7404fda --- /dev/null +++ b/test/lisp/net/mailcap-resources/mailcap | |||
| @@ -0,0 +1,9 @@ | |||
| 1 | audio/ogg; mpv %s | ||
| 2 | audio/flac; mpv %s | ||
| 3 | audio/x-wav; aplay %s | ||
| 4 | text/*; emacsclient -t %s | ||
| 5 | application/pdf; acroread %s; test=false | ||
| 6 | application/pdf; evince %s | ||
| 7 | application/pdf; okular %s | ||
| 8 | image/*; eog %s | ||
| 9 | image/svg+xml; inkscape %s | ||
diff --git a/test/lisp/net/mailcap-resources/test.test b/test/lisp/net/mailcap-resources/test.test new file mode 100644 index 00000000000..9daeafb9864 --- /dev/null +++ b/test/lisp/net/mailcap-resources/test.test | |||
| @@ -0,0 +1 @@ | |||
| test | |||
diff --git a/test/lisp/net/mailcap-tests.el b/test/lisp/net/mailcap-tests.el index 362cb22bb2f..b439c08c791 100644 --- a/test/lisp/net/mailcap-tests.el +++ b/test/lisp/net/mailcap-tests.el | |||
| @@ -63,4 +63,74 @@ | |||
| 63 | (append mailcap-tests-path-extensions | 63 | (append mailcap-tests-path-extensions |
| 64 | mailcap-tests-mime-extensions)))) | 64 | mailcap-tests-mime-extensions)))) |
| 65 | 65 | ||
| 66 | (defmacro with-pristine-mailcap (&rest body) | ||
| 67 | ;; We only want the mailcap info we define ourselves. | ||
| 68 | `(let (mailcap--computed-mime-data | ||
| 69 | mailcap-mime-data | ||
| 70 | mailcap-user-mime-data) | ||
| 71 | ;; `mailcap-mime-info' calls `mailcap-parse-mailcaps' which parses | ||
| 72 | ;; the system's mailcaps. We don't want that for our test. | ||
| 73 | (cl-letf (((symbol-function 'mailcap-parse-mailcaps) #'ignore)) | ||
| 74 | ,@body))) | ||
| 75 | |||
| 76 | (ert-deftest mailcap-parsing-and-mailcap-mime-info () | ||
| 77 | (with-pristine-mailcap | ||
| 78 | ;; One mailcap entry has a test=false field. The shell command | ||
| 79 | ;; execution errors when running the tests from the Makefile | ||
| 80 | ;; because then HOME=/nonexistent. | ||
| 81 | (ert-with-temp-directory home | ||
| 82 | (setenv "HOME" home) | ||
| 83 | ;; Now parse our resource mailcap file. | ||
| 84 | (mailcap-parse-mailcap (ert-resource-file "mailcap")) | ||
| 85 | |||
| 86 | ;; Assert that we get what we have defined. | ||
| 87 | (dolist (type '("audio/ogg" "audio/flac")) | ||
| 88 | (should (string= "mpv %s" (mailcap-mime-info type)))) | ||
| 89 | (should (string= "aplay %s" (mailcap-mime-info "audio/x-wav"))) | ||
| 90 | (should (string= "emacsclient -t %s" | ||
| 91 | (mailcap-mime-info "text/plain"))) | ||
| 92 | ;; evince is chosen because acroread has test=false and okular | ||
| 93 | ;; comes later. | ||
| 94 | (should (string= "evince %s" | ||
| 95 | (mailcap-mime-info "application/pdf"))) | ||
| 96 | (should (string= "inkscape %s" | ||
| 97 | (mailcap-mime-info "image/svg+xml"))) | ||
| 98 | (should (string= "eog %s" | ||
| 99 | (mailcap-mime-info "image/jpg"))) | ||
| 100 | ;; With REQUEST being a number, all fields of the selected entry | ||
| 101 | ;; should be returned. | ||
| 102 | (should (equal '((viewer . "evince %s") | ||
| 103 | (type . "application/pdf")) | ||
| 104 | (mailcap-mime-info "application/pdf" 1))) | ||
| 105 | ;; With 'all, all applicable entries should be returned. | ||
| 106 | (should (equal '(((viewer . "evince %s") | ||
| 107 | (type . "application/pdf")) | ||
| 108 | ((viewer . "okular %s") | ||
| 109 | (type . "application/pdf"))) | ||
| 110 | (mailcap-mime-info "application/pdf" 'all))) | ||
| 111 | (let* ((c nil) | ||
| 112 | (toggle (lambda (_) (setq c (not c))))) | ||
| 113 | (mailcap-add "audio/ogg" "toggle %s" toggle) | ||
| 114 | (should (string= "toggle %s" (mailcap-mime-info "audio/ogg"))) | ||
| 115 | ;; The test results are cached, so in order to have the test | ||
| 116 | ;; re-evaluated, one needs to clear the cache. | ||
| 117 | (setq mailcap-viewer-test-cache nil) | ||
| 118 | (should (string= "mpv %s" (mailcap-mime-info "audio/ogg"))) | ||
| 119 | (setq mailcap-viewer-test-cache nil) | ||
| 120 | (should (string= "toggle %s" (mailcap-mime-info "audio/ogg"))))))) | ||
| 121 | |||
| 122 | (defvar mailcap--test-result nil) | ||
| 123 | (defun mailcap--test-viewer () | ||
| 124 | (setq mailcap--test-result (string= (buffer-string) "test\n"))) | ||
| 125 | |||
| 126 | (ert-deftest mailcap-view-file () | ||
| 127 | (with-pristine-mailcap | ||
| 128 | ;; Try using a lambda as viewer and check wether | ||
| 129 | ;; `mailcap-view-file' works correctly. | ||
| 130 | (let* ((mailcap-mime-extensions '((".test" . "test/test")))) | ||
| 131 | (mailcap-add "test/test" 'mailcap--test-viewer) | ||
| 132 | (save-window-excursion | ||
| 133 | (mailcap-view-file (ert-resource-file "test.test"))) | ||
| 134 | (should mailcap--test-result)))) | ||
| 135 | |||
| 66 | ;;; mailcap-tests.el ends here | 136 | ;;; mailcap-tests.el ends here |