Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Add ability to specify patterns to be excluded from coverage report #6

Merged
merged 4 commits into from Feb 26, 2016
@@ -1,4 +1,4 @@
-function obj=MOcovMFileCollection(root_dir, method, monitor)
+function obj=MOcovMFileCollection(root_dir, method, monitor, exclude_pat)
% instantiate MOcovMFileCollection
%
% obj=MOcovMFileCollection(root_dir, method, monitor)
@@ -13,9 +13,14 @@
% - 'profile' use Matlab profiler
% default: 'file'
% monitor optional MOcovProgressMonitor instance
+% exclude_pat Optional cell array of patterns to exclude.
%
% See also: mocov
+ if nargin<4 || isempty(exclude_pat)
+ exclude_pat={};
+ end
+
if nargin<3 || isempty(monitor)
monitor=MOcovProgressMonitor();
end
@@ -27,6 +32,7 @@
props=struct();
props.root_dir=root_dir;
props.monitor=monitor;
+ props.exclude_pat=exclude_pat;
props.mfiles=[];
props.orig_path=[];
props.temp_dir=[];
@@ -14,7 +14,7 @@
monitor=obj.monitor;
- fns=mocov_find_files(obj.root_dir,'*.m',monitor);
+ fns=mocov_find_files(obj.root_dir,'*.m',monitor,obj.exclude_pat);
n=numel(fns);
mfiles=cell(n,1);
View
@@ -16,6 +16,10 @@
% Mutually exclusive with '-e' option.
% '-cover', covd Find coverage for files in dir covd and all
% of its subdirectories
+% '-cover_exclude', pat (optional) Exclude files and directories
+% which match this pattern, even if they are
+% in covd. Can be used multiple times to
+% specify multiple patterns to match.
% '-cover_json_file', cj (optional) Store coverage information in
% ` file cj in JSON format [use with coveralls]
% '-cover_xml_file', xc (optional) Store coverage information in
@@ -81,7 +85,8 @@
monitor=MOcovProgressMonitor(opt.verbose);
mfile_collection=MOcovMFileCollection(opt.cover,...
opt.method,...
- monitor);
+ monitor,...
+ opt.excludes);
mfile_collection=prepare(mfile_collection);
cleaner_collection=onCleanup(@()cleanup(mfile_collection));
@@ -151,6 +156,7 @@ function write_coverage_results(writers, mfile_collection, opt)
defaults=struct();
defaults.coverage_dir=pwd();
+ defaults.excludes={};
defaults.html_dir=[];
defaults.cobertura_xml=[];
defaults.coveralls_json=[];
@@ -185,6 +191,10 @@ function write_coverage_results(writers, mfile_collection, opt)
k=k+1;
opt.cover=varargin{k};
+ case '-cover_exclude'
+ k=k+1;
+ opt.excludes(end+1)=varargin(k);
+
case '-verbose'
opt.verbose=opt.verbose+1;
View
@@ -1,4 +1,4 @@
-function res=mocov_find_files(root_dir, file_pat, monitor)
+function res=mocov_find_files(root_dir, file_pat, monitor, exclude_pat)
% Finds files recursively in a directory
%
% res=mocov_find_files([root_dir[, file_pat]])
@@ -12,6 +12,9 @@
% is used, corresponding to all files.
% monitor Optional progress monitory that supports a
% 'notify' method.
+% exclude_pat Optional cell array of patterns to exclude. Both
+% files and directories which match one of these
+% patterns will be omitted from the output.
%
% Output:
% res Kx1 cell with names of files in root_dir matching
@@ -35,10 +38,28 @@
monitor=[];
end
+ if nargin<4
+ exclude_pat={};
+ end
+
+ if ischar(exclude_pat)
+ exclude_pat={exclude_pat};
+ end
+
file_re=['^' ... % start of the string
regexptranslate('wildcard',file_pat) ...
'$']; % end of the string
+ exclude_re='';
+ for k=1:numel(exclude_pat)
+ if ~isempty(exclude_re)
+ exclude_re=[exclude_re '|'];
+ end
+ exclude_re=[exclude_re ...
+ '^' ... % start of the string
+ regexptranslate('wildcard',exclude_pat{k}) ...
+ '$']; % end of the string
+ end
if ~isempty(monitor)
msg=sprintf('Finding files matching %s from %s',file_pat,root_dir);
@@ -47,7 +68,7 @@
res=find_files_recursively(root_dir,file_re,monitor);
-function res=find_files_recursively(root_dir,file_re,monitor)
+function res=find_files_recursively(root_dir,file_re,monitor,exclude_re)
if isempty(root_dir)
dir_arg={};
else
@@ -67,8 +88,10 @@
if ~ignore_fn
path_fn=fullfile(root_dir, fn);
- if isdir(path_fn)
- res=find_files_recursively(path_fn,file_re,monitor);
+ if ~isempty(regexp(fn,exclude_re,'once'));
+ continue;
+ elseif isdir(path_fn)
+ res=find_files_recursively(path_fn,file_re,monitor,exclude_re);
elseif ~isempty(regexp(fn,file_re,'once'));
res={path_fn};
if ~isempty(monitor)
@@ -79,4 +102,4 @@
res_cell{k}=res;
end
- res=cat(1,res_cell{:});
+ res=cat(1,res_cell{:});