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