-
Notifications
You must be signed in to change notification settings - Fork 722
/
cmd.js
122 lines (98 loc) · 3.46 KB
/
cmd.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
var shell = require('..');
var common = require('../src/common');
var assert = require('assert');
var path = require('path');
var os = require('os');
shell.config.silent = true;
//
// Invalids
//
shell.cmd();
assert.ok(shell.error());
var result = shell.cmd('asdfasdf'); // could not find command
assert.notEqual(result.code, 0);
// Test 'fatal' mode for cmd, temporarily overriding process.exit
var oldFatal = shell.config.fatal;
shell.config.fatal = true;
assert.throws(function () {
shell.cmd('asdfasdf'); // could not find command
}, 'cmd: must specify command');
shell.config.fatal = oldFatal;
//
// Valids
//
//
// sync
//
// check if stdout goes to output
result = shell.cmd(common.nodeBinPath, '-e', 'console.log(1234);');
assert.ok(!shell.error());
assert.equal(result.code, 0);
assert.equal(result.stdout, '1234\n');
// check if stderr goes to output
result = shell.cmd(common.nodeBinPath, '-e', 'console.error(1234);');
assert.ok(!shell.error());
assert.equal(result.code, 0);
assert.equal(result.stdout, '');
assert.equal(result.stderr, '1234\n');
// check if stdout + stderr go to output
result = shell.cmd(common.nodeBinPath, '-e', 'console.error(1234); console.log(666);');
assert.ok(!shell.error());
assert.equal(result.code, 0);
assert.equal(result.stdout, '666\n');
assert.equal(result.stderr, '1234\n');
// check exit code
result = shell.cmd(common.nodeBinPath, '-e', 'process.exit(12);');
assert.ok(shell.error());
assert.equal(result.code, 12);
// interaction with cd
shell.cd('resources/external');
result = shell.cmd(common.nodeBinPath, 'node_script.js');
assert.ok(!shell.error());
assert.equal(result.code, 0);
assert.equal(result.stdout, 'node_script_1234\n');
shell.cd('../..');
// set cwd
result = shell.cmd('shx', 'pwd', { cwd: '..' });
assert.ok(!shell.error());
assert.equal(result.code, 0);
assert.equal(result.stdout, path.resolve('..') + os.EOL);
// supports globbing by default
result = shell.cmd('echo', 'resources/*.txt');
assert.equal(result.stdout, 'resources/a.txt resources/file1.txt resources/file2.txt\n');
assert.equal(result.stderr, '');
assert.ok(!shell.error());
// globbing can be disabled
shell.set('-f');
result = shell.cmd('echo', 'resources/*.txt');
assert.equal(result.stdout, 'resources/*.txt\n');
assert.equal(result.stderr, '');
assert.ok(!shell.error());
shell.set('+f');
// cmd returns a ShellString
result = shell.cmd('echo', 'foo');
assert.equal(typeof result, 'object');
assert.ok(result instanceof String);
assert.equal(typeof result.stdout, 'string');
assert.strictEqual(result.toString(), result.stdout);
// TODO(nate): make it exactly equivalent to stderr, unless stderr === ''
// shell.error() contains the stderr of external command in the case of an error
result = shell.cmd(common.nodeBinPath, '-e', 'console.error(1234); process.exit(1);');
assert.equal(shell.error(), 'cmd: ' + result.stderr);
assert.equal(result.code, 1);
assert.equal(result.stdout, '');
assert.equal(result.stderr, '1234\n');
// option: realtimeOutput === false
result = shell.cmd(common.nodeBinPath, '-e', 'console.error(1234); console.log(5678);', {
realtimeOutput: false
});
assert.ok(!shell.error());
assert.equal(result.code, 0);
assert.equal(result.stdout, '5678\n');
assert.equal(result.stderr, '1234\n');
// cmd works, even if it's piped while in silent mode
result = shell.ShellString('foo bar baz').cmd('cat', { silent: true });
assert.equal(typeof result, 'object');
assert.ok(result instanceof String);
assert.equal(result.stdout, 'foo bar baz');
shell.exit(123);