From 57b702325782aa576fb83c9e3789f88b06fa817e Mon Sep 17 00:00:00 2001 From: Moshe Atlow Date: Thu, 20 Oct 2022 21:21:20 +0300 Subject: [PATCH] src: fix test runner coverage PR-URL: https://github.com/nodejs/node/pull/45055 Fixes: https://github.com/nodejs/node/issues/45013 Reviewed-By: Colin Ihrig Reviewed-By: James M Snell Reviewed-By: Benjamin Gruenbaum --- src/inspector_agent.cc | 9 ++++---- test/parallel/test-runner-inspect.mjs | 30 +++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/src/inspector_agent.cc b/src/inspector_agent.cc index 9ee779fb597b42..5773d3f3027d51 100644 --- a/src/inspector_agent.cc +++ b/src/inspector_agent.cc @@ -676,9 +676,6 @@ bool Agent::Start(const std::string& path, const DebugOptions& options, std::shared_ptr> host_port, bool is_main) { - if (!options.allow_attaching_debugger) { - return false; - } path_ = path; debug_options_ = options; CHECK_NOT_NULL(host_port); @@ -722,7 +719,8 @@ bool Agent::Start(const std::string& path, if (parent_handle_) { wait_for_connect = parent_handle_->WaitForConnect(); parent_handle_->WorkerStarted(client_->getThreadHandle(), wait_for_connect); - } else if (!options.inspector_enabled || !StartIoThread()) { + } else if (!options.inspector_enabled || !options.allow_attaching_debugger || + !StartIoThread()) { return false; } @@ -917,6 +915,9 @@ void Agent::RequestIoThreadStart() { // We need to attempt to interrupt V8 flow (in case Node is running // continuous JS code) and to wake up libuv thread (in case Node is waiting // for IO events) + if (!options().allow_attaching_debugger) { + return; + } CHECK(start_io_thread_async_initialized); uv_async_send(&start_io_thread_async); parent_env_->RequestInterrupt([this](Environment*) { diff --git a/test/parallel/test-runner-inspect.mjs b/test/parallel/test-runner-inspect.mjs index 67095291e2acd3..92117641870c16 100644 --- a/test/parallel/test-runner-inspect.mjs +++ b/test/parallel/test-runner-inspect.mjs @@ -2,6 +2,8 @@ import * as common from '../common/index.mjs'; import * as tmpdir from '../common/tmpdir.js'; import * as fixtures from '../common/fixtures.mjs'; import assert from 'node:assert'; +import path from 'node:path'; +import fs from 'node:fs/promises'; import { NodeInstance } from '../common/inspector-helper.js'; @@ -52,3 +54,31 @@ tmpdir.refresh(); assert.strictEqual(code, 1); assert.strictEqual(signal, null); } + + +// Outputs coverage when event loop is drained, with no async logic. +{ + const coverageDirectory = path.join(tmpdir.path, 'coverage'); + async function getCoveredFiles() { + const coverageFiles = await fs.readdir(coverageDirectory); + const files = new Set(); + for (const coverageFile of coverageFiles) { + const coverage = JSON.parse(await fs.readFile(path.join(coverageDirectory, coverageFile))); + for (const { url } of coverage.result) { + if (!url.startsWith('node:')) files.add(url); + } + } + return files; + } + + const { stderr, code, signal } = await common + .spawnPromisified(process.execPath, + ['--test', fixtures.path('v8-coverage/basic.js')], + { env: { ...process.env, NODE_V8_COVERAGE: coverageDirectory } }); + + assert.strictEqual(stderr, ''); + assert.strictEqual(code, 0); + assert.strictEqual(signal, null); + const files = await getCoveredFiles(coverageDirectory); + assert.ok(files.has(fixtures.fileURL('v8-coverage/basic.js').href)); +}