From 9a9409ff1f45c968173118de4cd37dea784f8ec9 Mon Sep 17 00:00:00 2001 From: Aviv Keller Date: Sun, 6 Oct 2024 16:27:32 -0400 Subject: [PATCH] test_runner: throw on invalid source map PR-URL: https://github.com/nodejs/node/pull/55055 Reviewed-By: Matteo Collina Reviewed-By: Colin Ihrig --- doc/api/errors.md | 6 ++++++ lib/internal/errors.js | 1 + lib/internal/test_runner/coverage.js | 2 ++ .../test-runner/source-maps/invalid-json/index.js | 2 ++ .../source-maps/invalid-json/index.js.map | 1 + .../test-runner/source-maps/missing-map.js | 1 + test/parallel/test-runner-coverage-source-map.js | 14 ++++++++++++++ 7 files changed, 27 insertions(+) create mode 100644 test/fixtures/test-runner/source-maps/invalid-json/index.js create mode 100644 test/fixtures/test-runner/source-maps/invalid-json/index.js.map create mode 100644 test/fixtures/test-runner/source-maps/missing-map.js diff --git a/doc/api/errors.md b/doc/api/errors.md index 290514b8229..9f82d9706c1 100644 --- a/doc/api/errors.md +++ b/doc/api/errors.md @@ -2568,6 +2568,12 @@ disconnected socket. A call was made and the UDP subsystem was not running. + + +### `ERR_SOURCE_MAP_CORRUPT` + +The source map could not be parsed because it does not exist, or is corrupt. + ### `ERR_SOURCE_MAP_MISSING_SOURCE` diff --git a/lib/internal/errors.js b/lib/internal/errors.js index c7cb7715dbc..e1228176a2d 100644 --- a/lib/internal/errors.js +++ b/lib/internal/errors.js @@ -1707,6 +1707,7 @@ E('ERR_SOCKET_CONNECTION_TIMEOUT', E('ERR_SOCKET_DGRAM_IS_CONNECTED', 'Already connected', Error); E('ERR_SOCKET_DGRAM_NOT_CONNECTED', 'Not connected', Error); E('ERR_SOCKET_DGRAM_NOT_RUNNING', 'Not running', Error); +E('ERR_SOURCE_MAP_CORRUPT', `The source map for '%s' does not exist or is corrupt.`, Error); E('ERR_SOURCE_MAP_MISSING_SOURCE', `Cannot find '%s' imported from the source map for '%s'`, Error); E('ERR_SRI_PARSE', 'Subresource Integrity string %j had an unexpected %j at position %d', diff --git a/lib/internal/test_runner/coverage.js b/lib/internal/test_runner/coverage.js index e7997f4f646..b6f3dd5aa9e 100644 --- a/lib/internal/test_runner/coverage.js +++ b/lib/internal/test_runner/coverage.js @@ -32,6 +32,7 @@ const { fileURLToPath } = require('internal/url'); const { kMappings, SourceMap } = require('internal/source_map/source_map'); const { codes: { + ERR_SOURCE_MAP_CORRUPT, ERR_SOURCE_MAP_MISSING_SOURCE, }, } = require('internal/errors'); @@ -353,6 +354,7 @@ class TestCoverage { continue; } const { data, lineLengths } = sourceMapCache[url]; + if (!data) throw new ERR_SOURCE_MAP_CORRUPT(url); let offset = 0; const executedLines = ArrayPrototypeMap(lineLengths, (length, i) => { const coverageLine = new CoverageLine(i + 1, offset, null, length + 1); diff --git a/test/fixtures/test-runner/source-maps/invalid-json/index.js b/test/fixtures/test-runner/source-maps/invalid-json/index.js new file mode 100644 index 00000000000..f8a711af8c0 --- /dev/null +++ b/test/fixtures/test-runner/source-maps/invalid-json/index.js @@ -0,0 +1,2 @@ +export {}; +//# sourceMappingURL=index.js.map \ No newline at end of file diff --git a/test/fixtures/test-runner/source-maps/invalid-json/index.js.map b/test/fixtures/test-runner/source-maps/invalid-json/index.js.map new file mode 100644 index 00000000000..e466dcbd8e8 --- /dev/null +++ b/test/fixtures/test-runner/source-maps/invalid-json/index.js.map @@ -0,0 +1 @@ +invalid \ No newline at end of file diff --git a/test/fixtures/test-runner/source-maps/missing-map.js b/test/fixtures/test-runner/source-maps/missing-map.js new file mode 100644 index 00000000000..af68b844e0a --- /dev/null +++ b/test/fixtures/test-runner/source-maps/missing-map.js @@ -0,0 +1 @@ +//# sourceMappingURL=missing.js.map \ No newline at end of file diff --git a/test/parallel/test-runner-coverage-source-map.js b/test/parallel/test-runner-coverage-source-map.js index 98752e757c5..0ded33713c7 100644 --- a/test/parallel/test-runner-coverage-source-map.js +++ b/test/parallel/test-runner-coverage-source-map.js @@ -80,4 +80,18 @@ describe('Coverage with source maps', async () => { t.assert.ok(spawned.stdout.includes(error)); t.assert.strictEqual(spawned.code, 1); }); + + for (const [file, message] of [ + [fixtures.path('test-runner', 'source-maps', 'invalid-json', 'index.js'), 'is not valid JSON'], + [fixtures.path('test-runner', 'source-maps', 'missing-map.js'), 'does not exist'], + ]) { + await it(`should throw when a source map ${message}`, async (t) => { + const spawned = await common.spawnPromisified(process.execPath, [...flags, file]); + + const error = `The source map for '${pathToFileURL(file)}' does not exist or is corrupt`; + t.assert.strictEqual(spawned.stderr, ''); + t.assert.ok(spawned.stdout.includes(error)); + t.assert.strictEqual(spawned.code, 1); + }); + } }).then(common.mustCall());