forked from opencobra/cobratoolbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestAll.m
249 lines (199 loc) · 7.46 KB
/
testAll.m
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
% define global paths
global CBTDIR
global GUROBI_PATH
global ILOG_CPLEX_PATH
global TOMLAB_PATH
fprintf('The COBRAToolbox testing suite\n')
fprintf('------------------------------\n')
if ~isempty(getenv('MOCOV_PATH')) && ~isempty(getenv('JSONLAB_PATH'))
addpath(genpath(getenv('MOCOV_PATH')))
addpath(genpath(getenv('JSONLAB_PATH')))
COVERAGE = true;
fprintf('MoCov and JsonLab are on path, coverage will be computed.\n')
else
COVERAGE = false;
end
% Save the folder we were in.
origDir = pwd;
% if the location of initCobraToolbox is not yet known
if length(which('initCobraToolbox.m')) == 0
% define the path to The COBRA Toolbox
pth = fileparts(which('testAll.m'));
% Now, we are in the test folder
cd(pth)
% Switch to the base folder
cd ..
% And assign the CBTDIR variable
CBTDIR = pwd;
else
CBTDIR = fileparts(which('initCobraToolbox.m'));
cd(CBTDIR);
end
% include the root folder and all subfolders.
addpath(genpath([pwd filesep 'test']));
% change to the root folder of The COBRA TOolbox
cd(CBTDIR);
% run the official initialisation script
initCobraToolbox;
%Init the cleanup:
currentDir = cd('test');
[~, testDirContent] = system('git ls-files');
testDirContent = strsplit(testDirContent, '\n');
testDirPath = pwd;
cd(currentDir);
if ~isempty(strfind(getenv('HOME'), 'jenkins')) || ~isempty(strfind(getenv('USERPROFILE'), 'jenkins'))
WAITBAR_TYPE = 0;
% add CellNetAnalyzer for testing purposes
addpath(genpath(getenv('CNA_PATH')));
% check the CNA installation
checkCNAinstallation(0);
else
WAITBAR_TYPE = 1;
end
if verLessThan('matlab', '8.2')
error('The testsuite of The COBRA Toolbox can only be run with MATLAB R2014b+.')
end
% define a success exit code
exit_code = 0;
% enable profiler
profile on;
if COVERAGE
% Get the ignored Files from gitIgnore
% only retain the lines that end with .txt and .m and
%are not comments and point to files in the /src folder
ignoredPatterns = {'^.{0,3}$',... % Is smaller than four.
['^[^s][^r][^c][^' regexptranslate('escape',filesep) ']']}; % does not start with src/
filterPatterns = {'\.txt$','\.m$'}; % Is either a .m file or a .txt file.
ignoreFiles = getIgnoredFiles(ignoredPatterns,filterPatterns);
% check the code quality
listFiles = rdir(['./src', '/**/*.m']);
% count the number of failed code quality checks per file
nMsgs = 0;
nCodeLines = 0;
nEmptyLines = 0;
nCommentLines = 0;
for i = 1:length(listFiles)
nMsgs = nMsgs + length(checkcode(listFiles(i).name));
fid = fopen(listFiles(i).name);
% check if the file is on the ignored list
countFlag = true;
for k = 1:length(ignoreFiles)
if ~isempty(strfind(listFiles(i).name, ignoreFiles{k}))
countFlag = false;
end
end
while ~feof(fid) && countFlag
lineOfFile = strtrim(char(fgetl(fid)));
if length(lineOfFile) > 0 && length(strfind(lineOfFile(1), '%')) ~= 1 ...
&& length(strfind(lineOfFile, 'end')) ~= 1 && length(strfind(lineOfFile, 'otherwise')) ~= 1 ...
&& length(strfind(lineOfFile, 'switch')) ~= 1 && length(strfind(lineOfFile, 'else')) ~= 1 ...
&& length(strfind(lineOfFile, 'case')) ~= 1 && length(strfind(lineOfFile, 'function')) ~= 1
nCodeLines = nCodeLines + 1;
elseif length(lineOfFile) == 0
nEmptyLines = nEmptyLines + 1;
elseif length(strfind(lineOfFile(1), '%')) == 1
nCommentLines = nCommentLines + 1;
end
end
fclose(fid);
end
% average number of messages per codeLines
avMsgsPerc = floor(nMsgs / nCodeLines * 100);
grades = {'A', 'B', 'C', 'D', 'E', 'F'};
intervals = [0, 3;
3, 6;
6, 9;
9, 12;
12, 15;
15, 100];
grade = 'F';
for i = 1:length(intervals)
if avMsgsPerc >= intervals(i, 1) && avMsgsPerc < intervals(i, 2)
grade = grades{i};
end
end
fprintf('\n\n -> The code grade is %s (%1.2f%%).\n\n', grade, avMsgsPerc);
if ~isempty(strfind(getenv('HOME'), 'jenkins'))
% set the new badge
system(['cp /mnt/prince-data/jenkins/userContent/codegrade-', grade, '.svg /mnt/prince-data/jenkins/userContent/codegrade.svg']);
% secure copy the badge from the slave
system('scp -P 8022 /mnt/prince-data/jenkins/userContent/codegrade.svg jenkins@prince-server.lcsb.uni.lux:/var/lib/jenkins/userContent');
end
end
try
% save the userpath
originalUserPath = path;
% run the tests in the subfolder verifiedTests/ recursively
result = runtests('./test/', 'Recursively', true, 'BaseFolder', '*verified*');
sumFailed = 0;
sumIncomplete = 0;
resulttable = result.table;
resulttable(:,'Details') = {''};
for i = 1:size(result, 2)
sumFailed = sumFailed + result(i).Failed;
sumIncomplete = sumIncomplete + result(i).Incomplete;
if result(i).Failed
try
Message = result(i).details.DiagnosticRecord.Exception.message;
catch
%Older Matlab versions might fail here
Message = 'Unknown Error, please check the log';
end
resulttable{i,'Details'} = {Message};
end
end
fprintf(['\n > ', num2str(sumFailed), ' tests failed. ', num2str(sumIncomplete), ' tests are incomplete.\n\n']);
% count the number of covered lines of code
if COVERAGE
% write coverage based on profile('info')
fprintf('Running MoCov ... \n')
mocov('-cover', 'src', ...
'-profile_info', ...
'-cover_json_file', 'coverage.json', ...
'-cover_html_dir', 'coverage_html', ...
'-cover_method', 'profile', ...
'-verbose');
% load the coverage file
data = loadjson('coverage.json', 'SimplifyCell', 1);
sf = data.source_files;
clFiles = zeros(length(sf), 1);
tlFiles = zeros(length(sf), 1);
for i = 1:length(sf)
clFiles(i) = nnz(sf(i).coverage);
tlFiles(i) = length(sf(i).coverage);
end
% average the values for each file
cl = sum(clFiles);
tl = sum(tlFiles);
% print out the coverage
fprintf('Covered Lines: %i, Total Lines: %i, Coverage: %f%%.\n', cl, tl, cl / tl * 100);
end
% print out a summary table
resulttable
% restore the original path
restoredefaultpath;
addpath(originalUserPath);
if sumFailed > 0 || sumIncomplete > 0
exit_code = 1;
end
fprintf(['\n > The exit code is ', num2str(exit_code), '.\n\n']);
%clean up temporary files.
removeGitIgnoredNewFiles(testDirPath,testDirContent);
% ensure that we ALWAYS call exit
if ~isempty(strfind(getenv('HOME'), 'jenkins')) || ~isempty(strfind(getenv('USERPROFILE'), 'jenkins'))
exit(exit_code);
end
catch ME
%Also clean up temporary files in case of an error.
removeGitIgnoredNewFiles(testDirPath,testDirContent);
if ~isempty(strfind(getenv('HOME'), 'jenkins')) || ~isempty(strfind(getenv('USERPROFILE'), 'jenkins'))
% only exit on jenkins.
exit(1);
else
% switch back to the folder we were in and rethrow the error
cd(origDir);
rethrow(ME);
end
end
% switch back to the original directory
cd(origDir)