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
|
|
|
];
|
|
|
|
|
|
|
|
var ret;
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
assert.deepEqual(ret.stdout, msgOutBuf);
|
|
|
|
assert.deepEqual(ret.stderr, msgErrBuf);
|
|
|
|
}
|
|
|
|
|
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]));
|
|
|
|
|
|
|
|
var options = {
|
|
|
|
input: 1234
|
|
|
|
};
|
|
|
|
|
|
|
|
assert.throws(function() {
|
|
|
|
spawnSync('cat', [], options);
|
|
|
|
}, /TypeError:.*should be Buffer or string not number/);
|
|
|
|
|
|
|
|
|
|
|
|
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);
|
2014-02-10 21:40:48 +01:00
|
|
|
assert.deepEqual(ret.stdout, options.input);
|
2016-01-26 00:00:06 +01:00
|
|
|
assert.deepEqual(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');
|