Skip to content

Commit 5e6af53

Browse files
cjihrigtargos
authored andcommitted
test: refactor test/common/report.js
- 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 <riclau@uk.ibm.com> Reviewed-By: Anto Aravinth <anto.aravinth.cse@gmail.com> Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com>
1 parent 798ee40 commit 5e6af53

File tree

1 file changed

+24
-28
lines changed

1 file changed

+24
-28
lines changed

test/common/report.js

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,39 @@
1+
/* eslint-disable node-core/required-modules */
12
'use strict';
2-
require('../common');
33
const assert = require('assert');
44
const fs = require('fs');
55
const path = require('path');
66

7-
const REPORT_SECTIONS = [ 'header',
8-
'javascriptStack',
9-
'nativeStack',
10-
'javascriptHeap',
11-
'libuv',
12-
'environmentVariables',
13-
'sharedObjects' ];
14-
15-
let tmppath = '';
16-
17-
exports.findReports = (pid, path) => {
7+
function findReports(pid, dir) {
188
// Default filenames are of the form
199
// report.<date>.<time>.<pid>.<seq>.json
20-
tmppath = path;
2110
const format = '^report\\.\\d+\\.\\d+\\.' + pid + '\\.\\d+\\.json$';
2211
const filePattern = new RegExp(format);
23-
const files = fs.readdirSync(path);
24-
return files.filter((file) => filePattern.test(file));
25-
};
26-
27-
exports.validate = (report, options) => {
28-
const jtmp = path.join(tmppath, report);
29-
fs.readFile(jtmp, (err, data) => {
30-
this.validateContent(data, options);
12+
const files = fs.readdirSync(dir);
13+
const results = [];
14+
15+
files.forEach((file) => {
16+
if (filePattern.test(file))
17+
results.push(path.join(dir, file));
3118
});
32-
};
3319

20+
return results;
21+
}
3422

35-
exports.validateContent = function validateContent(data, options) {
23+
function validate(report) {
24+
const data = fs.readFileSync(report, 'utf8');
25+
26+
validateContent(data);
27+
}
28+
29+
function validateContent(data) {
3630
const report = JSON.parse(data);
37-
const comp = Object.keys(report);
3831

39-
// Check all sections are present
40-
REPORT_SECTIONS.forEach((section) => {
41-
assert.ok(comp.includes(section));
32+
// Verify that all sections are present.
33+
['header', 'javascriptStack', 'nativeStack', 'javascriptHeap',
34+
'libuv', 'environmentVariables', 'sharedObjects'].forEach((section) => {
35+
assert(report.hasOwnProperty(section));
4236
});
43-
};
37+
}
38+
39+
module.exports = { findReports, validate, validateContent };

0 commit comments

Comments
 (0)