digesttestreport.pp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. unit DigestTestReport;
  2. {$mode objfpc}{$H+}
  3. interface
  4. uses
  5. classes, SysUtils, fpcunit, fpcunitreport, testutils{, tresults};
  6. { ---------------------------------------------------------------------------- }
  7. { This section is copy-pasted from the tresults unit of the testsuite }
  8. { because it is not possible to add a dependency on the testsuite }
  9. { ---------------------------------------------------------------------------- }
  10. const
  11. failed_to_compile = 'Failed to compile ';
  12. success_compilation_failed = 'Success, compilation failed ';
  13. failed_compilation_successful = 'Failed, compilation successful ';
  14. successfully_compiled = 'Successfully compiled ';
  15. failed_to_run = 'Failed to run ';
  16. successfully_run = 'Successfully run ';
  17. skipping_graph_test = 'Skipping test because it uses graph ';
  18. skipping_interactive_test = 'Skipping test because it is interactive ';
  19. skipping_known_bug = 'Skipping test because it is a known bug ';
  20. skipping_compiler_version_too_low = 'Skipping test because compiler version too low ';
  21. skipping_compiler_version_too_high = 'Skipping test because compiler version too high ';
  22. skipping_other_cpu = 'Skipping test because for other cpu ';
  23. skipping_other_target = 'Skipping test because for other target ';
  24. skipping_run_unit = 'Skipping test run because it is a unit ';
  25. skipping_run_test = 'Skipping run test ';
  26. known_problem = ' known problem: ';
  27. line_separation = '>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>';
  28. ResLogfile : string[32] = 'log';
  29. Type
  30. TTestStatus = (
  31. stFailedToCompile,
  32. stSuccessCompilationFailed,
  33. stFailedCompilationsuccessful,
  34. stSuccessfullyCompiled,
  35. stFailedToRun,
  36. stKnownRunProblem,
  37. stSuccessFullyRun,
  38. stSkippingGraphTest,
  39. stSkippingInteractiveTest,
  40. stSkippingKnownBug,
  41. stSkippingCompilerVersionTooLow,
  42. stSkippingCompilerVersionTooHigh,
  43. stSkippingOtherCpu,
  44. stSkippingOtherTarget,
  45. stskippingRunUnit,
  46. stskippingRunTest
  47. );
  48. const
  49. StatusText : Array[TTestStatus] of String = (
  50. failed_to_compile,
  51. success_compilation_failed,
  52. failed_compilation_successful ,
  53. successfully_compiled ,
  54. failed_to_run ,
  55. known_problem ,
  56. successfully_run ,
  57. skipping_graph_test ,
  58. skipping_interactive_test ,
  59. skipping_known_bug ,
  60. skipping_compiler_version_too_low,
  61. skipping_compiler_version_too_high,
  62. skipping_other_cpu ,
  63. skipping_other_target ,
  64. skipping_run_unit ,
  65. skipping_run_test
  66. );
  67. { ---------------------------------------------------------------------------- }
  68. { End of the code from tresults from the testsuite }
  69. { ---------------------------------------------------------------------------- }
  70. type
  71. { TDigestResultsWriter }
  72. TDigestResultsWriter = class(TCustomResultsWriter)
  73. private
  74. FTestResult : TTestStatus;
  75. FOutputDir : String;
  76. FHostName : String;
  77. FComment : String;
  78. FCategory : String;
  79. FRelSrcDir: string;
  80. procedure CreateTar;
  81. public
  82. {ITestListener}
  83. procedure AddFailure(ATest: TTest; AFailure: TTestFailure); override;
  84. procedure AddError(ATest: TTest; AError: TTestFailure); override;
  85. procedure StartTest(ATest: TTest); override;
  86. procedure EndTest(ATest: TTest); override;
  87. procedure StartTestSuite(ATestSuite: TTestSuite); override;
  88. procedure EndTestSuite(ATestSuite: TTestSuite); override;
  89. property Comment: string read FComment write FComment;
  90. property Category: string read FCategory write FCategory;
  91. property RelSrcDir: string read FRelSrcDir write FRelSrcDir;
  92. end;
  93. implementation
  94. uses LibTar,
  95. {$IFDEF UNIX}
  96. UnixType,Unix,BaseUnix,
  97. {$ENDIF}
  98. {$IFDEF MSWINDOWS}
  99. windows,
  100. {$ENDIF}
  101. zstream;
  102. Function PathExists (Const F : String) : Boolean;
  103. {
  104. Returns True if the file exists, False if not.
  105. }
  106. Var
  107. info : Tsearchrec;
  108. begin
  109. PathExists:=(FindFirst (F,faAnyFile,Info)=0) and ((Info.Attr and faDirectory)=faDirectory);
  110. sysutils.FindClose (Info);
  111. end;
  112. function CompilerFullTarget:string;
  113. begin
  114. CompilerFullTarget:={$I %FPCTARGETCPU%}+'-'+{$I %FPCTARGETOS%};
  115. end;
  116. procedure AddLog(const logfile,s:string);
  117. var
  118. t : text;
  119. begin
  120. assign(t,logfile);
  121. {$I-}
  122. append(t);
  123. {$I+}
  124. if ioresult<>0 then
  125. begin
  126. {$I-}
  127. rewrite(t);
  128. {$I+}
  129. if ioresult<>0 then
  130. begin
  131. writeln('Can''t append to '+logfile);
  132. exit;
  133. end;
  134. end;
  135. writeln(t,s);
  136. close(t);
  137. end;
  138. function SplitPath(const s:string):string;
  139. var
  140. i : longint;
  141. begin
  142. i:=Length(s);
  143. while (i>0) and not(s[i] in ['/','\'{$IFDEF MACOS},':'{$ENDIF}]) do
  144. dec(i);
  145. SplitPath:=Copy(s,1,i);
  146. end;
  147. procedure mkdirtree(const s:string);
  148. var
  149. hs : string;
  150. begin
  151. if s='' then
  152. exit;
  153. if s[length(s)] in ['\','/'{$IFDEF MACOS},':'{$ENDIF}] then
  154. hs:=Copy(s,1,length(s)-1)
  155. else
  156. hs:=s;
  157. if not PathExists(hs) then
  158. begin
  159. { Try parent first }
  160. mkdirtree(SplitPath(hs));
  161. { make this dir }
  162. {$I-}
  163. mkdir(s);
  164. {$I+}
  165. ioresult;
  166. end;
  167. end;
  168. { TDBResultsWriter }
  169. procedure TDigestResultsWriter.CreateTar;
  170. var TarWriter : TTarWriter;
  171. C : TGZFileStream;
  172. OldDir : String;
  173. TarFileName : String;
  174. CurrentDate : TDateTime;
  175. {$IFDEF MSWINDOWS}
  176. CFileTime : TFileTime;
  177. CSystemTime : TSystemTime;
  178. {$ENDIF}
  179. {$IFDEF UNIX}
  180. TimeVal : TTimeVal;
  181. TimeZone : TTimeZone;
  182. {$ENDIF}
  183. procedure AddTree(Const ADir : String);
  184. var d : TSearchRec;
  185. begin
  186. if FindFirst(adir+'/*',faAnyFile,d)=0 Then
  187. begin
  188. repeat
  189. if (d.Attr and faDirectory)=faDirectory then
  190. begin
  191. if (d.Name<>'.') and (d.Name<>'..') then
  192. begin
  193. TarWriter.AddDir(ADir+'/'+d.Name, CurrentDate);
  194. AddTree(ADir+'/'+d.Name);
  195. end;
  196. end
  197. else if d.Name <> TarFileName then
  198. TarWriter.AddFile (ADir+'/'+ d.Name);
  199. until findnext(d)<>0;
  200. sysutils.Findclose(d);
  201. end;
  202. end;
  203. begin
  204. TarFileName:= FHostName+'-'+FormatDateTime('yyyymmddhhmm',Now)+'.tar.gz';
  205. getdir(0,OldDir);
  206. Chdir(FOutputDir);
  207. C:=TGZFileStream.Create(TarFileName,gzOpenWrite);
  208. TarWriter := TTarWriter.Create (C);
  209. CurrentDate := Now;
  210. {$IFDEF UNIX}
  211. fpGetTimeOfDay (@TimeVal, @TimeZone);
  212. CurrentDate := CurrentDate + TimeZone.tz_minuteswest / (60 * 24);
  213. {$ENDIF}
  214. {$IFDEF MSWINDOWS}
  215. DateTimeToSystemTime(CurrentDate,CSystemTime);
  216. SystemTimeToFileTime(CSystemTime,CFileTime);
  217. LocalFileTimeToFileTime(CFileTime,CFileTime);
  218. FileTimeToSystemTime(CFileTime,CSystemTime);
  219. CurrentDate:= SystemTimeToDateTime(CSystemTime);
  220. {$ENDIF}
  221. AddTree('.');
  222. TarWriter.free;
  223. c.free;
  224. chdir(OldDir);
  225. end;
  226. procedure TDigestResultsWriter.AddFailure(ATest: TTest; AFailure: TTestFailure);
  227. begin
  228. if AFailure.IsIgnoredTest then
  229. FTestResult := stskippingRunTest
  230. else
  231. FTestResult := stFailedToRun;
  232. AddLog(FOutputDir+'/'+ATest.TestSuiteName+ '/' + ATest.TestName+'.elg',AFailure.AsString);
  233. end;
  234. procedure TDigestResultsWriter.AddError(ATest: TTest; AError: TTestFailure);
  235. begin
  236. if AError.IsIgnoredTest then
  237. FTestResult := stskippingRunTest
  238. else
  239. FTestResult := stFailedToRun;
  240. AddLog(FOutputDir+'/'+ATest.TestSuiteName+ '/' + ATest.TestName+'.elg',AError.AsString);
  241. end;
  242. procedure TDigestResultsWriter.StartTest(ATest: TTest);
  243. begin
  244. AddLog(FOutputDir+'/'+'log',StatusText[stSuccessfullyCompiled]+ATest.TestSuiteName+ '/' + ATest.TestName);
  245. FTestResult := stSuccessFullyRun;
  246. end;
  247. procedure TDigestResultsWriter.EndTest(ATest: TTest);
  248. begin
  249. AddLog(FOutputDir+'/'+'log',StatusText[FTestResult]+ATest.TestSuiteName+ '/' + ATest.TestName);
  250. end;
  251. procedure TDigestResultsWriter.StartTestSuite(ATestSuite: TTestSuite);
  252. var OldDir : String;
  253. begin
  254. if ATestSuite.TestName = '' then
  255. begin
  256. {$ifndef MACOS}
  257. FOutputDir:='output/'+{$ifdef LIMIT83FS}CompilerTarget{$else}CompilerFullTarget{$endif};
  258. {$else MACOS}
  259. FOutputDir:=':output:'+CompilerFullTarget;
  260. {$endif MACOS}
  261. end
  262. else
  263. begin
  264. getdir(0,OldDir);
  265. {$I-}
  266. chdir(FOutputDir+'/'+ATestSuite.TestName);
  267. {$I+}
  268. if IOResult<>0 then
  269. begin
  270. mkdirtree(FOutputDir+'/'+ATestSuite.TestName);
  271. end
  272. else
  273. chdir(OldDir);
  274. end;
  275. end;
  276. procedure TDigestResultsWriter.EndTestSuite(ATestSuite: TTestSuite);
  277. var DigestFileName : String;
  278. i : byte;
  279. begin
  280. if ATestSuite.TestName='' then
  281. begin
  282. DigestFileName:=FOutputDir+'/dbdigest.cfg';
  283. AddLog(DigestFileName,'OS='+{$I %FPCTARGETOS%});
  284. AddLog(DigestFileName,'CPU='+{$I %FPCTARGETCPU%});
  285. AddLog(DigestFileName,'Version='+{$I %FPCVERSION%});
  286. AddLog(DigestFileName,'LogFile=log');
  287. AddLog(DigestFileName,'Submitter='+sysutils.GetEnvironmentVariable('USER'));
  288. FHostName:=sysutils.GetEnvironmentVariable('HOSTNAME');
  289. if pos('.',FHostName)>0 then
  290. FHostName:=system.Copy(FHostName,1,pos('.',FHostName)-1);
  291. AddLog(DigestFileName,'Machine='+FHostName);
  292. AddLog(DigestFileName,'Comment='+FComment);
  293. AddLog(DigestFileName,'Category='+FCategory);
  294. AddLog(DigestFileName,'RelSrcDir='+FRelSrcDir);
  295. // Create .tar.gz file
  296. CreateTar;
  297. end;
  298. end;
  299. end.