Replace virtual-directory with more general virtual-fs implementation
parent
153ff71fcf
commit
ef9a7336cf
@ -0,0 +1,86 @@
|
||||
;;; phpinspect-fs.el --- PHP parsing and completion package -*- lexical-binding: t; -*-
|
||||
|
||||
;; Copyright (C) 2021 Free Software Foundation, Inc
|
||||
|
||||
;; Author: Hugo Thunnissen <devel@hugot.nl>
|
||||
;; Keywords: php, languages, tools, convenience
|
||||
;; Version: 0
|
||||
|
||||
;; This program is free software; you can redistribute it and/or modify
|
||||
;; it under the terms of the GNU General Public License as published by
|
||||
;; the Free Software Foundation, either version 3 of the License, or
|
||||
;; (at your option) any later version.
|
||||
|
||||
;; This program is distributed in the hope that it will be useful,
|
||||
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
;; GNU General Public License for more details.
|
||||
|
||||
;; You should have received a copy of the GNU General Public License
|
||||
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
;;; Commentary:
|
||||
|
||||
;;; Code:
|
||||
|
||||
(cl-defstruct (phpinspect-fs (:constructor phpinspect-make-fs)))
|
||||
|
||||
(cl-defstruct (phpinspect-virtual-fs (:constructor phpinspect-make-virtual-fs))
|
||||
(files (make-hash-table :test 'equal)
|
||||
:type hash-table
|
||||
:documentation
|
||||
"The files in the virtual filesystem"))
|
||||
|
||||
(cl-defgeneric phpinspect-fs-file-exists-p (fs file))
|
||||
(cl-defgeneric phpinspect-fs-insert-file-contents (fs file))
|
||||
(cl-defgeneric phpinspect-fs-directory-files (fs directory match))
|
||||
(cl-defgeneric phpinspect-fs-directory-files-recursively (fs directory match))
|
||||
|
||||
(cl-defmethod phpinspect-fs-file-exists-p ((fs phpinspect-fs) file)
|
||||
(file-exists-p file))
|
||||
|
||||
(cl-defmethod phpinspect-fs-file-exists-p ((fs phpinspect-virtual-fs) file)
|
||||
(and (gethash file (phpinspect-virtual-fs-files fs)) t))
|
||||
|
||||
(cl-defmethod phpinspect-fs-insert-file-contents ((fs phpinspect-fs) file)
|
||||
(insert-file-contents-literally file))
|
||||
|
||||
(cl-defmethod phpinspect-fs-insert-file-contents ((fs phpinspect-virtual-fs) file)
|
||||
(insert (or (gethash file (phpinspect-virtual-fs-files fs)) "")))
|
||||
|
||||
(cl-defmethod phpinspect-fs-directory-files ((fs phpinspect-fs) directory &optional match)
|
||||
(directory-files directory t match t))
|
||||
|
||||
(cl-defmethod phpinspect-fs-directory-files ((fs phpinspect-virtual-fs) directory &optional match)
|
||||
(setq directory (replace-regexp-in-string "[/]+" "/" (concat directory "/")))
|
||||
(let ((files))
|
||||
(maphash
|
||||
(lambda (file _ignored)
|
||||
(let ((basename (string-remove-prefix directory file)))
|
||||
(when (and (string-prefix-p directory file)
|
||||
(string-match-p "^[^/]*$" basename)
|
||||
(if match (string-match-p match basename) t))
|
||||
(push file files))))
|
||||
(phpinspect-virtual-fs-files fs))
|
||||
files))
|
||||
|
||||
(cl-defmethod phpinspect-fs-directory-files-recursively ((fs phpinspect-fs) directory &optional match)
|
||||
(directory-files-recursively directory
|
||||
match
|
||||
t ;; Ignore directories that cannot be read
|
||||
t ;; follow symlinks
|
||||
))
|
||||
|
||||
(cl-defmethod phpinspect-fs-directory-files-recursively ((fs phpinspect-virtual-fs) directory &optional match)
|
||||
(setq directory (replace-regexp-in-string "[/]+" "/" (concat directory "/")))
|
||||
(let ((files))
|
||||
(maphash
|
||||
(lambda (file _ignored)
|
||||
(when (and (string-prefix-p directory file)
|
||||
(if match (string-match-p match file) t))
|
||||
(push file files)))
|
||||
(phpinspect-virtual-fs-files fs))
|
||||
files))
|
||||
|
||||
(provide 'phpinspect-fs)
|
||||
;;; phpinspect-fs.el ends here
|
@ -0,0 +1,88 @@
|
||||
;;; test-autoload.el --- Unit tests for phpinspect.el -*- lexical-binding: t; -*-
|
||||
|
||||
;; Copyright (C) 2021 Free Software Foundation, Inc.
|
||||
|
||||
;; Author: Hugo Thunnissen <devel@hugot.nl>
|
||||
|
||||
;; This program is free software; you can redistribute it and/or modify
|
||||
;; it under the terms of the GNU General Public License as published by
|
||||
;; the Free Software Foundation, either version 3 of the License, or
|
||||
;; (at your option) any later version.
|
||||
|
||||
;; This program is distributed in the hope that it will be useful,
|
||||
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
;; GNU General Public License for more details.
|
||||
|
||||
;; You should have received a copy of the GNU General Public License
|
||||
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
;;; Commentary:
|
||||
|
||||
;;
|
||||
|
||||
;;; Code:
|
||||
|
||||
(require 'phpinspect-fs)
|
||||
|
||||
(ert-deftest phpinspect-virtual-fs-file-exists-p ()
|
||||
(let ((fs (phpinspect-make-virtual-fs)))
|
||||
(puthash "/test/test.txt" "contents" (phpinspect-virtual-fs-files fs))
|
||||
|
||||
(should (phpinspect-fs-file-exists-p fs "/test/test.txt"))))
|
||||
|
||||
(ert-deftest phpinspect-virtual-fs-insert-file-contents ()
|
||||
(let ((fs (phpinspect-make-virtual-fs)))
|
||||
(puthash "/test/test.txt" "contents" (phpinspect-virtual-fs-files fs))
|
||||
|
||||
(with-temp-buffer
|
||||
(phpinspect-fs-insert-file-contents fs "/test/test.txt")
|
||||
(should (string= "contents" (buffer-string))))
|
||||
|
||||
(with-temp-buffer
|
||||
(phpinspect-fs-insert-file-contents fs "/test/nonexistant")
|
||||
(should (string= "" (buffer-string))))))
|
||||
|
||||
(ert-deftest phpinspect-virtual-fs-directory-files-and-recursively ()
|
||||
(let ((fs (phpinspect-make-virtual-fs)))
|
||||
(puthash "/test/test.txt" "contents" (phpinspect-virtual-fs-files fs))
|
||||
(puthash "/a/b/c/dee.match" "contents" (phpinspect-virtual-fs-files fs))
|
||||
(puthash "/a/b/c/cee.match" "contents" (phpinspect-virtual-fs-files fs))
|
||||
(puthash "/a/b/c/aaa.match" "contents" (phpinspect-virtual-fs-files fs))
|
||||
(puthash "/a/b/c/nope.nomatch" "contents" (phpinspect-virtual-fs-files fs))
|
||||
(puthash "/a/b/d/jee.match" "contents" (phpinspect-virtual-fs-files fs))
|
||||
|
||||
(let ((files (phpinspect-fs-directory-files fs "/test/")))
|
||||
(should (equal '("/test/test.txt") files)))
|
||||
|
||||
(let ((files (phpinspect-fs-directory-files fs "/a/b/c")))
|
||||
(should (member "/a/b/c/dee.match" files))
|
||||
(should (member "/a/b/c/cee.match" files))
|
||||
(should (member "/a/b/c/aaa.match" files))
|
||||
(should (member "/a/b/c/nope.nomatch" files)))
|
||||
|
||||
(let ((files (phpinspect-fs-directory-files fs "/a/b/c" "\\.match$")))
|
||||
(should (member "/a/b/c/dee.match" files))
|
||||
(should (member "/a/b/c/cee.match" files))
|
||||
(should (member "/a/b/c/aaa.match" files))
|
||||
(should (not (member "/a/b/c/nope.nomatch" files))))
|
||||
|
||||
(let ((files (phpinspect-fs-directory-files-recursively fs "/a/b" "\\.match$")))
|
||||
(should (member "/a/b/c/dee.match" files))
|
||||
(should (member "/a/b/c/cee.match" files))
|
||||
(should (member "/a/b/c/aaa.match" files))
|
||||
(should (not (member "/a/b/c/nope.nomatch" files))))
|
||||
|
||||
(let ((files (phpinspect-fs-directory-files-recursively fs "/a/b")))
|
||||
(should (member "/a/b/c/dee.match" files))
|
||||
(should (member "/a/b/c/cee.match" files))
|
||||
(should (member "/a/b/c/aaa.match" files))
|
||||
(should (member "/a/b/c/nope.nomatch" files))
|
||||
(should (member "/a/b/d/jee.match" files)))
|
||||
|
||||
(let ((files (phpinspect-fs-directory-files-recursively fs "/a/b" "\\.match$")))
|
||||
(should (member "/a/b/c/dee.match" files))
|
||||
(should (member "/a/b/c/cee.match" files))
|
||||
(should (member "/a/b/c/aaa.match" files))
|
||||
(should (not (member "/a/b/c/nope.nomatch" files)))
|
||||
(should (member "/a/b/d/jee.match" files)))))
|
Loading…
Reference in New Issue