2015-05-19 13:00:06 +02:00
|
|
|
'use strict';
|
2015-11-17 06:17:16 +01:00
|
|
|
require('../common');
|
2014-02-10 21:40:48 +01:00
|
|
|
|
2015-11-17 06:17:16 +01:00
|
|
|
const assert = require('assert');
|
2014-02-10 21:40:48 +01:00
|
|
|
|
2015-11-17 06:17:16 +01:00
|
|
|
const spawnSync = require('child_process').spawnSync;
|
|
|
|
|
|
|
|
const msgOut = 'this is stdout';
|
|
|
|
const msgErr = 'this is stderr';
|
2014-02-10 21:40:48 +01:00
|
|
|
|
|
|
|
// this is actually not os.EOL?
|
2016-01-26 00:00:06 +01:00
|
|
|
const msgOutBuf = Buffer.from(msgOut + '\n');
|
|
|
|
const msgErrBuf = Buffer.from(msgErr + '\n');
|
2014-02-10 21:40:48 +01:00
|
|
|
|
2015-11-17 06:17:16 +01:00
|
|
|
const args = [
|
2014-02-10 21:40:48 +01:00
|
|
|
'-e',
|
2015-10-13 06:26:50 +02:00
|
|
|
`console.log("${msgOut}"); console.error("${msgErr}");`
|
2014-02-10 21:40:48 +01:00
|
|
|
];
|
|
|
|
|
2017-01-08 14:19:00 +01:00
|
|
|
let ret;
|
2014-02-10 21:40:48 +01:00
|
|
|
|
|
|
|
|
2015-11-17 06:17:16 +01:00
|
|
|
function checkSpawnSyncRet(ret) {
|
|
|
|
assert.strictEqual(ret.status, 0);
|
|
|
|
assert.strictEqual(ret.error, undefined);
|
2016-01-15 09:53:11 +01:00
|
|
|
}
|
2015-11-17 06:17:16 +01:00
|
|
|
|
|
|
|
function verifyBufOutput(ret) {
|
|
|
|
checkSpawnSyncRet(ret);
|
2016-04-20 00:37:45 +02:00
|
|
|
assert.deepStrictEqual(ret.stdout, msgOutBuf);
|
|
|
|
assert.deepStrictEqual(ret.stderr, msgErrBuf);
|
2015-11-17 06:17:16 +01:00
|
|
|
}
|
|
|
|
|
2014-02-10 21:40:48 +01:00
|
|
|
if (process.argv.indexOf('spawnchild') !== -1) {
|
|
|
|
switch (process.argv[3]) {
|
|
|
|
case '1':
|
|
|
|
ret = spawnSync(process.execPath, args, { stdio: 'inherit' });
|
2015-11-17 06:17:16 +01:00
|
|
|
checkSpawnSyncRet(ret);
|
2014-02-10 21:40:48 +01:00
|
|
|
break;
|
|
|
|
case '2':
|
|
|
|
ret = spawnSync(process.execPath, args, {
|
|
|
|
stdio: ['inherit', 'inherit', 'inherit']
|
|
|
|
});
|
2015-11-17 06:17:16 +01:00
|
|
|
checkSpawnSyncRet(ret);
|
2014-02-10 21:40:48 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
process.exit(0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
verifyBufOutput(spawnSync(process.execPath, [__filename, 'spawnchild', 1]));
|
|
|
|
verifyBufOutput(spawnSync(process.execPath, [__filename, 'spawnchild', 2]));
|
|
|
|
|
2017-01-08 14:19:00 +01:00
|
|
|
let options = {
|
2014-02-10 21:40:48 +01:00
|
|
|
input: 1234
|
|
|
|
};
|
|
|
|
|
|
|
|
assert.throws(function() {
|
|
|
|
spawnSync('cat', [], options);
|
2017-01-06 02:12:26 +01:00
|
|
|
}, /TypeError:.*should be Buffer, Uint8Array or string not number/);
|
2014-02-10 21:40:48 +01:00
|
|
|
|
|
|
|
|
|
|
|
options = {
|
|
|
|
input: 'hello world'
|
|
|
|
};
|
|
|
|
|
|
|
|
ret = spawnSync('cat', [], options);
|
|
|
|
|
2015-11-17 06:17:16 +01:00
|
|
|
checkSpawnSyncRet(ret);
|
2014-02-10 21:40:48 +01:00
|
|
|
assert.strictEqual(ret.stdout.toString('utf8'), options.input);
|
|
|
|
assert.strictEqual(ret.stderr.toString('utf8'), '');
|
|
|
|
|
|
|
|
options = {
|
2016-01-26 00:00:06 +01:00
|
|
|
input: Buffer.from('hello world')
|
2014-02-10 21:40:48 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
ret = spawnSync('cat', [], options);
|
|
|
|
|
2015-11-17 06:17:16 +01:00
|
|
|
checkSpawnSyncRet(ret);
|
2016-04-20 00:37:45 +02:00
|
|
|
assert.deepStrictEqual(ret.stdout, options.input);
|
|
|
|
assert.deepStrictEqual(ret.stderr, Buffer.from(''));
|
2014-02-10 21:40:48 +01:00
|
|
|
|
2017-01-06 02:12:26 +01:00
|
|
|
options = {
|
|
|
|
input: Uint8Array.from(Buffer.from('hello world'))
|
|
|
|
};
|
|
|
|
|
|
|
|
ret = spawnSync('cat', [], options);
|
|
|
|
|
|
|
|
checkSpawnSyncRet(ret);
|
2017-02-02 21:06:25 +01:00
|
|
|
// Wrap options.input because Uint8Array and Buffer have different prototypes.
|
|
|
|
assert.deepStrictEqual(ret.stdout, Buffer.from(options.input));
|
2017-01-06 02:12:26 +01:00
|
|
|
assert.deepStrictEqual(ret.stderr, Buffer.from(''));
|
|
|
|
|
2014-02-10 21:40:48 +01:00
|
|
|
verifyBufOutput(spawnSync(process.execPath, args));
|
|
|
|
|
|
|
|
ret = spawnSync(process.execPath, args, { encoding: 'utf8' });
|
|
|
|
|
2015-11-17 06:17:16 +01:00
|
|
|
checkSpawnSyncRet(ret);
|
2014-02-10 21:40:48 +01:00
|
|
|
assert.strictEqual(ret.stdout, msgOut + '\n');
|
|
|
|
assert.strictEqual(ret.stderr, msgErr + '\n');
|