mirror of
https://github.com/mozilla/pdf.js.git
synced 2026-05-31 07:11:00 +02:00
Note that for the integration tests the coverage information ends up being processed in the Node.js context where `window` is not available, so we use `globalThis` instead for the function that merges individual test's coverage information into the global object because that is available in all contexts we support. For clarity we also rename said function since we're not exclusively dealing with `window` nor worker data anymore.
59 lines
1.9 KiB
JavaScript
59 lines
1.9 KiB
JavaScript
/* Copyright 2026 Mozilla Foundation
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*/
|
|
|
|
// Istanbul coverage objects use s (statements), b (branches), and f (functions)
|
|
// as shorthand keys for the hit-count maps.
|
|
function mergeCoverageIntoGlobal(coverage) {
|
|
if (!coverage || Object.keys(coverage).length === 0) {
|
|
return;
|
|
}
|
|
globalThis.__coverage__ ??= {};
|
|
for (const [key, fileCoverage] of Object.entries(coverage)) {
|
|
const existing = globalThis.__coverage__[key];
|
|
if (!existing) {
|
|
globalThis.__coverage__[key] = fileCoverage;
|
|
continue;
|
|
}
|
|
for (const id of Object.keys(fileCoverage.s)) {
|
|
existing.s[id] = (existing.s[id] ?? 0) + fileCoverage.s[id];
|
|
}
|
|
for (const id of Object.keys(fileCoverage.b)) {
|
|
existing.b[id] = fileCoverage.b[id].map(
|
|
(c, i) => (existing.b[id]?.[i] ?? 0) + c
|
|
);
|
|
}
|
|
for (const id of Object.keys(fileCoverage.f)) {
|
|
existing.f[id] = (existing.f[id] ?? 0) + fileCoverage.f[id];
|
|
}
|
|
}
|
|
}
|
|
|
|
async function fetchAndMergeWorkerCoverage(pdfWorker) {
|
|
if (!pdfWorker) {
|
|
return;
|
|
}
|
|
try {
|
|
const coverage = await pdfWorker.messageHandler.sendWithPromise(
|
|
"GetWorkerCoverage",
|
|
null
|
|
);
|
|
mergeCoverageIntoGlobal(coverage);
|
|
} catch (e) {
|
|
console.warn(`Failed to collect worker coverage: ${e}`);
|
|
}
|
|
}
|
|
|
|
export { fetchAndMergeWorkerCoverage, mergeCoverageIntoGlobal };
|