fpts2junit.pp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. if pos('../', classname) = 1 then
  101. Delete(classname, 1, 3);
  102. TDOMElement(caseNode).SetAttribute('classname',WideString(className));
  103. TDOMElement(caseNode).SetAttribute('name',WideString(caseName));
  104. rootNode.AppendChild(caseNode);
  105. if AnsiStartsText(PATTERN_FAILED, tmpLine) then
  106. begin
  107. tmpString:=TrimSet(AnsiLeftStr(tmpLine, AnsiPos(className, tmpLine)-1),[' ']);
  108. // handle compiler errors as errors, otherwise failures
  109. if AnsiPos('compile',tmpString) <> 0 then
  110. begin
  111. Inc(error);
  112. tmpNode:=junitXML.CreateElement('error');
  113. end
  114. else
  115. begin
  116. Inc(failed);
  117. tmpNode:=junitXML.CreateElement('failure');
  118. end;
  119. TDOMElement(tmpNode).SetAttribute('message',WideString(tmpString));
  120. startIdx:=getIndexInList(logLong, className, caseName);
  121. tmpString:='';
  122. while startIdx > 0 do
  123. begin
  124. tmpString:=tmpString + #10 + logLong[startIdx];
  125. Inc(startIdx);
  126. if (startIdx >= logLong.Count) or
  127. AnsiStartsText('>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>', logLong[startIdx]) then break;
  128. end;
  129. if tmpString <> '' then
  130. begin
  131. if Length(tmpString) > MAX_XML_CHARS then
  132. tmpString := '--- ' + IntToStr(Length(tmpstring) - MAX_XML_CHARS) + 'bytes cut away --- '+ #10 + Copy(tmpString, Length(tmpstring) - MAX_XML_CHARS, MAX_XML_CHARS);
  133. for i := 1 to Length(tmpString) do
  134. if tmpString[i] in [#0..#9,#11..#31] then
  135. tmpString[i] := ' ';
  136. tmpNode.AppendChild(junitXML.CreateTextNode(WideString(tmpString+#10)));
  137. end;
  138. caseNode.AppendChild(tmpNode);
  139. continue;
  140. end;
  141. if AnsiStartsText(PATTERN_SKIPPED, tmpLine) then
  142. begin
  143. Inc(skipped);
  144. caseNode.AppendChild(junitXML.CreateElement('skipped'));
  145. continue;
  146. end;
  147. if AnsiStartsText(PATTERN_SUCCESS, tmpLine) then
  148. begin
  149. Inc(success);
  150. continue;
  151. end;
  152. writeln('Unparseable line: [',tmpLine,']');
  153. Halt(1);
  154. end;
  155. // set required elements in the root node
  156. TDOMElement(rootNode).SetAttribute('errors',WideString(IntToStr(error)));
  157. TDOMElement(rootNode).SetAttribute('failures',WideString(IntToStr(failed)));
  158. TDOMElement(rootNode).SetAttribute('tests',WideString(IntToStr(logShort.Count)));
  159. TDOMElement(rootNode).SetAttribute('name','Compiler.Testsuite');
  160. TDOMElement(rootNode).SetAttribute('package','FPC');
  161. writeln('Writing results to file: ',junitXMLName);
  162. writeXMLFile(junitXML, junitXMLName);
  163. end;
  164. procedure printusage;
  165. begin
  166. writeln('Usage:');
  167. writeln(' ',ExtractFileName(ParamStr(0)),' <path_to_test_output_dir> [output.xml]');
  168. writeln(' * if no output filename is specified, "',DEFAULT_JUNIT_XML,'" will be used.');
  169. writeln(' * if specified, output filename must end with ".xml", otherwise default ');
  170. writeln(' name will be used.');
  171. halt(1);
  172. end;
  173. begin
  174. if (ParamCount < 1) or (ParamCount > 2) then
  175. printusage;
  176. convertLogFiles(ParamStr(1),ParamStr(2));
  177. end.