Skip to content

Commit

Permalink
fixed source filter for untested files
Browse files Browse the repository at this point in the history
  • Loading branch information
cenfun committed Sep 16, 2024
1 parent abaa793 commit 06c3fad
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 56 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
## Changelog

- 2.10.7
- fixed source filter for untested files

- 2.10.6
- fixed untested file coverage

Expand Down
43 changes: 1 addition & 42 deletions lib/converter/converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ const {
const { getSourceType, initSourceMapSourcesPath } = require('../utils/source-path.js');

const { decode } = require('@jridgewell/sourcemap-codec');
const { minimatch } = require('../packages/monocart-coverage-vendor.js');

const InfoBranch = require('./info-branch.js');
const InfoFunction = require('./info-function.js');
Expand Down Expand Up @@ -956,50 +955,10 @@ const getOriginalDecodedMappings = (originalDecodedMap, sourceIndex, locator) =>

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

const getSourceFilter = (options) => {
let input = options.sourceFilter || options.filter;

// 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 --sourceFilter {'**/node_modules/**':false,'**/src/*.js':true}
// mcr npx mocha --sourceFilter "{'**/node_modules/**': false, '**/src/*.js': true}"
const obj = Util.strToObj(input);
if (obj) {
input = obj;
} else {
return (sourcePath) => {
return minimatch(sourcePath, input);
};
}
}

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

// default
return () => true;
};

const initOriginalList = (state, originalDecodedMap, options) => {

// source filter
const sourceFilter = getSourceFilter(options);
const sourceFilter = Util.getSourceFilter(options);

// create original content mappings
const originalStateMap = new Map();
Expand Down
29 changes: 20 additions & 9 deletions lib/converter/untested.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,22 +204,33 @@ const getUntestedList = (testedMap, options, coverageType = 'v8') => {
const {
dirList, fileFilter, fileTransformer
} = allOptions;

const sourceFilter = Util.getSourceFilter(options);

const fileList = [];
dirList.forEach((dir) => {
Util.forEachFile(dir, [], (fileName, fileDir) => {
const filePath = path.resolve(fileDir, fileName);
// return file extname for file type
const fileType = fileFilter(filePath);
if (fileType) {
const sourcePath = Util.relativePath(filePath);
if (!testedMap.has(sourcePath)) {
fileList.push({
filePath,
fileType,
sourcePath
});
}
if (!fileType) {
return;
}

const sourcePath = Util.relativePath(filePath);
if (testedMap.has(sourcePath)) {
return;
}

if (!sourceFilter(sourcePath)) {
return;
}

fileList.push({
filePath,
fileType,
sourcePath
});
});
});
// console.log('fileList', fileList);
Expand Down
4 changes: 2 additions & 2 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -361,8 +361,8 @@ declare namespace MCR {
dir: string | string[];

/**
* the file filter, return the file type, if true, defaults to css if ".css" otherwise is js
*
* the file filter is triggered before `sourceFilter`, no need to use it in normal case
* the filter could return the file type, if true, defaults to css if ".css" otherwise is js
* {string} `minimatch` pattern for file path; {object} multiple patterns; {function} A filter function for file path;
*/
filter?: string | {
Expand Down
57 changes: 56 additions & 1 deletion lib/utils/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ const request = require('./request.js');
const version = require('../../package.json').version;
const markdownGrid = require('./markdown.js');

const { findUpSync, supportsColor } = require('../packages/monocart-coverage-vendor.js');
const {
findUpSync, supportsColor, minimatch
} = require('../packages/monocart-coverage-vendor.js');

// https://github.com/chalk/supports-color
// disabled color if Terminal stdout does not support color
Expand Down Expand Up @@ -161,6 +163,59 @@ const Util = {
};
},

// eslint-disable-next-line complexity
getSourceFilter: (options) => {

if (options.sourceFilterHandler) {
return options.sourceFilterHandler;
}

let input = options.sourceFilter || options.filter;

// for function handler
if (typeof input === 'function') {
options.sourceFilterHandler = input;
return input;
}

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

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

// default
const handler = () => true;
options.sourceFilterHandler = handler;
return handler;
},

saveSourceCacheFile: async (sourceData, options, fileCache) => {
const { cacheName, cachePath } = Util.getCacheFileInfo('source', sourceData.id, options.cacheDir);

Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions test/mcr.config.cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ module.exports = {

sourceFilter: {
// '**/ignore/**': false,
'**/mock/node/**': true,
'**/src/**': true
},

Expand Down

0 comments on commit 06c3fad

Please sign in to comment.