Skip to content

Commit

Permalink
Merge pull request #75 from cenfun/tstyche
Browse files Browse the repository at this point in the history
fix issue for Tstyche
  • Loading branch information
cenfun authored Sep 16, 2024
2 parents 89456b7 + 2d199da commit 5e2a1a9
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 44 deletions.
163 changes: 122 additions & 41 deletions lib/converter/untested.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ 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 } = require('../packages/monocart-coverage-vendor.js');
const { minimatch, convertSourceMap } = require('../packages/monocart-coverage-vendor.js');

// ========================================================================================================

Expand Down Expand Up @@ -109,16 +111,64 @@ const resolveFileType = (fileType, filePath) => {
return 'js';
};

const getUntestedCoverageData = (emptyList, options, coverageType) => {
const saveUntestedFileSource = async (entryFile, options) => {
const {
id,
url,
source,
sourceMap
} = entryFile;
// console.log('-', entry.sourcePath);

const { cachePath } = Util.getCacheFileInfo('source', id, options.cacheDir);
if (fs.existsSync(cachePath)) {
return;
}

// save source and sourceMap to separated json file
const sourceData = {
id,
url,
source,
sourceMap
};

// remove comments if not debug
if (!Util.isDebug()) {
sourceData.source = convertSourceMap.removeComments(source);
}

// console.log('save untested file', id, url);

await Util.saveSourceCacheFile(sourceData, options);

};

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

// save all empty coverage
const dataId = Util.uid();
const results = {
id: dataId
};

// NOTE: do not save untested file to raw report
if (coverageType === 'istanbul') {
results.type = 'istanbul';
results.data = {};
} else {
results.type = 'v8';
results.data = [];
}

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(entry, options);

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

if (coverageType === 'istanbul') {

Expand All @@ -133,66 +183,107 @@ const getUntestedCoverageData = (emptyList, options, coverageType) => {
b: {}
};
// object
results.data[item.path] = item;
emptyCoverageList.push(item);

} else {

// ===============================================
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 = {
... 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);

}

}

// for raw report: coverage file
const { cachePath } = Util.getCacheFileInfo('coverage', dataId, options.cacheDir);
await Util.writeFile(cachePath, JSON.stringify(results));

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);

emptyList.push(entryFile);
entryList.push(entry);
}


// 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 @@ -201,44 +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;
if (fileType) {
fileList.push({
filePath,
fileType,
fileDir,
fileName
});
}

const sourcePath = Util.relativePath(filePath);
if (testedMap.has(sourcePath)) {
return;
}

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
2 changes: 2 additions & 0 deletions lib/utils/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ const Util = {

// eslint-disable-next-line complexity
getEntryFilter: (options) => {
// for entry.url

if (options.entryFilterHandler) {
return options.entryFilterHandler;
Expand Down Expand Up @@ -219,6 +220,7 @@ const Util = {

// eslint-disable-next-line complexity
getSourceFilter: (options) => {
// for sourcePath

if (options.sourceFilterHandler) {
return options.sourceFilterHandler;
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/test-merge.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const coverageOptions = {
assetsPath: '../assets',
// lcov: true,

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

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

0 comments on commit 5e2a1a9

Please sign in to comment.