-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathbash-compiled-sets.js
199 lines (168 loc) · 6.98 KB
/
bash-compiled-sets.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
'use strict';
require('mocha');
const assert = require('assert').strict;
const bashPath = require('bash-path');
const cp = require('child_process');
const braces = require('..');
const bash = input => {
return cp.spawnSync(bashPath(), ['-c', `echo ${input}`])
.stdout.toString()
.split(/\s+/)
.filter(Boolean);
};
const equal = (input, expected = bash(input), options) => {
assert.deepEqual(braces.compile(input, options), expected);
};
/**
* Bash 4.3 unit tests with `braces.expand()`
*/
describe('bash sets - braces.compile()', () => {
const fixtures = [
['{a,b,c,d,e}', {}, '(a|b|c|d|e)'],
['a/\\{b,c,d,{x,y}}{e,f\\}/g', {}, 'a/{b,c,d,(x|y)}{e,f}/g'],
['a/\\{b,c,d\\}\\{e,f\\}/g', {}, 'a/{b,c,d}{e,f}/g'],
['a/\\{b,c,d\\}\\{e,f}/g', {}, 'a/{b,c,d}{e,f}/g'],
['a/\\{b,c,d\\}{e,f}/g', {}, 'a/{b,c,d}(e|f)/g'],
['a/\\{b,c,d{x,y}}{e,f\\}/g', {}, 'a/{b,c,d(x|y)}{e,f}/g'],
['a/\\{b,c,d}{e,f\\}/g', {}, 'a/{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(b|c)/(d|e)}/f'],
['a{b{c{d,e}f{x,y{{g}h', {}, 'a{b{c(d|e)f{x,y{{g}h'],
['{a,b,{c,d},e}', {}, '(a|b|(c|d)|e)'],
['{a,b,{c,d}e}', {}, '(a|b|(c|d)e)'],
['{a,b,{c,d}}', {}, '(a|b|(c|d))'],
['{a,b{c,d}}', {}, '(a|b(c|d))'],
['{a,b}/{c,d}', {}, '(a|b)/(c|d)'],
['{a,b}{c,d}', {}, '(a|b)(c|d)'],
['{{a,b},{c,d}}', {}, '((a|b)|(c|d))'],
['{{a,b}/{c,d}}', {}, '{(a|b)/(c|d)}'],
['{{a,b}/{c,d}}/z', {}, '{(a|b)/(c|d)}/z'],
// should not process glob characters
['{generate,{assemble,update,verb}{file,-generate-*},generator}.js', {}, '(generate|(assemble|update|verb)(file|-generate-*)|generator).js'],
['**/{foo,bar}.js', {}, '**/(foo|bar).js'],
['**/{a,b,c}/*.js', {}, '**/(a|b|c)/*.js'],
['**/{a,b,*}/*.js', {}, '**/(a|b|*)/*.js'],
['**/{**,b,*}/*.js', {}, '**/(**|b|*)/*.js'],
// should not expand escaped braces
['\\{a,b,c,d,e}', {}, '{a,b,c,d,e}'],
['a/b/c/{x,y\\}', {}, 'a/b/c/{x,y}'],
['a/\\{x,y}/cde', {}, 'a/{x,y}/cde'],
['abcd{efgh', {}, 'abcd{efgh'],
['\\{abc\\}', {}, '{abc}'],
['{x,y,\\{a,b,c\\}}', {}, '(x|y|{a|b|c})'],
['{x,y,{a,b,c\\}}', {}, '{x,y,(a|b|c})'],
['{x,y,{abc},trie}', {}, '(x|y|{abc}|trie)'],
['x,y,{abc},trie', {}, 'x,y,{abc},trie'],
['{b{c,d},e}', {}, '(b(c|d)|e)'],
['{b{c,d},e}/f', {}, '(b(c|d)|e)/f'],
['{abc}', {}, '{abc}'],
// should handle empty braces
['{ }', {}, '{ }'],
['{', {}, '{'],
['{}', {}, '{}'],
['}', {}, '}'],
// should escape braces when only one value is defined
['a{b}c', {}, 'a{b}c'],
['a/b/c{d}e', {}, 'a/b/c{d}e'],
// should escape closing braces when open is not defined
['{a,b}c,d}', {}, '(a|b)c,d}'],
['a,b,c,d}', {}, 'a,b,c,d}'],
// should not expand braces in sets with es6/bash-like variables
['abc/${ddd}/xyz', {}, 'abc/${ddd}/xyz'],
['a${b}c', {}, 'a${b}c'],
['a${b{a,b}}c', {}, 'a${b{a,b}}c'],
['a/{${b},c}/d', {}, 'a/(${b}|c)/d'],
['a${b,d}/{foo,bar}c', {}, 'a${b,d}/(foo|bar)c'],
// should not expand escaped commas
['a{b\\,c\\,d}e', {}, 'a{b,c,d}e'],
['a{b\\,c}d', {}, 'a{b,c}d'],
['{abc\\,def}', {}, '{abc,def}'],
['{abc\\,def,ghi}', {}, '(abc,def|ghi)'],
['a/{b,c}/{x\\,y}/d/e', {}, 'a/(b|c)/{x,y}/d/e'],
// should not expand escaped braces
['{a,b\\}c,d}', {}, '(a|b}c|d)'],
['a/{z,\\{a,b,c,d,e}/d', {}, 'a/(z|{a|b|c|d|e)/d'],
['a/\\{b,c}/{d,e}/f', {}, 'a/{b,c}/(d|e)/f'],
// should not expand escaped braces or commas
['{x\\,y,\\{abc\\},trie}', {}, '(x,y|{abc}|trie)'],
// should support sequence brace operators
['ff{c,b,a}', {}, 'ff(c|b|a)'],
['f{d,e,f}g', {}, 'f(d|e|f)g'],
['{a,b,c}', {}, '(a|b|c)'],
['{l,n,m}xyz', {}, '(l|n|m)xyz'],
// should expand multiple sets
['a/{a,b}/{c,d}/e', {}, 'a/(a|b)/(c|d)/e'],
['a{b,c}d{e,f}g', {}, 'a(b|c)d(e|f)g'],
['a/{x,y}/c{d,e}f.{md,txt}', {}, 'a/(x|y)/c(d|e)f.(md|txt)'],
// should expand nested sets
['{a,b}{{a,b},a,b}', {}, '(a|b)((a|b)|a|b)'],
['/usr/{ucb/{ex,edit},lib/{ex,how_ex}}', {}, '/usr/(ucb/(ex|edit)|lib/(ex|how_ex))'],
['a{b,c{d,e}f}g', {}, 'a(b|c(d|e)f)g'],
['a{{x,y},z}b', {}, 'a((x|y)|z)b'],
['f{x,y{g,z}}h', {}, 'f(x|y(g|z))h'],
['a{b,c{d,e},h}x/z', {}, 'a(b|c(d|e)|h)x/z'],
['a{b,c{d,e},h}x{y,z}', {}, 'a(b|c(d|e)|h)x(y|z)'],
['a{b,c{d,e},{f,g}h}x{y,z}', {}, 'a(b|c(d|e)|(f|g)h)x(y|z)'],
['a-{b{d,e}}-c', {}, 'a-{b(d|e)}-c'],
// should expand not modify non-brace characters
['a/b/{d,e}/*.js', {}, 'a/b/(d|e)/*.js'],
['a/**/c/{d,e}/f*.js', {}, 'a/**/c/(d|e)/f*.js'],
['a/**/c/{d,e}/f*.{md,txt}', {}, 'a/**/c/(d|e)/f*.(md|txt)'],
// should work with leading and trailing commas
['a{b,}c', {}, 'a(b|)c'],
['a{,b}c', {}, 'a(|b)c'],
// should handle spaces
// Bash 4.3 says the this first one should be equivalent to `foo|(1|2)|bar
// That makes sense in Bash, since ' ' is a separator, but not here.
['foo {1,2} bar', {}, 'foo (1|2) bar'],
['a{ ,c{d, },h}x', {}, 'a( |c(d| )|h)x'],
['a{ ,c{d, },h} ', {}, 'a( |c(d| )|h) '],
// see https://github.com/jonschlinkert/microequal/issues/66
['/Users/tobiasreich/Sites/aaa/bbb/ccc 2016/src/**/[^_]*.{html,ejs}', {}, '/Users/tobiasreich/Sites/aaa/bbb/ccc 2016/src/**/[^_]*.(html|ejs)']
];
const seen = new Map();
const dupes = [];
for (let i = 0; i < fixtures.length; i++) {
const fixture = fixtures[i];
const key = fixture[0] + String(fixture[1].bash);
if (seen.has(key)) {
dupes.push(i + 21, fixture[0]);
} else {
seen.set(key, i + 21);
}
}
fixtures.forEach(arr => {
if (typeof arr === 'string') {
return;
}
const options = { ...arr[1] };
const pattern = arr[0];
const expected = arr[2];
if (options.skip === true) {
return;
}
it('should compile: ' + pattern, () => {
equal(pattern, expected, options);
});
});
});