Skip to content

Commit

Permalink
fixed empty coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
cenfun committed Sep 17, 2024
1 parent 51b27ed commit f3c6148
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 35 deletions.
24 changes: 5 additions & 19 deletions lib/converter/untested.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,8 @@ const saveUntestedFileSource = async (entryFile, options) => {

const getUntestedCoverageData = async (entryList, options, coverageType) => {

// save all empty coverage
const dataId = Util.uid();
// save all empty coverage, 20 - 5(empty)
const dataId = Util.uid(15, 'empty');
const results = {
id: dataId
};
Expand All @@ -166,15 +166,14 @@ const getUntestedCoverageData = async (entryList, options, coverageType) => {
for (const entry of entryList) {

// for raw report: source file
// id, url, source, sourceMap
await saveUntestedFileSource(entry, options);

const { type, url } = entry;

if (coverageType === 'istanbul') {

// ===============================================
const item = {
path: fileURLToPath(url),
path: fileURLToPath(entry.url),
statementMap: {},
fnMap: {},
branchMap: {},
Expand All @@ -189,20 +188,7 @@ const getUntestedCoverageData = async (entryList, options, coverageType) => {
} else {

// ===============================================
if (type === 'js') {
// empty js
entry.functions = entry.functions || [{
functionName: '',
ranges: [{
startOffset: 0,
endOffset: entry.source.length,
count: 0
}]
}];
} else {
// empty css
entry.ranges = entry.ranges || [];
}
Util.setEmptyV8Coverage(entry);

const item = {
... entry
Expand Down
21 changes: 21 additions & 0 deletions lib/utils/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,27 @@ const Util = {
return handler;
},

setEmptyV8Coverage: (entry) => {
if (entry.type === 'css') {
// empty css
if (!entry.ranges) {
entry.ranges = [];
}
} else {
// empty js
if (!entry.functions) {
entry.functions = [{
functionName: '',
ranges: [{
startOffset: 0,
endOffset: entry.source ? entry.source.length : 0,
count: 0
}]
}];
}
}
},

saveSourceCacheFile: async (sourceData, options, fileCache) => {
const { cacheName, cachePath } = Util.getCacheFileInfo('source', sourceData.id, options.cacheDir);

Expand Down
33 changes: 19 additions & 14 deletions lib/v8/v8.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,11 @@ const initV8ListAndSourcemap = async (mcr, v8list) => {
distFile: entry.distFile
};

// coverage
if (data.type === 'js') {
data.functions = entry.functions;
} else {
// coverage info
if (data.type === 'css') {
data.ranges = entry.ranges;
} else {
data.functions = entry.functions;
}

// resolve source path
Expand Down Expand Up @@ -213,10 +213,10 @@ const mergeV8Coverage = async (dataList, sourceCache, options) => {
for (const id of mergeIds) {
const itemList = mergeMap[id];
const item = itemMap[id];
if (item.type === 'js') {
item.functions = await mergeJsFunctions(itemList);
} else {
if (item.type === 'css') {
item.ranges = await mergeCssRanges(itemList);
} else {
item.functions = await mergeJsFunctions(itemList);
}
}

Expand All @@ -236,16 +236,21 @@ const mergeV8Coverage = async (dataList, sourceCache, options) => {
const mergedList = emptyList.concat(Object.values(itemMap));

// try to load coverage and source by id
for (const item of mergedList) {
const { id } = item;
const json = sourceCache.get(id);
for (const entry of mergedList) {
const json = sourceCache.get(entry.id);
if (json) {
item.source = json.source;
item.sourceMap = json.sourceMap;
entry.source = json.source;
entry.sourceMap = json.sourceMap;
} else {
Util.logError(`Not found source data: ${Util.relativePath(item.sourcePath)}`);
item.source = '';
Util.logError(`Not found source data: ${Util.relativePath(entry.sourcePath)}`);
entry.source = '';
}

// add empty coverage after source init
if (entry.empty) {
Util.setEmptyV8Coverage(entry);
}

}

return mergedList;
Expand Down
2 changes: 0 additions & 2 deletions test/test-merge.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ const coverageOptions = {
assetsPath: '../assets',
// lcov: true,

// all: ['test/mock/src', 'test/mock/node/lib'],

sourcePath: (filePath) => {
const list = ['monocart-coverage-reports/', 'coverage-v8/'];
for (const str of list) {
Expand Down

0 comments on commit f3c6148

Please sign in to comment.