0
0
mirror of https://github.com/nodejs/node.git synced 2024-12-01 16:10:02 +01:00
nodejs/test/parallel/test-child-process-spawnsync-input.js
Myles Borins 6c032a8333 test: replace util with backtick strings
Now that we have backticks we no longer need to use util.format
to template strings!

This commit was inspired by #3324, and it replaces instances of
util.format with backtick strings in a number of tests

Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Brian White <mscdex@mscdex.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/3359
2015-10-14 10:02:47 -07:00

99 lines
2.3 KiB
JavaScript

'use strict';
var common = require('../common');
var assert = require('assert');
var os = require('os');
var spawnSync = require('child_process').spawnSync;
var msgOut = 'this is stdout';
var msgErr = 'this is stderr';
// this is actually not os.EOL?
var msgOutBuf = new Buffer(msgOut + '\n');
var msgErrBuf = new Buffer(msgErr + '\n');
var args = [
'-e',
`console.log("${msgOut}"); console.error("${msgErr}");`
];
var ret;
if (process.argv.indexOf('spawnchild') !== -1) {
switch (process.argv[3]) {
case '1':
ret = spawnSync(process.execPath, args, { stdio: 'inherit' });
common.checkSpawnSyncRet(ret);
break;
case '2':
ret = spawnSync(process.execPath, args, {
stdio: ['inherit', 'inherit', 'inherit']
});
common.checkSpawnSyncRet(ret);
break;
}
process.exit(0);
return;
}
function verifyBufOutput(ret) {
common.checkSpawnSyncRet(ret);
assert.deepEqual(ret.stdout, msgOutBuf);
assert.deepEqual(ret.stderr, msgErrBuf);
}
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);
common.checkSpawnSyncRet(ret);
assert.strictEqual(ret.stdout.toString('utf8'), options.input);
assert.strictEqual(ret.stderr.toString('utf8'), '');
options = {
input: new Buffer('hello world')
};
ret = spawnSync('cat', [], options);
common.checkSpawnSyncRet(ret);
assert.deepEqual(ret.stdout, options.input);
assert.deepEqual(ret.stderr, new Buffer(''));
verifyBufOutput(spawnSync(process.execPath, args));
ret = spawnSync(process.execPath, args, { encoding: 'utf8' });
common.checkSpawnSyncRet(ret);
assert.strictEqual(ret.stdout, msgOut + '\n');
assert.strictEqual(ret.stderr, msgErr + '\n');
options = {
maxBuffer: 1
};
ret = spawnSync(process.execPath, args, options);
assert.ok(ret.error, 'maxBuffer should error');
assert.strictEqual(ret.error.errno, 'ENOBUFS');
// we can have buffers larger than maxBuffer because underneath we alloc 64k
// that matches our read sizes
assert.deepEqual(ret.stdout, msgOutBuf);