Skip to content

Commit

Permalink
support istanbul subdir
Browse files Browse the repository at this point in the history
  • Loading branch information
cenfun committed Dec 6, 2023
1 parent d155d5b commit 387fb01
Show file tree
Hide file tree
Showing 9 changed files with 117 additions and 20 deletions.
2 changes: 1 addition & 1 deletion lib/istanbul/istanbul-summary.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { istanbulLibReport } = require('../packages/monocart-coverage-vendor.js');
const istanbulLibReport = require('istanbul-lib-report');

const ReportBase = istanbulLibReport.ReportBase;
class IstanbulSummary extends ReportBase {
Expand Down
41 changes: 37 additions & 4 deletions lib/istanbul/istanbul.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ const fs = require('fs');
const path = require('path');

const istanbulReports = require('istanbul-reports');
const istanbulLibCoverage = require('istanbul-lib-coverage');
const istanbulLibReport = require('istanbul-lib-report');

const Util = require('../utils/util.js');
const IstanbulSummary = require('./istanbul-summary.js');

const { istanbulLibReport, istanbulLibCoverage } = require('../packages/monocart-coverage-vendor.js');

const { initIstanbulSourcePath } = require('../converter/source-path.js');

const getIstanbulReportList = (toIstanbul) => {
Expand Down Expand Up @@ -45,6 +45,38 @@ const getIstanbulReportList = (toIstanbul) => {
}];
};

const findHtmlPath = (outputDir) => {
const defaultHtml = path.resolve(outputDir, 'index.html');
if (fs.existsSync(defaultHtml)) {
return defaultHtml;
}

let htmlPath;
const fileList = [];
Util.forEachFile(outputDir, ['.html', '.json', '.info'], (name, dir) => {
if (name === 'index.html') {
htmlPath = path.resolve(dir, name);
return 'break';
}
fileList.push({
name,
dir
});
});

if (htmlPath) {
return htmlPath;
}

if (fileList.length) {
const f = fileList[0];
return path.resolve(f.dir, f.name);
}

return path.resolve(outputDir, 'not-found-index.html');

};

const saveIstanbulReport = (coverageData, fileSources, options) => {

const coverageMap = istanbulLibCoverage.createCoverageMap(coverageData);
Expand Down Expand Up @@ -120,13 +152,14 @@ const saveIstanbulReport = (coverageData, fileSources, options) => {
lcovReport.execute(context);
}

const htmlPath = Util.relativePath(path.resolve(options.outputDir, 'index.html'));
let htmlPath = findHtmlPath(options.outputDir);
htmlPath = Util.relativePath(htmlPath);

// add watermarks and color
const coverageReport = new IstanbulSummary();
coverageReport.execute(context);
const report = {
title: options.title,
name: options.name,
htmlPath,
watermarks: contextOptions.watermarks,
... coverageReport.getReport()
Expand Down
42 changes: 42 additions & 0 deletions lib/utils/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,48 @@ const Util = {
return '\n';
},

forEachFile: function(dir, extList, callback) {
if (!fs.existsSync(dir)) {
return;
}
// all exts, full name, ext
const isMatched = (name) => !Util.isList(extList) || extList.includes(name) || extList.includes(path.extname(name));
const subDirs = [];
const subDirHandler = () => {
if (!subDirs.length) {
return;
}

for (const subDir of subDirs) {
const res = Util.forEachFile(subDir, extList, callback);
if (res === 'break') {
return;
}
}
};

const list = fs.readdirSync(dir);
for (const name of list) {
const abs = path.resolve(dir, name);
const info = fs.statSync(abs);
if (info.isDirectory()) {
subDirs.push(abs);
continue;
}

if (info.isFile() && isMatched(name)) {
const res = callback(name, dir);
if (res === 'break') {
return;
}
}

}

subDirHandler();

},

readFileSync: function(filePath) {
if (fs.existsSync(filePath)) {
// Returns: <string> | <Buffer>
Expand Down
4 changes: 2 additions & 2 deletions lib/v8/v8.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,14 +240,14 @@ const saveV8Report = async (v8list, options) => {
status
};

const htmlReport = {
const htmlOptions = {
name: options.name,
watermarks,
summary,
files: v8list
};

const htmlPath = await saveV8HtmlReport(htmlReport, options);
const htmlPath = await saveV8HtmlReport(htmlOptions, options);

const report = {
type: 'v8',
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
"dependencies": {
"console-grid": "^2.0.1",
"eight-colors": "^1.0.3",
"istanbul-lib-coverage": "^3.2.2",
"istanbul-lib-report": "^3.0.1",
"istanbul-reports": "^3.1.6",
"lz-utils": "^2.0.1",
"monocart-code-viewer": "^1.0.8",
Expand All @@ -60,4 +62,4 @@
"webpack": "^5.89.0",
"webpack-cli": "^5.1.4"
}
}
}
4 changes: 1 addition & 3 deletions packages/vendor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@
"@bcoe/v8-coverage": "^0.2.3",
"@jridgewell/sourcemap-codec": "^1.4.15",
"axios": "^1.6.2",
"convert-source-map": "^2.0.0",
"istanbul-lib-coverage": "^3.2.2",
"istanbul-lib-report": "^3.0.1"
"convert-source-map": "^2.0.0"
},
"platform": "node"
}
6 changes: 0 additions & 6 deletions packages/vendor/src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import istanbulLibCoverage from 'istanbul-lib-coverage';
import istanbulLibReport from 'istanbul-lib-report';

import * as convertSourceMap from 'convert-source-map';
import axios from 'axios';

Expand All @@ -10,9 +7,6 @@ import { mergeScriptCovs } from '@bcoe/v8-coverage';

export {

istanbulLibCoverage,
istanbulLibReport,

convertSourceMap,
axios,

Expand Down
30 changes: 27 additions & 3 deletions tests/test-istanbul.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,29 @@
const { chromium } = require('playwright');
const EC = require('eight-colors');

const CoverageReport = require('../');

const coverageOptions = {
// logging: 'debug',
// watermarks: [60, 90],
lcov: true,
outputFile: 'docs/istanbul/index.html'
};

const subDirOptions = {
// logging: 'debug',
// watermarks: [60, 90],
toIstanbul: [{
name: 'html',
options: {
subdir: 'my-sub-dir'
}
}],
lcov: true,

outputFile: 'docs/istanbul-sub/index.html'
};

const test1 = async (serverUrl) => {

console.log('start istanbul test1 ...');
Expand Down Expand Up @@ -40,6 +56,9 @@ const test1 = async (serverUrl) => {

const coverageReport = new CoverageReport(coverageOptions);
const report = await coverageReport.add(coverageData);

await new CoverageReport(subDirOptions).add(coverageData);

console.log('istanbul coverage1 added', report.type);

await browser.close();
Expand Down Expand Up @@ -73,6 +92,9 @@ const test2 = async (serverUrl) => {

const coverageReport = new CoverageReport(coverageOptions);
const report = await coverageReport.add(coverageData);

await new CoverageReport(subDirOptions).add(coverageData);

console.log('istanbul coverage2 added', report.type);

await browser.close();
Expand All @@ -83,10 +105,12 @@ const generate = async () => {

console.log('generate istanbul coverage reports ...');

const coverageReport = new CoverageReport(coverageOptions);
const results = await coverageReport.generate();
const report = await new CoverageReport(coverageOptions).generate();

const reportSub = await new CoverageReport(subDirOptions).generate();
console.log('sub html path', EC.magenta(reportSub.htmlPath));

console.log('istanbul coverage generated', results.summary);
console.log('istanbul coverage generated', report.summary);
};


Expand Down
4 changes: 4 additions & 0 deletions tests/test-v8.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ const coverageOptions = {
// watermarks: [60, 90],
name: 'My V8 Coverage Report',
assetsPath: '../assets',
lcov: true,

outputFile: 'docs/v8/index.html'
};

Expand All @@ -16,6 +18,8 @@ const toIstanbulOptions = {
toIstanbul: true,
// toIstanbul: 'html',

lcov: true,

// https://github.com/istanbuljs/istanbuljs/tree/master/packages/istanbul-reports/lib
// toIstanbul: ['html', {
// name: 'json',
Expand Down

0 comments on commit 387fb01

Please sign in to comment.