Menu

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

Download this file

1092 lines (961 with data), 33.1 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
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
(***********************************************************************)
(* 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 CoverageConfiguration;
interface
uses
Classes,
SysUtils,
XMLIntf,
I_CoverageConfiguration,
I_ParameterProvider,
I_LogManager,
ModuleNameSpaceUnit,
uConsoleOutput;
type
TCoverageConfiguration = class(TInterfacedObject, ICoverageConfiguration)
strict private
FExeFileName: string;
FMapFileName: string;
FSourceDir: string;
FOutputDir: string;
FDebugLogFileName: string;
FApiLogging: Boolean;
FParameterProvider: IParameterProvider;
FDProjUnitsLst: TStringList;
FUnitsStrLst: TStringList;
FExcludedUnitsStrLst: TStringList;
FExeParamsStrLst: TStrings;
FSourcePathLst: TStrings;
FStripFileExtension: Boolean;
FEmmaOutput: Boolean;
FEmmaOutput21: Boolean;
FSeparateMeta: Boolean;
FXmlOutput: Boolean;
FXmlLines: Boolean;
FHtmlOutput: Boolean;
FTestExeExitCode: Boolean;
FExcludeSourceMaskLst: TStrings;
FLoadingFromDProj: Boolean;
FModuleNameSpaces: TModuleNameSpaceList;
FUnitNameSpaces: TUnitNameSpaceList;
FLineCountLimit: Integer;
FLogManager: ILogManager;
procedure ReadSourcePathFile(const ASourceFileName: string);
function ParseParameter(const AParameter: Integer): string;
procedure ParseSwitch(var AParameter: Integer);
procedure ParseBooleanSwitches;
function GetCurrentConfig(const Project: IXMLNode): string;
function GetExeOutputFromDProj(const Project: IXMLNode; const ProjectName: TFileName): string;
procedure ParseDProj(const DProjFilename: TFileName);
function IsPathInExclusionList(const APath: TFileName): Boolean;
procedure ExcludeSourcePaths;
procedure RemovePathsFromUnits;
function ExpandEnvString(const APath: string): string;
procedure LogTracking;
function IsExecutableSet(var AReason: string): Boolean;
function IsMapFileSet(var AReason: string): Boolean;
procedure OpenInputFileForReading(const AFileName: string; var InputFile: TextFile);
function MakePathAbsolute(const APath, ASourceFileName: string): string;
procedure ParseExecutableSwitch(var AParameter: Integer);
procedure ParseMapFileSwitch(var AParameter: Integer);
procedure ParseUnitSwitch(var AParameter: Integer);
procedure AddUnitString(AUnitString: string);
procedure ParseUnitFileSwitch(var AParameter: Integer);
procedure ReadUnitsFile(const AUnitsFileName: string);
procedure ParseExecutableParametersSwitch(var AParameter: Integer);
procedure ParseSourceDirectorySwitch(var AParameter: Integer);
procedure ParseSourcePathsSwitch(var AParameter: Integer);
procedure ParseSourcePathsFileSwitch(var AParameter: Integer);
procedure ParseOutputDirectorySwitch(var AParameter: Integer);
procedure ParseLoggingTextSwitch(var AParameter: Integer);
procedure ParseWinApiLoggingSwitch(var AParameter: Integer);
procedure ParseDprojSwitch(var AParameter: Integer);
procedure ParseExcludeSourceMaskSwitch(var AParameter: Integer);
procedure ParseModuleNameSpaceSwitch(var AParameter: Integer);
procedure ParseUnitNameSpaceSwitch(var AParameter: Integer);
procedure ParseLineCountSwitch(var AParameter: Integer);
public
constructor Create(const AParameterProvider: IParameterProvider);
destructor Destroy; override;
procedure ParseCommandLine(const ALogManager: ILogManager = nil);
function ApplicationParameters: string;
function ExeFileName: string;
function MapFileName: string;
function OutputDir: string;
function SourceDir: string;
function DebugLogFile: string;
function SourcePaths: TStrings;
function Units: TStrings;
function ExcludedUnits: TStrings;
function UseApiDebug: Boolean;
function IsComplete(var AReason: string): Boolean;
function EmmaOutput: Boolean;
function EmmaOutput21: Boolean;
function SeparateMeta: Boolean;
function XmlOutput: Boolean;
function XmlLines: Boolean;
function HtmlOutput: Boolean;
function TestExeExitCode: Boolean;
function LineCountLimit: Integer;
function ModuleNameSpace(const AModuleName: string): TModuleNameSpace;
function UnitNameSpace(const AModuleName: string): TUnitNameSpace;
end;
EConfigurationException = class(Exception);
implementation
uses
System.StrUtils,
JclFileUtils,
{$IF CompilerVersion < 21}
IOUtilsD9,
{$ELSE}
IOUtils,
{$ENDIF}
LoggerTextFile,
LoggerAPI,
XMLDoc,
Windows,
Masks;
function Unescape(const AParameter: string): string;
var
lp: Integer;
begin
Result := '';
if Length(AParameter) > 0 then
begin
lp := Low(AParameter);
while lp <= High(AParameter) do
begin
if AParameter[lp] = I_CoverageConfiguration.cESCAPE_CHARACTER then
Inc(lp);
Result := Result + AParameter[lp];
Inc(lp);
end;
end;
end;
constructor TCoverageConfiguration.Create(const AParameterProvider: IParameterProvider);
begin
inherited Create;
FLogManager := nil;
FParameterProvider := AParameterProvider;
FExeParamsStrLst := TStringList.Create;
FUnitsStrLst := TStringList.Create;
FUnitsStrLst.CaseSensitive := False;
FUnitsStrLst.Sorted := True;
FUnitsStrLst.Duplicates := dupIgnore;
FExcludedUnitsStrLst := TStringList.Create;
FExcludedUnitsStrLst.CaseSensitive := False;
FExcludedUnitsStrLst.Sorted := True;
FExcludedUnitsStrLst.Duplicates := dupIgnore;
FDProjUnitsLst := TStringList.Create;
FDProjUnitsLst.CaseSensitive := False;
FDProjUnitsLst.Sorted := True;
FDProjUnitsLst.Duplicates := dupIgnore;
FApiLogging := False;
FStripFileExtension := True;
FSourcePathLst := TStringList.Create;
FEmmaOutput := False;
FEmmaOutput21 := False;
FSeparateMeta := False;
FHtmlOutput := False;
FXmlOutput := False;
FXmlLines := False;
FExcludeSourceMaskLst := TStringList.Create;
FModuleNameSpaces := TModuleNameSpaceList.Create;
FUnitNameSpaces := TUnitNameSpaceList.Create;
FLineCountLimit := 0;
end;
destructor TCoverageConfiguration.Destroy;
begin
FLogManager := nil;
FUnitsStrLst.Free;
FExcludedUnitsStrLst.Free;
FExeParamsStrLst.Free;
FSourcePathLst.Free;
FExcludeSourceMaskLst.Free;
FModuleNameSpaces.Free;
FUnitNameSpaces.free;
inherited;
end;
function TCoverageConfiguration.LineCountLimit: integer;
begin
Result := FLineCountLimit;
end;
function TCoverageConfiguration.IsComplete(var AReason: string): Boolean;
begin
if FSourcePathLst.Count = 0 then
FSourcePathLst.Add(''); // Default directory.
Result := IsExecutableSet(AReason) and IsMapFileSet(AReason);
end;
function TCoverageConfiguration.IsExecutableSet(var AReason: string): Boolean;
begin
AReason := '';
if (FExeFileName = '') then
AReason := 'No executable was specified'
else if not FileExists(FExeFileName) then
AReason := 'The executable file ' + FExeFileName + ' does not exist. Current dir is ' + GetCurrentDir;
Result := (AReason = '');
end;
function TCoverageConfiguration.IsMapFileSet(var AReason: string): Boolean;
begin
AReason := '';
if (FMapFileName = '') then
AReason := 'No map file was specified'
else if not FileExists(FMapFileName) then
AReason := 'The map file ' + FMapFileName + ' does not exist. Current dir is ' + GetCurrentDir;
Result := (AReason = '');
end;
function TCoverageConfiguration.Units : TStrings;
begin
Result := FUnitsStrLst;
end;
function TCoverageConfiguration.ExcludedUnits : TStrings;
begin
Result := FExcludedUnitsStrLst;
end;
function TCoverageConfiguration.SourcePaths: TStrings;
begin
Result := FSourcePathLst;
end;
function TCoverageConfiguration.ApplicationParameters: string;
var
lp: Integer;
begin
Result := '';
for lp := 0 to FExeParamsStrLst.Count - 1 do
Result := Result + FExeParamsStrLst[lp] + ' ';
Result := Copy(Result, 1, Length(Result) - 1);
end;
function TCoverageConfiguration.DebugLogFile: string;
begin
Result := FDebugLogFileName;
end;
function TCoverageConfiguration.MapFileName: string;
begin
Result := FMapFileName;
end;
function TCoverageConfiguration.ExeFileName: string;
begin
Result := FExeFileName;
end;
function TCoverageConfiguration.OutputDir: string;
begin
Result := FOutputDir;
end;
function TCoverageConfiguration.SourceDir: string;
begin
Result := FSourceDir;
end;
function TCoverageConfiguration.ModuleNameSpace(const AModuleName: string):TModuleNameSpace;
begin
Result := FModuleNameSpaces[AModuleName];
end;
function TCoverageConfiguration.UnitNameSpace(const AModuleName: string):TUnitNameSpace;
begin
Result := FUnitNameSpaces[AModuleName];
end;
procedure TCoverageConfiguration.OpenInputFileForReading(const AFileName: string; var InputFile: TextFile);
begin
AssignFile(InputFile, AFileName);
try
System.FileMode := fmOpenRead;
Reset(InputFile);
except
on E: EInOutError do
begin
ConsoleOutput('Could not open:' + AFileName);
raise ;
end;
end;
end;
function TCoverageConfiguration.UseApiDebug: Boolean;
begin
Result := FApiLogging;
end;
function TCoverageConfiguration.EmmaOutput: Boolean;
begin
Result := FEmmaOutput;
end;
function TCoverageConfiguration.EmmaOutput21: Boolean;
begin
Result := FEmmaOutput21;
end;
function TCoverageConfiguration.SeparateMeta;
begin
Result := FSeparateMeta;
end;
function TCoverageConfiguration.XmlOutput: Boolean;
begin
Result := FXmlOutput or not FHtmlOutput;
end;
function TCoverageConfiguration.XmlLines: Boolean;
begin
Result := FXmlLines;
end;
function TCoverageConfiguration.HtmlOutput: Boolean;
begin
Result := FHtmlOutput;
end;
function TCoverageConfiguration.TestExeExitCode: Boolean;
begin
Result := FTestExeExitCode;
end;
function TCoverageConfiguration.IsPathInExclusionList(const APath: TFileName): Boolean;
var
Mask: string;
begin
Result := False;
for Mask in FExcludeSourceMaskLst do
begin
if MatchesMask(APath, Mask) then
Exit(True);
end;
end;
procedure TCoverageConfiguration.ParseBooleanSwitches;
function CleanSwitch(const Switch: string): string;
begin
Result := Switch;
if StartsStr('-', Result) then
Delete(Result, 1, 1);
end;
function IsSet(const Switch: string): Boolean;
begin
Result := FindCmdLineSwitch(CleanSwitch(Switch), ['-'], true);
end;
begin
FEmmaOutput := IsSet(I_CoverageConfiguration.cPARAMETER_EMMA_OUTPUT);
FEmmaOutput21 := IsSet(I_CoverageConfiguration.cPARAMETER_EMMA21_OUTPUT);
FSeparateMeta := IsSet(I_CoverageConfiguration.cPARAMETER_EMMA_SEPARATE_META);
FXmlOutput := IsSet(I_CoverageConfiguration.cPARAMETER_XML_OUTPUT);
FXmlLines := IsSet(I_CoverageConfiguration.cPARAMETER_XML_LINES);
FHtmlOutput := IsSet(I_CoverageConfiguration.cPARAMETER_HTML_OUTPUT);
uConsoleOutput.G_Verbose_Output := IsSet(I_CoverageConfiguration.cPARAMETER_VERBOSE);
FTestExeExitCode := IsSet(I_CoverageConfiguration.cPARAMETER_TESTEXE_EXIT_CODE);
end;
procedure TCoverageConfiguration.ExcludeSourcePaths;
var
I: Integer;
begin
I := 0;
while I < FUnitsStrLst.Count do
begin
if IsPathInExclusionList(FUnitsStrLst[I]) then
begin
VerboseOutput('Skipping Unit ' + FUnitsStrLst[I] + ' from tracking because source path is excluded.');
FUnitsStrLst.Delete(I);
end
else
Inc(I);
end;
I := 0;
while I < FDprojUnitsLst.Count do
begin
if IsPathInExclusionList(FDprojUnitsLst[I]) then
begin
VerboseOutput('Skipping Unit ' + FDprojUnitsLst[I] + ' from tracking because source path is excluded.');
FDprojUnitsLst.Delete(I);
end
else
Inc(I);
end;
I := 0;
while I < FExcludedUnitsStrLst.Count do
begin
if IsPathInExclusionList(FExcludedUnitsStrLst[I]) then
FExcludedUnitsStrLst.Delete(I)
else
Inc(I);
end;
I := 0;
while I < FSourcePathLst.Count do
begin
if IsPathInExclusionList(FSourcePathLst[I]) then
FSourcePathLst.Delete(I)
else
Inc(I);
end;
end;
procedure TCoverageConfiguration.RemovePathsFromUnits;
var
NewUnitsList: TStrings;
CurrentUnit: string;
begin
NewUnitsList := TStringList.Create;
try
for CurrentUnit in FUnitsStrLst do
NewUnitsList.Add(CurrentUnit);
for CurrentUnit in FDProjUnitsLst do
NewUnitsList.Add(ChangeFileExt(ExtractFileName(CurrentUnit), ''));
FUnitsStrLst.Clear;
for CurrentUnit in NewUnitsList do
begin
if FExcludedUnitsStrLst.IndexOf(CurrentUnit) < 0 then
FUnitsStrLst.Add(CurrentUnit);
end;
finally
NewUnitsList.Free;
end;
end;
procedure TCoverageConfiguration.ParseCommandLine(const ALogManager: ILogManager = nil);
var
ParameterIdx: Integer;
begin
FLogManager := ALogManager;
// parse boolean switches first, so we don't have to care about the order here
ParseBooleanSwitches;
ParameterIdx := 1;
while ParameterIdx <= FParameterProvider.Count do
begin
ParseSwitch(ParameterIdx);
Inc(ParameterIdx);
end;
// exclude not matching source paths
ExcludeSourcePaths;
RemovePathsFromUnits;
LogTracking;
end;
procedure TCoverageConfiguration.LogTracking;
var
CurrentUnit: string;
begin
for CurrentUnit in FUnitsStrLst do
VerboseOutput('Will track coverage for:' + CurrentUnit);
for CurrentUnit in FExcludedUnitsStrLst do
VerboseOutput('Exclude from coverage tracking for:' + CurrentUnit);
end;
function TCoverageConfiguration.ParseParameter(const AParameter: Integer): string;
var
Param: string;
begin
Result := '';
if AParameter <= FParameterProvider.Count then
begin
Param := FParameterProvider.ParamString(AParameter);
if (LeftStr(Param, 1) <> '-') then
Result := ExpandEnvString(Unescape(Param));
end;
end;
function TCoverageConfiguration.ExpandEnvString(const APath: string): string;
var
Size: Cardinal;
begin
Result := APath;
Size := ExpandEnvironmentStrings(PChar(APath), nil, 0);
if Size > 0 then
begin
SetLength(Result, Size);
ExpandEnvironmentStrings(PChar(APath), PChar(Result), Size);
SetLength(Result, Length(Result) - 1);
end;
end;
procedure TCoverageConfiguration.ParseSwitch(var AParameter: Integer);
var
SwitchItem: string;
begin
SwitchItem := FParameterProvider.ParamString(AParameter);
if SwitchItem = I_CoverageConfiguration.cPARAMETER_EXECUTABLE then
ParseExecutableSwitch(AParameter)
else if SwitchItem = I_CoverageConfiguration.cPARAMETER_MAP_FILE then
ParseMapFileSwitch(AParameter)
else if SwitchItem = I_CoverageConfiguration.cPARAMETER_UNIT then
ParseUnitSwitch(AParameter)
else if SwitchItem = I_CoverageConfiguration.cPARAMETER_UNIT_FILE then
ParseUnitFileSwitch(AParameter)
else if SwitchItem = I_CoverageConfiguration.cPARAMETER_EXECUTABLE_PARAMETER then
ParseExecutableParametersSwitch(AParameter)
else if SwitchItem = I_CoverageConfiguration.cPARAMETER_SOURCE_DIRECTORY then
ParseSourceDirectorySwitch(AParameter)
else if SwitchItem = I_CoverageConfiguration.cPARAMETER_SOURCE_PATHS then
ParseSourcePathsSwitch(AParameter)
else if SwitchItem = I_CoverageConfiguration.cPARAMETER_SOURCE_PATHS_FILE then
ParseSourcePathsFileSwitch(AParameter)
else if SwitchItem = I_CoverageConfiguration.cPARAMETER_OUTPUT_DIRECTORY then
ParseOutputDirectorySwitch(AParameter)
else if SwitchItem = I_CoverageConfiguration.cPARAMETER_LOGGING_TEXT then
ParseLoggingTextSwitch(AParameter)
else if SwitchItem = I_CoverageConfiguration.cPARAMETER_LOGGING_WINAPI then
ParseWinApiLoggingSwitch(AParameter)
else if (SwitchItem = I_CoverageConfiguration.cPARAMETER_FILE_EXTENSION_EXCLUDE) then
FStripFileExtension := True
else if (SwitchItem = I_CoverageConfiguration.cPARAMETER_FILE_EXTENSION_INCLUDE) then
FStripFileExtension := False
else if (SwitchItem = I_CoverageConfiguration.cPARAMETER_LINE_COUNT) then
ParseLineCountSwitch(AParameter)
else if (SwitchItem = I_CoverageConfiguration.cPARAMETER_EMMA_OUTPUT)
or (SwitchItem = I_CoverageConfiguration.cPARAMETER_EMMA21_OUTPUT)
or (SwitchItem = I_CoverageConfiguration.cPARAMETER_EMMA_SEPARATE_META)
or (SwitchItem = I_CoverageConfiguration.cPARAMETER_XML_OUTPUT)
or (SwitchItem = I_CoverageConfiguration.cPARAMETER_XML_LINES)
or (SwitchItem = I_CoverageConfiguration.cPARAMETER_HTML_OUTPUT)
or (SwitchItem = I_CoverageConfiguration.cPARAMETER_VERBOSE)
or (SwitchItem = I_CoverageConfiguration.cPARAMETER_TESTEXE_EXIT_CODE) then
begin
// do nothing, because its already parsed
end
else if SwitchItem = I_CoverageConfiguration.cPARAMETER_DPROJ then
ParseDprojSwitch(AParameter)
else if SwitchItem = I_CoverageConfiguration.cPARAMETER_EXCLUDE_SOURCE_MASK then
ParseExcludeSourceMaskSwitch(AParameter)
else if SwitchItem = I_CoverageConfiguration.cPARAMETER_MODULE_NAMESPACE then
ParseModuleNameSpaceSwitch(AParameter)
else if SwitchItem = I_CoverageConfiguration.cPARAMETER_UNIT_NAMESPACE then
ParseUnitNameSpaceSwitch(AParameter)
else
raise EConfigurationException.Create('Unexpected switch:' + SwitchItem);
end;
procedure TCoverageConfiguration.ParseExecutableSwitch(var AParameter: Integer);
begin
Inc(AParameter);
FExeFileName := ParseParameter(AParameter);
if FExeFileName = '' then
raise EConfigurationException.Create('Expected parameter for executable');
// Now if we haven't yet set the mapfile, we set it by default to be the executable name +.map
if FMapFileName = '' then
FMapFileName := ChangeFileExt(FExeFileName, '.map');
end;
procedure TCoverageConfiguration.ParseMapFileSwitch(var AParameter: Integer);
begin
Inc(AParameter);
try
FMapFileName := ParseParameter(AParameter);
if FMapFileName = '' then
raise EConfigurationException.Create('Expected parameter for mapfile');
except
on EParameterIndexException do
raise EConfigurationException.Create('Expected parameter for mapfile');
end;
end;
procedure TCoverageConfiguration.ParseUnitSwitch(var AParameter: Integer);
var
UnitString: string;
begin
Inc(AParameter);
try
UnitString := ParseParameter(AParameter);
while UnitString <> '' do
begin
if FStripFileExtension then
UnitString := PathRemoveExtension(UnitString); // Ensures that we strip out .pas if it was added for some reason
AddUnitString(UnitString);
Inc(AParameter);
UnitString := ParseParameter(AParameter);
end;
if FUnitsStrLst.Count = 0 then
raise EConfigurationException.Create('Expected at least one unit');
Dec(AParameter);
except
on EParameterIndexException do
raise EConfigurationException.Create('Expected at least one unit');
end;
end;
procedure TCoverageConfiguration.AddUnitString(AUnitString: string);
begin
if Length(AUnitString) > 0 then
begin
if AUnitString[1] = cIGNORE_UNIT_PREFIX then
begin
Delete(AUnitString, 1, 1);
if Length(AUnitString) > 0 then
FExcludedUnitsStrLst.Add(AUnitString);
end
else
FUnitsStrLst.add(AUnitString);
end;
end;
procedure TCoverageConfiguration.ParseUnitFileSwitch(var AParameter: Integer);
var
UnitsFileName: string;
begin
Inc(AParameter);
try
UnitsFileName := ParseParameter(AParameter);
if UnitsFileName <> '' then
ReadUnitsFile(UnitsFileName)
else
raise EConfigurationException.Create('Expected parameter for units file name');
except
on EParameterIndexException do
raise EConfigurationException.Create('Expected parameter for units file name');
end;
end;
procedure TCoverageConfiguration.ReadUnitsFile(const AUnitsFileName: string);
var
InputFile: TextFile;
UnitLine: string;
begin
VerboseOutput('Reading units from the following file: ' + AUnitsFileName);
OpenInputFileForReading(AUnitsFileName, InputFile);
try
while not Eof(InputFile) do
begin
ReadLn(InputFile, UnitLine);
// Ensures that we strip out .pas if it was added for some reason
if FStripFileExtension then
UnitLine := PathExtractFileNameNoExt(UnitLine);
AddUnitString(UnitLine);
end;
finally
CloseFile(InputFile);
end;
end;
procedure TCoverageConfiguration.ParseExecutableParametersSwitch(var AParameter: Integer);
var
ExecutableParam: string;
begin
Inc(AParameter);
try
ExecutableParam := ParseParameter(AParameter);
while ExecutableParam <> '' do
begin
FExeParamsStrLst.add(ExecutableParam);
Inc(AParameter);
ExecutableParam := ParseParameter(AParameter);
end;
if FExeParamsStrLst.Count = 0 then
raise EConfigurationException.Create('Expected at least one executable parameter');
Dec(AParameter);
except
on EParameterIndexException do
raise EConfigurationException.Create('Expected at least one executable parameter');
end;
end;
procedure TCoverageConfiguration.ParseSourceDirectorySwitch(var AParameter: Integer);
begin
Inc(AParameter);
try
FSourceDir := ParseParameter(AParameter);
if FSourceDir = '' then
raise EConfigurationException.Create('Expected parameter for source directory');
// Source Directory should be checked first.
FSourcePathLst.Insert(0, ExpandEnvString(FSourceDir));
except
on EParameterIndexException do
raise EConfigurationException.Create('Expected parameter for source directory');
end;
end;
procedure TCoverageConfiguration.ParseSourcePathsSwitch(var AParameter: Integer);
var
SourcePathString: string;
begin
Inc(AParameter);
try
SourcePathString := ParseParameter(AParameter);
while SourcePathString <> '' do
begin
SourcePathString := MakePathAbsolute(SourcePathString, GetCurrentDir);
if DirectoryExists(SourcePathString) then
FSourcePathLst.Add(SourcePathString);
Inc(AParameter);
SourcePathString := ParseParameter(AParameter);
end;
if FSourcePathLst.Count = 0 then
raise EConfigurationException.Create('Expected at least one source path');
Dec(AParameter);
except
on EParameterIndexException do
raise EConfigurationException.Create('Expected at least one source path');
end;
end;
procedure TCoverageConfiguration.ParseSourcePathsFileSwitch(var AParameter: Integer);
var
SourcePathFileName: string;
begin
Inc(AParameter);
try
SourcePathFileName := ParseParameter(AParameter);
if SourcePathFileName <> '' then
ReadSourcePathFile(SourcePathFileName)
else
raise EConfigurationException.Create('Expected parameter for source path file name');
except
on EParameterIndexException do
raise EConfigurationException.Create('Expected parameter for source path file name');
end;
end;
procedure TCoverageConfiguration.ReadSourcePathFile(const ASourceFileName: string);
var
InputFile: TextFile;
SourcePathLine: string;
begin
OpenInputFileForReading(ASourceFileName, InputFile);
try
while (not Eof(InputFile)) do
begin
ReadLn(InputFile, SourcePathLine);
SourcePathLine := MakePathAbsolute(SourcePathLine, ASourceFileName);
if DirectoryExists(SourcePathLine) then
FSourcePathLst.Add(SourcePathLine);
end;
finally
CloseFile(InputFile);
end;
end;
function TCoverageConfiguration.MakePathAbsolute(const APath, ASourceFileName: string): string;
var
RootPath: string;
begin
Result := ExpandEnvString(APath);
if TPath.IsRelativePath(Result) then
begin
RootPath := TPath.GetDirectoryName(TPath.GetFullPath(ASourceFileName));
Result := TPath.GetFullPath(TPath.Combine(RootPath, Result));
end;
end;
procedure TCoverageConfiguration.ParseOutputDirectorySwitch(var AParameter: Integer);
begin
Inc(AParameter);
try
FOutputDir := ParseParameter(AParameter);
if FOutputDir = '' then
raise EConfigurationException.Create('Expected parameter for output directory');
except
on EParameterIndexException do
raise EConfigurationException.Create('Expected parameter for output directory')
end;
end;
procedure TCoverageConfiguration.ParseLoggingTextSwitch(var AParameter: Integer);
begin
inc(AParameter);
try
FDebugLogFileName := ParseParameter(AParameter);
if FDebugLogFileName = '' then
begin
FDebugLogFileName := I_CoverageConfiguration.cDEFULT_DEBUG_LOG_FILENAME;
Dec(AParameter);
end;
if Assigned(FLogManager) and (FDebugLogFileName <> '') then
FLogManager.AddLogger(TLoggerTextFile.Create(FDebugLogFileName));
except
on EParameterIndexException do
raise EConfigurationException.Create('Expected parameter for debug log file');
end;
end;
procedure TCoverageConfiguration.ParseWinApiLoggingSwitch(var AParameter: Integer);
begin
Inc(AParameter);
FApiLogging := True;
if Assigned(FLogManager) then
FLogManager.AddLogger(TLoggerAPI.Create);
end;
procedure TCoverageConfiguration.ParseDprojSwitch(var AParameter: Integer);
var
DProjPath: TFileName;
begin
Inc(AParameter);
try
DProjPath := ParseParameter(AParameter);
ParseDProj(DProjPath);
except
on EParameterIndexException do
raise EConfigurationException.Create('Expected parameter for project file');
end;
end;
function TCoverageConfiguration.GetCurrentConfig(const Project: IXMLNode): string;
var
Node: IXMLNode;
CurrentConfigNode: IXMLNode;
begin
Assert(Assigned(Project));
Result := '';
Node := Project.ChildNodes.Get(0);
if (Node.LocalName = 'PropertyGroup') then
begin
CurrentConfigNode := Node.ChildNodes.FindNode('Config');
if CurrentConfigNode <> nil then
Result := CurrentConfigNode.Text;
end;
end;
function TCoverageConfiguration.GetExeOutputFromDProj(const Project: IXMLNode; const ProjectName: TFileName): string;
var
CurrentConfig: string;
CurrentPlatform: string;
DCC_ExeOutputNode: IXMLNode;
DCC_ExeOutput: string;
GroupIndex: Integer;
Node: IXMLNode;
begin
Result := '';
Assert(Assigned(Project));
CurrentConfig := GetCurrentConfig(Project);
{$IFDEF WIN64}
CurrentPlatform := 'Win64';
{$ELSE}
CurrentPlatform := 'Win32';
{$ENDIF}
for GroupIndex := 0 to Project.ChildNodes.Count - 1 do
begin
Node := Project.ChildNodes.Get(GroupIndex);
if (Node.LocalName = 'PropertyGroup')
and Node.HasAttribute('Condition')
and (
(Node.Attributes['Condition'] = '''$(Base)''!=''''')
or (Node.Attributes['Condition'] = '''$(Basis)''!=''''')
) then
begin
if CurrentConfig <> '' then
begin
DCC_ExeOutputNode := Node.ChildNodes.FindNode('DCC_ExeOutput');
if DCC_ExeOutputNode <> nil then
begin
DCC_ExeOutput := DCC_ExeOutputNode.Text;
DCC_ExeOutput := StringReplace(DCC_ExeOutput, '$(Platform)', CurrentPlatform, [rfReplaceAll, rfIgnoreCase]);
DCC_ExeOutput := StringReplace(DCC_ExeOutput, '$(Config)', CurrentConfig, [rfReplaceAll, rfIgnoreCase]);
Result := IncludeTrailingPathDelimiter(DCC_ExeOutput) + ChangeFileExt(ExtractFileName(ProjectName), '.exe');
end
else
Result := ChangeFileExt(ProjectName, '.exe');
end;
end;
end;
end;
procedure TCoverageConfiguration.ParseDProj(const DProjFilename: TFileName);
var
Document: IXMLDocument;
ItemGroup: IXMLNode;
Node: IXMLNode;
Project: IXMLNode;
Unitname: string;
I: Integer;
RootPath: TFileName;
SourcePath: TFileName;
ExeFileName: TFileName;
begin
RootPath := ExtractFilePath(TPath.GetFullPath(DProjFilename));
Document := TXMLDocument.Create(nil);
Document.LoadFromFile(DProjFilename);
Project := Document.ChildNodes.FindNode('Project');
if Project <> nil then
begin
ExeFileName := GetExeOutputFromDProj(Project, DProjFilename);
if ExeFileName <> '' then
begin
if FExeFileName = '' then
FExeFileName := TPath.GetFullPath(TPath.Combine(RootPath, ExeFileName));
if FMapFileName = '' then
FMapFileName := ChangeFileExt(FExeFileName, '.map');
end;
ItemGroup := Project.ChildNodes.FindNode('ItemGroup');
if ItemGroup <> nil then
begin
FLoadingFromDProj := True;
for I := 0 to ItemGroup.ChildNodes.Count - 1 do
begin
Node := ItemGroup.ChildNodes.Get(I);
if Node.LocalName = 'DCCReference' then
begin
Unitname := TPath.GetFullPath(TPath.Combine(RootPath, Node.Attributes['Include']));
SourcePath := TPath.GetDirectoryName(Unitname);
if FSourcePathLst.IndexOf(SourcePath) = -1 then
FSourcePathLst.Add(SourcePath);
if FDProjUnitsLst.IndexOf(UnitName) = -1 then
FDProjUnitsLst.Add(UnitName);
end;
end;
end;
end;
end;
procedure TCoverageConfiguration.ParseExcludeSourceMaskSwitch(var AParameter: Integer);
var
SourcePathString: string;
begin
Inc(AParameter);
try
SourcePathString := ParseParameter(AParameter);
while SourcePathString <> '' do
begin
FExcludeSourceMaskLst.Add(ReplaceStr(SourcePathString, '/', TPath.DirectorySeparatorChar));
Inc(AParameter);
SourcePathString := ParseParameter(AParameter);
end;
if FExcludeSourceMaskLst.Count = 0 then
raise EConfigurationException.Create('Expected at least one exclude source mask');
Dec(AParameter);
except
on EParameterIndexException do
raise EConfigurationException.Create('Expected at least one exclude source mask');
end;
end;
procedure TCoverageConfiguration.ParseModuleNameSpaceSwitch(var AParameter: Integer);
var
ModuleNameSpace: TModuleNameSpace;
ModuleName: string;
begin
Inc(AParameter);
try
ModuleName := ParseParameter(AParameter);
ModuleNameSpace := TModuleNameSpace.Create(ModuleName);
Inc(AParameter);
ModuleName := ParseParameter(AParameter);
while ModuleName <> '' do
begin
ModuleNameSpace.AddModule(ModuleName);
Inc(AParameter);
ModuleName := ParseParameter(AParameter);
end;
if ModuleNameSpace.Count = 0 then
begin
ModuleNameSpace.Free;
raise EConfigurationException.Create('Expected at least one module');
end;
FModuleNameSpaces.Add(ModuleNameSpace);
Dec(AParameter);
except
on EParameterIndexException do
raise EConfigurationException.Create('Expected at least one module');
end;
end;
procedure TCoverageConfiguration.ParseUnitNameSpaceSwitch(var AParameter: Integer);
var
UnitNameSpace: TUnitNameSpace;
ModuleName: string;
begin
Inc(AParameter);
try
ModuleName := ParseParameter(AParameter);
UnitNameSpace := TUnitNameSpace.Create(ModuleName);
Inc(AParameter);
ModuleName := ParseParameter(AParameter);
while ModuleName <> '' do
begin
UnitNameSpace.AddUnit(ModuleName);
Inc(AParameter);
ModuleName := ParseParameter(AParameter);
end;
if UnitNameSpace.Count = 0 then
begin
UnitNameSpace.Free;
raise EConfigurationException.Create('Expected at least one module');
end;
FUnitNameSpaces.Add(UnitNameSpace);
Dec(AParameter);
except
on EParameterIndexException do
raise EConfigurationException.Create('Expected at least one module');
end;
end;
procedure TCoverageConfiguration.ParseLineCountSwitch(var AParameter: Integer);
var
ParsedParameter: string;
begin
Inc(AParameter);
ParsedParameter := ParseParameter(AParameter);
if ParsedParameter.StartsWith('-') then // This is a switch, not a number
begin
Dec(AParameter);
end
else
begin
{$IFDEF WIN32}
FLineCountLimit := StrToIntDef(ParsedParameter, 0);
{$ELSE}
FLineCountLimit := 0;
{$ENDIF}
end;
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.