fpts2junit.pp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 = 10000;
  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. lastname: string;
  62. begin
  63. logShort:=TStringList.Create;
  64. logLong:=TStringList.Create;
  65. // sanity check arguments
  66. testOutputPath:=IncludeTrailingPathDelimiter(ExpandFileName(testOutputPath));
  67. if not DirectoryExists(testOutputPath) then
  68. begin
  69. writeln('Path: ',testOutputPath,' is not valid.');
  70. halt(1);
  71. end;
  72. if not AnsiEndsText('.xml', junitXMLName) then
  73. junitXMLName:=DEFAULT_JUNIT_XML;
  74. // read *ALL* the logs! (ok, some of them)
  75. writeln('Reading logs from directory: ',testOutputPath);
  76. logShort.LoadFromFile(testOutputPath+LOG_SHORT);
  77. logLong.LoadFromFile(testOutputPath+LOG_LONG);
  78. junitXML:=TXMLDocument.Create;
  79. // convert
  80. failed:=0;
  81. error:=0;
  82. skipped:=0;
  83. success:=0;
  84. lastname := '';
  85. rootNode:=junitXML.CreateElement('testsuite');
  86. junitXML.AppendChild(rootNode);
  87. for Line in logShort do
  88. begin
  89. tmpline := Line;
  90. // this is pretty fubar in the logfile, to break the format
  91. // lets fix it up...
  92. if AnsiEndsText(IE_FUBAR, tmpLine) then
  93. begin
  94. tmpLine:=AnsiReplaceText(tmpLine, IE_FUBAR, '');
  95. end;
  96. // extract useful stuff
  97. tmpString:=ExtractWord(WordCount(tmpLine,[' '])-2,tmpLine,[' ']);
  98. className:=AnsiLeftStr(tmpString,RPos(DirectorySeparator,tmpString)-1);
  99. caseName:=ExtractWord(WordCount(tmpString,[DirectorySeparator]),tmpString,[DirectorySeparator]);
  100. if LastName <> (classname + '.' + casename) then
  101. begin
  102. LastName := classname + '.' + casename;
  103. // create testcase node
  104. caseNode:=junitXML.CreateElement('testcase');
  105. if pos('../', classname) = 1 then
  106. Delete(classname, 1, 3);
  107. TDOMElement(caseNode).SetAttribute('classname',WideString(className));
  108. TDOMElement(caseNode).SetAttribute('name',WideString(caseName));
  109. rootNode.AppendChild(caseNode);
  110. end;
  111. if AnsiStartsText(PATTERN_FAILED, tmpLine) then
  112. begin
  113. tmpString:=TrimSet(AnsiLeftStr(tmpLine, AnsiPos(className, tmpLine)-1),[' ']);
  114. // handle compiler errors as errors, otherwise failures
  115. if AnsiPos('compile',tmpString) <> 0 then
  116. begin
  117. Inc(error);
  118. tmpNode:=junitXML.CreateElement('error');
  119. end
  120. else
  121. begin
  122. Inc(failed);
  123. tmpNode:=junitXML.CreateElement('failure');
  124. end;
  125. TDOMElement(tmpNode).SetAttribute('message',WideString(tmpString));
  126. startIdx:=getIndexInList(logLong, className, caseName);
  127. tmpString:='';
  128. while startIdx > 0 do
  129. begin
  130. tmpString:=tmpString + #10 + logLong[startIdx];
  131. Inc(startIdx);
  132. if (startIdx >= logLong.Count) or
  133. AnsiStartsText('>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>', logLong[startIdx]) then break;
  134. end;
  135. if tmpString <> '' then
  136. begin
  137. if Length(tmpString) > MAX_XML_CHARS then
  138. tmpString := '--- ' + IntToStr(Length(tmpstring) - MAX_XML_CHARS) + 'bytes cut away --- '+ #10 + Copy(tmpString, Length(tmpstring) - MAX_XML_CHARS, MAX_XML_CHARS);
  139. for i := 1 to Length(tmpString) do
  140. if tmpString[i] in [#0..#9,#11..#31] then
  141. tmpString[i] := ' ';
  142. tmpNode.AppendChild(junitXML.CreateTextNode(WideString(tmpString+#10)));
  143. end;
  144. caseNode.AppendChild(tmpNode);
  145. continue;
  146. end;
  147. if AnsiStartsText(PATTERN_SKIPPED, tmpLine) then
  148. begin
  149. Inc(skipped);
  150. caseNode.AppendChild(junitXML.CreateElement('skipped'));
  151. continue;
  152. end;
  153. if AnsiStartsText(PATTERN_SUCCESS, tmpLine) then
  154. begin
  155. Inc(success);
  156. continue;
  157. end;
  158. writeln('Unparseable line: [',tmpLine,']');
  159. Halt(1);
  160. end;
  161. // set required elements in the root node
  162. TDOMElement(rootNode).SetAttribute('errors',WideString(IntToStr(error)));
  163. TDOMElement(rootNode).SetAttribute('failures',WideString(IntToStr(failed)));
  164. TDOMElement(rootNode).SetAttribute('tests',WideString(IntToStr(logShort.Count)));
  165. TDOMElement(rootNode).SetAttribute('name','Compiler.Testsuite');
  166. TDOMElement(rootNode).SetAttribute('package','FPC');
  167. writeln('Writing results to file: ',junitXMLName);
  168. writeXMLFile(junitXML, junitXMLName);
  169. end;
  170. procedure printusage;
  171. begin
  172. writeln('Usage:');
  173. writeln(' ',ExtractFileName(ParamStr(0)),' <path_to_test_output_dir> [output.xml]');
  174. writeln(' * if no output filename is specified, "',DEFAULT_JUNIT_XML,'" will be used.');
  175. writeln(' * if specified, output filename must end with ".xml", otherwise default ');
  176. writeln(' name will be used.');
  177. halt(1);
  178. end;
  179. begin
  180. if (ParamCount < 1) or (ParamCount > 2) then
  181. printusage;
  182. convertLogFiles(ParamStr(1),ParamStr(2));
  183. end.