Skip to content

Commit

Permalink
refactor reports
Browse files Browse the repository at this point in the history
  • Loading branch information
cenfun committed Jul 27, 2024
1 parent 1298a04 commit d7c6dd9
Show file tree
Hide file tree
Showing 10 changed files with 567 additions and 491 deletions.
555 changes: 75 additions & 480 deletions lib/generate.js

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@ declare namespace MCR {
maxCols?: number;
skipPercent?: number;
metrics?: Array<"bytes" | "statements" | "branches" | "functions" | "lines">;
filter?: string | {
[pattern: string]: boolean;
} | ((row: any) => boolean);
}] |
['markdown-summary'] | ['markdown-summary', {
color: 'unicode' | 'html' | 'tex' | string;
Expand All @@ -144,6 +147,9 @@ declare namespace MCR {
maxCols?: number;
skipPercent?: number;
metrics?: Array<"bytes" | "statements" | "branches" | "functions" | "lines">;
filter?: string | {
[pattern: string]: boolean;
} | ((row: any) => boolean);
/**
* defaults to `coverage-details.md`
*/
Expand Down
43 changes: 43 additions & 0 deletions lib/reports/codacy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
const path = require('path');
const Util = require('../utils/util.js');

const codacyReport = async (reportData, reportOptions, options) => {
const codacyOptions = {
outputFile: 'codacy.json',
... reportOptions
};

const jsonPath = path.resolve(options.outputDir, codacyOptions.outputFile);

// https://api.codacy.com/swagger#tocscoveragereport
const fileReports = [];
reportData.files.forEach((item) => {
const { sourcePath, data } = item;

const coverage = {};
for (const [key, value] of Object.entries(data.lines)) {
if (typeof value === 'number') {
coverage[key] = value;
} else {
// partial coverage not supported?
coverage[key] = 0;
}
}

fileReports.push({
filename: sourcePath,
coverage
});
});
const codacyData = {
fileReports
};

await Util.writeFile(jsonPath, JSON.stringify(codacyData));
return Util.relativePath(jsonPath);
};


module.exports = {
codacyReport
};
28 changes: 28 additions & 0 deletions lib/reports/codecov.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const path = require('path');
const Util = require('../utils/util.js');

const codecovReport = async (reportData, reportOptions, options) => {
const codecovOptions = {
outputFile: 'codecov.json',
... reportOptions
};

const jsonPath = path.resolve(options.outputDir, codecovOptions.outputFile);

// https://docs.codecov.com/docs/codecov-custom-coverage-format
const coverage = {};
reportData.files.forEach((item) => {
const { sourcePath, data } = item;
coverage[sourcePath] = data.lines;
});
const codecovData = {
coverage
};

await Util.writeFile(jsonPath, JSON.stringify(codecovData));
return Util.relativePath(jsonPath);
};

module.exports = {
codecovReport
};
125 changes: 125 additions & 0 deletions lib/reports/console-details.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
const CG = require('console-grid');
const EC = require('eight-colors');
const { getGroupedRows } = require('../utils/snapshot.js');
const Util = require('../utils/util.js');

const getRowData = (rowName, summary, metrics, color) => {
const summaryRow = {};
let lowest = {
pct: 100,
status: 'high'
};
metrics.map((k) => {
const s = summary[k];
if (!s) {
return;
}
const percent = s.pct;
if (typeof percent !== 'number') {
return;
}
summaryRow[k] = Util.getColorStrByStatus(Util.PSF(percent, 100, 2), s.status, color);
if (percent < lowest.pct) {
lowest = s;
}
});
summaryRow.nameStatus = lowest.status;
summaryRow.name = rowName;
return summaryRow;
};

const getDetailsRows = (files, metrics, cdOptions, color) => {

const skipPercent = cdOptions.skipPercent;
if (typeof skipPercent === 'number' && skipPercent > 0) {
files = files.filter((file) => {
const { summary } = file;
for (const k of metrics) {
const percent = summary[k].pct;
if (typeof percent === 'number' && percent < skipPercent) {
return true;
}
}
return false;
});
}

const flatRows = [];
files.forEach((file) => {

// do NOT add debug file
if (file.debug) {
return;
}

const { sourcePath, summary } = file;
const fileRow = getRowData(sourcePath, summary, metrics, color);
fileRow.uncoveredLines = Util.getUncoveredLines(file.data.lines, color);
flatRows.push(fileRow);
});

return getGroupedRows(flatRows);
};


const consoleDetailsReport = (reportData, reportOptions, options) => {
const cdOptions = {
maxCols: 50,
skipPercent: 0,
metrics: [],
... reportOptions
};

const {
type, name, summary, files
} = reportData;

if (name) {
Util.logInfo(EC.cyan(name));
}

const color = 'ansicode';

const metrics = Util.getMetrics(cdOptions.metrics, type);
const rows = getDetailsRows(files, metrics, cdOptions, color);
const summaryRow = getRowData('Summary', summary, metrics, color);
if (summaryRow.nameStatus) {
summaryRow.name = Util.getColorStrByStatus(summaryRow.name, summaryRow.nameStatus, color);
}
// no rows if skipped all by skipPercent
if (rows.length) {
rows.push({
innerBorder: true
});
}
rows.push(summaryRow);

const columns = [{
id: 'name',
name: 'Name'
}, ... metrics.map((m) => {
return {
id: m,
name: Util.capitalizeFirstLetter(m),
align: 'right'
};
}), {
id: 'uncoveredLines',
name: 'Uncovered Lines'
}];

return CG({
options: {
silent: cdOptions.silent,
nullPlaceholder: '',
defaultMaxWidth: cdOptions.maxCols
},
columns,
rows
});
};

module.exports = {
getRowData,
consoleDetailsReport
};
83 changes: 83 additions & 0 deletions lib/reports/console-summary.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
const CG = require('console-grid');
const EC = require('eight-colors');
const Util = require('../utils/util.js');

const nFormatter = (v) => {
if (typeof v === 'number') {
return Util.NF(v);
}
return v;
};


const getSummaryColumns = (color) => {

const columns = [{
id: 'name',
name: 'Name'
}, {
id: 'pct',
name: 'Coverage %',
align: 'right',
formatter: (v, row, column) => {
if (typeof v === 'number') {
return Util.getColorStrByStatus(Util.PSF(v, 100, 2), row.status, color);
}
return v;
}
}, {
id: 'covered',
name: 'Covered',
align: 'right',
formatter: nFormatter
}, {
id: 'uncovered',
name: 'Uncovered',
align: 'right',
formatter: nFormatter
}, {
id: 'total',
name: 'Total',
align: 'right',
formatter: nFormatter
}];

return columns;
};

const consoleSummaryReport = (reportData, reportOptions, options) => {

const csOptions = {
metrics: [],
... reportOptions
};

const {
summary, name, type
} = reportData;

if (name) {
Util.logInfo(EC.cyan(name));
}

const metrics = Util.getMetrics(csOptions.metrics, type);

const rows = metrics.map((k) => {
return {
... summary[k],
name: Util.capitalizeFirstLetter(k)
};
});

const columns = getSummaryColumns('ansicode');

CG({
columns,
rows
});
};

module.exports = {
getSummaryColumns,
consoleSummaryReport
};
Loading

0 comments on commit d7c6dd9

Please sign in to comment.