process: handle process.env.NODE_V8_COVERAGE in pre-execution
Since this depends on environment variable, and the worker threads do not need to persist the variable value because they cannot switch cwd. PR-URL: https://github.com/nodejs/node/pull/26466 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
This commit is contained in:
parent
d7543a79d6
commit
f617a732f8
@ -277,29 +277,6 @@ Object.defineProperty(process, 'features', {
|
|||||||
hasUncaughtExceptionCaptureCallback;
|
hasUncaughtExceptionCaptureCallback;
|
||||||
}
|
}
|
||||||
|
|
||||||
// User-facing NODE_V8_COVERAGE environment variable that writes
|
|
||||||
// ScriptCoverage to a specified file.
|
|
||||||
if (process.env.NODE_V8_COVERAGE) {
|
|
||||||
const originalReallyExit = process.reallyExit;
|
|
||||||
const cwd = NativeModule.require('internal/process/execution').tryGetCwd();
|
|
||||||
const { resolve } = NativeModule.require('path');
|
|
||||||
// Resolve the coverage directory to an absolute path, and
|
|
||||||
// overwrite process.env so that the original path gets passed
|
|
||||||
// to child processes even when they switch cwd.
|
|
||||||
const coverageDirectory = resolve(cwd, process.env.NODE_V8_COVERAGE);
|
|
||||||
process.env.NODE_V8_COVERAGE = coverageDirectory;
|
|
||||||
const {
|
|
||||||
writeCoverage,
|
|
||||||
setCoverageDirectory
|
|
||||||
} = NativeModule.require('internal/coverage-gen/with_profiler');
|
|
||||||
setCoverageDirectory(coverageDirectory);
|
|
||||||
process.on('exit', writeCoverage);
|
|
||||||
process.reallyExit = (code) => {
|
|
||||||
writeCoverage();
|
|
||||||
originalReallyExit(code);
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
function setupProcessObject() {
|
function setupProcessObject() {
|
||||||
const EventEmitter = NativeModule.require('events');
|
const EventEmitter = NativeModule.require('events');
|
||||||
const origProcProto = Object.getPrototypeOf(process);
|
const origProcProto = Object.getPrototypeOf(process);
|
||||||
|
@ -10,6 +10,14 @@ function prepareMainThreadExecution() {
|
|||||||
|
|
||||||
setupWarningHandler();
|
setupWarningHandler();
|
||||||
|
|
||||||
|
// Resolve the coverage directory to an absolute path, and
|
||||||
|
// overwrite process.env so that the original path gets passed
|
||||||
|
// to child processes even when they switch cwd.
|
||||||
|
if (process.env.NODE_V8_COVERAGE) {
|
||||||
|
process.env.NODE_V8_COVERAGE =
|
||||||
|
setupCoverageHooks(process.env.NODE_V8_COVERAGE);
|
||||||
|
}
|
||||||
|
|
||||||
// Only main thread receives signals.
|
// Only main thread receives signals.
|
||||||
setupSignalHandlers();
|
setupSignalHandlers();
|
||||||
|
|
||||||
@ -47,6 +55,26 @@ function setupWarningHandler() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Setup User-facing NODE_V8_COVERAGE environment variable that writes
|
||||||
|
// ScriptCoverage to a specified file.
|
||||||
|
function setupCoverageHooks(dir) {
|
||||||
|
const originalReallyExit = process.reallyExit;
|
||||||
|
const cwd = require('internal/process/execution').tryGetCwd();
|
||||||
|
const { resolve } = require('path');
|
||||||
|
const coverageDirectory = resolve(cwd, dir);
|
||||||
|
const {
|
||||||
|
writeCoverage,
|
||||||
|
setCoverageDirectory
|
||||||
|
} = require('internal/coverage-gen/with_profiler');
|
||||||
|
setCoverageDirectory(coverageDirectory);
|
||||||
|
process.on('exit', writeCoverage);
|
||||||
|
process.reallyExit = (code) => {
|
||||||
|
writeCoverage();
|
||||||
|
originalReallyExit(code);
|
||||||
|
};
|
||||||
|
return coverageDirectory;
|
||||||
|
}
|
||||||
|
|
||||||
function initializeReport() {
|
function initializeReport() {
|
||||||
if (!getOptionValue('--experimental-report')) {
|
if (!getOptionValue('--experimental-report')) {
|
||||||
return;
|
return;
|
||||||
@ -256,6 +284,7 @@ function loadPreloadModules() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
setupCoverageHooks,
|
||||||
setupWarningHandler,
|
setupWarningHandler,
|
||||||
prepareMainThreadExecution,
|
prepareMainThreadExecution,
|
||||||
initializeDeprecations,
|
initializeDeprecations,
|
||||||
|
@ -4,6 +4,7 @@
|
|||||||
// message port.
|
// message port.
|
||||||
|
|
||||||
const {
|
const {
|
||||||
|
setupCoverageHooks,
|
||||||
setupWarningHandler,
|
setupWarningHandler,
|
||||||
initializeDeprecations,
|
initializeDeprecations,
|
||||||
initializeESMLoader,
|
initializeESMLoader,
|
||||||
@ -42,6 +43,12 @@ const debug = require('util').debuglog('worker');
|
|||||||
|
|
||||||
setupWarningHandler();
|
setupWarningHandler();
|
||||||
|
|
||||||
|
// Since worker threads cannot switch cwd, we do not need to
|
||||||
|
// overwrite the process.env.NODE_V8_COVERAGE variable.
|
||||||
|
if (process.env.NODE_V8_COVERAGE) {
|
||||||
|
setupCoverageHooks(process.env.NODE_V8_COVERAGE);
|
||||||
|
}
|
||||||
|
|
||||||
debug(`[${threadId}] is setting up worker child environment`);
|
debug(`[${threadId}] is setting up worker child environment`);
|
||||||
|
|
||||||
// Set up the message port and start listening
|
// Set up the message port and start listening
|
||||||
|
Loading…
x
Reference in New Issue
Block a user