-
Notifications
You must be signed in to change notification settings - Fork 44
/
cli.js
452 lines (396 loc) · 14.6 KB
/
cli.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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
import * as shell from 'shelljs';
import fs from 'fs';
import path from 'path';
import { shx } from '../../src/shx';
import { EXIT_CODES, CONFIG_FILE, shouldReadStdin } from '../../src/config';
import * as mocks from '../mocks';
import 'should';
/* eslint no-bitwise: "off" */
/* eslint prefer-numeric-literals: "off" */
const skipIf = (cond, ...args) => {
if (cond) {
it.skip(...args);
} else {
it(...args);
}
};
// Run the cli with args as argv
const cli = (...args) => {
mocks.init();
const stdin = mocks.stdin();
let code;
try {
code = shx.call(stdin, ['', '', ...args]);
} catch (e) {
if (Object.prototype.hasOwnProperty.call(e, 'code')) {
// Shx is returning an error with a specified code
code = e.code;
} else {
// Shx is throwing an exception
throw e;
}
} finally {
mocks.restore();
}
return {
code,
stdout: mocks.stdout(),
stderr: mocks.stderr(),
};
};
describe('cli', () => {
beforeEach(() => {
// Clear this out before the test, so that the test can mock the value if
// necessary
mocks.stdin(null);
});
it('calls shx', () => {
const output = cli('echo');
output.stdout.should.equal('\n');
output.stderr.should.equal('');
});
it('fails if no command name is given', () => {
const output = cli();
output.stdout.should.equal('');
output.stderr.should.match(/Error: Missing ShellJS command name\n/);
output.stderr.should.match(/Usage/); // make sure help is printed
output.code.should.equal(EXIT_CODES.SHX_ERROR);
});
it('fails for unrecognized commands', () => {
const output = cli('foobar');
output.stdout.should.equal('');
output.stderr.should.match(/Error: Invalid ShellJS command: foobar.\n/);
output.stderr.should.match(/Usage/); // make sure help is printed
output.code.should.equal(EXIT_CODES.SHX_ERROR);
});
it('fails for blocked commands', () => {
const output = cli('cd', 'src');
output.stdout.should.equal('');
output.stderr.should.match(/Warning: shx cd is not supported\n/);
output.stderr.should.match(/help/); // make we tell them to use help.
output.code.should.equal(EXIT_CODES.SHX_ERROR);
});
it('returns error codes from commands', () => {
const output = cli('ls', 'fakeFileName');
output.stdout.should.equal('');
output.stderr.should.equal('ls: no such file or directory: fakeFileName\n');
output.code.should.equal(2);
});
it('does not print out boolean return values', () => {
let output = cli('test', '-f', 'README.md'); // true
output.stdout.should.equal('');
output.stderr.should.equal('');
output.code.should.equal(0);
output = cli('test', '-L', 'src'); // false
output.stdout.should.equal('');
output.stderr.should.equal('');
output.code.should.equal(1);
});
it('outputs to stdout', () => {
const output = cli('echo', 'hello', 'world');
output.stdout.should.equal('hello world\n');
output.stderr.should.equal('');
});
it('appends a newline for pwd', () => {
const output = cli('pwd');
output.stdout[output.stdout.length - 1].should.equal('\n');
});
it('works for commands with no output', () => {
let output = cli('cp', 'README.md', 'deleteme');
output.stdout.should.equal('');
output.stderr.should.equal('');
output = cli('rm', 'deleteme'); // cleanup, but also test rm's output
output.stdout.should.equal('');
output.stderr.should.equal('');
});
describe('global flags', () => {
it('supports --version', () => {
const output = cli('--version');
output.stdout.should.match(/shx v\S+ \(using ShellJS v\S+\)\n/);
output.stderr.should.equal('');
output.code.should.equal(0);
});
it('allows --silent to change config.silent', () => {
const output = cli('--silent', 'ls', 'fakeFileName');
output.stdout.should.equal('');
output.stderr.should.equal('');
output.code.should.equal(2);
});
it('adds flags to the help output', () => {
const output = cli('help');
output.stderr.should.equal('');
output.stdout.should.match(/Usage/); // make sure help is printed
output.stdout.should.match(/\* --verbose/);
output.stdout.should.match(/\* --silent/);
output.stdout.should.match(/\* --version/);
});
});
describe('stdin', () => {
const oldTTY = process.stdin.isTTY;
after(() => {
process.stdin.isTTY = oldTTY;
});
it('has basic support', () => {
mocks.stdin('foo\nbar\nfoobar');
const output = cli('grep', 'foo');
output.stdout.should.equal('foo\nfoobar\n');
output.stderr.should.equal('');
output.code.should.equal(0);
});
it('reads stdin for commands that accept stdin', () => {
process.stdin.isTTY = undefined;
shouldReadStdin(['cat']).should.equal(true);
shouldReadStdin(['head']).should.equal(true);
shouldReadStdin(['sed', 's/foo/bar/g']).should.equal(true);
shouldReadStdin(['grep', 'a.*z']).should.equal(true);
});
it('reads stdin if not enough arguments are given', () => {
process.stdin.isTTY = undefined;
shouldReadStdin(['head', '-n', '1']).should.equal(true);
shouldReadStdin(['tail', '-n', '1']).should.equal(true);
shouldReadStdin(['grep', '-i', 'a.*z']).should.equal(true);
});
it('does not read stdin if process.stdin is a TTY', () => {
process.stdin.isTTY = true;
shouldReadStdin(['cat']).should.equal(false);
shouldReadStdin(['head']).should.equal(false);
shouldReadStdin(['sed', 's/foo/bar/g']).should.equal(false);
shouldReadStdin(['grep', 'a.*z']).should.equal(false);
});
it('does not read stdin if command does not accept stdin', () => {
process.stdin.isTTY = undefined;
// It shouldn't matter for this function if these have valid numbers of
// arguments or not, so let's test both
shouldReadStdin(['rm']).should.equal(false);
shouldReadStdin(['ls', 'dir']).should.equal(false);
shouldReadStdin(['cp', 'a']).should.equal(false);
});
it('does not read stdin if files are given as arguments', () => {
process.stdin.isTTY = undefined;
shouldReadStdin(['cat', 'file.txt']).should.equal(false);
shouldReadStdin(['head', 'file.txt']).should.equal(false);
shouldReadStdin(['sed', 's/foo/bar/g', 'file.txt']).should.equal(false);
shouldReadStdin(['grep', 'a.*z', 'file.txt']).should.equal(false);
// Lots of files
shouldReadStdin(['cat', 'file.txt', 'file2.txt', 'file3.txt'])
.should.equal(false);
});
it('does not read stdin if both options and files are given', () => {
process.stdin.isTTY = undefined;
shouldReadStdin(['head', '-n', '1', 'file.txt']).should.equal(false);
shouldReadStdin(['sed', '-i', 's/foo/bar/g', 'file.txt'])
.should.equal(false);
shouldReadStdin(['grep', '-i', 'a.*z', 'file.txt']).should.equal(false);
});
});
describe('plugin', () => {
afterEach(() => {
shell.rm('-f', CONFIG_FILE);
const CONFIG_PATH = path.join(process.cwd(), CONFIG_FILE);
delete require.cache[require.resolve(CONFIG_PATH)];
});
it('throws exception for missing plugins', () => {
const config = {
plugins: [
'shelljs-plugin-fake',
],
};
shell.ShellString(JSON.stringify(config)).to(CONFIG_FILE);
(() => {
cli('ls');
}).should.throw(Error);
});
it('does not load any plugins if config file lacks plugin section', () => {
const config = {
notplugins: [ // the plugins attribute is mandatory for loading plugins
'shelljs-plugin-open',
],
};
shell.ShellString(JSON.stringify(config)).to(CONFIG_FILE);
const output = cli('help');
output.stderr.should.equal(''); // Runs successfully
output.stdout.should.match(/Usage/); // make sure help is printed
output.stdout.should.not.match(/- open/); // should *not* load the plugin
});
it('defends against malicious config files', () => {
const notValidJSON = `
var shell = require('shelljs');
shell.rm('-rf', 'myPreciousFile.txt');
module.export = {};
`;
shell.ShellString(notValidJSON).to(CONFIG_FILE);
(() => {
cli('ls');
}).should.throw(Error);
});
it('adds plugin commands to the help files', () => {
const config = {
plugins: [
'shelljs-plugin-open',
],
};
shell.ShellString(JSON.stringify(config)).to(CONFIG_FILE);
const output = cli('help');
output.stderr.should.equal('');
output.stdout.should.match(/Usage/); // make sure help is printed
output.stdout.should.match(/\* open/); // help should include new command
});
it('loads shx true by default', () => {
const output = cli('true');
output.stdout.should.equal('');
output.stderr.should.equal('');
output.code.should.equal(0);
});
it('loads shx false by default', () => {
const output = cli('false');
output.stdout.should.equal('');
output.stderr.should.equal('');
output.code.should.equal(1);
});
});
describe('ls', () => {
beforeEach(() => {
shell.mkdir('empty');
shell.mkdir('not-empty');
shell.touch('not-empty/file1.txt');
shell.touch('not-empty/file2.txt');
});
afterEach(() => {
shell.rm('-rf', 'empty');
shell.rm('-rf', 'not-empty');
});
it('works with an empty directory', () => {
const output = cli('ls', 'empty');
output.stdout.should.equal('');
});
it('works with a non-empty directory', () => {
const output = cli('ls', 'not-empty');
output.stdout.should.equal('file1.txt\nfile2.txt\n');
});
});
describe('sed', () => {
const testFileName1 = 'foo.txt';
const testContents1 = 'foo\nfoosomething\nfoofoosomething\n';
const testFileName2 = 's/weirdfile/name/g';
const testContents2 = testContents1;
const testFileName3 = 'urls.txt';
const testContents3 = 'http://www.nochange.com\nhttp://www.google.com\n';
const testFileName4 = 'windowsPath.txt';
const testContents4 = 'C:\\Some\\Windows\\file\\path.txt';
beforeEach(() => {
// create test files
shell.touch(testFileName1);
shell.ShellString(testContents1).to(testFileName1);
shell.mkdir('-p', path.dirname(testFileName2));
shell.touch(testFileName2);
shell.ShellString(testContents2).to(testFileName2);
shell.touch(testFileName3);
shell.ShellString(testContents3).to(testFileName3);
shell.touch(testFileName4);
shell.ShellString(testContents4).to(testFileName4);
});
afterEach(() => {
shell.rm('-f', testFileName1);
shell.rm('-rf', 's/'); // For testFileName2
shell.rm('-f', testFileName3);
shell.rm('-f', testFileName4);
});
it('works with no /g and no -i', () => {
const output = cli('sed', 's/foo/bar/', testFileName1);
output.stdout.should.equal('bar\nbarsomething\nbarfoosomething\n');
shell.cat(testFileName1).stdout.should.equal(testContents1);
});
it('works with /g and -i', () => {
const output = cli('sed', '-i', 's/foo/bar/g', testFileName1);
const expected = 'bar\nbarsomething\nbarbarsomething\n';
output.stdout.should.equal(expected);
shell.cat(testFileName1).stdout.should.equal(expected);
});
it('works with regexes containing slashes', () => {
const output = cli(
'sed',
's/http:\\/\\/www\\.google\\.com/https:\\/\\/www\\.facebook\\.com/',
testFileName3,
);
output.stdout.should
.equal('http://www.nochange.com\nhttps://www.facebook.com\n');
shell.cat(testFileName3).stdout.should.equal(testContents3);
});
it('works with backslashes and forward slashes in pattern', () => {
const output = cli(
'sed',
's/\\\\/\\//g',
testFileName4,
);
output.stdout.should.equal('C:/Some/Windows/file/path.txt');
shell.cat(testFileName4).stdout.should.equal(testContents4);
});
it('works with empty replacement strings (with /g)', () => {
const output = cli('sed', 's/foo//g', testFileName1);
output.stdout.should
.equal('\nsomething\nsomething\n');
});
it('does not work with empty regex strings', () => {
(() => {
cli('sed', 's//foo/g', testFileName1);
}).should.throw('Bad sed pattern (empty regex)');
});
it('works with empty replacement strings (without /g)', () => {
const output = cli('sed', 's/foo//', testFileName1);
output.stdout.should
.equal('\nsomething\nfoosomething\n');
});
it('works with weird file names', () => {
const output = cli('sed', 's/foo/bar/', testFileName2);
output.stdout.should.equal('bar\nbarsomething\nbarfoosomething\n');
shell.cat(testFileName2).stdout.should.equal(testContents2);
});
});
describe('grep', () => {
const testFileName = 'file.txt';
beforeEach(() => {
shell.touch(testFileName);
shell.ShellString('1st line\nfoo\nf\ndoes not match\nsomething foo\n')
.to(testFileName);
});
afterEach(() => {
shell.rm('-f', testFileName);
});
it('works with regex syntax', () => {
const ret = cli('grep', 'fo*', testFileName);
ret.stdout.should.equal('foo\nf\nsomething foo\n');
});
});
describe('chmod', () => {
const testFileName = 'file.txt';
const bitMask = parseInt('777', 8);
beforeEach(() => {
shell.touch(testFileName);
});
afterEach(() => {
shell.rm('-f', testFileName);
});
skipIf(process.platform === 'win32', 'works with numeric mode', () => {
// bitmasking is to ignore the upper bits
cli('chmod', '644', testFileName);
(fs.statSync(testFileName).mode & bitMask).should
.equal(parseInt('644', 8));
cli('chmod', '755', testFileName);
(fs.statSync(testFileName).mode & bitMask).should
.equal(parseInt('755', 8));
});
skipIf(process.platform === 'win32', 'works with symbolic mode (all)', () => {
// bitmasking is to ignore the upper bits
cli('chmod', '644', testFileName);
(fs.statSync(testFileName).mode & bitMask).should.equal(parseInt('644', 8));
cli('chmod', '+x', testFileName);
(fs.statSync(testFileName).mode & bitMask).should.equal(parseInt('755', 8));
});
skipIf(process.platform === 'win32', 'works with symbolic mode (user only)', () => {
cli('chmod', '644', testFileName);
(fs.statSync(testFileName).mode & bitMask).should.equal(parseInt('644', 8));
cli('chmod', 'u+x', testFileName);
(fs.statSync(testFileName).mode & bitMask).should.equal(parseInt('744', 8));
});
});
});