diff --git a/lib/converter/untested.js b/lib/converter/untested.js index 8ea4c02..64168e0 100644 --- a/lib/converter/untested.js +++ b/lib/converter/untested.js @@ -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'); @@ -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(); @@ -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') { @@ -189,21 +191,21 @@ 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; @@ -211,7 +213,7 @@ const getUntestedCoverageData = async (emptyList, options, coverageType) => { // array results.data.push(item); // will be parsed to AST and converted to V8 coverage - emptyCoverageList.push(entryFile); + emptyCoverageList.push(entry); } @@ -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') => { @@ -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); };