fpts2junit.pp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. {
  2. This file is part of the Free Pascal test suite.
  3. Copyright (c) 2013 by Karoly Balogh <[email protected]>
  4. Copyright (c) 2013 by Viprinet Europe GmbH
  5. This program can convert Free Pascal Testsute results to JUnit
  6. results to be used (for example) with Jenkins CI suite.
  7. See the file COPYING.FPC, included in this distribution,
  8. for details about the copyright.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  12. **********************************************************************}
  13. {$MODE DELPHI}
  14. {$I+}
  15. program fpts2junit;
  16. uses
  17. classes, sysutils, strutils,
  18. DOM, XMLWrite;
  19. const
  20. MAX_XML_CHARS = 1000;
  21. LOG_SHORT = 'log';
  22. LOG_LONG = 'longlog';
  23. DEFAULT_JUNIT_XML = 'fpc_testsuite.xml';
  24. PATTERN_SUCCESS = 'Success';
  25. PATTERN_FAILED = 'Fail';
  26. PATTERN_SKIPPED = 'Skip';
  27. IE_FUBAR = 'internalerror generated';
  28. function getIndexInList(l: TStringList; className: String; caseName: String): LongInt;
  29. var
  30. line: String;
  31. filename: String;
  32. begin
  33. result:=-1;
  34. filename:=className+'/'+caseName;
  35. for line in l do
  36. if (Pos(filename, line) > 0) then
  37. begin
  38. result:=l.IndexOf(line);
  39. break;
  40. end;
  41. end;
  42. procedure convertLogFiles(testOutputPath: String; junitXMLName: String);
  43. var
  44. logShort: TStringList;
  45. logLong: TStringList;
  46. junitXML: TXMLDocument;
  47. rootNode: TDOMNode;
  48. caseNode: TDOMNode;
  49. tmpNode: TDOMNode;
  50. failed: LongInt;
  51. error: LongInt;
  52. skipped: LongInt;
  53. success: LongInt;
  54. tmpLine: String;
  55. Line: string;
  56. startIdx: LongInt;
  57. tmpString: String;
  58. className: String;
  59. caseName: String;
  60. i: Integer;
  61. begin
  62. logShort:=TStringList.Create;
  63. logLong:=TStringList.Create;
  64. // sanity check arguments
  65. testOutputPath:=IncludeTrailingPathDelimiter(ExpandFileName(testOutputPath));
  66. if not DirectoryExists(testOutputPath) then
  67. begin
  68. writeln('Path: ',testOutputPath,' is not valid.');
  69. halt(1);
  70. end;
  71. if not AnsiEndsText('.xml', junitXMLName) then
  72. junitXMLName:=DEFAULT_JUNIT_XML;
  73. // read *ALL* the logs! (ok, some of them)
  74. writeln('Reading logs from directory: ',testOutputPath);
  75. logShort.LoadFromFile(testOutputPath+LOG_SHORT);
  76. logLong.LoadFromFile(testOutputPath+LOG_LONG);
  77. junitXML:=TXMLDocument.Create;
  78. // convert
  79. failed:=0;
  80. error:=0;
  81. skipped:=0;
  82. success:=0;
  83. rootNode:=junitXML.CreateElement('testsuite');
  84. junitXML.AppendChild(rootNode);
  85. for Line in logShort do
  86. begin
  87. tmpline := Line;
  88. // this is pretty fubar in the logfile, to break the format
  89. // lets fix it up...
  90. if AnsiEndsText(IE_FUBAR, tmpLine) then
  91. begin
  92. tmpLine:=AnsiReplaceText(tmpLine, IE_FUBAR, '');
  93. end;
  94. // extract useful stuff
  95. tmpString:=ExtractWord(WordCount(tmpLine,[' '])-2,tmpLine,[' ']);
  96. className:=AnsiLeftStr(tmpString,RPos(DirectorySeparator,tmpString)-1);
  97. caseName:=ExtractWord(WordCount(tmpString,[DirectorySeparator]),tmpString,[DirectorySeparator]);
  98. // create testcase node
  99. caseNode:=junitXML.CreateElement('testcase');
  100. TDOMElement(caseNode).SetAttribute('classname',WideString(className));
  101. TDOMElement(caseNode).SetAttribute('name',WideString(caseName));
  102. rootNode.AppendChild(caseNode);
  103. if AnsiStartsText(PATTERN_FAILED, tmpLine) then
  104. begin
  105. tmpString:=TrimSet(AnsiLeftStr(tmpLine, AnsiPos(className, tmpLine)-1),[' ']);
  106. // handle compiler errors as errors, otherwise failures
  107. if AnsiPos('compile',tmpString) <> 0 then
  108. begin
  109. Inc(error);
  110. tmpNode:=junitXML.CreateElement('error');
  111. end
  112. else
  113. begin
  114. Inc(failed);
  115. tmpNode:=junitXML.CreateElement('failure');
  116. end;
  117. TDOMElement(tmpNode).SetAttribute('message',WideString(tmpString));
  118. startIdx:=getIndexInList(logLong, className, caseName);
  119. tmpString:='';
  120. while startIdx > 0 do
  121. begin
  122. tmpString:=tmpString + #10 + logLong[startIdx];
  123. Inc(startIdx);
  124. if (startIdx >= logLong.Count) or
  125. AnsiStartsText('>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>', logLong[startIdx]) then break;
  126. end;
  127. if tmpString <> '' then
  128. begin
  129. if Length(tmpString) > MAX_XML_CHARS then
  130. tmpString := '--- ' + IntToStr(Length(tmpstring) - MAX_XML_CHARS) + 'bytes cut away --- '+ #10 + Copy(tmpString, Length(tmpstring) - MAX_XML_CHARS, MAX_XML_CHARS);
  131. for i := 1 to Length(tmpString) do
  132. if tmpString[i] in [#0..#9,#11..#31] then
  133. tmpString[i] := ' ';
  134. tmpNode.AppendChild(junitXML.CreateTextNode(WideString(tmpString+#10)));
  135. end;
  136. caseNode.AppendChild(tmpNode);
  137. continue;
  138. end;
  139. if AnsiStartsText(PATTERN_SKIPPED, tmpLine) then
  140. begin
  141. Inc(skipped);
  142. caseNode.AppendChild(junitXML.CreateElement('skipped'));
  143. continue;
  144. end;
  145. if AnsiStartsText(PATTERN_SUCCESS, tmpLine) then
  146. begin
  147. Inc(success);
  148. continue;
  149. end;
  150. writeln('Unparseable line: [',tmpLine,']');
  151. Halt(1);
  152. end;
  153. // set required elements in the root node
  154. TDOMElement(rootNode).SetAttribute('errors',WideString(IntToStr(error)));
  155. TDOMElement(rootNode).SetAttribute('failures',WideString(IntToStr(failed)));
  156. TDOMElement(rootNode).SetAttribute('tests',WideString(IntToStr(logShort.Count)));
  157. TDOMElement(rootNode).SetAttribute('name','Compiler.Testsuite');
  158. TDOMElement(rootNode).SetAttribute('package','FPC');
  159. writeln('Writing results to file: ',junitXMLName);
  160. writeXMLFile(junitXML, junitXMLName);
  161. end;
  162. procedure printusage;
  163. begin
  164. writeln('Usage:');
  165. writeln(' ',ExtractFileName(ParamStr(0)),' <path_to_test_output_dir> [output.xml]');
  166. writeln(' * if no output filename is specified, "',DEFAULT_JUNIT_XML,'" will be used.');
  167. writeln(' * if specified, output filename must end with ".xml", otherwise default ');
  168. writeln(' name will be used.');
  169. halt(1);
  170. end;
  171. begin
  172. if (ParamCount < 1) or (ParamCount > 2) then
  173. printusage;
  174. convertLogFiles(ParamStr(1),ParamStr(2));
  175. end.