Skip to content

Commit

Permalink
try markdown report
Browse files Browse the repository at this point in the history
  • Loading branch information
cenfun committed Jul 1, 2024
1 parent 7c98441 commit 4958f43
Show file tree
Hide file tree
Showing 5 changed files with 206 additions and 30 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ jobs:
path: ./docs

- run: ls -al

- name: Generate list using Markdown
run: cat docs/v8/coverage-summary.md > $GITHUB_STEP_SUMMARY

- name: Analyze with SonarCloud
uses: sonarsource/sonarcloud-github-action@master
Expand Down
93 changes: 67 additions & 26 deletions lib/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const { resolveSourceMap } = require('./converter/collect-source-maps.js');

const { minimatch } = require('./packages/monocart-coverage-vendor.js');
const { getGroupedRows } = require('./utils/snapshot.js');
const generateMarkdownGrid = require('./utils/markdown.js');

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

Expand Down Expand Up @@ -93,6 +94,7 @@ const getReportGroup = (reports, lcov, dataType) => {
'codacy': 'both',
'console-details': 'both',
'console-summary': 'both',
'markdown-summary': 'both',
'raw': 'both'
};

Expand Down Expand Up @@ -129,9 +131,9 @@ const getReportGroup = (reports, lcov, dataType) => {

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

const pFormatter = (v, row, column) => {
const pFormatter = (v, row, column, markdown) => {
if (typeof v === 'number') {
return Util.getColorStrByStatus(Util.PSF(v, 100, 2), row.status);
return Util.getColorStrByStatus(Util.PSF(v, 100, 2), row.status, markdown);
}
return v;
};
Expand Down Expand Up @@ -372,30 +374,7 @@ const handleConsoleDetailsReport = (reportData, reportOptions, options) => {
});
};

const handleConsoleSummaryReport = (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 getSummaryColumns = () => {
const columns = [{
id: 'name',
name: 'Name'
Expand All @@ -421,12 +400,73 @@ const handleConsoleSummaryReport = (reportData, reportOptions, options) => {
formatter: nFormatter
}];

return columns;
};

const handleConsoleSummaryReport = (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();

CG({
columns,
rows
});
};

const handleMarkdownSummaryReport = async (reportData, reportOptions, options) => {
const markdownSummaryOptions = {
metrics: [],
outputFile: 'coverage-summary.md',
... reportOptions
};

const mdPath = path.resolve(options.outputDir, markdownSummaryOptions.outputFile);

const metrics = Util.getMetrics(markdownSummaryOptions.metrics, reportData.type);

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

const columns = getSummaryColumns();

const markdownGrid = generateMarkdownGrid({
options: {
name: reportData.name
},
columns,
rows
});

await Util.writeFile(mdPath, markdownGrid);
return Util.relativePath(mdPath);
};

const handleRawReport = (reportData, reportOptions, options) => {
const rawOptions = {
outputDir: 'raw',
Expand Down Expand Up @@ -504,6 +544,7 @@ const generateCoverageReports = async (dataList, sourceCache, options) => {
'codacy': handleCodacyReport,
'console-details': handleConsoleDetailsReport,
'console-summary': handleConsoleSummaryReport,
'markdown-summary': handleMarkdownSummaryReport,
'raw': handleRawReport
};

Expand Down
131 changes: 131 additions & 0 deletions lib/utils/markdown.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
const os = require('os');

const renderLine = (ls, padding) => {
const spacing = ''.padEnd(padding, ' ');
const line = ls.join(`${spacing}|${spacing}`);
return `|${spacing}${line}${spacing}|`;
};

const renderCell = (cellValue, column) => {

// To include a pipe | as content within your cell, use a \ before the pipe:
cellValue = cellValue.split(/\\?\|/g).join('\\|');
const valueWidth = cellValue.length;
const width = column.width;

if (width <= valueWidth) {
return cellValue;
}

const spacingWidth = width - valueWidth;
const spacing = ''.padEnd(spacingWidth, ' ');

const align = column.align;

if (align === 'right') {
return spacing + cellValue;
}

if (align === 'center') {
const l = Math.round(spacingWidth * 0.5);
const r = spacingWidth - l;
const spacingL = ''.padEnd(l, ' ');
const spacingR = ''.padEnd(r, ' ');
return spacingL + cellValue + spacingR;

}

return cellValue + spacing;
};

const renderHyphen = (column) => {
const width = column.width;
const align = column.align;
if (align === 'left') {
return ':'.padEnd(width, '-');
}
if (align === 'right') {
return ':'.padStart(width, '-');
}
if (align === 'center') {
const hyphen = ''.padEnd(width - 2, '-');
return `:${hyphen}:`;
}
return ''.padEnd(width, '-');
};

const generateMarkdownGrid = (data) => {

const options = {
name: '',
padding: 0,
... data.options
};

const padding = options.padding;

// console.log(data, options);

const lines = [];

if (options.name) {
lines.push(`## ${options.name}`);
}

// init columns
const columns = data.columns.map((item) => {
if (typeof item === 'string') {
item = {
name: item
};
}
const column = {
... item
};
column.name = `${column.name}`;
if (typeof column.width !== 'number') {
column.width = column.name.length;
}
column.width = Math.max(column.width, 3);
return column;
});

// header
const headers = [];
columns.forEach((column) => {
headers.push(renderCell(column.name, column));
});
lines.push(renderLine(headers, padding));

const hyphens = [];
columns.forEach((column) => {
hyphens.push(renderHyphen(column));
});
lines.push(renderLine(hyphens, padding));

// rows
data.rows.forEach((row) => {
const cells = [];
columns.forEach((column, i) => {
let cellValue = '';
if (Array.isArray(row)) {
cellValue += row[i];
} else {
let v = row[column.id];
if (column.formatter) {
const markdown = true;
v = column.formatter(v, row, column, markdown);
}
cellValue += v;
}
cells.push(renderCell(cellValue, column));
});
lines.push(renderLine(cells, padding));
});

return lines.join(os.EOL) + os.EOL;

};


module.exports = generateMarkdownGrid;
8 changes: 4 additions & 4 deletions lib/utils/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -501,15 +501,15 @@ const Util = {
return template;
},

getColorStrByStatus: (str, status) => {
getColorStrByStatus: (str, status, markdown) => {
if (status === 'low') {
return EC.red(str);
return markdown ? `🔴 ${str}` : EC.red(str);
}
if (status === 'medium') {
return EC.yellow(str);
return markdown ? `🟡 ${str}` : EC.yellow(str);
}
if (status === 'high') {
return EC.green(str);
return markdown ? `🟢 ${str}` : EC.green(str);
}
return str;
},
Expand Down
1 change: 1 addition & 0 deletions test/test-v8.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const coverageOptions = {
// metrics: ['bytes', 'functions', 'lines']
}],
'v8-json',
'markdown-summary',
[path.resolve('./test/custom-istanbul-reporter.js'), {
type: 'istanbul',
file: 'custom-istanbul-coverage.text'
Expand Down

0 comments on commit 4958f43

Please sign in to comment.