Skip to content

Commit

Permalink
test: refactor test/common/report.js
Browse files Browse the repository at this point in the history
- Don't unnecessarily require('../common').
- Eliminate state maintained in tmppath.
- validate() was being used synchronously, but was actually
  asynchronous. Make it synchronous.
- Other misc. drive by cleanup.

PR-URL: #25754
Reviewed-By: Richard Lau <[email protected]>
Reviewed-By: Anto Aravinth <[email protected]>
Reviewed-By: Gireesh Punathil <[email protected]>
  • Loading branch information
cjihrig committed Jan 30, 2019
1 parent 7fa5157 commit c64b1ae
Showing 1 changed file with 24 additions and 28 deletions.
52 changes: 24 additions & 28 deletions test/common/report.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,39 @@
/* eslint-disable node-core/required-modules */
'use strict';
require('../common');
const assert = require('assert');
const fs = require('fs');
const path = require('path');

const REPORT_SECTIONS = [ 'header',
'javascriptStack',
'nativeStack',
'javascriptHeap',
'libuv',
'environmentVariables',
'sharedObjects' ];

let tmppath = '';

exports.findReports = (pid, path) => {
function findReports(pid, dir) {
// Default filenames are of the form
// report.<date>.<time>.<pid>.<seq>.json
tmppath = path;
const format = '^report\\.\\d+\\.\\d+\\.' + pid + '\\.\\d+\\.json$';
const filePattern = new RegExp(format);
const files = fs.readdirSync(path);
return files.filter((file) => filePattern.test(file));
};

exports.validate = (report, options) => {
const jtmp = path.join(tmppath, report);
fs.readFile(jtmp, (err, data) => {
this.validateContent(data, options);
const files = fs.readdirSync(dir);
const results = [];

files.forEach((file) => {
if (filePattern.test(file))
results.push(path.join(dir, file));
});
};

return results;
}

exports.validateContent = function validateContent(data, options) {
function validate(report) {
const data = fs.readFileSync(report, 'utf8');

validateContent(data);
}

function validateContent(data) {
const report = JSON.parse(data);
const comp = Object.keys(report);

// Check all sections are present
REPORT_SECTIONS.forEach((section) => {
assert.ok(comp.includes(section));
// Verify that all sections are present.
['header', 'javascriptStack', 'nativeStack', 'javascriptHeap',
'libuv', 'environmentVariables', 'sharedObjects'].forEach((section) => {
assert(report.hasOwnProperty(section));
});
};
}

module.exports = { findReports, validate, validateContent };

0 comments on commit c64b1ae

Please sign in to comment.