/
spawn.js
92 lines (76 loc) · 2.4 KB
/
spawn.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
'use strict';
const should = require('chai').should(); // eslint-disable-line
const pathFn = require('path');
const fs = require('fs');
const rewire = require('rewire');
describe('spawn', () => {
const spawn = require('../../lib/spawn');
const CacheStream = require('../../lib/cache_stream');
const fixturePath = pathFn.join(__dirname, 'spawn_test.txt');
const fixture = 'test content';
before(done => {
fs.writeFile(fixturePath, fixture, done);
});
after(done => {
fs.unlink(fixturePath, done);
});
it('default', () => spawn('cat', [fixturePath]).then(content => {
content.should.eql(fixture);
}));
it('command is required', () => {
try {
spawn();
} catch (err) {
err.should.have.property('message', 'command is required!');
}
});
it('error', () => spawn('cat', ['nothing']).catch(err => {
err.message.trim().should.eql('cat: nothing: No such file or directory');
err.code.should.eql(1);
}));
it('verbose - stdout', () => {
const spawn = rewire('../../lib/spawn');
const stdoutCache = new CacheStream();
const stderrCache = new CacheStream();
const content = 'something';
spawn.__set__('process', {
stdout: stdoutCache,
stderr: stderrCache,
removeListener: () => {},
on: () => {},
exit: () => {}
});
return spawn('echo', [content], {
verbose: true
}).then(() => {
stdoutCache.getCache().toString('utf8').trim().should.eql(content);
});
});
it('verbose - stderr', () => {
const spawn = rewire('../../lib/spawn');
const stdoutCache = new CacheStream();
const stderrCache = new CacheStream();
spawn.__set__('process', {
stdout: stdoutCache,
stderr: stderrCache,
removeListener: () => {},
on: () => {},
exit: () => {}
});
return spawn('cat', ['nothing'], {
verbose: true
}).catch(() => {
stderrCache.getCache().toString('utf8').trim().should
.eql('cat: nothing: No such file or directory');
});
});
it('custom encoding', () => spawn('cat', [fixturePath], {encoding: 'hex'}).then(content => {
content.should.eql(Buffer.from(fixture).toString('hex'));
}));
it('encoding = null', () => spawn('cat', [fixturePath], {encoding: null}).then(content => {
content.should.eql(Buffer.from(fixture));
}));
it('stdio = inherit', () => spawn('echo', ['something'], {
stdio: 'inherit'
}));
});