UNPKG

1.6 kBJavaScriptView Raw
1var spawn = require('child_process').spawn;
2var assert = require('assert');
3
4exports.dotSlashEmpty = function () {
5 testCmd('./bin.js', []);
6};
7
8exports.dotSlashArgs = function () {
9 testCmd('./bin.js', [ 'a', 'b', 'c' ]);
10};
11
12exports.nodeEmpty = function () {
13 testCmd('node bin.js', []);
14};
15
16exports.nodeArgs = function () {
17 testCmd('node bin.js', [ 'x', 'y', 'z' ]);
18};
19
20exports.whichNodeEmpty = function () {
21 var which = spawn('which', ['node']);
22
23 which.stdout.on('data', function (buf) {
24 testCmd(buf.toString().trim() + ' bin.js', []);
25 });
26
27 which.stderr.on('data', function (err) {
28 assert.fail(err.toString());
29 });
30};
31
32exports.whichNodeArgs = function () {
33 var which = spawn('which', ['node']);
34
35 which.stdout.on('data', function (buf) {
36 testCmd(buf.toString().trim() + ' bin.js', [ 'q', 'r' ]);
37 });
38
39 which.stderr.on('data', function (err) {
40 assert.fail(err.toString());
41 });
42};
43
44function testCmd (cmd, args) {
45 var to = setTimeout(function () {
46 assert.fail('Never got stdout data.')
47 }, 5000);
48
49 var oldDir = process.cwd();
50 process.chdir(__dirname + '/_');
51
52 var cmds = cmd.split(' ');
53
54 var bin = spawn(cmds[0], cmds.slice(1).concat(args.map(String)));
55 process.chdir(oldDir);
56
57 bin.stderr.on('data', function (err) {
58 assert.fail(err.toString());
59 });
60
61 bin.stdout.on('data', function (buf) {
62 clearTimeout(to);
63 var _ = JSON.parse(buf.toString());
64 assert.eql(_.map(String), args.map(String));
65 });
66}