Skip to content

Commit

Permalink
support filter for details report
Browse files Browse the repository at this point in the history
  • Loading branch information
cenfun committed Jul 27, 2024
1 parent d7c6dd9 commit d8d7351
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 6 deletions.
4 changes: 2 additions & 2 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ declare namespace MCR {
metrics?: Array<"bytes" | "statements" | "branches" | "functions" | "lines">;
filter?: string | {
[pattern: string]: boolean;
} | ((row: any) => boolean);
} | ((file: CoverageFile) => boolean);
}] |
['markdown-summary'] | ['markdown-summary', {
color: 'unicode' | 'html' | 'tex' | string;
Expand All @@ -149,7 +149,7 @@ declare namespace MCR {
metrics?: Array<"bytes" | "statements" | "branches" | "functions" | "lines">;
filter?: string | {
[pattern: string]: boolean;
} | ((row: any) => boolean);
} | ((file: CoverageFile) => boolean);
/**
* defaults to `coverage-details.md`
*/
Expand Down
51 changes: 50 additions & 1 deletion lib/reports/console-details.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const CG = require('console-grid');
const EC = require('eight-colors');
const { minimatch } = require('minimatch');
const { getGroupedRows } = require('../utils/snapshot.js');
const Util = require('../utils/util.js');

Expand Down Expand Up @@ -61,24 +62,71 @@ const getDetailsRows = (files, metrics, cdOptions, color) => {
return getGroupedRows(flatRows);
};

const getFileFilter = (input) => {
// for function handler
if (typeof input === 'function') {
return input;
}

// for single minimatch pattern
if (input && typeof input === 'string') {
// string to multiple patterns "{...}"
// mcr npx mocha --entryFilter {'**/node_modules/**':false,'**/src/*.js':true}
// mcr npx mocha --entryFilter "{'**/node_modules/**': false, '**/src/*.js': true}"
const obj = Util.strToObj(input);
if (obj) {
input = obj;
} else {
return (file) => {
return minimatch(file.sourcePath, input);
};
}
}

// for patterns
if (input && typeof input === 'object') {
const patterns = Object.keys(input);
return (file) => {
const sourcePath = file.sourcePath;
for (const pattern of patterns) {
if (minimatch(sourcePath, pattern)) {
return input[pattern];
}
}
// false if not matched
};
}

};

const getFilteredFiles = (filter, files) => {
const fileFilter = getFileFilter(filter);
if (fileFilter) {
return files.filter(fileFilter);
}
return files;
};

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

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

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

const color = 'ansicode';
let files = reportData.files;
files = getFilteredFiles(cdOptions.filter, files);

const metrics = Util.getMetrics(cdOptions.metrics, type);
const rows = getDetailsRows(files, metrics, cdOptions, color);
Expand Down Expand Up @@ -121,5 +169,6 @@ const consoleDetailsReport = (reportData, reportOptions, options) => {

module.exports = {
getRowData,
getFilteredFiles,
consoleDetailsReport
};
5 changes: 4 additions & 1 deletion lib/reports/markdown-details.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const path = require('path');
const generateMarkdownGrid = require('../utils/markdown.js');
const Util = require('../utils/util.js');

const { getRowData } = require('./console-details.js');
const { getRowData, getFilteredFiles } = require('./console-details.js');

const cutWithMaxCols = (str, maxCols, before) => {
if (str && maxCols && str.length > maxCols) {
Expand All @@ -23,6 +23,7 @@ const markdownDetailsReport = async (reportData, reportOptions, options) => {
maxCols: 50,
skipPercent: 0,
metrics: [],
filter: null,
outputFile: 'coverage-details.md',
... reportOptions
};
Expand Down Expand Up @@ -50,6 +51,8 @@ const markdownDetailsReport = async (reportData, reportOptions, options) => {
});
}

files = getFilteredFiles(mdOptions.filter, files);

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

Expand Down
6 changes: 4 additions & 2 deletions test/test-pr.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,10 @@ const test = async () => {
const prChanges = await getPullRequestChanges();
console.log('prChanges', prChanges);

const filter = (row) => {
console.log(row);
const filter = (file) => {
// console.log(file.sourcePath);

return true;
};

const coverageOptions = {
Expand Down

0 comments on commit d8d7351

Please sign in to comment.