From 598a128ff46f6ab8d562877f401b32b50366b483 Mon Sep 17 00:00:00 2001 From: cjihrig Date: Fri, 7 Jul 2017 09:33:23 -0400 Subject: [PATCH] test: handle missing V8 tests in n-api test MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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: https://github.com/nodejs/node/issues/14113 Fixes: https://github.com/nodejs/node/issues/13344 PR-URL: https://github.com/nodejs/node/pull/14123 Reviewed-By: Daniel Bevenius Reviewed-By: Michaƫl Zasso Reviewed-By: Ben Noordhuis Reviewed-By: Michael Dawson Reviewed-By: James M Snell --- .../test_general/testInstanceOf.js | 30 ++++++++++++------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/test/addons-napi/test_general/testInstanceOf.js b/test/addons-napi/test_general/testInstanceOf.js index 3279979719..de52994fba 100644 --- a/test/addons-napi/test_general/testInstanceOf.js +++ b/test/addons-napi/test_general/testInstanceOf.js @@ -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 @@ -34,19 +39,22 @@ 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 &&