diff --git a/CHANGELOG.md b/CHANGELOG.md index 50dd0b6..1bf9f6c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ ## Changelog +- 2.10.7 + - fixed source filter for untested files + - 2.10.6 - fixed untested file coverage diff --git a/lib/converter/converter.js b/lib/converter/converter.js index 92c9ade..a772748 100644 --- a/lib/converter/converter.js +++ b/lib/converter/converter.js @@ -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'); @@ -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(); diff --git a/lib/converter/untested.js b/lib/converter/untested.js index f1e746d..7f62137 100644 --- a/lib/converter/untested.js +++ b/lib/converter/untested.js @@ -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); diff --git a/lib/index.d.ts b/lib/index.d.ts index 247b760..175c382 100644 --- a/lib/index.d.ts +++ b/lib/index.d.ts @@ -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 | { diff --git a/lib/utils/util.js b/lib/utils/util.js index ad06ec8..02b760b 100644 --- a/lib/utils/util.js +++ b/lib/utils/util.js @@ -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 @@ -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); diff --git a/package-lock.json b/package-lock.json index d7e84b4..a30936b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "monocart-coverage-reports", - "version": "2.10.5", + "version": "2.10.6", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "monocart-coverage-reports", - "version": "2.10.5", + "version": "2.10.6", "license": "MIT", "workspaces": [ "test" diff --git a/test/mcr.config.cli.js b/test/mcr.config.cli.js index 8c21903..2923f13 100644 --- a/test/mcr.config.cli.js +++ b/test/mcr.config.cli.js @@ -45,6 +45,7 @@ module.exports = { sourceFilter: { // '**/ignore/**': false, + '**/mock/node/**': true, '**/src/**': true },