Menu

[4a4584]: / Source / HTMLCoverageReport.pas  Maximize  Restore  History

Download this file

556 lines (478 with data), 17.9 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
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
(***********************************************************************)
(* Delphi Code Coverage *)
(* *)
(* A quick hack of a Code Coverage Tool for Delphi *)
(* by Christer Fahlgren and Nick Ring *)
(* *)
(* This Source Code Form is subject to the terms of the Mozilla Public *)
(* License, v. 2.0. If a copy of the MPL was not distributed with this *)
(* file, You can obtain one at http://mozilla.org/MPL/2.0/. *)
unit HTMLCoverageReport;
interface
uses
Classes,
I_Report,
I_CoverageStats,
I_CoverageConfiguration,
ClassInfoUnit,
I_LogManager,
uConsoleOutput;
type
THtmlDetails = record
LinkFileName: string;
LinkName: string;
HasFile: Boolean;
end;
type
TCoverageStatsProc = function(const ACoverageModule: ICoverageStats): THtmlDetails of object;
type
THTMLCoverageReport = class(TInterfacedObject, IReport)
private
FCoverageConfiguration : ICoverageConfiguration;
procedure AddTableHeader(const ATableHeading: string;
const AColumnHeading: string;
const AOutputFile: TTextWriter);
procedure AddTableFooter(const AHeading: string;
const ACoverageStats: ICoverageStats;
const AOutputFile: TTextWriter);
procedure IterateOverStats(const ACoverageStats: ICoverageStats;
const AOutputFile: TTextWriter;
const ACoverageStatsProc: TCoverageStatsProc);
procedure SetPrePostLink(const AHtmlDetails: THtmlDetails;
out PreLink: string;
out PostLink: string);
procedure AddPostAmble(const AOutFile: TTextWriter);
procedure AddPreAmble(const AOutFile: TTextWriter);
function FindSourceFile(const ACoverageUnit: ICoverageStats;
var HtmlDetails: THtmlDetails): string;
procedure AddStatistics(const ACoverageBase: ICoverageStats;
const ASourceFileName: string;
const AOutFile: TTextWriter);
procedure GenerateCoverageTable(const ACoverageModule: ICoverageStats;
const AOutputFile: TTextWriter;
const AInputFile: TTextReader);
function GenerateModuleReport(const ACoverageModule: ICoverageStats): THtmlDetails;
function GenerateUnitReport(const ACoverageUnit: ICoverageStats): THtmlDetails;
procedure AddGeneratedAt(var OutputFile: TTextWriter);
public
constructor Create(const ACoverageConfiguration: ICoverageConfiguration);
procedure Generate(
const ACoverage: ICoverageStats;
const AModuleInfoList: TModuleList;
const ALogManager: ILogManager);
end;
const
SourceClass: string = ' class="s"';
OverviewClass: string = ' class="o"';
SummaryClass: string = ' class="sum"';
implementation
uses
SysUtils,
System.Math,
JclFileUtils,
JvStrToHtml,
HtmlHelper;
procedure THTMLCoverageReport.Generate(
const ACoverage: ICoverageStats;
const AModuleInfoList: TModuleList;
const ALogManager: ILogManager);
var
OutputFile: TTextWriter;
OutputFileName: TFileName;
begin
ALogManager.Log('Generating coverage report');
if (FCoverageConfiguration.SourcePaths.Count > 0) then
VerboseOutput('Source dir: ' + FCoverageConfiguration.SourcePaths.Strings[0])
else
VerboseOutput('Source dir: <none>');
VerboseOutput('Output dir: ' + FCoverageConfiguration.OutputDir);
OutputFileName := PathAppend(FCoverageConfiguration.OutputDir, 'CodeCoverage_summary.html');
OutputFile := TStreamWriter.Create(OutputFileName, False, TEncoding.UTF8);
try
AddPreAmble(OutputFile);
OutputFile.WriteLine(heading('Summary Coverage Report', 1));
AddGeneratedAt(OutputFile);
AddTableHeader('Aggregate statistics for all modules', 'Unit Name', OutputFile);
IterateOverStats(ACoverage, OutputFile, GenerateModuleReport);
AddTableFooter('Aggregated for all units', ACoverage, OutputFile);
AddPostAmble(OutputFile);
finally
OutputFile.Free;
end;
end;
procedure THTMLCoverageReport.AddGeneratedAt(var OutputFile: TTextWriter);
var
LinkText: string;
ParagraphText: string;
begin
LinkText := link(
'DelphiCodeCoverage',
'https://sourceforge.net/projects/delphicodecoverage/',
'Code Coverage for Delphi 5+'
);
ParagraphText :=
' Generated at ' + DateToStr(now) + ' ' + TimeToStr(now)
+ ' by ' + LinkText
+ ' - an open source tool for Delphi Code Coverage.';
OutputFile.WriteLine(p(ParagraphText));
end;
function THTMLCoverageReport.GenerateModuleReport(
const ACoverageModule: ICoverageStats): THtmlDetails;
var
OutputFile: TTextWriter;
OutputFileName: string;
begin
if (ACoverageModule.Count = 1) then
begin
Result := GenerateUnitReport(ACoverageModule.CoverageReport[0]);
Result.LinkName := ACoverageModule.Name;
Exit;
end;
try
Result.HasFile := False;
Result.LinkFileName := ACoverageModule.Name + '.html';
Result.LinkName := ACoverageModule.Name;
OutputFileName := PathAppend(FCoverageConfiguration.OutputDir, Result.LinkFileName);
OutputFile := TStreamWriter.Create(OutputFileName, False, TEncoding.UTF8);
try
AddPreAmble(OutputFile);
OutputFile.WriteLine(p('Coverage report for ' + bold(ACoverageModule.Name) + '.'));
AddGeneratedAt(OutputFile);
AddTableHeader('Aggregate statistics for all units', 'Source File Name', OutputFile);
IterateOverStats(ACoverageModule, OutputFile, GenerateUnitReport);
AddTableFooter('Aggregated for all files', ACoverageModule, OutputFile);
AddPostAmble(OutputFile);
finally
OutputFile.Free;
end;
Result.HasFile := True;
except
on E: EFileStreamError do
ConsoleOutput('Exception during generation of unit coverage for:' + ACoverageModule.Name +
' could not write to: ' + OutputFileName +
' exception:' + E.message)
else
raise;
end;
end;
function THTMLCoverageReport.GenerateUnitReport(
const ACoverageUnit: ICoverageStats): THtmlDetails;
var
InputFile: TTextReader;
OutputFile: TTextWriter;
SourceFileName: string;
OutputFileName: string;
begin
Result.HasFile:= False;
Result.LinkFileName:= ACoverageUnit.ReportFileName + '.html';
Result.LinkName:= ACoverageUnit.Name;
if FCoverageConfiguration.ExcludedUnits.IndexOf(StringReplace(ExtractFileName(ACoverageUnit.Name), ExtractFileExt(ACoverageUnit.Name), '', [rfReplaceAll, rfIgnoreCase])) < 0 then
try
SourceFileName := FindSourceFile(ACoverageUnit, Result);
try
InputFile := TStreamReader.Create(SourceFileName, TEncoding.ANSI, True);
except
on E: EFileStreamError do
begin
ConsoleOutput(
'Exception during generation of unit coverage for:' + ACoverageUnit.Name
+ ' could not open:' + SourceFileName
);
ConsoleOutput('Current directory:' + GetCurrentDir);
raise;
end;
end;
try
OutputFileName := Result.LinkFileName;
OutputFileName := PathAppend(FCoverageConfiguration.OutputDir, OutputFileName);
try
OutputFile := TStreamWriter.Create(OutputFileName, False, TEncoding.UTF8);
try
AddPreAmble(OutputFile);
OutputFile.WriteLine(p('Coverage report for ' + bold(ACoverageUnit.Parent.Name + ' (' + SourceFileName + ')') + '.'));
AddGeneratedAt(OutputFile);
AddStatistics(ACoverageUnit, SourceFileName, OutputFile);
GenerateCoverageTable(ACoverageUnit, OutputFile, InputFile);
AddPostAmble(OutputFile);
finally
OutputFile.Free;
end;
except
on E: EFileStreamError do
begin
ConsoleOutput(
'Exception during generation of unit coverage for:' + ACoverageUnit.Name
+ ' could not write to:' + OutputFileName
);
ConsoleOutput('Current directory:' + GetCurrentDir);
raise;
end;
end;
Result.HasFile := True;
finally
InputFile.Free;
end;
except
on E: EFileStreamError do
ConsoleOutput(
'Exception during generation of unit coverage for:' + ACoverageUnit.Name
+ ' exception:' + E.message
)
else
raise;
end;
end;
procedure THTMLCoverageReport.IterateOverStats(
const ACoverageStats: ICoverageStats;
const AOutputFile: TTextWriter;
const ACoverageStatsProc: TCoverageStatsProc);
var
StatIndex: Integer;
HtmlDetails : THtmlDetails;
PostLink: string;
PreLink: string;
CurrentStats: ICoverageStats;
begin
for StatIndex := 0 to Pred(ACoverageStats.Count) do
begin
CurrentStats := ACoverageStats.CoverageReport[StatIndex];
HtmlDetails.HasFile := False;
if Assigned(ACoverageStatsProc) then
HtmlDetails := ACoverageStatsProc(CurrentStats);
SetPrePostLink(HtmlDetails, PreLink, PostLink);
AOutputFile.WriteLine(
tr(
td(PreLink + HtmlDetails.LinkName + PostLink) +
td(IntToStr(CurrentStats.CoveredLineCount)) +
td(IntToStr(CurrentStats.LineCount)) +
td(em(IntToStr(CurrentStats.PercentCovered) + '%'))
)
);
end;
end;
procedure THTMLCoverageReport.SetPrePostLink(
const AHtmlDetails: THtmlDetails;
out PreLink: string;
out PostLink: string);
var
LLinkFileName : string;
begin
PreLink := '';
PostLink := '';
if AHtmlDetails.HasFile then
begin
LLinkFileName := StringReplace(AHtmlDetails.LinkFileName, '\', '/', [rfReplaceAll]);
PreLink := StartTag('a', 'href="' + LLinkFileName + '"');
PostLink := EndTag('a');
end;
end;
procedure THTMLCoverageReport.AddPreAmble(const AOutFile: TTextWriter);
begin
AOutFile.WriteLine('<!DOCTYPE html>');
AOutFile.WriteLine(StartTag('html'));
AOutFile.WriteLine(StartTag('head'));
AOutFile.WriteLine(' <meta content="text/html; charset=utf-8" http-equiv="Content-Type" />');
AOutFile.WriteLine(' ' + WrapTag('Delphi CodeCoverage Coverage Report', 'title'));
if FileExists('style.css') then
AOutFile.WriteLine(' <link rel="stylesheet" href="style.css" type="text/css" />')
else
begin
AOutFile.WriteLine(StartTag('style', 'type="text/css"'));
AOutFile.WriteLine('table {border-spacing:0; border-collapse:collapse;}');
AOutFile.WriteLine('table, td, th {border: 1px solid black;}');
AOutFile.WriteLine('td, th {background: white; margin: 0; padding: 2px 0.5em 2px 0.5em}');
AOutFile.WriteLine('td {border-width: 0 1px 0 0;}');
AOutFile.WriteLine('th {border-width: 1px 1px 1px 0;}');
AOutFile.WriteLine('p, h1, h2, h3, th {font-family: verdana,arial,sans-serif; font-size: 10pt;}');
AOutFile.WriteLine('td {font-family: courier,monospace; font-size: 10pt;}');
AOutFile.WriteLine('th {background: #CCCCCC;}');
AOutFile.WriteLine('table.o tr td:nth-child(1) {font-weight: bold;}');
AOutFile.WriteLine('table.o tr td:nth-child(2) {text-align: right;}');
AOutFile.WriteLine('table.o tr td {border-width: 1px;}');
AOutFile.WriteLine('table.s {width: 100%;}');
AOutFile.WriteLine('table.s tr td {padding: 0 0.25em 0 0.25em;}');
AOutFile.WriteLine('table.s tr td:first-child {text-align: right; font-weight: bold;}');
AOutFile.WriteLine('table.s tr.notcovered td {background: #DDDDFF;}');
AOutFile.WriteLine('table.s tr.nocodegen td {background: #FFFFEE;}');
AOutFile.WriteLine('table.s tr.covered td {background: #CCFFCC;}');
AOutFile.WriteLine('table.s tr.covered td:first-child {color: green;}');
AOutFile.WriteLine('table.s {border-width: 1px 0 1px 1px;}');
AOutFile.WriteLine('table.sum tr td {border-width: 1px;}');
AOutFile.WriteLine('table.sum tr th {text-align:right;}');
AOutFile.WriteLine('table.sum tr th:first-child {text-align:center;}');
AOutFile.WriteLine('table.sum tr td {text-align:right;}');
AOutFile.WriteLine('table.sum tr td:first-child {text-align:left;}');
AOutFile.WriteLine(EndTag('style'));
end;
AOutFile.WriteLine(EndTag('head'));
AOutFile.WriteLine(StartTag('body'));
end;
procedure THTMLCoverageReport.AddPostAmble(const AOutFile: TTextWriter);
begin
AOutFile.WriteLine(EndTag('body'));
AOutFile.WriteLine(EndTag('html'));
end;
procedure THTMLCoverageReport.AddStatistics(
const ACoverageBase: ICoverageStats;
const ASourceFileName: string;
const AOutFile: TTextWriter);
begin
AOutFile.WriteLine( p(' Statistics for ' + ASourceFileName + ' '));
AOutFile.WriteLine(
table(
tr(
td('Number of lines covered') +
td(IntToStr(ACoverageBase.CoveredLineCount))
) +
tr(
td('Number of lines with code gen') +
td(IntToStr(ACoverageBase.LineCount))
) +
tr(
td('Line coverage') +
td(IntToStr(ACoverageBase.PercentCovered) + '%')
),
OverviewClass
)
);
AOutFile.WriteLine(lineBreak + lineBreak);
end;
procedure THTMLCoverageReport.AddTableFooter(
const AHeading: string;
const ACoverageStats: ICoverageStats;
const AOutputFile: TTextWriter);
begin
AOutputFile.WriteLine(
tr(
th(JvStrToHtml.StringToHtml(AHeading)) +
th(IntToStr(ACoverageStats.CoveredLineCount)) +
th(IntToStr(ACoverageStats.LineCount)) +
th(em(IntToStr(ACoverageStats.PercentCovered) + '%'))
)
);
AOutputFile.WriteLine(EndTag('table'));
end;
procedure THTMLCoverageReport.AddTableHeader(
const ATableHeading: string;
const AColumnHeading: string;
const AOutputFile: TTextWriter);
begin
AOutputFile.WriteLine(p(JvStrToHtml.StringToHtml(ATableHeading)));
AOutputFile.WriteLine(StartTag('table', SummaryClass));
AOutputFile.WriteLine(
tr(
th(JvStrToHtml.StringToHtml(AColumnHeading)) +
th('Number of covered lines') +
th('Number of lines (which generated code)') +
th('Percent(s) covered')
)
);
end;
constructor THTMLCoverageReport.Create(
const ACoverageConfiguration: ICoverageConfiguration);
begin
inherited Create;
FCoverageConfiguration := ACoverageConfiguration;
end;
function THTMLCoverageReport.FindSourceFile(
const ACoverageUnit: ICoverageStats;
var HtmlDetails: THtmlDetails): string;
var
SourceFound: Boolean;
CurrentSourcePath: string;
SourcePathIndex: Integer;
UnitIndex: Integer;
ACoverageModule: ICoverageStats;
begin
SourceFound := False;
SourcePathIndex := 0;
while (SourcePathIndex < FCoverageConfiguration.SourcePaths.Count)
and not SourceFound do
begin
CurrentSourcePath := FCoverageConfiguration.SourcePaths[SourcePathIndex];
Result := PathAppend(CurrentSourcePath, ACoverageUnit.Name);
if not FileExists(Result) then
begin
ACoverageModule := ACoverageUnit.Parent;
UnitIndex := 0;
while (UnitIndex < ACoverageModule.Count)
and not SourceFound do
begin
Result := PathAppend(
PathAppend(
CurrentSourcePath,
ExtractFilePath(ACoverageModule.CoverageReport[UnitIndex].Name)
),
ACoverageUnit.Name
);
if FileExists(Result) then
begin
HtmlDetails.LinkName := PathAppend(
ExtractFilePath(ACoverageModule.CoverageReport[UnitIndex].Name),
HtmlDetails.LinkName
);
SourceFound := True;
end;
Inc(UnitIndex, 1);
end;
end
else
SourceFound := True;
Inc(SourcePathIndex, 1);
end;
if (not SourceFound) then
Result := ACoverageUnit.Name;
end;
procedure THTMLCoverageReport.GenerateCoverageTable(
const ACoverageModule: ICoverageStats;
const AOutputFile: TTextWriter;
const AInputFile: TTextReader);
var
LineCoverage : TCoverageLine;
InputLine : string;
LineCoverageIter : Integer;
LineCount : Integer;
procedure WriteTableRow(const AClass: string; const ACount: Integer = -1);
var
HtmlLineCount: string;
Count: Integer;
begin
Count := Min(FCoverageConfiguration.LineCountLimit, ACount);
if FCoverageConfiguration.LineCountLimit = 0 then
HtmlLineCount := '' // No column for count
else if Count < 0 then
HtmlLineCount := td('') // Count is blank
else
HtmlLineCount := td(IntToStr(Count)); // Count is given
AOutputFile.WriteLine(
tr(
td(IntToStr(LineCount)) + HtmlLineCount +
td(pre(InputLine)),
'class="' + AClass + '"'
)
);
end;
begin
LineCoverageIter := 0;
LineCount := 1;
AOutputFile.WriteLine(StartTag('table', SourceClass));
while AInputFile.Peek <> -1 do
begin
InputLine := AInputFile.ReadLine;
InputLine := JvStrToHtml.StringToHtml(TrimRight(InputLine));
LineCoverage := ACoverageModule.CoverageLine[LineCoverageIter];
if (LineCount = LineCoverage.LineNumber) then
begin
if LineCoverage.IsCovered then
WriteTableRow('covered', LineCoverage.LineCount)
else
WriteTableRow('notcovered');
Inc(LineCoverageIter);
end
else
WriteTableRow('nocodegen');
Inc(LineCount);
end;
AOutputFile.WriteLine(EndTag('table'));
end;
end.
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.