-
Notifications
You must be signed in to change notification settings - Fork 747
Expand file tree
/
Copy pathexec.js
More file actions
205 lines (176 loc) · 5.72 KB
/
exec.js
File metadata and controls
205 lines (176 loc) · 5.72 KB
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
import os from 'os';
import path from 'path';
import util from 'util';
import test from 'ava';
import shell from '..';
import common from '../src/common';
const CWD = process.cwd();
const ORIG_EXEC_PATH = common.config.execPath;
shell.config.silent = true;
test.afterEach.always(() => {
process.chdir(CWD);
common.config.execPath = ORIG_EXEC_PATH;
});
//
// Invalids
//
test('no args', t => {
shell.exec();
t.truthy(shell.error());
});
test('unknown command', t => {
const result = shell.exec('asdfasdf'); // could not find command
t.truthy(result.code > 0);
});
test('config.fatal and unknown command', t => {
const oldFatal = shell.config.fatal;
shell.config.fatal = true;
t.throws(() => {
shell.exec('asdfasdf'); // could not find command
}, /exec: internal error/);
shell.config.fatal = oldFatal;
});
test('exec exits gracefully if we cannot find the execPath', t => {
common.config.execPath = null;
shell.exec('echo foo');
t.regex(
shell.error(),
/Unable to find a path to the node binary\. Please manually set config\.execPath/
);
});
//
// Valids
//
//
// sync
//
test('check if stdout goes to output', t => {
const result = shell.exec(`${JSON.stringify(common.config.execPath)} -e "console.log(1234);"`);
t.falsy(shell.error());
t.is(result.code, 0);
t.is(result.stdout, '1234\n');
});
test('check if stderr goes to output', t => {
const result = shell.exec(`${JSON.stringify(common.config.execPath)} -e "console.error(1234);"`);
t.falsy(shell.error());
t.is(result.code, 0);
t.is(result.stdout, '');
t.is(result.stderr, '1234\n');
});
test('check if stdout + stderr go to output', t => {
const result = shell.exec(`${JSON.stringify(common.config.execPath)} -e "console.error(1234); console.log(666);"`);
t.falsy(shell.error());
t.is(result.code, 0);
t.is(result.stdout, '666\n');
t.is(result.stderr, '1234\n');
});
test('check exit code', t => {
const result = shell.exec(`${JSON.stringify(common.config.execPath)} -e "process.exit(12);"`);
t.truthy(shell.error());
t.is(result.code, 12);
});
test('interaction with cd', t => {
shell.cd('resources/external');
const result = shell.exec(`${JSON.stringify(common.config.execPath)} node_script.js`);
t.falsy(shell.error());
t.is(result.code, 0);
t.is(result.stdout, 'node_script_1234\n');
});
test('check quotes escaping', t => {
const result = shell.exec(util.format(JSON.stringify(common.config.execPath) + ' -e "console.log(%s);"', "\\\"\\'+\\'_\\'+\\'\\\""));
t.falsy(shell.error());
t.is(result.code, 0);
t.is(result.stdout, "'+'_'+'\n");
});
test('set cwd', t => {
const cmdString = process.platform === 'win32' ? 'cd' : 'pwd';
const result = shell.exec(cmdString, { cwd: '..' });
t.falsy(shell.error());
t.is(result.code, 0);
t.is(result.stdout, path.resolve('..') + os.EOL);
});
test('set maxBuffer (very small)', t => {
const result = shell.exec('echo 1234567890'); // default maxBuffer is ok
t.falsy(shell.error());
t.is(result.code, 0);
t.is(result.stdout, '1234567890' + os.EOL);
if (process.version >= 'v0.11') { // this option doesn't work on v0.10
shell.exec('echo 1234567890', { maxBuffer: 6 });
t.truthy(shell.error());
}
});
test('set timeout option', t => {
const result = shell.exec(`${JSON.stringify(common.config.execPath)} resources/exec/slow.js 100`); // default timeout is ok
t.falsy(shell.error());
t.is(result.code, 0);
if (process.version >= 'v0.11') {
// this option doesn't work on v0.10
shell.exec(`${JSON.stringify(common.config.execPath)} resources/exec/slow.js 100`, { timeout: 10 }); // times out
t.truthy(shell.error());
}
});
test('check process.env works', t => {
t.falsy(shell.env.FOO);
shell.env.FOO = 'Hello world';
const result = shell.exec(process.platform !== 'win32' ? 'echo $FOO' : 'echo %FOO%');
t.falsy(shell.error());
t.is(result.code, 0);
t.is(result.stdout, 'Hello world' + os.EOL);
t.is(result.stderr, '');
});
test('set shell option (TODO: add tests for Windows)', t => {
if (process.platform !== 'win32') {
let result = shell.exec('echo $0');
t.falsy(shell.error());
t.is(result.code, 0);
t.is(result.stdout, '/bin/sh\n'); // sh by default
const bashPath = shell.which('bash').trim();
// this option doesn't work on v0.10
if (bashPath && process.version >= 'v0.11') {
result = shell.exec('echo $0', { shell: '/bin/bash' });
t.falsy(shell.error());
t.is(result.code, 0);
t.is(result.stdout, '/bin/bash\n');
}
}
});
test('exec returns a ShellString', t => {
const result = shell.exec('echo foo');
t.is(typeof result, 'object');
t.truthy(result instanceof String);
t.is(typeof result.stdout, 'string');
t.is(result.toString(), result.stdout);
});
//
// async
//
test.cb('no callback', t => {
const c = shell.exec(`${JSON.stringify(common.config.execPath)} -e "console.log(1234)"`, { async: true });
t.falsy(shell.error());
t.truthy('stdout' in c, 'async exec returns child process object');
t.end();
});
test.cb('callback as 2nd argument', t => {
shell.exec(`${JSON.stringify(common.config.execPath)} -e "console.log(5678);"`, (code, stdout, stderr) => {
t.is(code, 0);
t.is(stdout, '5678\n');
t.is(stderr, '');
t.end();
});
});
test.cb('callback as end argument', t => {
shell.exec(`${JSON.stringify(common.config.execPath)} -e "console.log(5566);"`, { async: true }, (code, stdout, stderr) => {
t.is(code, 0);
t.is(stdout, '5566\n');
t.is(stderr, '');
t.end();
});
});
test.cb('callback as 3rd argument (silent:true)', t => {
shell.exec(`${JSON.stringify(common.config.execPath)} -e "console.log(5678);"`, { silent: true }, (code, stdout, stderr) => {
t.is(code, 0);
t.is(stdout, '5678\n');
t.is(stderr, '');
t.end();
});
});