PageRenderTime 68ms CodeModel.GetById 39ms RepoModel.GetById 0ms app.codeStats 0ms

/test/lisp/eshell/em-glob-tests.el

https://github.com/mirrors/emacs
Emacs Lisp | 197 lines | 150 code | 29 blank | 18 comment | 3 complexity | 368ee839f2f13114feabbd1c5d7a2ae2 MD5 | raw file
  1. ;;; em-glob-tests.el --- em-glob test suite -*- lexical-binding:t -*-
  2. ;; Copyright (C) 2022 Free Software Foundation, Inc.
  3. ;; This file is part of GNU Emacs.
  4. ;; GNU Emacs is free software: you can redistribute it and/or modify
  5. ;; it under the terms of the GNU General Public License as published by
  6. ;; the Free Software Foundation, either version 3 of the License, or
  7. ;; (at your option) any later version.
  8. ;; GNU Emacs is distributed in the hope that it will be useful,
  9. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. ;; GNU General Public License for more details.
  12. ;; You should have received a copy of the GNU General Public License
  13. ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
  14. ;;; Commentary:
  15. ;; Tests for Eshell's glob expansion.
  16. ;;; Code:
  17. (require 'ert)
  18. (require 'em-glob)
  19. (defmacro with-fake-files (files &rest body)
  20. "Evaluate BODY forms, pretending that FILES exist on the filesystem.
  21. FILES is a list of file names that should be reported as
  22. appropriate by `file-name-all-completions'. Any file name
  23. component ending in \"symlink\" is treated as a symbolic link."
  24. (declare (indent 1))
  25. `(cl-letf (((symbol-function 'file-name-all-completions)
  26. (lambda (file directory)
  27. (cl-assert (string= file ""))
  28. (setq directory (expand-file-name directory))
  29. `("./" "../"
  30. ,@(delete-dups
  31. (remq nil
  32. (mapcar
  33. (lambda (file)
  34. (setq file (expand-file-name file))
  35. (when (string-prefix-p directory file)
  36. (replace-regexp-in-string
  37. "/.*" "/"
  38. (substring file (length directory)))))
  39. ,files))))))
  40. ((symbol-function 'file-symlink-p)
  41. (lambda (file)
  42. (string-suffix-p "symlink" file))))
  43. ,@body))
  44. ;;; Tests:
  45. (ert-deftest em-glob-test/match-any-string ()
  46. "Test that \"*\" pattern matches any string."
  47. (with-fake-files '("a.el" "b.el" "c.txt" "dir/a.el")
  48. (should (equal (eshell-extended-glob "*.el")
  49. '("a.el" "b.el")))))
  50. (ert-deftest em-glob-test/match-any-directory ()
  51. "Test that \"*/\" pattern matches any directory."
  52. (with-fake-files '("a.el" "b.el" "dir/a.el" "dir/sub/a.el" "symlink/")
  53. (should (equal (eshell-extended-glob "*/")
  54. '("dir/" "symlink/")))))
  55. (ert-deftest em-glob-test/match-any-character ()
  56. "Test that \"?\" pattern matches any character."
  57. (with-fake-files '("a.el" "b.el" "ccc.el" "d.txt" "dir/a.el")
  58. (should (equal (eshell-extended-glob "?.el")
  59. '("a.el" "b.el")))))
  60. (ert-deftest em-glob-test/match-recursive ()
  61. "Test that \"**/\" recursively matches directories."
  62. (with-fake-files '("a.el" "b.el" "ccc.el" "d.txt" "dir/a.el" "dir/sub/a.el"
  63. "dir/symlink/a.el" "symlink/a.el" "symlink/sub/a.el")
  64. (should (equal (eshell-extended-glob "**/a.el")
  65. '("a.el" "dir/a.el" "dir/sub/a.el")))
  66. (should (equal (eshell-extended-glob "**/")
  67. '("dir/" "dir/sub/")))))
  68. (ert-deftest em-glob-test/match-recursive-follow-symlinks ()
  69. "Test that \"***/\" recursively matches directories, following symlinks."
  70. (with-fake-files '("a.el" "b.el" "ccc.el" "d.txt" "dir/a.el" "dir/sub/a.el"
  71. "dir/symlink/a.el" "symlink/a.el" "symlink/sub/a.el")
  72. (should (equal (eshell-extended-glob "***/a.el")
  73. '("a.el" "dir/a.el" "dir/sub/a.el" "dir/symlink/a.el"
  74. "symlink/a.el" "symlink/sub/a.el")))
  75. (should (equal (eshell-extended-glob "***/")
  76. '("dir/" "dir/sub/" "dir/symlink/" "symlink/"
  77. "symlink/sub/")))))
  78. (ert-deftest em-glob-test/match-recursive-mixed ()
  79. "Test combination of \"**/\" and \"***/\"."
  80. (with-fake-files '("dir/a.el" "dir/sub/a.el" "dir/sub2/a.el"
  81. "dir/symlink/a.el" "dir/sub/symlink/a.el" "symlink/a.el"
  82. "symlink/sub/a.el" "symlink/sub/symlink/a.el")
  83. (should (equal (eshell-extended-glob "**/sub/***/a.el")
  84. '("dir/sub/a.el" "dir/sub/symlink/a.el")))
  85. (should (equal (eshell-extended-glob "***/sub/**/a.el")
  86. '("dir/sub/a.el" "symlink/sub/a.el")))))
  87. (ert-deftest em-glob-test/match-character-set-individual ()
  88. "Test \"[...]\" for individual characters."
  89. (with-fake-files '("a.el" "b.el" "c.el" "d.el" "dir/a.el")
  90. (should (equal (eshell-extended-glob "[ab].el")
  91. '("a.el" "b.el")))
  92. (should (equal (eshell-extended-glob "[^ab].el")
  93. '("c.el" "d.el")))))
  94. (ert-deftest em-glob-test/match-character-set-range ()
  95. "Test \"[...]\" for character ranges."
  96. (with-fake-files '("a.el" "b.el" "c.el" "d.el" "dir/a.el")
  97. (should (equal (eshell-extended-glob "[a-c].el")
  98. '("a.el" "b.el" "c.el")))
  99. (should (equal (eshell-extended-glob "[^a-c].el")
  100. '("d.el")))))
  101. (ert-deftest em-glob-test/match-character-set-class ()
  102. "Test \"[...]\" for character classes."
  103. (with-fake-files '("1.el" "a.el" "b.el" "c.el" "dir/a.el")
  104. (should (equal (eshell-extended-glob "[[:alpha:]].el")
  105. '("a.el" "b.el" "c.el")))
  106. (should (equal (eshell-extended-glob "[^[:alpha:]].el")
  107. '("1.el")))))
  108. (ert-deftest em-glob-test/match-character-set-mixed ()
  109. "Test \"[...]\" with multiple kinds of members at once."
  110. (with-fake-files '("1.el" "a.el" "b.el" "c.el" "d.el" "dir/a.el")
  111. (should (equal (eshell-extended-glob "[ac-d[:digit:]].el")
  112. '("1.el" "a.el" "c.el" "d.el")))
  113. (should (equal (eshell-extended-glob "[^ac-d[:digit:]].el")
  114. '("b.el")))))
  115. (ert-deftest em-glob-test/match-group-alternative ()
  116. "Test \"(x|y)\" matches either \"x\" or \"y\"."
  117. (with-fake-files '("em-alias.el" "em-banner.el" "esh-arg.el" "misc.el"
  118. "test/em-xtra.el")
  119. (should (equal (eshell-extended-glob "e(m|sh)-*.el")
  120. '("em-alias.el" "em-banner.el" "esh-arg.el")))))
  121. (ert-deftest em-glob-test/match-n-or-more-characters ()
  122. "Test that \"x#\" and \"x#\" match zero or more instances of \"x\"."
  123. (with-fake-files '("h.el" "ha.el" "hi.el" "hii.el" "dir/hi.el")
  124. (should (equal (eshell-extended-glob "hi#.el")
  125. '("h.el" "hi.el" "hii.el")))
  126. (should (equal (eshell-extended-glob "hi##.el")
  127. '("hi.el" "hii.el")))))
  128. (ert-deftest em-glob-test/match-n-or-more-groups ()
  129. "Test that \"(x)#\" and \"(x)#\" match zero or more instances of \"(x)\"."
  130. (with-fake-files '("h.el" "ha.el" "hi.el" "hii.el" "dir/hi.el")
  131. (should (equal (eshell-extended-glob "hi#.el")
  132. '("h.el" "hi.el" "hii.el")))
  133. (should (equal (eshell-extended-glob "hi##.el")
  134. '("hi.el" "hii.el")))))
  135. (ert-deftest em-glob-test/match-n-or-more-character-sets ()
  136. "Test that \"[x]#\" and \"[x]#\" match zero or more instances of \"[x]\"."
  137. (with-fake-files '("w.el" "wh.el" "wha.el" "whi.el" "whaha.el" "dir/wha.el")
  138. (should (equal (eshell-extended-glob "w[ah]#.el")
  139. '("w.el" "wh.el" "wha.el" "whaha.el")))
  140. (should (equal (eshell-extended-glob "w[ah]##.el")
  141. '("wh.el" "wha.el" "whaha.el")))))
  142. (ert-deftest em-glob-test/match-x-but-not-y ()
  143. "Test that \"x~y\" matches \"x\" but not \"y\"."
  144. (with-fake-files '("1" "12" "123" "42" "dir/1")
  145. (should (equal (eshell-extended-glob "[[:digit:]]##~4?")
  146. '("1" "12" "123")))))
  147. (ert-deftest em-glob-test/match-dot-files ()
  148. "Test that dot files are matched correctly."
  149. (with-fake-files '("foo.el" ".emacs")
  150. (should (equal (eshell-extended-glob ".*")
  151. '("../" "./" ".emacs")))
  152. (let (eshell-glob-include-dot-dot)
  153. (should (equal (eshell-extended-glob ".*")
  154. '(".emacs"))))
  155. (let ((eshell-glob-include-dot-files t))
  156. (should (equal (eshell-extended-glob "*")
  157. '("../" "./" ".emacs" "foo.el")))
  158. (let (eshell-glob-include-dot-dot)
  159. (should (equal (eshell-extended-glob "*")
  160. '(".emacs" "foo.el")))))))
  161. (ert-deftest em-glob-test/no-matches ()
  162. "Test behavior when a glob fails to match any files."
  163. (with-fake-files '("foo.el" "bar.el")
  164. (should (equal (eshell-extended-glob "*.txt")
  165. "*.txt"))
  166. (let ((eshell-error-if-no-glob t))
  167. (should-error (eshell-extended-glob "*.txt")))))
  168. ;; em-glob-tests.el ends here