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
Joyee Cheung 562cf5a81c assert: fix premature deep strict comparison
Refactors _deepEqual and fixes a few code paths that lead to
behaviors contradicting what the doc says. Before this commit
certain types of objects (Buffers, Dates, etc.) are not checked
properly, and can get away with different prototypes AND different
enumerable owned properties because _deepEqual would jump to
premature conclusion for them.

Since we no longer follow CommonJS unit testing spec,
the checks for primitives and object prototypes are moved
forward for faster failure.

Improve regexp and float* array checks:

* Don't compare lastIndex of regexps, because they are not
  enumerable, so according to the docs they should not be compared
* Compare flags of regexps instead of separate properties
* Use built-in tags to test for float* arrays instead of using
  instanceof

Use full link to the archived GitHub repository.

Use util.objectToString for future improvements to that function
that makes sure the call won't be tampered with.

PR-URL: https://github.com/nodejs/node/pull/11128
Refs: https://github.com/nodejs/node/pull/10282#issuecomment-274267895
Refs: https://github.com/nodejs/node/issues/10258#issuecomment-266963234
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
2017-02-27 18:39:40 +08:00

101 lines
2.4 KiB
JavaScript

'use strict';
require('../common');
const assert = require('assert');
const spawnSync = require('child_process').spawnSync;
const msgOut = 'this is stdout';
const msgErr = 'this is stderr';
// this is actually not os.EOL?
const msgOutBuf = Buffer.from(msgOut + '\n');
const msgErrBuf = Buffer.from(msgErr + '\n');
const args = [
'-e',
`console.log("${msgOut}"); console.error("${msgErr}");`
];
let ret;
function checkSpawnSyncRet(ret) {
assert.strictEqual(ret.status, 0);
assert.strictEqual(ret.error, undefined);
}
function verifyBufOutput(ret) {
checkSpawnSyncRet(ret);
assert.deepStrictEqual(ret.stdout, msgOutBuf);
assert.deepStrictEqual(ret.stderr, msgErrBuf);
}
if (process.argv.indexOf('spawnchild') !== -1) {
switch (process.argv[3]) {
case '1':
ret = spawnSync(process.execPath, args, { stdio: 'inherit' });
checkSpawnSyncRet(ret);
break;
case '2':
ret = spawnSync(process.execPath, args, {
stdio: ['inherit', 'inherit', 'inherit']
});
checkSpawnSyncRet(ret);
break;
}
process.exit(0);
return;
}
verifyBufOutput(spawnSync(process.execPath, [__filename, 'spawnchild', 1]));
verifyBufOutput(spawnSync(process.execPath, [__filename, 'spawnchild', 2]));
let options = {
input: 1234
};
assert.throws(function() {
spawnSync('cat', [], options);
}, /TypeError:.*should be Buffer, Uint8Array or string not number/);
options = {
input: 'hello world'
};
ret = spawnSync('cat', [], options);
checkSpawnSyncRet(ret);
assert.strictEqual(ret.stdout.toString('utf8'), options.input);
assert.strictEqual(ret.stderr.toString('utf8'), '');
options = {
input: Buffer.from('hello world')
};
ret = spawnSync('cat', [], options);
checkSpawnSyncRet(ret);
assert.deepStrictEqual(ret.stdout, options.input);
assert.deepStrictEqual(ret.stderr, Buffer.from(''));
options = {
input: Uint8Array.from(Buffer.from('hello world'))
};
ret = spawnSync('cat', [], options);
checkSpawnSyncRet(ret);
// Wrap options.input because Uint8Array and Buffer have different prototypes.
assert.deepStrictEqual(ret.stdout, Buffer.from(options.input));
assert.deepStrictEqual(ret.stderr, Buffer.from(''));
verifyBufOutput(spawnSync(process.execPath, args));
ret = spawnSync(process.execPath, args, { encoding: 'utf8' });
checkSpawnSyncRet(ret);
assert.strictEqual(ret.stdout, msgOut + '\n');
assert.strictEqual(ret.stderr, msgErr + '\n');