From bb468b50ca8d0963e162e38700e7aeb515521f11 Mon Sep 17 00:00:00 2001 From: Richard Lau Date: Wed, 26 Jun 2019 12:37:41 -0400 Subject: [PATCH 1/4] Add test for errors thrown from n-api addon --- .gitignore | 1 + test/napi_throw/binding.gyp | 10 ++++++++++ test/napi_throw/package-lock.json | 5 +++++ test/napi_throw/package.json | 13 +++++++++++++ test/napi_throw/test_napi_throw.c | 28 ++++++++++++++++++++++++++++ test/test-napi-throw.js | 22 ++++++++++++++++++++++ 6 files changed, 79 insertions(+) create mode 100644 test/napi_throw/binding.gyp create mode 100644 test/napi_throw/package-lock.json create mode 100644 test/napi_throw/package.json create mode 100644 test/napi_throw/test_napi_throw.c create mode 100644 test/test-napi-throw.js diff --git a/.gitignore b/.gitignore index 1767441..c191d65 100644 --- a/.gitignore +++ b/.gitignore @@ -36,6 +36,7 @@ # Build directories /build/ /node_modules/ +/test/*/build/ # Package files NodeReport*.txt diff --git a/test/napi_throw/binding.gyp b/test/napi_throw/binding.gyp new file mode 100644 index 0000000..083dc1d --- /dev/null +++ b/test/napi_throw/binding.gyp @@ -0,0 +1,10 @@ +{ + "targets": [ + { + "target_name": "test_napi_throw", + "sources": [ + "test_napi_throw.c" + ] + } + ] +} diff --git a/test/napi_throw/package-lock.json b/test/napi_throw/package-lock.json new file mode 100644 index 0000000..492265b --- /dev/null +++ b/test/napi_throw/package-lock.json @@ -0,0 +1,5 @@ +{ + "name": "test_napi_throw", + "version": "1.0.0", + "lockfileVersion": 1 +} diff --git a/test/napi_throw/package.json b/test/napi_throw/package.json new file mode 100644 index 0000000..31e01eb --- /dev/null +++ b/test/napi_throw/package.json @@ -0,0 +1,13 @@ +{ + "name": "test_napi_throw", + "version": "1.0.0", + "description": "Throw an error.", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1", + "install": "node-gyp rebuild" + }, + "author": "", + "license": "ISC", + "repository": "https://github.com/nodejs/node-report", + "gypfile": true +} diff --git a/test/napi_throw/test_napi_throw.c b/test/napi_throw/test_napi_throw.c new file mode 100644 index 0000000..c5b338a --- /dev/null +++ b/test/napi_throw/test_napi_throw.c @@ -0,0 +1,28 @@ +#include + +#include + +napi_value ThrowError(napi_env env, napi_callback_info info) { + napi_throw_error(env, NULL, "an error occurred"); + return NULL; +} + +static napi_value Init(napi_env env, napi_value exports) { + napi_value result; + assert(napi_create_object(env, &result) == napi_ok); + napi_value exported_function; + assert(napi_create_function(env, + "throwError", + NAPI_AUTO_LENGTH, + ThrowError, + NULL, + &exported_function) == napi_ok); + + assert(napi_set_named_property(env, + result, + "throwError", + exported_function) == napi_ok); + return result; +} + +NAPI_MODULE(NODE_GYP_MODULE_NAME, Init) diff --git a/test/test-napi-throw.js b/test/test-napi-throw.js new file mode 100644 index 0000000..c76ae4f --- /dev/null +++ b/test/test-napi-throw.js @@ -0,0 +1,22 @@ +'use strict'; + +const tap = require('tap'); + +// Build addon that throws an error +const cp = require('child_process'); +const path = require('path'); +const build = cp.spawnSync('npm', [ 'install', '.' ], + { cwd: path.join(__dirname, 'napi_throw'), + encoding: 'utf8' }); +tap.equal(build.stderr, '', 'No errors building addon'); +tap.equal(build.signal, null, 'Failed to build addon'); +tap.equal(build.status, 0, 'Failed to build addon'); + +// Test catching the error does not trigger an UncaughtException report +const common = require('./common.js'); +const report = require('../'); +const obj = require('./napi_throw/build/Release/test_napi_throw.node'); +tap.throws(obj.throwError); + +const reports = common.findReports(process.pid); +tap.equals(reports, [], 'No reports should be generated'); From 3cfe5f8a05e26ab32e19807e7db03815d67ca741 Mon Sep 17 00:00:00 2001 From: Richard Lau Date: Wed, 26 Jun 2019 13:56:06 -0400 Subject: [PATCH 2/4] fixup! Add test for errors thrown from n-api addon --- test/test-napi-throw.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test-napi-throw.js b/test/test-napi-throw.js index c76ae4f..35b9167 100644 --- a/test/test-napi-throw.js +++ b/test/test-napi-throw.js @@ -19,4 +19,4 @@ const obj = require('./napi_throw/build/Release/test_napi_throw.node'); tap.throws(obj.throwError); const reports = common.findReports(process.pid); -tap.equals(reports, [], 'No reports should be generated'); +tap.same(reports, [], 'No reports should be generated'); From c58cc017ec5379ba04dca5a7fe98d224cfd56168 Mon Sep 17 00:00:00 2001 From: Richard Lau Date: Wed, 26 Jun 2019 13:56:48 -0400 Subject: [PATCH 3/4] Add test for errors thrown from a nan addon --- test/nan_throw/binding.gyp | 17 +++++++++++++++++ test/nan_throw/package-lock.json | 5 +++++ test/nan_throw/package.json | 13 +++++++++++++ test/nan_throw/test_nan_throw.cc | 13 +++++++++++++ test/test-nan-throw.js | 22 ++++++++++++++++++++++ 5 files changed, 70 insertions(+) create mode 100644 test/nan_throw/binding.gyp create mode 100644 test/nan_throw/package-lock.json create mode 100644 test/nan_throw/package.json create mode 100644 test/nan_throw/test_nan_throw.cc create mode 100644 test/test-nan-throw.js diff --git a/test/nan_throw/binding.gyp b/test/nan_throw/binding.gyp new file mode 100644 index 0000000..668058b --- /dev/null +++ b/test/nan_throw/binding.gyp @@ -0,0 +1,17 @@ +{ + "targets": [ + { + "target_name": "test_nan_throw", + "include_dirs": [ ' + +#include + +NAN_METHOD(ThrowError) { + Nan::ThrowError("an error occurred"); +} + +NAN_MODULE_INIT(Init) { + Nan::SetMethod(target, "throwError", ThrowError); +} + +NODE_MODULE(NODE_GYP_MODULE_NAME, Init) diff --git a/test/test-nan-throw.js b/test/test-nan-throw.js new file mode 100644 index 0000000..a52f74e --- /dev/null +++ b/test/test-nan-throw.js @@ -0,0 +1,22 @@ +'use strict'; + +const tap = require('tap'); + +// Build addon that throws an error +const cp = require('child_process'); +const path = require('path'); +const build = cp.spawnSync('npm', [ 'install', '.',], + { cwd: path.join(__dirname, 'nan_throw'), + encoding: 'utf8' }); +tap.equal(build.stderr, '', 'No errors building addon'); +tap.equal(build.signal, null, 'Failed to build addon'); +tap.equal(build.status, 0, 'Failed to build addon'); + +// Test catching the error does not trigger an UncaughtException report +const common = require('./common.js'); +const report = require('../'); +const obj = require('./nan_throw/build/Release/test_nan_throw.node'); +tap.throws(obj.throwError); + +const reports = common.findReports(process.pid); +tap.same(reports, [], 'No reports should be generated'); From 7cf7ca418e5b8dbe5423dd071d029fac8cc71931 Mon Sep 17 00:00:00 2001 From: Richard Lau Date: Wed, 26 Jun 2019 19:52:57 -0400 Subject: [PATCH 4/4] fixup! Add test for errors thrown from n-api addon --- test/napi_throw/test_napi_throw.c | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/test/napi_throw/test_napi_throw.c b/test/napi_throw/test_napi_throw.c index c5b338a..3f80ccb 100644 --- a/test/napi_throw/test_napi_throw.c +++ b/test/napi_throw/test_napi_throw.c @@ -8,20 +8,23 @@ napi_value ThrowError(napi_env env, napi_callback_info info) { } static napi_value Init(napi_env env, napi_value exports) { + napi_status status; napi_value result; - assert(napi_create_object(env, &result) == napi_ok); napi_value exported_function; - assert(napi_create_function(env, - "throwError", - NAPI_AUTO_LENGTH, - ThrowError, - NULL, - &exported_function) == napi_ok); - - assert(napi_set_named_property(env, - result, - "throwError", - exported_function) == napi_ok); + status = napi_create_object(env, &result); + assert(status == napi_ok); + status = napi_create_function(env, + "throwError", + NAPI_AUTO_LENGTH, + ThrowError, + NULL, + &exported_function); + assert(status == napi_ok); + status = napi_set_named_property(env, + result, + "throwError", + exported_function); + assert(status == napi_ok); return result; }