Skip to content

Commit

Permalink
Merge pull request #82 from cenfun/merge-raw
Browse files Browse the repository at this point in the history
Merge raw
  • Loading branch information
cenfun authored Sep 29, 2024
2 parents 2fbc8b3 + 60cdb2d commit 3547a97
Show file tree
Hide file tree
Showing 15 changed files with 724 additions and 58 deletions.
3 changes: 1 addition & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
## Changelog

- 2.11.0
- removed the data dir for `addFromDir` and `dataDir`
- added `zip` option for `raw` report
- added `zip` and `merge` option for `raw` report

- 2.10.9
- fixed empty coverage issue
Expand Down
5 changes: 1 addition & 4 deletions lib/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -251,10 +251,7 @@ const addJsonData = async (mcr, dataList, sourceCache, input, filename) => {
if (mcr.fileCache.has(filename)) {
json = mcr.fileCache.get(filename);
} else {
const content = await Util.readFile(path.resolve(input, filename));
if (content) {
json = JSON.parse(content);
}
json = await Util.readJson(path.resolve(input, filename));
}
}
if (json) {
Expand Down
5 changes: 3 additions & 2 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ declare namespace MCR {
outputFile?: string;
}] |
['raw'] | ['raw', {
merge?: boolean;
zip?: boolean;
outputDir?: string;
}] |
[string] | [string, {
Expand Down Expand Up @@ -439,9 +441,8 @@ declare namespace MCR {
/**
* add V8 coverage from a dir
* @param dir node v8 coverage dir
* @param remove whether to remove dir after added
*/
addFromDir: (dir: string, remove?: boolean) => Promise<void>;
addFromDir: (dir: string) => Promise<void>;

/** generate report */
generate: () => Promise<CoverageResults | undefined>;
Expand Down
9 changes: 1 addition & 8 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,16 +122,9 @@ class CoverageReport {
}

// add coverage from dir
async addFromDir(dir, remove) {
async addFromDir(dir) {
const time_start = Date.now();
const results = await readFromDir(this, dir);
// remove dir after added
if (typeof remove !== 'boolean') {
remove = !Util.isDebug();
}
if (remove) {
Util.rmSync(dir);
}
Util.logTime(`added from dir: ${dir}`, time_start);
return results;
}
Expand Down
8 changes: 6 additions & 2 deletions lib/istanbul/istanbul.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,16 +144,19 @@ const addUntestedFiles = async (istanbulData, options) => {

};

const mergeIstanbulCoverage = async (dataList, options) => {
const mergeIstanbulDataList = (dataList) => {
const istanbulCoverageList = dataList.map((it) => it.data);
const coverageMap = istanbulLibCoverage.createCoverageMap();
istanbulCoverageList.forEach((coverage) => {
coverageMap.merge(coverage);
});
const istanbulData = coverageMap.toJSON();
return istanbulData;
};

const mergeIstanbulCoverage = async (dataList, options) => {
const istanbulData = mergeIstanbulDataList(dataList);
await addUntestedFiles(istanbulData, options);

return istanbulData;
};

Expand All @@ -169,6 +172,7 @@ const initIstanbulData = (istanbulData, options) => {

module.exports = {
saveIstanbulReports,
mergeIstanbulDataList,
mergeIstanbulCoverage,
initIstanbulData
};
101 changes: 78 additions & 23 deletions lib/reports/raw.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,48 @@ const fs = require('fs');
const path = require('path');
const Util = require('../utils/util.js');
const { ZipFile } = require('../packages/monocart-coverage-vendor.js');
const { mergeIstanbulDataList } = require('../istanbul/istanbul.js');
const { mergeV8DataList } = require('../v8/v8.js');

const rawReport = (reportData, reportOptions, options) => {
const rawOptions = {
outputDir: 'raw',
zip: false,
... reportOptions
};

const cacheDir = options.cacheDir;
const rawDir = path.resolve(options.outputDir, rawOptions.outputDir);
// console.log(rawDir, cacheDir);
if (fs.existsSync(rawDir)) {
Util.rmSync(rawDir);
const getMergedData = (type, dataList) => {
if (type === 'istanbul') {
return mergeIstanbulDataList(dataList);
}
return mergeV8DataList(dataList);
};

const rawParent = path.dirname(rawDir);
if (!fs.existsSync(rawParent)) {
fs.mkdirSync(rawParent, {
recursive: true
});
const mergeReport = async (rawDir, rawParent) => {
const coverageFiles = fs.readdirSync(rawDir).filter((n) => n.endsWith('.json') && n.startsWith('coverage-'));
// console.log(coverageFiles);

let type;
const dataList = [];
for (const coverageFile of coverageFiles) {
const jsonPath = path.resolve(rawDir, coverageFile);
const json = await Util.readJson(jsonPath);
if (json) {
// 'id', 'type', 'data'
type = json.type;
dataList.push(json);
}
Util.rmSync(jsonPath);
}

// just rename the cache folder name
fs.renameSync(cacheDir, rawDir);
const mergedData = await getMergedData(type, dataList);

// zip
if (!rawOptions.zip) {
return;
}
const dataId = Util.uid();
const results = {
id: dataId,
type,
data: mergedData
};

const { cachePath } = Util.getCacheFileInfo('coverage', `${dataId}.merged`, rawDir);
await Util.writeFile(cachePath, JSON.stringify(results));

};

const zipReport = (rawDir, rawParent) => {
const zipName = path.basename(rawDir);
const zipPath = path.resolve(rawParent, `${zipName}.zip`);

Expand All @@ -52,6 +64,49 @@ const rawReport = (reportData, reportOptions, options) => {
});
zipFile.end();
});
};

const rawReport = async (reportData, reportOptions, options) => {
const rawOptions = {
outputDir: 'raw',
merge: false,
zip: false,
... reportOptions
};

const cacheDir = options.cacheDir;
if (!fs.existsSync(cacheDir)) {
// there is no cache if only inputDir
Util.logInfo('There is no cache dir for "raw" report');
return;
}

const rawDir = path.resolve(options.outputDir, rawOptions.outputDir);
// console.log(rawDir, cacheDir);
if (fs.existsSync(rawDir)) {
Util.rmSync(rawDir);
}

const rawParent = path.dirname(rawDir);
if (!fs.existsSync(rawParent)) {
fs.mkdirSync(rawParent, {
recursive: true
});
}

// just rename the cache folder name
fs.renameSync(cacheDir, rawDir);

// merge
if (rawOptions.merge) {
await mergeReport(rawDir, rawParent);
}

// zip
if (rawOptions.zip) {
await zipReport(rawDir, rawParent);
}


};

Expand Down
Loading

0 comments on commit 3547a97

Please sign in to comment.