Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge raw #82

Merged
merged 8 commits into from
Sep 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
outputFile?: string;
}] |
['raw'] | ['raw', {
merge?: boolean;
zip?: boolean;
outputDir?: string;
}] |
[string] | [string, {
Expand Down Expand Up @@ -439,9 +441,8 @@
/**
* 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>;

Check failure on line 445 in lib/index.d.ts

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

lib/index.d.ts#L445

'dir' is defined but never used.

/** 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 path = require('path');
const Util = require('../utils/util.js');
const { ZipFile } = require('../packages/monocart-coverage-vendor.js');
const { mergeIstanbulDataList } = require('../istanbul/istanbul.js');

Check failure on line 5 in lib/reports/raw.js

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

lib/reports/raw.js#L5

Require statement not part of import statement.
const { mergeV8DataList } = require('../v8/v8.js');

Check failure on line 6 in lib/reports/raw.js

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

lib/reports/raw.js#L6

Require statement not part of import statement.

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) => {

Check failure on line 15 in lib/reports/raw.js

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

lib/reports/raw.js#L15

'rawParent' is defined but never used.

Check failure on line 15 in lib/reports/raw.js

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

lib/reports/raw.js#L15

'rawParent' is defined but never used.
const coverageFiles = fs.readdirSync(rawDir).filter((n) => n.endsWith('.json') && n.startsWith('coverage-'));

Check warning on line 16 in lib/reports/raw.js

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

lib/reports/raw.js#L16

Found readdirSync from package "fs" with non literal argument at index 0

Check warning on line 16 in lib/reports/raw.js

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

lib/reports/raw.js#L16

The application dynamically constructs file or path information.
// console.log(coverageFiles);

let type;
const dataList = [];
for (const coverageFile of coverageFiles) {
const jsonPath = path.resolve(rawDir, coverageFile);

Check warning on line 22 in lib/reports/raw.js

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

lib/reports/raw.js#L22

Detected possible user input going into a `path.join` or `path.resolve` function.
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 @@
});
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)) {

Check warning on line 78 in lib/reports/raw.js

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

lib/reports/raw.js#L78

Found existsSync from package "fs" with non literal argument at index 0

Check warning on line 78 in lib/reports/raw.js

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

lib/reports/raw.js#L78

The application dynamically constructs file or path information.
// 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