diff options
| author | Nicolas Petton | 2018-03-13 22:07:08 +0100 |
|---|---|---|
| committer | Nicolas Petton | 2018-03-18 20:47:35 +0100 |
| commit | 6d2b50a2451a9d7aae040cb82040c2211df75c85 (patch) | |
| tree | d6beb72fc8f9da6e350cc056fa9d49b0be525560 | |
| parent | 9942734c75c7b78efba41204432533cd3fe3b419 (diff) | |
| download | emacs-6d2b50a2451a9d7aae040cb82040c2211df75c85.tar.gz emacs-6d2b50a2451a9d7aae040cb82040c2211df75c85.zip | |
Add URL handler for file-name-directory (Bug#30444)
* lisp/url/url-handlers.el (url-handler-file-name-directory): New
function which handles special cases for `file-name-directory' and
URLs.
* test/lisp/url/url-handlers-test.el: New file. Add tests for
`url-handler-file-name-directory'.
| -rw-r--r-- | lisp/url/url-handlers.el | 9 | ||||
| -rw-r--r-- | test/lisp/url/url-handlers-test.el | 75 |
2 files changed, 84 insertions, 0 deletions
diff --git a/lisp/url/url-handlers.el b/lisp/url/url-handlers.el index 7d0320cb5b1..17c76cff75a 100644 --- a/lisp/url/url-handlers.el +++ b/lisp/url/url-handlers.el | |||
| @@ -186,6 +186,7 @@ the arguments that would have been passed to OPERATION." | |||
| 186 | (put 'file-name-absolute-p 'url-file-handlers (lambda (&rest ignored) t)) | 186 | (put 'file-name-absolute-p 'url-file-handlers (lambda (&rest ignored) t)) |
| 187 | (put 'expand-file-name 'url-file-handlers 'url-handler-expand-file-name) | 187 | (put 'expand-file-name 'url-file-handlers 'url-handler-expand-file-name) |
| 188 | (put 'directory-file-name 'url-file-handlers 'url-handler-directory-file-name) | 188 | (put 'directory-file-name 'url-file-handlers 'url-handler-directory-file-name) |
| 189 | (put 'file-name-directory 'url-file-handlers 'url-handler-file-name-directory) | ||
| 189 | (put 'unhandled-file-name-directory 'url-file-handlers 'url-handler-unhandled-file-name-directory) | 190 | (put 'unhandled-file-name-directory 'url-file-handlers 'url-handler-unhandled-file-name-directory) |
| 190 | (put 'file-remote-p 'url-file-handlers 'url-handler-file-remote-p) | 191 | (put 'file-remote-p 'url-file-handlers 'url-handler-file-remote-p) |
| 191 | ;; (put 'file-name-as-directory 'url-file-handlers 'url-handler-file-name-as-directory) | 192 | ;; (put 'file-name-as-directory 'url-file-handlers 'url-handler-file-name-as-directory) |
| @@ -231,6 +232,14 @@ the arguments that would have been passed to OPERATION." | |||
| 231 | ;; a local process. | 232 | ;; a local process. |
| 232 | nil))) | 233 | nil))) |
| 233 | 234 | ||
| 235 | (defun url-handler-file-name-directory (dir) | ||
| 236 | (let ((url (url-generic-parse-url dir))) | ||
| 237 | ;; Do not attempt to handle `file' URLs which are local. | ||
| 238 | (if (and (not (equal (url-type url) "file")) | ||
| 239 | (string-empty-p (url-filename url))) | ||
| 240 | (url-handler-file-name-directory (concat dir "/")) | ||
| 241 | (url-run-real-handler 'file-name-directory (list dir))))) | ||
| 242 | |||
| 234 | (defun url-handler-file-remote-p (filename &optional identification _connected) | 243 | (defun url-handler-file-remote-p (filename &optional identification _connected) |
| 235 | (let ((url (url-generic-parse-url filename))) | 244 | (let ((url (url-generic-parse-url filename))) |
| 236 | (if (and (url-type url) (not (equal (url-type url) "file"))) | 245 | (if (and (url-type url) (not (equal (url-type url) "file"))) |
diff --git a/test/lisp/url/url-handlers-test.el b/test/lisp/url/url-handlers-test.el new file mode 100644 index 00000000000..5822e16a88a --- /dev/null +++ b/test/lisp/url/url-handlers-test.el | |||
| @@ -0,0 +1,75 @@ | |||
| 1 | ;;; url-handlers-test.el --- Test suite for url-handlers.el -*- lexical-binding: t; -*- | ||
| 2 | |||
| 3 | ;; Copyright (C) 2018 Free Software Foundation, Inc. | ||
| 4 | |||
| 5 | ;; Author: Nicolas Petton <nicolas@petton.fr> | ||
| 6 | |||
| 7 | ;; This program is free software; you can redistribute it and/or modify | ||
| 8 | ;; it under the terms of the GNU General Public License as published by | ||
| 9 | ;; the Free Software Foundation, either version 3 of the License, or | ||
| 10 | ;; (at your option) any later version. | ||
| 11 | |||
| 12 | ;; This program is distributed in the hope that it will be useful, | ||
| 13 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 14 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 15 | ;; GNU General Public License for more details. | ||
| 16 | |||
| 17 | ;; You should have received a copy of the GNU General Public License | ||
| 18 | ;; along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
| 19 | |||
| 20 | ;;; Commentary: | ||
| 21 | |||
| 22 | ;;; Code: | ||
| 23 | |||
| 24 | (require 'ert) | ||
| 25 | (require 'url-handlers) | ||
| 26 | |||
| 27 | (defmacro with-url-handler-mode (&rest body) | ||
| 28 | "Evaluate BODY with `url-handler-mode' turned on." | ||
| 29 | (declare (indent 0) (debug t)) | ||
| 30 | (let ((url-handler-mode-active (make-symbol "url-handler-mode-active"))) | ||
| 31 | `(let ((,url-handler-mode-active url-handler-mode)) | ||
| 32 | (unwind-protect | ||
| 33 | (progn | ||
| 34 | (unless ,url-handler-mode-active | ||
| 35 | (url-handler-mode)) | ||
| 36 | ,@body) | ||
| 37 | (unless ,url-handler-mode-active | ||
| 38 | (url-handler-mode -1)))))) | ||
| 39 | |||
| 40 | (ert-deftest url-handlers-file-name-directory/preserve-url-types () | ||
| 41 | (with-url-handler-mode | ||
| 42 | (should (equal (file-name-directory "https://gnu.org/index.html") | ||
| 43 | "https://gnu.org/")) | ||
| 44 | (should (equal (file-name-directory "http://gnu.org/index.html") | ||
| 45 | "http://gnu.org/")) | ||
| 46 | (should (equal (file-name-directory "ftp://gnu.org/index.html") | ||
| 47 | "ftp://gnu.org/")))) | ||
| 48 | |||
| 49 | (ert-deftest url-handlers-file-name-directory/should-not-handle-non-url-file-names () | ||
| 50 | (with-url-handler-mode | ||
| 51 | (should-not (equal (file-name-directory "not-uri://gnu.org") | ||
| 52 | "not-uri://gnu.org/")))) | ||
| 53 | |||
| 54 | (ert-deftest url-handlers-file-name-directory/sub-directories () | ||
| 55 | (with-url-handler-mode | ||
| 56 | (should (equal (file-name-directory "https://foo/bar/baz/index.html") | ||
| 57 | "https://foo/bar/baz/")))) | ||
| 58 | |||
| 59 | (ert-deftest url-handlers-file-name-directory/file-urls () | ||
| 60 | (with-url-handler-mode | ||
| 61 | (should (equal (file-name-directory "file:///foo/bar/baz.txt") | ||
| 62 | "file:///foo/bar/")) | ||
| 63 | (should (equal (file-name-directory "file:///") | ||
| 64 | "file:///")))) | ||
| 65 | |||
| 66 | ;; Regression test for bug#30444 | ||
| 67 | (ert-deftest url-handlers-file-name-directory/no-filename () | ||
| 68 | (with-url-handler-mode | ||
| 69 | (should (equal (file-name-directory "https://foo.org") | ||
| 70 | "https://foo.org/")) | ||
| 71 | (should (equal (file-name-directory "https://foo.org/") | ||
| 72 | "https://foo.org/")))) | ||
| 73 | |||
| 74 | (provide 'url-handlers-test) | ||
| 75 | ;;; url-handlers-test.el ends here | ||