summaryrefslogtreecommitdiff
path: root/fpcsrc/packages/fcl-fpcunit/src/digesttestreport.pp
blob: 91c78bdc72d305732a6a52e388ccaa73d3dc11b9 (plain)
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
unit DigestTestReport;

{$mode objfpc}{$H+}

interface

uses
  classes, SysUtils, fpcunit, fpcunitreport, testutils{, tresults};

{ ---------------------------------------------------------------------------- }
{ This section is copy-pasted from the tresults unit of the testsuite          }
{ because it is not possible to add a dependency on the testsuite              }
{ ---------------------------------------------------------------------------- }

const
  failed_to_compile = 'Failed to compile ';
  success_compilation_failed = 'Success, compilation failed ';
  failed_compilation_successful = 'Failed, compilation successful ';
  successfully_compiled = 'Successfully compiled ';
  failed_to_run = 'Failed to run ';
  successfully_run = 'Successfully run ';
  skipping_graph_test = 'Skipping test because it uses graph ';
  skipping_interactive_test = 'Skipping test because it is interactive ';
  skipping_known_bug = 'Skipping test because it is a known bug ';
  skipping_compiler_version_too_low = 'Skipping test because compiler version too low ';
  skipping_compiler_version_too_high = 'Skipping test because compiler version too high ';
  skipping_other_cpu = 'Skipping test because for other cpu ';
  skipping_other_target = 'Skipping test because for other target ';
  skipping_run_unit = 'Skipping test run because it is a unit ';
  skipping_run_test = 'Skipping run test ';
  known_problem = ' known problem: ';
  line_separation = '>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>';

  ResLogfile  : string[32] = 'log';

Type
  TTestStatus = (
  stFailedToCompile,
  stSuccessCompilationFailed,
  stFailedCompilationsuccessful,
  stSuccessfullyCompiled,
  stFailedToRun,
  stKnownRunProblem,
  stSuccessFullyRun,
  stSkippingGraphTest,
  stSkippingInteractiveTest,
  stSkippingKnownBug,
  stSkippingCompilerVersionTooLow,
  stSkippingCompilerVersionTooHigh,
  stSkippingOtherCpu,
  stSkippingOtherTarget,
  stskippingRunUnit,
  stskippingRunTest
  );

const
  StatusText : Array[TTestStatus] of String = (
    failed_to_compile,
    success_compilation_failed,
    failed_compilation_successful ,
    successfully_compiled ,
    failed_to_run ,
    known_problem ,
    successfully_run ,
    skipping_graph_test ,
    skipping_interactive_test ,
    skipping_known_bug ,
    skipping_compiler_version_too_low,
    skipping_compiler_version_too_high,
    skipping_other_cpu ,
    skipping_other_target ,
    skipping_run_unit ,
    skipping_run_test
  );

{ ---------------------------------------------------------------------------- }
{ End of the code from tresults from the testsuite                             }
{ ---------------------------------------------------------------------------- }

type

  { TDigestResultsWriter }

  TDigestResultsWriter = class(TCustomResultsWriter)
  private
    FTestResult : TTestStatus;
    FOutputDir : String;
    FHostName : String;
    FComment : String;
    FCategory : String;
    FRelSrcDir: string;
    procedure CreateTar;
  public
  {ITestListener}
    procedure AddFailure(ATest: TTest; AFailure: TTestFailure); override;
    procedure AddError(ATest: TTest; AError: TTestFailure); override;
    procedure StartTest(ATest: TTest); override;
    procedure EndTest(ATest: TTest); override;
    procedure StartTestSuite(ATestSuite: TTestSuite); override;
    procedure EndTestSuite(ATestSuite: TTestSuite); override;
    
    property Comment: string read FComment write FComment;
    property Category: string read FCategory write FCategory;
    property RelSrcDir: string read FRelSrcDir write FRelSrcDir;
  end;
  
implementation

uses LibTar,
{$IFDEF UNIX}
     UnixType,Unix,BaseUnix,
{$ENDIF}
{$IFDEF MSWINDOWS}
     windows,
{$ENDIF}
     zstream;
     
Function PathExists (Const F : String) : Boolean;
{
  Returns True if the file exists, False if not.
}
Var
  info : Tsearchrec;
begin
  PathExists:=(FindFirst (F,faAnyFile,Info)=0) and  ((Info.Attr and faDirectory)=faDirectory);
  sysutils.FindClose (Info);
end;

function CompilerFullTarget:string;
begin
  CompilerFullTarget:={$I %FPCTARGETCPU%}+'-'+{$I %FPCTARGETOS%};
end;

procedure AddLog(const logfile,s:string);
var
  t : text;
begin
  assign(t,logfile);
  {$I-}
  append(t);
  {$I+}
  if ioresult<>0 then
    begin
    {$I-}
    rewrite(t);
    {$I+}
    if ioresult<>0 then
      begin
      writeln('Can''t append to '+logfile);
      exit;
      end;
    end;
  writeln(t,s);
  close(t);
end;

function SplitPath(const s:string):string;
var
  i : longint;
begin
  i:=Length(s);
  while (i>0) and not(s[i] in ['/','\'{$IFDEF MACOS},':'{$ENDIF}]) do
   dec(i);
  SplitPath:=Copy(s,1,i);
end;

procedure mkdirtree(const s:string);
var
  hs : string;
begin
  if s='' then
    exit;
  if s[length(s)] in ['\','/'{$IFDEF MACOS},':'{$ENDIF}] then
    hs:=Copy(s,1,length(s)-1)
  else
    hs:=s;
  if not PathExists(hs) then
    begin
      { Try parent first }
      mkdirtree(SplitPath(hs));
      { make this dir }
      {$I-}
       mkdir(s);
      {$I+}
      ioresult;
    end;
end;

{ TDBResultsWriter }

procedure TDigestResultsWriter.CreateTar;

var TarWriter : TTarWriter;
    C : TGZFileStream;
    OldDir : String;
    TarFileName : String;
    CurrentDate : TDateTime;
{$IFDEF MSWINDOWS}
    CFileTime : TFileTime;
    CSystemTime : TSystemTime;
{$ENDIF}
{$IFDEF UNIX}
    TimeVal  : TTimeVal;
    TimeZone : TTimeZone;
{$ENDIF}

  procedure AddTree(Const ADir : String);

  var d : TSearchRec;

  begin
    if FindFirst(adir+'/*',faAnyFile,d)=0 Then
      begin
        repeat
          if (d.Attr and faDirectory)=faDirectory then
            begin
            if (d.Name<>'.') and (d.Name<>'..') then
              begin
              TarWriter.AddDir(ADir+'/'+d.Name, CurrentDate);
              AddTree(ADir+'/'+d.Name);
              end;
            end
          else if d.Name <> TarFileName then
            TarWriter.AddFile (ADir+'/'+ d.Name);
        until findnext(d)<>0;
        sysutils.Findclose(d);
      end;
  end;


begin
  TarFileName:= FHostName+'-'+FormatDateTime('yyyymmddhhmm',Now)+'.tar.gz';
  getdir(0,OldDir);
  Chdir(FOutputDir);
  
  C:=TGZFileStream.Create(TarFileName,gzOpenWrite);
  TarWriter := TTarWriter.Create (C);
  CurrentDate := Now;
{$IFDEF UNIX}
  fpGetTimeOfDay (@TimeVal, @TimeZone);
  CurrentDate := CurrentDate + TimeZone.tz_minuteswest / (60 * 24);
{$ENDIF}
{$IFDEF MSWINDOWS}
  DateTimeToSystemTime(CurrentDate,CSystemTime);
  SystemTimeToFileTime(CSystemTime,CFileTime);
  LocalFileTimeToFileTime(CFileTime,CFileTime);
  FileTimeToSystemTime(CFileTime,CSystemTime);
  CurrentDate:= SystemTimeToDateTime(CSystemTime);
{$ENDIF}
  AddTree('.');

  TarWriter.free;
  c.free;
  chdir(OldDir);
end;

procedure TDigestResultsWriter.AddFailure(ATest: TTest; AFailure: TTestFailure);
begin
  if AFailure.IsIgnoredTest then
    FTestResult := stskippingRunTest
  else
    FTestResult := stFailedToRun;
  AddLog(FOutputDir+'/'+ATest.TestSuiteName+ '/' + ATest.TestName+'.elg',AFailure.AsString);
end;

procedure TDigestResultsWriter.AddError(ATest: TTest; AError: TTestFailure);
begin
  if AError.IsIgnoredTest then
    FTestResult := stskippingRunTest
  else
    FTestResult := stFailedToRun;
  AddLog(FOutputDir+'/'+ATest.TestSuiteName+ '/' + ATest.TestName+'.elg',AError.AsString);
end;

procedure TDigestResultsWriter.StartTest(ATest: TTest);
begin
  AddLog(FOutputDir+'/'+'log',StatusText[stSuccessfullyCompiled]+ATest.TestSuiteName+ '/' + ATest.TestName);
  FTestResult := stSuccessFullyRun;
end;

procedure TDigestResultsWriter.EndTest(ATest: TTest);
begin
  AddLog(FOutputDir+'/'+'log',StatusText[FTestResult]+ATest.TestSuiteName+ '/' + ATest.TestName);
end;

procedure TDigestResultsWriter.StartTestSuite(ATestSuite: TTestSuite);
var OldDir : String;
begin
  if ATestSuite.TestName = '' then
    begin
{$ifndef MACOS}
    FOutputDir:='output/'+{$ifdef LIMIT83FS}CompilerTarget{$else}CompilerFullTarget{$endif};
{$else MACOS}
    FOutputDir:=':output:'+CompilerFullTarget;
{$endif MACOS}

    end
  else
    begin
    getdir(0,OldDir);
    {$I-}
    chdir(FOutputDir+'/'+ATestSuite.TestName);
    {$I+}
    if IOResult<>0 then
      begin
      mkdirtree(FOutputDir+'/'+ATestSuite.TestName);
      end
    else
      chdir(OldDir);
    end;
end;

procedure TDigestResultsWriter.EndTestSuite(ATestSuite: TTestSuite);
var DigestFileName : String;
    i              : byte;
begin
  if ATestSuite.TestName='' then
    begin
    DigestFileName:=FOutputDir+'/dbdigest.cfg';
    AddLog(DigestFileName,'OS='+{$I %FPCTARGETOS%});
    AddLog(DigestFileName,'CPU='+{$I %FPCTARGETCPU%});
    AddLog(DigestFileName,'Version='+{$I %FPCVERSION%});
    AddLog(DigestFileName,'LogFile=log');
    AddLog(DigestFileName,'Submitter='+sysutils.GetEnvironmentVariable('USER'));
    FHostName:=sysutils.GetEnvironmentVariable('HOSTNAME');
    if pos('.',FHostName)>0 then
      FHostName:=system.Copy(FHostName,1,pos('.',FHostName)-1);
    AddLog(DigestFileName,'Machine='+FHostName);
    AddLog(DigestFileName,'Comment='+FComment);
    AddLog(DigestFileName,'Category='+FCategory);
    AddLog(DigestFileName,'RelSrcDir='+FRelSrcDir);
// Create .tar.gz file
    CreateTar;
    end;
end;

end.