diff options
author | Christopher Hiller <boneskull@boneskull.com> | 2019-07-10 13:27:44 -0700 |
---|---|---|
committer | Michaƫl Zasso <targos@protonmail.com> | 2019-07-20 11:10:23 +0200 |
commit | 35e3f1f449d39253afe6b7075194876541948e52 (patch) | |
tree | 58c490ba5034bcbf78042964974aa7e76035c6e1 /test | |
parent | ebc3876754de3d3b0ddd33697a5e3f38e8c488e2 (diff) | |
download | android-node-v8-35e3f1f449d39253afe6b7075194876541948e52.tar.gz android-node-v8-35e3f1f449d39253afe6b7075194876541948e52.tar.bz2 android-node-v8-35e3f1f449d39253afe6b7075194876541948e52.zip |
report: modify getReport() to return an Object
It's likely that anyone using `process.report.getReport()` will be
processing the return value thereafter (e.g., filtering fields or
redacting secrets). This change eliminates boilerplate by calling
`JSON.parse()` on the return value.
Also modified the `validateContent()` and `validate()` test helpers in
`test/common/report.js` to be somewhat more obvious and helpful. Of
note, a report failing validation will now be easier (though still not
_easy_) to read when prepended to the stack trace.
- Refs: https://github.com/nodejs/diagnostics/issues/315
PR-URL: https://github.com/nodejs/node/pull/28630
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Richard Lau <riclau@uk.ibm.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Jiawen Geng <technicalcute@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Diffstat (limited to 'test')
-rw-r--r-- | test/addons/worker-addon/test.js | 2 | ||||
-rw-r--r-- | test/common/README.md | 13 | ||||
-rw-r--r-- | test/common/report.js | 26 | ||||
-rw-r--r-- | test/report/test-report-uv-handles.js | 2 |
4 files changed, 26 insertions, 17 deletions
diff --git a/test/addons/worker-addon/test.js b/test/addons/worker-addon/test.js index ef158e98b1..85a5e27016 100644 --- a/test/addons/worker-addon/test.js +++ b/test/addons/worker-addon/test.js @@ -31,7 +31,7 @@ switch (process.argv[2]) { } // Use process.report to figure out if we might be running under musl libc. -const glibc = JSON.parse(process.report.getReport()).header.glibcVersionRuntime; +const glibc = process.report.getReport().header.glibcVersionRuntime; assert(typeof glibc === 'string' || glibc === undefined, glibc); const libcMayBeMusl = common.isLinux && glibc === undefined; diff --git a/test/common/README.md b/test/common/README.md index 2b8f852a3b..0fd03ce767 100644 --- a/test/common/README.md +++ b/test/common/README.md @@ -859,19 +859,20 @@ functionality. Returns an array of diagnotic report file names found in `dir`. The files should have been generated by a process whose PID matches `pid`. -### validate(report) +### validate(filepath) -* `report` [<string>] Diagnostic report file name to validate. +* `filepath` [<string>] Diagnostic report filepath to validate. Validates the schema of a diagnostic report file whose path is specified in -`report`. If the report fails validation, an exception is thrown. +`filepath`. If the report fails validation, an exception is thrown. -### validateContent(data) +### validateContent(report) -* `data` [<string>] Contents of a diagnostic report file. +* `report` [<Object|string>] JSON contents of a diagnostic report file, the +parsed Object thereof, or the result of `process.report.getReport()`. Validates the schema of a diagnostic report whose content is specified in -`data`. If the report fails validation, an exception is thrown. +`report`. If the report fails validation, an exception is thrown. ## tick Module diff --git a/test/common/report.js b/test/common/report.js index 7637e5ed95..eda3ad4ae2 100644 --- a/test/common/report.js +++ b/test/common/report.js @@ -4,6 +4,7 @@ const assert = require('assert'); const fs = require('fs'); const os = require('os'); const path = require('path'); +const util = require('util'); function findReports(pid, dir) { // Default filenames are of the form @@ -21,24 +22,31 @@ function findReports(pid, dir) { return results; } -function validate(report) { - const data = fs.readFileSync(report, 'utf8'); - - validateContent(data); +function validate(filepath) { + validateContent(JSON.parse(fs.readFileSync(filepath, 'utf8'))); } -function validateContent(data) { +function validateContent(report) { + if (typeof report === 'string') { + try { + report = JSON.parse(report); + } catch { + throw new TypeError( + 'validateContent() expects a JSON string or JavaScript Object'); + } + } try { - _validateContent(data); + _validateContent(report); } catch (err) { - err.stack += `\n------\nFailing Report:\n${data}`; + try { + err.stack += util.format('\n------\nFailing Report:\n%O', report); + } catch {} throw err; } } -function _validateContent(data) { +function _validateContent(report) { const isWindows = process.platform === 'win32'; - const report = JSON.parse(data); // Verify that all sections are present as own properties of the report. const sections = ['header', 'javascriptStack', 'nativeStack', diff --git a/test/report/test-report-uv-handles.js b/test/report/test-report-uv-handles.js index 2061920432..4d9bb12b44 100644 --- a/test/report/test-report-uv-handles.js +++ b/test/report/test-report-uv-handles.js @@ -43,7 +43,7 @@ if (process.argv[2] === 'child') { const server = http.createServer((req, res) => { req.on('end', () => { // Generate the report while the connection is active. - console.log(process.report.getReport()); + console.log(JSON.stringify(process.report.getReport(), null, 2)); child_process.kill(); res.writeHead(200, { 'Content-Type': 'text/plain' }); |