Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
src: add testing for --diagnostic-dir option
Add testing for diagnostic dir to ensure that it
behaves as expected when overwritting defaults
  • Loading branch information
AshCripps authored and AshCripps committed Jun 19, 2020
commit 57e00286d0883c2d44bafb735ae960542e1d45d1
11 changes: 8 additions & 3 deletions lib/internal/process/warning.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ const {

const assert = require('internal/assert');
const { ERR_INVALID_ARG_TYPE } = require('internal/errors').codes;
const path = require('path');

// Lazily loaded
let fs;
Expand All @@ -20,8 +19,14 @@ function lazyOption() {
// `warningFile` will be set to an empty string.
if (warningFile === undefined) {
options = require('internal/options');
warningFile = path.resolve(options.getOptionValue('--diagnostic-dir'),
options.getOptionValue('--redirect-warnings'));
if (options.getOptionValue('--diagnostic-dir') !== '') {
warningFile = options.getOptionValue('--diagnostic-dir');
}
if (options.getOptionValue('--redirect-warnings') !== '') {
warningFile = options.getOptionValue('--redirect-warnings');
} else {
warningFile = '';
}
}
return warningFile;
}
Expand Down
76 changes: 76 additions & 0 deletions test/sequential/test-diagnostic-dir-cpu-prof.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
'use strict';

// This test is to ensure that --diagnostic-dir does not change the directory
// for --cpu-prof when --cpu-prof-dir is specified

const common = require('../common');
const fixtures = require('../common/fixtures');
common.skipIfInspectorDisabled();

const assert = require('assert');
const fs = require('fs');
const path = require('path');
const { spawnSync } = require('child_process');

const tmpdir = require('../common/tmpdir');
const {
getCpuProfiles,
kCpuProfInterval,
env,
verifyFrames
} = require('../common/cpu-prof');

// Test --diagnostic-dir changes the default for --cpu-prof

{
tmpdir.refresh();
const dir = path.join(tmpdir.path, 'prof');
const output = spawnSync(process.execPath, [
'--cpu-prof',
'--cpu-prof-interval',
kCpuProfInterval,
'--diagnostic-dir',
dir,
fixtures.path('workload', 'fibonacci.js'),
], {
cwd: tmpdir.path,
env
});
if (output.status !== 0) {
console.log(output.stderr.toString());
}
assert.strictEqual(output.status, 0);
assert(fs.existsSync(dir));
const profiles = getCpuProfiles(dir);
assert.strictEqual(profiles.length, 1);
verifyFrames(output, profiles[0], 'fibonacci.js');
}

// Test --cpu-prof-dir overwrites --diagnostic-dir

{
tmpdir.refresh();
const dir = path.join(tmpdir.path, 'diag');
const dir2 = path.join(tmpdir.path, 'prof');
const output = spawnSync(process.execPath, [
'--cpu-prof',
'--cpu-prof-interval',
kCpuProfInterval,
'--diagnostic-dir',
dir,
'--cpu-prof-dir',
dir2,
fixtures.path('workload', 'fibonacci.js'),
], {
cwd: tmpdir.path,
env
});
if (output.status !== 0) {
console.log(output.stderr.toString());
}
assert.strictEqual(output.status, 0);
assert(fs.existsSync(dir2));
const profiles = getCpuProfiles(dir2);
assert.strictEqual(profiles.length, 1);
verifyFrames(output, profiles[0], 'fibonacci.js');
}
117 changes: 117 additions & 0 deletions test/sequential/test-diagnostic-dir-heap-prof.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
'use strict';

// This test is to ensure that --diagnostic-dir does not change the directory
// for --cpu-prof when --cpu-prof-dir is specified

const common = require('../common');
const fixtures = require('../common/fixtures');
common.skipIfInspectorDisabled();

const assert = require('assert');
const fs = require('fs');
const path = require('path');
const { spawnSync } = require('child_process');

const tmpdir = require('../common/tmpdir');

function findFirstFrameInNode(root, func) {
const first = root.children.find(
(child) => child.callFrame.functionName === func
);
if (first) {
return first;
}
for (const child of root.children) {
const first = findFirstFrameInNode(child, func);
if (first) {
return first;
}
}
return undefined;
}

function findFirstFrame(file, func) {
const data = fs.readFileSync(file, 'utf8');
const profile = JSON.parse(data);
const first = findFirstFrameInNode(profile.head, func);
return { frame: first, roots: profile.head.children };
}

function verifyFrames(output, file, func) {
const { frame, roots } = findFirstFrame(file, func);
if (!frame) {
// Show native debug output and the profile for debugging.
console.log(output.stderr.toString());
console.log(roots);
}
assert.notDeepStrictEqual(frame, undefined);
}

const kHeapProfInterval = 128;
const TEST_ALLOCATION = kHeapProfInterval * 2;

const env = {
...process.env,
TEST_ALLOCATION,
NODE_DEBUG_NATIVE: 'INSPECTOR_PROFILER'
};

function getHeapProfiles(dir) {
const list = fs.readdirSync(dir);
return list
.filter((file) => file.endsWith('.heapprofile'))
.map((file) => path.join(dir, file));
}

// Test --diagnostic-dir changes the default for --cpu-prof
{
tmpdir.refresh();
const dir = path.join(tmpdir.path, 'prof');
const output = spawnSync(process.execPath, [
'--heap-prof',
'--diagnostic-dir',
dir,
'--heap-prof-interval',
kHeapProfInterval,
fixtures.path('workload', 'allocation.js'),
], {
cwd: tmpdir.path,
env
});
if (output.status !== 0) {
console.log(output.stderr.toString());
}
assert.strictEqual(output.status, 0);
assert(fs.existsSync(dir));
const profiles = getHeapProfiles(dir);
assert.strictEqual(profiles.length, 1);
verifyFrames(output, profiles[0], 'runAllocation');
}

// Test --heap-prof-dir overwrites --diagnostic-dir
{
tmpdir.refresh();
const dir = path.join(tmpdir.path, 'diag');
const dir2 = path.join(tmpdir.path, 'prof');
const output = spawnSync(process.execPath, [
'--heap-prof',
'--heap-prof-interval',
kHeapProfInterval,
'--diagnostic-dir',
dir,
'--heap-prof-dir',
dir2,
fixtures.path('workload', 'allocation.js'),
], {
cwd: tmpdir.path,
env
});
if (output.status !== 0) {
console.log(output.stderr.toString());
}
assert.strictEqual(output.status, 0);
assert(fs.existsSync(dir2));
const profiles = getHeapProfiles(dir2);
assert.strictEqual(profiles.length, 1);
verifyFrames(output, profiles[0], 'runAllocation');
}