Skip to content

Commit

Permalink
fix sourcePath for untested files
Browse files Browse the repository at this point in the history
  • Loading branch information
cenfun committed Sep 16, 2024
1 parent a136c04 commit 2d199da
Showing 1 changed file with 61 additions and 44 deletions.
105 changes: 61 additions & 44 deletions lib/converter/untested.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ const Util = require('../utils/util.js');

const { pathToFileURL, fileURLToPath } = require('url');

const { normalizeSourcePath, getSourcePathReplacer } = require('../utils/source-path.js');

// const EC = require('eight-colors');
const { minimatch, convertSourceMap } = require('../packages/monocart-coverage-vendor.js');

Expand Down Expand Up @@ -142,7 +144,7 @@ const saveUntestedFileSource = async (entryFile, options) => {

};

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

// save all empty coverage
const dataId = Util.uid();
Expand All @@ -161,12 +163,12 @@ const getUntestedCoverageData = async (emptyList, options, coverageType) => {
const emptyCoverageList = [];

// save all empty source and sourcemap
for (const entryFile of emptyList) {
for (const entry of entryList) {

// for raw report: source file
await saveUntestedFileSource(entryFile, options);
await saveUntestedFileSource(entry, options);

const { type, url } = entryFile;
const { type, url } = entry;

if (coverageType === 'istanbul') {

Expand All @@ -189,29 +191,29 @@ const getUntestedCoverageData = async (emptyList, options, coverageType) => {
// ===============================================
if (type === 'js') {
// empty js
entryFile.functions = entryFile.functions || [{
entry.functions = entry.functions || [{
functionName: '',
ranges: [{
startOffset: 0,
endOffset: entryFile.source.length,
endOffset: entry.source.length,
count: 0
}]
}];
} else {
// empty css
entryFile.ranges = entryFile.ranges || [];
entry.ranges = entry.ranges || [];
}

const item = {
... entryFile
... entry
};
delete item.source;
delete item.sourceMap;

// array
results.data.push(item);
// will be parsed to AST and converted to V8 coverage
emptyCoverageList.push(entryFile);
emptyCoverageList.push(entry);

}

Expand All @@ -224,36 +226,64 @@ const getUntestedCoverageData = async (emptyList, options, coverageType) => {
return emptyCoverageList;
};

const getEmptyCoverages = async (fileList, options, coverageType, fileTransformer) => {
const getEmptyCoverages = async (testedMap, options, coverageType, fileList, fileTransformer) => {
const sourceFilter = Util.getSourceFilter(options);
const sourcePathReplacer = getSourcePathReplacer(options);
const baseDir = options.baseDir;

const emptyList = [];
// console.log(fileList);

const entryList = [];
for (const item of fileList) {
const {
filePath, fileType, sourcePath
} = item;

const { fileType, filePath } = item;
const type = resolveFileType(fileType, filePath);
const url = pathToFileURL(filePath).toString();
const source = Util.readFileSync(filePath);
const entryFile = {
const entry = {
empty: true,
type,
url,
sourcePath,
// for empty, css supports both source and text
source
};

await fileTransformer(entryFile, coverageType);
// normalize sourcePath here
let sourcePath = normalizeSourcePath(url, baseDir);
if (sourcePathReplacer) {
const newSourcePath = sourcePathReplacer(sourcePath, entry);
if (typeof newSourcePath === 'string' && newSourcePath) {
sourcePath = newSourcePath;
}
}

// console.log(sourcePath);

if (testedMap.has(sourcePath)) {
continue;
}

if (!sourceFilter(sourcePath)) {
continue;
}

entry.sourcePath = sourcePath;

await fileTransformer(entry, coverageType);
// after transformer
entryFile.id = Util.calculateSha1(entryFile.sourcePath + entryFile.source);
entry.id = Util.calculateSha1(entry.sourcePath + entry.source);

entryList.push(entry);
}


emptyList.push(entryFile);
// console.log('fileList', fileList);
if (!entryList.length) {
return;
}


// save empty coverage for merging raw reports
return getUntestedCoverageData(emptyList, options, coverageType);
return getUntestedCoverageData(entryList, options, coverageType);
};

const getUntestedList = (testedMap, options, coverageType = 'v8') => {
Expand All @@ -262,47 +292,34 @@ const getUntestedList = (testedMap, options, coverageType = 'v8') => {
if (!allOptions) {
return;
}

const {
dirList, fileFilter, fileTransformer
} = allOptions;

const sourceFilter = Util.getSourceFilter(options);

const fileList = [];
dirList.forEach((dir) => {
Util.forEachFile(dir, [], (fileName, fileDir) => {
const filePath = path.resolve(fileDir, fileName);
// return file extname for file type
const fileType = fileFilter(filePath);
if (!fileType) {
return;
}

const sourcePath = Util.relativePath(filePath);

// normalize sourcePath here

if (testedMap.has(sourcePath)) {
return;
if (fileType) {
fileList.push({
filePath,
fileType,
fileDir,
fileName
});
}

if (!sourceFilter(sourcePath)) {
return;
}

fileList.push({
filePath,
fileType,
sourcePath
});
});
});
// console.log('fileList', fileList);

if (!fileList.length) {
return;
}

return getEmptyCoverages(fileList, options, coverageType, fileTransformer);
return getEmptyCoverages(testedMap, options, coverageType, fileList, fileTransformer);

};

Expand Down

0 comments on commit 2d199da

Please sign in to comment.