Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80736 views
1
// http://www.bashcookbook.com/bashinfo/source/bash-1.14.7/tests/glob-test
2
//
3
// TODO: Some of these tests do very bad things with backslashes, and will
4
// most likely fail badly on windows. They should probably be skipped.
5
6
var tap = require("tap")
7
, globalBefore = Object.keys(global)
8
, mm = require("../")
9
, files = [ "a", "b", "c", "d", "abc"
10
, "abd", "abe", "bb", "bcd"
11
, "ca", "cb", "dd", "de"
12
, "bdir/", "bdir/cfile"]
13
, next = files.concat([ "a-b", "aXb"
14
, ".x", ".y" ])
15
16
tap.test("basic tests", function (t) {
17
var start = Date.now()
18
19
// [ pattern, [matches], MM opts, files, TAP opts]
20
; [ "http://www.bashcookbook.com/bashinfo" +
21
"/source/bash-1.14.7/tests/glob-test"
22
, ["a*", ["a", "abc", "abd", "abe"]]
23
, ["X*", ["X*"], {nonull: true}]
24
25
// allow null glob expansion
26
, ["X*", []]
27
28
// isaacs: Slightly different than bash/sh/ksh
29
// \\* is not un-escaped to literal "*" in a failed match,
30
// but it does make it get treated as a literal star
31
, ["\\*", ["\\*"], {nonull: true}]
32
, ["\\**", ["\\**"], {nonull: true}]
33
, ["\\*\\*", ["\\*\\*"], {nonull: true}]
34
35
, ["b*/", ["bdir/"]]
36
, ["c*", ["c", "ca", "cb"]]
37
, ["**", files]
38
39
, ["\\.\\./*/", ["\\.\\./*/"], {nonull: true}]
40
, ["s/\\..*//", ["s/\\..*//"], {nonull: true}]
41
42
, "legendary larry crashes bashes"
43
, ["/^root:/{s/^[^:]*:[^:]*:\([^:]*\).*$/\\1/"
44
, ["/^root:/{s/^[^:]*:[^:]*:\([^:]*\).*$/\\1/"], {nonull: true}]
45
, ["/^root:/{s/^[^:]*:[^:]*:\([^:]*\).*$/\1/"
46
, ["/^root:/{s/^[^:]*:[^:]*:\([^:]*\).*$/\1/"], {nonull: true}]
47
48
, "character classes"
49
, ["[a-c]b*", ["abc", "abd", "abe", "bb", "cb"]]
50
, ["[a-y]*[^c]", ["abd", "abe", "bb", "bcd",
51
"bdir/", "ca", "cb", "dd", "de"]]
52
, ["a*[^c]", ["abd", "abe"]]
53
, function () { files.push("a-b", "aXb") }
54
, ["a[X-]b", ["a-b", "aXb"]]
55
, function () { files.push(".x", ".y") }
56
, ["[^a-c]*", ["d", "dd", "de"]]
57
, function () { files.push("a*b/", "a*b/ooo") }
58
, ["a\\*b/*", ["a*b/ooo"]]
59
, ["a\\*?/*", ["a*b/ooo"]]
60
, ["*\\\\!*", [], {null: true}, ["echo !7"]]
61
, ["*\\!*", ["echo !7"], null, ["echo !7"]]
62
, ["*.\\*", ["r.*"], null, ["r.*"]]
63
, ["a[b]c", ["abc"]]
64
, ["a[\\b]c", ["abc"]]
65
, ["a?c", ["abc"]]
66
, ["a\\*c", [], {null: true}, ["abc"]]
67
, ["", [""], { null: true }, [""]]
68
69
, "http://www.opensource.apple.com/source/bash/bash-23/" +
70
"bash/tests/glob-test"
71
, function () { files.push("man/", "man/man1/", "man/man1/bash.1") }
72
, ["*/man*/bash.*", ["man/man1/bash.1"]]
73
, ["man/man1/bash.1", ["man/man1/bash.1"]]
74
, ["a***c", ["abc"], null, ["abc"]]
75
, ["a*****?c", ["abc"], null, ["abc"]]
76
, ["?*****??", ["abc"], null, ["abc"]]
77
, ["*****??", ["abc"], null, ["abc"]]
78
, ["?*****?c", ["abc"], null, ["abc"]]
79
, ["?***?****c", ["abc"], null, ["abc"]]
80
, ["?***?****?", ["abc"], null, ["abc"]]
81
, ["?***?****", ["abc"], null, ["abc"]]
82
, ["*******c", ["abc"], null, ["abc"]]
83
, ["*******?", ["abc"], null, ["abc"]]
84
, ["a*cd**?**??k", ["abcdecdhjk"], null, ["abcdecdhjk"]]
85
, ["a**?**cd**?**??k", ["abcdecdhjk"], null, ["abcdecdhjk"]]
86
, ["a**?**cd**?**??k***", ["abcdecdhjk"], null, ["abcdecdhjk"]]
87
, ["a**?**cd**?**??***k", ["abcdecdhjk"], null, ["abcdecdhjk"]]
88
, ["a**?**cd**?**??***k**", ["abcdecdhjk"], null, ["abcdecdhjk"]]
89
, ["a****c**?**??*****", ["abcdecdhjk"], null, ["abcdecdhjk"]]
90
, ["[-abc]", ["-"], null, ["-"]]
91
, ["[abc-]", ["-"], null, ["-"]]
92
, ["\\", ["\\"], null, ["\\"]]
93
, ["[\\\\]", ["\\"], null, ["\\"]]
94
, ["[[]", ["["], null, ["["]]
95
, ["[", ["["], null, ["["]]
96
, ["[*", ["[abc"], null, ["[abc"]]
97
, "a right bracket shall lose its special meaning and\n" +
98
"represent itself in a bracket expression if it occurs\n" +
99
"first in the list. -- POSIX.2 2.8.3.2"
100
, ["[]]", ["]"], null, ["]"]]
101
, ["[]-]", ["]"], null, ["]"]]
102
, ["[a-\z]", ["p"], null, ["p"]]
103
, ["??**********?****?", [], { null: true }, ["abc"]]
104
, ["??**********?****c", [], { null: true }, ["abc"]]
105
, ["?************c****?****", [], { null: true }, ["abc"]]
106
, ["*c*?**", [], { null: true }, ["abc"]]
107
, ["a*****c*?**", [], { null: true }, ["abc"]]
108
, ["a********???*******", [], { null: true }, ["abc"]]
109
, ["[]", [], { null: true }, ["a"]]
110
, ["[abc", [], { null: true }, ["["]]
111
112
, "nocase tests"
113
, ["XYZ", ["xYz"], { nocase: true, null: true }
114
, ["xYz", "ABC", "IjK"]]
115
, ["ab*", ["ABC"], { nocase: true, null: true }
116
, ["xYz", "ABC", "IjK"]]
117
, ["[ia]?[ck]", ["ABC", "IjK"], { nocase: true, null: true }
118
, ["xYz", "ABC", "IjK"]]
119
120
// [ pattern, [matches], MM opts, files, TAP opts]
121
, "onestar/twostar"
122
, ["{/*,*}", [], {null: true}, ["/asdf/asdf/asdf"]]
123
, ["{/?,*}", ["/a", "bb"], {null: true}
124
, ["/a", "/b/b", "/a/b/c", "bb"]]
125
126
, "dots should not match unless requested"
127
, ["**", ["a/b"], {}, ["a/b", "a/.d", ".a/.d"]]
128
129
// .. and . can only match patterns starting with .,
130
// even when options.dot is set.
131
, function () {
132
files = ["a/./b", "a/../b", "a/c/b", "a/.d/b"]
133
}
134
, ["a/*/b", ["a/c/b", "a/.d/b"], {dot: true}]
135
, ["a/.*/b", ["a/./b", "a/../b", "a/.d/b"], {dot: true}]
136
, ["a/*/b", ["a/c/b"], {dot:false}]
137
, ["a/.*/b", ["a/./b", "a/../b", "a/.d/b"], {dot: false}]
138
139
140
// this also tests that changing the options needs
141
// to change the cache key, even if the pattern is
142
// the same!
143
, ["**", ["a/b","a/.d",".a/.d"], { dot: true }
144
, [ ".a/.d", "a/.d", "a/b"]]
145
146
, "paren sets cannot contain slashes"
147
, ["*(a/b)", ["*(a/b)"], {nonull: true}, ["a/b"]]
148
149
// brace sets trump all else.
150
//
151
// invalid glob pattern. fails on bash4 and bsdglob.
152
// however, in this implementation, it's easier just
153
// to do the intuitive thing, and let brace-expansion
154
// actually come before parsing any extglob patterns,
155
// like the documentation seems to say.
156
//
157
// XXX: if anyone complains about this, either fix it
158
// or tell them to grow up and stop complaining.
159
//
160
// bash/bsdglob says this:
161
// , ["*(a|{b),c)}", ["*(a|{b),c)}"], {}, ["a", "ab", "ac", "ad"]]
162
// but we do this instead:
163
, ["*(a|{b),c)}", ["a", "ab", "ac"], {}, ["a", "ab", "ac", "ad"]]
164
165
// test partial parsing in the presence of comment/negation chars
166
, ["[!a*", ["[!ab"], {}, ["[!ab", "[ab"]]
167
, ["[#a*", ["[#ab"], {}, ["[#ab", "[ab"]]
168
169
// like: {a,b|c\\,d\\\|e} except it's unclosed, so it has to be escaped.
170
, ["+(a|*\\|c\\\\|d\\\\\\|e\\\\\\\\|f\\\\\\\\\\|g"
171
, ["+(a|b\\|c\\\\|d\\\\|e\\\\\\\\|f\\\\\\\\|g"]
172
, {}
173
, ["+(a|b\\|c\\\\|d\\\\|e\\\\\\\\|f\\\\\\\\|g", "a", "b\\c"]]
174
175
176
// crazy nested {,,} and *(||) tests.
177
, function () {
178
files = [ "a", "b", "c", "d"
179
, "ab", "ac", "ad"
180
, "bc", "cb"
181
, "bc,d", "c,db", "c,d"
182
, "d)", "(b|c", "*(b|c"
183
, "b|c", "b|cc", "cb|c"
184
, "x(a|b|c)", "x(a|c)"
185
, "(a|b|c)", "(a|c)"]
186
}
187
, ["*(a|{b,c})", ["a", "b", "c", "ab", "ac"]]
188
, ["{a,*(b|c,d)}", ["a","(b|c", "*(b|c", "d)"]]
189
// a
190
// *(b|c)
191
// *(b|d)
192
, ["{a,*(b|{c,d})}", ["a","b", "bc", "cb", "c", "d"]]
193
, ["*(a|{b|c,c})", ["a", "b", "c", "ab", "ac", "bc", "cb"]]
194
195
196
// test various flag settings.
197
, [ "*(a|{b|c,c})", ["x(a|b|c)", "x(a|c)", "(a|b|c)", "(a|c)"]
198
, { noext: true } ]
199
, ["a?b", ["x/y/acb", "acb/"], {matchBase: true}
200
, ["x/y/acb", "acb/", "acb/d/e", "x/y/acb/d"] ]
201
, ["#*", ["#a", "#b"], {nocomment: true}, ["#a", "#b", "c#d"]]
202
203
204
// begin channelling Boole and deMorgan...
205
, "negation tests"
206
, function () {
207
files = ["d", "e", "!ab", "!abc", "a!b", "\\!a"]
208
}
209
210
// anything that is NOT a* matches.
211
, ["!a*", ["\\!a", "d", "e", "!ab", "!abc"]]
212
213
// anything that IS !a* matches.
214
, ["!a*", ["!ab", "!abc"], {nonegate: true}]
215
216
// anything that IS a* matches
217
, ["!!a*", ["a!b"]]
218
219
// anything that is NOT !a* matches
220
, ["!\\!a*", ["a!b", "d", "e", "\\!a"]]
221
222
// negation nestled within a pattern
223
, function () {
224
files = [ "foo.js"
225
, "foo.bar"
226
// can't match this one without negative lookbehind.
227
, "foo.js.js"
228
, "blar.js"
229
, "foo."
230
, "boo.js.boo" ]
231
}
232
, ["*.!(js)", ["foo.bar", "foo.", "boo.js.boo"] ]
233
234
].forEach(function (c) {
235
if (typeof c === "function") return c()
236
if (typeof c === "string") return t.comment(c)
237
238
var pattern = c[0]
239
, expect = c[1].sort(alpha)
240
, options = c[2]
241
, f = c[3] || files
242
, tapOpts = c[4] || {}
243
244
// options.debug = true
245
var Class = mm.defaults(options).Minimatch
246
var m = new Class(pattern, {})
247
var r = m.makeRe()
248
tapOpts.re = String(r) || JSON.stringify(r)
249
tapOpts.files = JSON.stringify(f)
250
tapOpts.pattern = pattern
251
tapOpts.set = m.set
252
tapOpts.negated = m.negate
253
254
var actual = mm.match(f, pattern, options)
255
actual.sort(alpha)
256
257
t.equivalent( actual, expect
258
, JSON.stringify(pattern) + " " + JSON.stringify(expect)
259
, tapOpts )
260
})
261
262
t.comment("time=" + (Date.now() - start) + "ms")
263
t.end()
264
})
265
266
tap.test("global leak test", function (t) {
267
var globalAfter = Object.keys(global)
268
t.equivalent(globalAfter, globalBefore, "no new globals, please")
269
t.end()
270
})
271
272
function alpha (a, b) {
273
return a > b ? 1 : -1
274
}
275
276