Skip to content

Commit

Permalink
test: handle missing V8 tests in n-api test
Browse files Browse the repository at this point in the history
The N-API test testInstanceOf.js relies on several V8 test files
which may not exist in downloadable archives. If the files are
missing, this commit causes a warning to be emitted rather than
failing the test.

Refs: #14113
Fixes: #13344
Backport-PR-URL: #19447
PR-URL: #14123
Reviewed-By: Daniel Bevenius <daniel.bevenius@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
cjihrig authored and MylesBorins committed Apr 16, 2018
1 parent cd30154 commit 4827421
Showing 1 changed file with 20 additions and 12 deletions.
32 changes: 20 additions & 12 deletions test/addons-napi/test_general/testInstanceOf.js
Expand Up @@ -9,6 +9,11 @@ const assert = require('assert');
const addon = require(`./build/${common.buildType}/test_general`);
const path = require('path');

// This test depends on a number of V8 tests.
const v8TestsDir = path.resolve(__dirname, '..', '..', '..', 'deps', 'v8',
'test', 'mjsunit');
const v8TestsDirExists = fs.existsSync(v8TestsDir);

// The following assert functions are referenced by v8's unit tests
// See for instance deps/v8/test/mjsunit/instanceof.js
// eslint-disable-next-line no-unused-vars
Expand All @@ -34,27 +39,30 @@ function assertThrows(statement) {
}

function testFile(fileName) {
const contents = fs.readFileSync(fileName, { encoding: 'utf8' });
eval(contents.replace(/[(]([^\s(]+)\s+instanceof\s+([^)]+)[)]/g,
'(addon.doInstanceOf($1, $2))'));
try {
const contents = fs.readFileSync(fileName, { encoding: 'utf8' });
eval(contents.replace(/[(]([^\s(]+)\s+instanceof\s+([^)]+)[)]/g,
'(addon.doInstanceOf($1, $2))'));
} catch (err) {
// This test depends on V8 test files, which may not exist in downloaded
// archives. Emit a warning if the tests cannot be found instead of failing.
if (err.code === 'ENOENT' && !v8TestsDirExists)
process.emitWarning(`test file ${fileName} does not exist.`);
else
throw err;
}
}

testFile(
path.join(path.resolve(__dirname, '..', '..', '..',
'deps', 'v8', 'test', 'mjsunit'),
'instanceof.js'));
testFile(
path.join(path.resolve(__dirname, '..', '..', '..',
'deps', 'v8', 'test', 'mjsunit'),
'instanceof-2.js'));
testFile(path.join(v8TestsDir, 'instanceof.js'));
testFile(path.join(v8TestsDir, 'instanceof-2.js'));

// We can only perform this test if we have a working Symbol.hasInstance
if (typeof Symbol !== 'undefined' && 'hasInstance' in Symbol &&
typeof Symbol.hasInstance === 'symbol') {

function compareToNative(theObject, theConstructor) {
assert.strictEqual(addon.doInstanceOf(theObject, theConstructor),
(theObject instanceof theConstructor));
(theObject instanceof theConstructor));
}

function MyClass() {}
Expand Down

0 comments on commit 4827421

Please sign in to comment.