stylize_as_junit.rb 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. #!/usr/bin/ruby
  2. #
  3. # unity_to_junit.rb
  4. #
  5. require 'fileutils'
  6. require 'optparse'
  7. require 'ostruct'
  8. require 'set'
  9. require 'pp'
  10. VERSION = 1.0
  11. class ArgvParser
  12. #
  13. # Return a structure describing the options.
  14. #
  15. def self.parse(args)
  16. # The options specified on the command line will be collected in *options*.
  17. # We set default values here.
  18. options = OpenStruct.new
  19. options.results_dir = '.'
  20. options.root_path = '.'
  21. options.out_file = 'results.xml'
  22. opts = OptionParser.new do |o|
  23. o.banner = 'Usage: unity_to_junit.rb [options]'
  24. o.separator ''
  25. o.separator 'Specific options:'
  26. o.on('-r', '--results <dir>', 'Look for Unity Results files here.') do |results|
  27. # puts "results #{results}"
  28. options.results_dir = results
  29. end
  30. o.on('-p', '--root_path <path>', 'Prepend this path to files in results.') do |root_path|
  31. options.root_path = root_path
  32. end
  33. o.on('-o', '--output <filename>', 'XML file to generate.') do |out_file|
  34. # puts "out_file: #{out_file}"
  35. options.out_file = out_file
  36. end
  37. o.separator ''
  38. o.separator 'Common options:'
  39. # No argument, shows at tail. This will print an options summary.
  40. o.on_tail('-h', '--help', 'Show this message') do
  41. puts o
  42. exit
  43. end
  44. # Another typical switch to print the version.
  45. o.on_tail('--version', 'Show version') do
  46. puts "unity_to_junit.rb version #{VERSION}"
  47. exit
  48. end
  49. end
  50. opts.parse!(args)
  51. options
  52. end # parse()
  53. end # class OptparseExample
  54. class UnityToJUnit
  55. include FileUtils::Verbose
  56. attr_reader :report, :total_tests, :failures, :ignored
  57. attr_writer :targets, :root, :out_file
  58. def initialize
  59. @report = ''
  60. @unit_name = ''
  61. end
  62. def run
  63. # Clean up result file names
  64. results = @targets.map { |target| target.tr('\\', '/') }
  65. # puts "Output File: #{@out_file}"
  66. f = File.new(@out_file, 'w')
  67. write_xml_header(f)
  68. write_suites_header(f)
  69. results.each do |result_file|
  70. lines = File.readlines(result_file).map(&:chomp)
  71. raise "Empty test result file: #{result_file}" if lines.empty?
  72. result_output = get_details(result_file, lines)
  73. tests, failures, ignored = parse_test_summary(lines)
  74. result_output[:counts][:total] = tests
  75. result_output[:counts][:failed] = failures
  76. result_output[:counts][:ignored] = ignored
  77. result_output[:counts][:passed] = (result_output[:counts][:total] - result_output[:counts][:failed] - result_output[:counts][:ignored])
  78. # use line[0] from the test output to get the test_file path and name
  79. test_file_str = lines[0].tr('\\', '/')
  80. test_file_str = test_file_str.split(':')
  81. test_file = if test_file_str.length < 2
  82. result_file
  83. else
  84. test_file_str[0] + ':' + test_file_str[1]
  85. end
  86. result_output[:source][:path] = File.dirname(test_file)
  87. result_output[:source][:file] = File.basename(test_file)
  88. # save result_output
  89. @unit_name = File.basename(test_file, '.*')
  90. write_suite_header(result_output[:counts], f)
  91. write_failures(result_output, f)
  92. write_tests(result_output, f)
  93. write_ignored(result_output, f)
  94. write_suite_footer(f)
  95. end
  96. write_suites_footer(f)
  97. f.close
  98. end
  99. def usage(err_msg = nil)
  100. puts "\nERROR: "
  101. puts err_msg if err_msg
  102. puts 'Usage: unity_to_junit.rb [options]'
  103. puts ''
  104. puts 'Specific options:'
  105. puts ' -r, --results <dir> Look for Unity Results files here.'
  106. puts ' -p, --root_path <path> Prepend this path to files in results.'
  107. puts ' -o, --output <filename> XML file to generate.'
  108. puts ''
  109. puts 'Common options:'
  110. puts ' -h, --help Show this message'
  111. puts ' --version Show version'
  112. exit 1
  113. end
  114. protected
  115. def get_details(_result_file, lines)
  116. results = results_structure
  117. lines.each do |line|
  118. line = line.tr('\\', '/')
  119. _src_file, src_line, test_name, status, msg = line.split(/:/)
  120. case status
  121. when 'IGNORE' then results[:ignores] << { test: test_name, line: src_line, message: msg }
  122. when 'FAIL' then results[:failures] << { test: test_name, line: src_line, message: msg }
  123. when 'PASS' then results[:successes] << { test: test_name, line: src_line, message: msg }
  124. end
  125. end
  126. results
  127. end
  128. def parse_test_summary(summary)
  129. raise "Couldn't parse test results: #{summary}" unless summary.find { |v| v =~ /(\d+) Tests (\d+) Failures (\d+) Ignored/ }
  130. [Regexp.last_match(1).to_i, Regexp.last_match(2).to_i, Regexp.last_match(3).to_i]
  131. end
  132. def here
  133. File.expand_path(File.dirname(__FILE__))
  134. end
  135. private
  136. def results_structure
  137. {
  138. source: { path: '', file: '' },
  139. successes: [],
  140. failures: [],
  141. ignores: [],
  142. counts: { total: 0, passed: 0, failed: 0, ignored: 0 },
  143. stdout: []
  144. }
  145. end
  146. def write_xml_header(stream)
  147. stream.puts "<?xml version='1.0' encoding='utf-8' ?>"
  148. end
  149. def write_suites_header(stream)
  150. stream.puts '<testsuites>'
  151. end
  152. def write_suite_header(counts, stream)
  153. stream.puts "\t<testsuite errors=\"0\" skipped=\"#{counts[:ignored]}\" failures=\"#{counts[:failed]}\" tests=\"#{counts[:total]}\" name=\"unity\">"
  154. end
  155. def write_failures(results, stream)
  156. result = results[:failures]
  157. result.each do |item|
  158. filename = File.join(results[:source][:path], File.basename(results[:source][:file], '.*'))
  159. stream.puts "\t\t<testcase classname=\"#{@unit_name}\" name=\"#{item[:test]}\" time=\"0\">"
  160. stream.puts "\t\t\t<failure message=\"#{item[:message]}\" type=\"Assertion\"/>"
  161. stream.puts "\t\t\t<system-err>&#xD;[File] #{filename}&#xD;[Line] #{item[:line]}&#xD;</system-err>"
  162. stream.puts "\t\t</testcase>"
  163. end
  164. end
  165. def write_tests(results, stream)
  166. result = results[:successes]
  167. result.each do |item|
  168. stream.puts "\t\t<testcase classname=\"#{@unit_name}\" name=\"#{item[:test]}\" time=\"0\" />"
  169. end
  170. end
  171. def write_ignored(results, stream)
  172. result = results[:ignores]
  173. result.each do |item|
  174. filename = File.join(results[:source][:path], File.basename(results[:source][:file], '.*'))
  175. puts "Writing ignored tests for test harness: #{filename}"
  176. stream.puts "\t\t<testcase classname=\"#{@unit_name}\" name=\"#{item[:test]}\" time=\"0\">"
  177. stream.puts "\t\t\t<skipped message=\"#{item[:message]}\" type=\"Assertion\"/>"
  178. stream.puts "\t\t\t<system-err>&#xD;[File] #{filename}&#xD;[Line] #{item[:line]}&#xD;</system-err>"
  179. stream.puts "\t\t</testcase>"
  180. end
  181. end
  182. def write_suite_footer(stream)
  183. stream.puts "\t</testsuite>"
  184. end
  185. def write_suites_footer(stream)
  186. stream.puts '</testsuites>'
  187. end
  188. end # UnityToJUnit
  189. if __FILE__ == $0
  190. # parse out the command options
  191. options = ArgvParser.parse(ARGV)
  192. # create an instance to work with
  193. utj = UnityToJUnit.new
  194. begin
  195. # look in the specified or current directory for result files
  196. targets = "#{options.results_dir.tr('\\', '/')}**/*.test*"
  197. results = Dir[targets]
  198. raise "No *.testpass, *.testfail, or *.testresults files found in '#{targets}'" if results.empty?
  199. utj.targets = results
  200. # set the root path
  201. utj.root = options.root_path
  202. # set the output XML file name
  203. # puts "Output File from options: #{options.out_file}"
  204. utj.out_file = options.out_file
  205. # run the summarizer
  206. puts utj.run
  207. rescue StandardError => e
  208. utj.usage e.message
  209. end
  210. end