/
index.test.js
221 lines (185 loc) · 7.82 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
'use strict';
var gp = require('../');
var assert = require('assert');
var isWin32 = require('os').platform() === 'win32';
describe('glob-parent', function() {
it('should strip glob magic to return parent path', function(done) {
assert.equal(gp('.'), '.');
assert.equal(gp('.*'), '.');
assert.equal(gp('/.*'), '/');
assert.equal(gp('/.*/'), '/');
assert.equal(gp('a/.*/b'), 'a');
assert.equal(gp('a*/.*/b'), '.');
assert.equal(gp('*/a/b/c'), '.');
assert.equal(gp('*'), '.');
assert.equal(gp('*/'), '.');
assert.equal(gp('*/*'), '.');
assert.equal(gp('*/*/'), '.');
assert.equal(gp('**'), '.');
assert.equal(gp('**/'), '.');
assert.equal(gp('**/*'), '.');
assert.equal(gp('**/*/'), '.');
assert.equal(gp('/*.js'), '/');
assert.equal(gp('*.js'), '.');
assert.equal(gp('**/*.js'), '.');
assert.equal(gp('{a,b}'), '.');
assert.equal(gp('/{a,b}'), '/');
assert.equal(gp('/{a,b}/'), '/');
assert.equal(gp('(a|b)'), '.');
assert.equal(gp('/(a|b)'), '/');
assert.equal(gp('./(a|b)'), '.');
assert.equal(gp('a/(b c)'), 'a', 'not an extglob');
assert.equal(gp('a/(b c)/'), 'a/(b c)', 'not an extglob');
assert.equal(gp('a/(b c)/d'), 'a/(b c)', 'not an extglob');
assert.equal(gp('path/to/*.js'), 'path/to');
assert.equal(gp('/root/path/to/*.js'), '/root/path/to');
assert.equal(gp('chapter/foo [bar]/'), 'chapter');
assert.equal(gp('path/[a-z]'), 'path');
assert.equal(gp('[a-z]'), '.');
assert.equal(gp('path/{to,from}'), 'path');
assert.equal(gp('path/(to|from)'), 'path');
assert.equal(gp('path/(foo bar)/subdir/foo.*'), 'path/(foo bar)/subdir');
assert.equal(gp('path/!(to|from)'), 'path');
assert.equal(gp('path/?(to|from)'), 'path');
assert.equal(gp('path/+(to|from)'), 'path');
assert.equal(gp('path/*(to|from)'), 'path');
assert.equal(gp('path/@(to|from)'), 'path');
assert.equal(gp('path/!/foo'), 'path/!');
assert.equal(gp('path/?/foo'), 'path/?');
assert.equal(gp('path/+/foo'), 'path/+');
assert.equal(gp('path/*/foo'), 'path');
assert.equal(gp('path/@/foo'), 'path/@');
assert.equal(gp('path/!/foo/'), 'path/!/foo');
assert.equal(gp('path/?/foo/'), 'path/?/foo');
assert.equal(gp('path/+/foo/'), 'path/+/foo');
assert.equal(gp('path/*/foo/'), 'path');
assert.equal(gp('path/@/foo/'), 'path/@/foo');
assert.equal(gp('path/**/*'), 'path');
assert.equal(gp('path/**/subdir/foo.*'), 'path');
assert.equal(gp('path/subdir/**/foo.js'), 'path/subdir');
assert.equal(gp('path/!subdir/foo.js'), 'path/!subdir');
assert.equal(gp('path/{foo,bar}/'), 'path');
done();
});
it('should respect escaped characters', function(done) {
assert.equal(gp('path/\\*\\*/subdir/foo.*'), 'path/**/subdir');
assert.equal(gp('path/\\[\\*\\]/subdir/foo.*'), 'path/[*]/subdir');
assert.equal(gp('path/\\*(a|b)/subdir/foo.*'), 'path');
assert.equal(gp('path/\\*/(a|b)/subdir/foo.*'), 'path/*');
assert.equal(gp('path/\\*\\(a\\|b\\)/subdir/foo.*'), 'path/*(a|b)/subdir');
assert.equal(gp('path/\\[foo bar\\]/subdir/foo.*'), 'path/[foo bar]/subdir');
assert.equal(gp('path/\\[bar]/'), 'path/[bar]');
assert.equal(gp('path/\\[bar]'), 'path/[bar]');
assert.equal(gp('[bar]'), '.');
assert.equal(gp('[bar]/'), '.');
assert.equal(gp('./\\[bar]'), './[bar]');
assert.equal(gp('\\[bar]/'), '[bar]');
assert.equal(gp('[bar\\]/'), '.');
assert.equal(gp('path/foo \\[bar]/'), 'path/foo [bar]');
assert.equal(gp('path/\\{foo,bar}/'), 'path/{foo,bar}');
assert.equal(gp('\\{foo,bar}/'), '{foo,bar}');
assert.equal(gp('\\{foo,bar\\}/'), '{foo,bar}');
assert.equal(gp('{foo,bar\\}/'), '.');
if (isWin32) {
// On Windows we are trying to flip backslashes foo-\\( → foo-/(
assert.equal(gp('foo-\\(bar\\).md'), 'foo-');
} else {
assert.equal(gp('foo-\\(bar\\).md'), '.');
assert.equal(gp('\\[bar]'), '[bar]');
assert.equal(gp('[bar\\]'), '.');
assert.equal(gp('\\{foo,bar\\}'), '{foo,bar}');
assert.equal(gp('{foo,bar\\}'), '.');
}
done();
});
it('should respect glob enclosures with embedded separators', function(done) {
assert.equal(gp('path/{,/,bar/baz,qux}/'), 'path');
assert.equal(gp('path/\\{,/,bar/baz,qux}/'), 'path/{,/,bar/baz,qux}');
assert.equal(gp('path/\\{,/,bar/baz,qux\\}/'), 'path/{,/,bar/baz,qux}');
assert.equal(gp('/{,/,bar/baz,qux}/'), '/');
assert.equal(gp('/\\{,/,bar/baz,qux}/'), '/{,/,bar/baz,qux}');
assert.equal(gp('{,/,bar/baz,qux}'), '.');
assert.equal(gp('\\{,/,bar/baz,qux\\}'), '{,/,bar/baz,qux}');
assert.equal(gp('\\{,/,bar/baz,qux}/'), '{,/,bar/baz,qux}');
assert.equal(gp('path/foo[a\\\/]/'), 'path');
assert.equal(gp('path/foo\\[a\\\/]/'), 'path/foo[a\\\/]');
assert.equal(gp('foo[a\\\/]'), '.');
assert.equal(gp('foo\\[a\\\/]'), 'foo[a\\\/]');
assert.equal(gp('path/(foo/bar|baz)'), 'path');
assert.equal(gp('path/(foo/bar|baz)/'), 'path');
assert.equal(gp('path/\\(foo/bar|baz)/'), 'path/(foo/bar|baz)');
done();
});
it('should handle nested braces', function(done) {
assert.equal(gp('path/{../,./,{bar,/baz\\},qux\\}/'), 'path');
assert.equal(gp('path/{../,./,\\{bar,/baz},qux}/'), 'path');
assert.equal(gp('path/\\{../,./,\\{bar,/baz\\},qux\\}/'), 'path/{../,./,{bar,/baz},qux}');
assert.equal(gp('{../,./,{bar,/baz\\},qux\\}/'), '.');
assert.equal(gp('{../,./,{bar,/baz\\},qux\\}'), '.');
assert.equal(gp('path/{,/,bar/{baz,qux\\}}/'), 'path');
assert.equal(gp('path/{,/,bar/{baz,qux}\\}/'), 'path');
// assert.equal(gp('path/\\{../,./,{bar,/baz},qux}/'), 'path');
done();
});
it('should return parent dirname from non-glob paths', function(done) {
assert.equal(gp('path'), '.');
assert.equal(gp('path/foo'), 'path');
assert.equal(gp('path/foo/'), 'path/foo');
assert.equal(gp('path/foo/bar.js'), 'path/foo');
done();
});
it('should respect disabled auto flip backslashes', function(done) {
assert.equal(gp('foo-\\(bar\\).md', { flipBackslashes: false }), '.');
done();
});
});
describe('glob2base test patterns', function() {
it('should get a base name', function(done) {
assert.equal(gp('js/*.js'), 'js');
done();
});
it('should get a base name from a nested glob', function(done) {
assert.equal(gp('js/**/test/*.js'), 'js');
done();
});
it('should get a base name from a flat file', function(done) {
assert.equal(gp('js/test/wow.js'), 'js/test');
assert.equal(gp('js/test/wow.js'), 'js/test');
done();
});
it('should get a base name from character class pattern', function(done) {
assert.equal(gp('js/t[a-z]st}/*.js'), 'js');
done();
});
it('should get a base name from brace , expansion', function(done) {
assert.equal(gp('js/{src,test}/*.js'), 'js');
done();
});
it('should get a base name from brace .. expansion', function(done) {
assert.equal(gp('js/test{0..9}/*.js'), 'js');
done();
});
it('should get a base name from extglob', function(done) {
assert.equal(gp('js/t+(wo|est)/*.js'), 'js');
done();
});
it('should get a base name from a path with non-exglob parens', function(done) {
assert.equal(gp('js/t(wo|est)/*.js'), 'js');
assert.equal(gp('js/t/(wo|est)/*.js'), 'js/t');
done();
});
it('should get a base name from a complex brace glob', function(done) {
assert.equal(gp('lib/{components,pages}/**/{test,another}/*.txt'), 'lib');
assert.equal(gp('js/test/**/{images,components}/*.js'), 'js/test');
assert.equal(gp('ooga/{booga,sooga}/**/dooga/{eooga,fooga}'), 'ooga');
done();
});
});
if (isWin32) {
describe('technically invalid windows globs', function() {
it('should manage simple globs with backslash path separator', function(done) {
assert.equal(gp('C:\\path\\*.js'), 'C:/path');
done();
});
});
}