-
Notifications
You must be signed in to change notification settings - Fork 722
/
common.js
341 lines (291 loc) · 8.15 KB
/
common.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
import test from 'ava';
import shell from '..';
import common from '../src/common';
import utils from './utils/utils';
test.beforeEach(() => {
shell.config.silent = true;
common.state.error = null;
common.state.errorCode = 0;
});
test.afterEach(() => {
common.config.resetForTesting();
});
//
// Invalids
//
test('too few args', t => {
t.throws(() => {
common.expand();
}, TypeError);
});
test('should be a list', t => {
t.throws(() => {
common.expand('test/resources');
}, TypeError);
});
test('parseOptions (invalid option in options object)', t => {
t.throws(() => {
common.parseOptions({ q: 'some string value' }, {
R: 'recursive',
f: 'force',
r: 'reverse',
});
}, common.CommandError);
});
test('parseOptions (without a hyphen in the string)', t => {
t.throws(() => {
common.parseOptions('f', {
f: 'force',
});
}, Error);
});
test('parseOptions (opt is not a string/object)', t => {
t.throws(() => {
common.parseOptions(1, {
f: 'force',
});
}, TypeError);
});
test('parseOptions (map is not an object)', t => {
t.throws(() => {
common.parseOptions('-f', 27);
}, TypeError);
});
test('parseOptions (errorOptions is not an object)', t => {
t.throws(() => {
common.parseOptions('-f', {
f: 'force',
}, 'not a valid errorOptions');
}, TypeError);
});
test('parseOptions (unrecognized string option)', t => {
t.throws(() => {
common.parseOptions('-z', {
f: 'force',
});
}, common.CommandError);
});
test('parseOptions (unrecognized option in Object)', t => {
t.throws(() => {
common.parseOptions({ '-c': 7 }, {
f: 'force',
});
});
});
test('parseOptions (invalid type)', t => {
t.throws(() => {
common.parseOptions(12, {
R: 'recursive',
f: 'force',
r: 'reverse',
});
});
});
test('convertErrorOutput: no args', t => {
t.throws(() => {
common.convertErrorOutput();
}, TypeError);
});
test('convertErrorOutput: input must be a vanilla string', t => {
t.throws(() => {
common.convertErrorOutput(3);
}, TypeError);
t.throws(() => {
common.convertErrorOutput({});
}, TypeError);
});
//
// Valids
//
//
// common.convertErrorOutput()
//
test('convertErrorOutput: nothing to convert', t => {
const input = 'hello world';
const result = common.convertErrorOutput(input);
t.is(result, input);
});
test('convertErrorOutput: does not change forward slash', t => {
const input = 'dir/sub/file.txt';
const result = common.convertErrorOutput(input);
t.is(result, input);
});
test('convertErrorOutput: changes backslashes to forward slashes', t => {
const input = 'dir\\sub\\file.txt';
const result = common.convertErrorOutput(input);
t.is(result, 'dir/sub/file.txt');
});
//
// common.expand()
//
test('single file, array syntax', t => {
const result = common.expand(['test/resources/file1.txt']);
t.deepEqual(result, ['test/resources/file1.txt']);
});
test('multiple file, glob syntax, * for file name', t => {
const result = common.expand(['test/resources/file*.txt']);
t.deepEqual(result.sort(), ['test/resources/file1.txt', 'test/resources/file2.txt'].sort());
});
test('multiple file, glob syntax, * for directory name', t => {
const result = common.expand(['test/r*/file*.txt']);
t.deepEqual(result.sort(), ['test/resources/file1.txt', 'test/resources/file2.txt'].sort());
});
test('multiple file, glob syntax, ** for directory name', t => {
const result = common.expand(['test/resources/**/file*.js']);
t.deepEqual(
result.sort(),
['test/resources/file1.js', 'test/resources/file2.js', 'test/resources/ls/file1.js', 'test/resources/ls/file2.js'].sort()
);
});
test('broken links still expand', t => {
const result = common.expand(['test/resources/b*dlink']);
t.deepEqual(result, ['test/resources/badlink']);
});
test('empty array', t => {
const result = common.expand([]);
t.deepEqual(result, []);
});
test('empty string', t => {
const result = common.expand(['']);
t.deepEqual(result, ['']);
});
test('non-string', t => {
const result = common.expand([5]);
t.deepEqual(result, [5]);
});
//
// common.buffer()
//
test('common.buffer returns buffer', t => {
const buf = common.buffer();
t.truthy(buf instanceof Buffer);
t.is(buf.length, 64 * 1024);
});
test('common.buffer with explicit length', t => {
const buf = common.buffer(20);
t.truthy(buf instanceof Buffer);
t.is(buf.length, 20);
});
test('common.buffer with different config.bufLength', t => {
common.config.bufLength = 20;
const buf = common.buffer();
t.truthy(buf instanceof Buffer);
t.is(buf.length, 20);
});
test('common.parseOptions (normal case)', t => {
const result = common.parseOptions('-Rf', {
R: 'recursive',
f: 'force',
r: 'reverse',
});
t.truthy(result.recursive);
t.truthy(result.force);
t.falsy(result.reverse);
});
test('common.parseOptions (with mutually-negating options)', t => {
const result = common.parseOptions('-f', {
n: 'no_force',
f: '!no_force',
R: 'recursive',
});
t.falsy(result.recursive);
t.falsy(result.no_force);
t.is(result.force, undefined); // this key shouldn't exist
});
test(
'common.parseOptions (the last of the conflicting options should hold)',
t => {
const options = {
n: 'no_force',
f: '!no_force',
R: 'recursive',
};
let result = common.parseOptions('-fn', options);
t.false(result.recursive);
t.truthy(result.no_force);
t.is(result.force, undefined); // this key shouldn't exist
result = common.parseOptions('-nf', options);
t.false(result.recursive);
t.false(result.no_force);
t.is(result.force, undefined); // this key shouldn't exist
}
);
test('common.parseOptions using an object to hold options', t => {
const result = common.parseOptions({ '-v': 'some text here' }, {
v: 'value',
f: 'force',
r: 'reverse',
});
t.is(result.value, 'some text here');
t.false(result.force);
t.false(result.reverse);
});
test('common.parseOptions throws when passed a string not starting with "-"', t => {
t.throws(() => {
common.parseOptions('a', { '-a': 'throws' });
}, Error, "Options string must start with a '-'");
});
test('common.parseOptions allows long options', t => {
const result = common.parseOptions({ value: true }, {
v: 'value',
});
t.truthy(result.value);
});
test('common.parseOptions allows long options with values', t => {
const someObject = {};
const result = common.parseOptions({ value: someObject }, {
v: 'value',
});
t.is(result.value, someObject);
});
test('common.parseOptions throws for unknown long option', t => {
t.throws(() => {
common.parseOptions({ throws: true }, {
v: 'value',
});
}, common.CommandError);
});
test('common.parseOptions with -- argument', t => {
const result = common.parseOptions('--', {
R: 'recursive',
f: 'force',
r: 'reverse',
});
t.falsy(result.recursive);
t.falsy(result.force);
t.falsy(result.reverse);
});
test('Some basic tests on the ShellString type', t => {
const result = shell.ShellString('foo');
t.is(result.toString(), 'foo');
t.is(result.stdout, 'foo');
t.is(result.stderr, undefined);
t.truthy(result.to);
t.truthy(result.toEnd);
});
test.cb('Commands that fail will still output error messages to stderr', t => {
const script = 'require(\'./global\'); ls(\'noexist\'); cd(\'noexist\');';
utils.runScript(script, (err, stdout, stderr) => {
t.is(stdout, '');
t.is(
stderr,
'ls: no such file or directory: noexist\ncd: no such file or directory: noexist\n'
);
t.end();
});
});
test('execPath value makes sense', t => {
// TODO(nate): change this test if we add electron support in the unit tests
t.is(shell.config.execPath, process.execPath);
t.is(typeof shell.config.execPath, 'string');
});
test('Changing shell.config.execPath does not modify process', t => {
shell.config.execPath = 'foo';
t.not(shell.config.execPath, process.execPath);
});
test('CommandError is a subclass of Error', t => {
const e = new common.CommandError(new common.ShellString('some value'));
t.truthy(e instanceof common.CommandError);
t.truthy(e instanceof Error);
t.is(e.constructor, common.CommandError);
});