-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExecuteExamplesInDirectory.m
More file actions
159 lines (144 loc) · 5.5 KB
/
ExecuteExamplesInDirectory.m
File metadata and controls
159 lines (144 loc) · 5.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
function [functionNames, functionStatus ] = ExecuteExamplesInDirectory(parentDir,varargin)
% Recursively descend directory tree and execute examples in all functions
%
% Syntax:
% [functionNames, functionStatus ] = ExecuteExamplesInDirectory(parentDir)
%
% Description:
% Run ExecuteExamplesInFunction on all of the .m files in a directory,
% as well as recurses down all the subdirectories and do the same.
%
% Prints out a report of successful and failed example running.
%
% Checks for itself and does not run its own examples, to prevent
% infinite recursion.
%
% Also does not descend into directories whose name contains the string
% "underDevelopment"
%
% Inputs:
% parentDir - String. The directory to start in.
%
% Outputs:
% functionNames - Cell array with names of functions examined using
% ExecuteExamplesInFunction.
% functionStatus - Status returned by ExecuteExamplesInFunction for each
% function examined.
%
% Optional key/value pairs
% 'verbose' - Boolean. Be verbose? Default false
% 'printnoexamples' - Boolean. List functions that have no examples.
% Default false.
% . 'printreport' - Boolean. Print a report at the end? Default
% true. Set to false when recursing so we just
% get a report at the end of the top level.
% 'closefigs' - Close figures after running each example. Default
% true.
%
% Examples are provided in the code
%
% See also:
% ExecuteExamplesInFunction, ExecuteTextInScript
%
% History
% 01/16/18 dhb Wrote it.
% 01/23/18 dhb Pass verbose into subfunction.
% Examples:
%{
theDir = fileparts(which('ExecuteExamplesInDirectory'));
ExecuteExamplesInDirectory(fullfile(theDir,'TestSubdir'),'verbose',true,'printnoexamples',true);
%}
%{
% If you use isetbio and also have it on your path, you can try this.
if (exist('isetbioRootPath','file'))
ExecuteExamplesInDirectory(fullfile(isetbioRootPath,...
'isettools','wavefront'),'verbose',true);
% Although there is an example in synchronizeISETBIOWithRepository,
% it contains an "% ETTBSkip" comment and thus is skipped.
ExecuteExamplesInDirectory(fullfile(isetbioRootPath,...
'external'),'verbose',true);
end
%}
%% Input parser
p = inputParser;
p.addParameter('verbose',false,@islogical);
p.addParameter('printnoexamples',false,@islogical);
p.addParameter('printreport',true,@islogical);
p.addParameter('closefigs',true,@islogical);
p.parse(varargin{:});
% Get current directory and change to parentDir
curDir = pwd;
cd(parentDir);
% Get everyting in the directory
theContents = dir(fullfile('*'));
%% Here we go
nRunFunctions = 0;
functionNames = {};
functionStatus = [];
for ii = 1:length(theContents)
%theContents(ii)
% Desend into directory?
if (theContents(ii).isdir & ...
~strcmp(theContents(ii).name,'.') ...
& ~strcmp(theContents(ii).name,'..') ...
& isempty(strfind(theContents(ii).name,'underDevelopment')))
if (p.Results.verbose)
fprintf('Descending into %s\n',theContents(ii).name)
end
% Recurse!
[tempFunctionNames,tempFunctionStatus] = ...
ExecuteExamplesInDirectory(fullfile(parentDir,theContents(ii).name),...
'printreport',false, ...
'verbose',p.Results.verbose, ...
'closefigs',p.Results.closefigs);
tempNRunFunctions = length(tempFunctionNames);
functionNames = {functionNames{:} tempFunctionNames{:}};
functionStatus = [functionStatus(:) ; tempFunctionStatus(:)];
nRunFunctions = nRunFunctions + tempNRunFunctions;
% Run on a .m file? But don't run on self.
elseif (length(theContents(ii).name) > 2)
if (strcmp(theContents(ii).name(end-1:end),'.m') & ...
~strcmp(theContents(ii).name,[mfilename '.m']))
% Check examples and report status
fprintf('Checking function %s\n',theContents(ii).name);
status = ExecuteExamplesInFunction(theContents(ii).name, ...
'verbose',p.Results.verbose, ...
'closefigs',p.Results.closefigs);
nRunFunctions = nRunFunctions+1;
functionNames{nRunFunctions} = theContents(ii).name(1:end-2);
functionStatus(nRunFunctions) = status;
else
if (p.Results.verbose) && ~isequal(theContents(ii).name(1),'.')
fprintf('%s: Ignoring\n',theContents(ii).name);
end
end
else
if (p.Results.verbose) && ~isequal(theContents(ii).name(1),'.')
fprintf('%s: Ignoring\n',theContents(ii).name);
end
end
end
%% Report at the end
if (p.Results.printreport)
fprintf('\n*** Example Test Report ***\n\n');
for ii = 1:length(functionNames)
% Get function name and its status
name = functionNames{ii};
status = functionStatus(ii);
% Report as appropriate
if (status == -1)
fprintf(2,'%s: At least one example FAILED!\n',name);
elseif (status == 0)
if (p.Results.printnoexamples)
fprintf('%s: No examples found\n',name);
end
elseif (status > 0)
fprintf('%s: Ran %d examples OK!\n',name,status);
else
error('Unexpected value for returned status');
end
end
end
% Return to calling dir
cd(curDir);
end