forked from gulpjs/glob-parent
/
index.test.js
237 lines (199 loc) · 8.38 KB
/
index.test.js
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
'use strict';
var gp = require('../');
var expect = require('expect');
var isWin32 = require('os').platform() === 'win32';
describe('glob-parent', function () {
it('should strip glob magic to return parent path', function (done) {
expect(gp('.')).toEqual('.');
expect(gp('.*')).toEqual('.');
expect(gp('/.*')).toEqual('/');
expect(gp('/.*/')).toEqual('/');
expect(gp('a/.*/b')).toEqual('a');
expect(gp('a*/.*/b')).toEqual('.');
expect(gp('*/a/b/c')).toEqual('.');
expect(gp('*')).toEqual('.');
expect(gp('*/')).toEqual('.');
expect(gp('*/*')).toEqual('.');
expect(gp('*/*/')).toEqual('.');
expect(gp('**')).toEqual('.');
expect(gp('**/')).toEqual('.');
expect(gp('**/*')).toEqual('.');
expect(gp('**/*/')).toEqual('.');
expect(gp('/*.js')).toEqual('/');
expect(gp('*.js')).toEqual('.');
expect(gp('**/*.js')).toEqual('.');
expect(gp('{a,b}')).toEqual('.');
expect(gp('/{a,b}')).toEqual('/');
expect(gp('/{a,b}/')).toEqual('/');
expect(gp('(a|b)')).toEqual('.');
expect(gp('/(a|b)')).toEqual('/');
expect(gp('./(a|b)')).toEqual('.');
expect(gp('a/(b c)')).toEqual('a'); // not an extglob
expect(gp('a/(b c)/')).toEqual('a/(b c)'); // not an extglob
expect(gp('a/(b c)/d')).toEqual('a/(b c)'); // not an extglob
expect(gp('path/to/*.js')).toEqual('path/to');
expect(gp('/root/path/to/*.js')).toEqual('/root/path/to');
expect(gp('chapter/foo [bar]/')).toEqual('chapter');
expect(gp('path/[a-z]')).toEqual('path');
expect(gp('[a-z]')).toEqual('.');
expect(gp('path/{to,from}')).toEqual('path');
expect(gp('path/(to|from)')).toEqual('path');
expect(gp('path/(foo bar)/subdir/foo.*')).toEqual('path/(foo bar)/subdir');
expect(gp('path/!(to|from)')).toEqual('path');
expect(gp('path/?(to|from)')).toEqual('path');
expect(gp('path/+(to|from)')).toEqual('path');
expect(gp('path/*(to|from)')).toEqual('path');
expect(gp('path/@(to|from)')).toEqual('path');
expect(gp('path/!/foo')).toEqual('path/!');
expect(gp('path/?/foo')).toEqual('path/?');
expect(gp('path/+/foo')).toEqual('path/+');
expect(gp('path/*/foo')).toEqual('path');
expect(gp('path/@/foo')).toEqual('path/@');
expect(gp('path/!/foo/')).toEqual('path/!/foo');
expect(gp('path/?/foo/')).toEqual('path/?/foo');
expect(gp('path/+/foo/')).toEqual('path/+/foo');
expect(gp('path/*/foo/')).toEqual('path');
expect(gp('path/@/foo/')).toEqual('path/@/foo');
expect(gp('path/**/*')).toEqual('path');
expect(gp('path/**/subdir/foo.*')).toEqual('path');
expect(gp('path/subdir/**/foo.js')).toEqual('path/subdir');
expect(gp('path/!subdir/foo.js')).toEqual('path/!subdir');
expect(gp('path/{foo,bar}/')).toEqual('path');
done();
});
it('should respect escaped characters', function (done) {
expect(gp('path/\\*\\*/subdir/foo.*')).toEqual('path/**/subdir');
expect(gp('path/\\[\\*\\]/subdir/foo.*')).toEqual('path/[*]/subdir');
expect(gp('path/\\*(a|b)/subdir/foo.*')).toEqual('path');
expect(gp('path/\\*/(a|b)/subdir/foo.*')).toEqual('path/*');
expect(gp('path/\\*\\(a\\|b\\)/subdir/foo.*')).toEqual(
'path/*(a|b)/subdir'
);
expect(gp('path/\\[foo bar\\]/subdir/foo.*')).toEqual(
'path/[foo bar]/subdir'
);
expect(gp('path/\\[bar]/')).toEqual('path/[bar]');
expect(gp('path/\\[bar]')).toEqual('path');
expect(gp('[bar]')).toEqual('.');
expect(gp('[bar]/')).toEqual('.');
expect(gp('./\\[bar]')).toEqual('.');
expect(gp('\\[bar]/')).toEqual('[bar]');
expect(gp('\\!dir/*')).toEqual('!dir');
expect(gp('[bar\\]/')).toEqual('.');
expect(gp('path/foo \\[bar]/')).toEqual('path/foo [bar]');
expect(gp('path/\\{foo,bar}/')).toEqual('path/{foo,bar}');
expect(gp('\\{foo,bar}/')).toEqual('{foo,bar}');
expect(gp('\\{foo,bar\\}/')).toEqual('{foo,bar}');
expect(gp('{foo,bar\\}/')).toEqual('.');
if (isWin32) {
// On Windows we are trying to flip backslashes foo-\\( → foo-/(
expect(gp('foo-\\(bar\\).md')).toEqual('foo-');
} else {
expect(gp('foo-\\(bar\\).md')).toEqual('.');
expect(gp('\\[bar]')).toEqual('.');
expect(gp('[bar\\]')).toEqual('.');
expect(gp('\\{foo,bar\\}')).toEqual('.');
expect(gp('{foo,bar\\}')).toEqual('.');
}
done();
});
it('should respect glob enclosures with embedded separators', function (done) {
/* eslint-disable no-useless-escape */
expect(gp('path/{,/,bar/baz,qux}/')).toEqual('path');
expect(gp('path/\\{,/,bar/baz,qux}/')).toEqual('path/{,/,bar/baz,qux}');
expect(gp('path/\\{,/,bar/baz,qux\\}/')).toEqual('path/{,/,bar/baz,qux}');
expect(gp('/{,/,bar/baz,qux}/')).toEqual('/');
expect(gp('/\\{,/,bar/baz,qux}/')).toEqual('/{,/,bar/baz,qux}');
expect(gp('{,/,bar/baz,qux}')).toEqual('.');
expect(gp('\\{,/,bar/baz,qux\\}')).toEqual('{,/,bar/baz,qux}');
expect(gp('\\{,/,bar/baz,qux}/')).toEqual('{,/,bar/baz,qux}');
expect(gp('path/foo[a\\/]/')).toEqual('path');
expect(gp('path/foo\\[a\\/]/')).toEqual('path/foo[a\\/]');
expect(gp('foo[a\\/]')).toEqual('.');
expect(gp('foo\\[a\\/]')).toEqual('foo[a\\/]');
expect(gp('path/(foo/bar|baz)')).toEqual('path');
expect(gp('path/(foo/bar|baz)/')).toEqual('path');
expect(gp('path/\\(foo/bar|baz)/')).toEqual('path/(foo/bar|baz)');
/* eslint-enable no-useless-escape */
done();
});
it('should handle nested braces', function (done) {
expect(gp('path/{../,./,{bar,/baz\\},qux\\}/')).toEqual('path');
expect(gp('path/{../,./,\\{bar,/baz},qux}/')).toEqual('path');
expect(gp('path/\\{../,./,\\{bar,/baz\\},qux\\}/')).toEqual(
'path/{../,./,{bar,/baz},qux}'
);
expect(gp('{../,./,{bar,/baz\\},qux\\}/')).toEqual('.');
expect(gp('{../,./,{bar,/baz\\},qux\\}')).toEqual('.');
expect(gp('path/{,/,bar/{baz,qux\\}}/')).toEqual('path');
expect(gp('path/{,/,bar/{baz,qux}\\}/')).toEqual('path');
// expect(gp('path/\\{../,./,{bar,/baz},qux}/')).toEqual('path');
done();
});
it('should return parent dirname from non-glob paths', function (done) {
expect(gp('path')).toEqual('.');
expect(gp('path/foo')).toEqual('path');
expect(gp('path/foo/')).toEqual('path/foo');
expect(gp('path/foo/bar.js')).toEqual('path/foo');
done();
});
it('should respect disabled auto flip backslashes', function (done) {
expect(gp('foo-\\(bar\\).md', { flipBackslashes: false })).toEqual('.');
done();
});
});
describe('glob2base test patterns', function () {
it('should get a base name', function (done) {
expect(gp('js/*.js')).toEqual('js');
done();
});
it('should get a base name from a nested glob', function (done) {
expect(gp('js/**/test/*.js')).toEqual('js');
done();
});
it('should get a base name from a flat file', function (done) {
expect(gp('js/test/wow.js')).toEqual('js/test');
expect(gp('js/test/wow.js')).toEqual('js/test');
done();
});
it('should get a base name from character class pattern', function (done) {
expect(gp('js/t[a-z]st}/*.js')).toEqual('js');
done();
});
it('should get a base name from brace , expansion', function (done) {
expect(gp('js/{src,test}/*.js')).toEqual('js');
done();
});
it('should get a base name from brace .. expansion', function (done) {
expect(gp('js/test{0..9}/*.js')).toEqual('js');
done();
});
it('should get a base name from extglob', function (done) {
expect(gp('js/t+(wo|est)/*.js')).toEqual('js');
done();
});
it('should get a base name from a path with non-exglob parens', function (done) {
expect(gp('js/t(wo|est)/*.js')).toEqual('js');
expect(gp('js/t/(wo|est)/*.js')).toEqual('js/t');
done();
});
it('should get a base name from a complex brace glob', function (done) {
expect(gp('lib/{components,pages}/**/{test,another}/*.txt')).toEqual('lib');
expect(gp('js/test/**/{images,components}/*.js')).toEqual('js/test');
expect(gp('ooga/{booga,sooga}/**/dooga/{eooga,fooga}')).toEqual('ooga');
done();
});
it('should not be susceptible to SNYK-JS-GLOBPARENT-1016905', function (done) {
// This will time out if susceptible.
gp('{' + '/'.repeat(5000));
done();
});
});
if (isWin32) {
describe('technically invalid windows globs', function () {
it('should manage simple globs with backslash path separator', function (done) {
expect(gp('C:\\path\\*.js')).toEqual('C:/path');
done();
});
});
}