ESP32-basiertes Kraftort-Suchger�t mit GPS, LED-Ring und PlatformIO-Firmware.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

254 lines
7.7KB

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