stylize_as_junit.rb 7.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
#!/usr/bin/ruby
#
# unity_to_junit.rb
#
require 'fileutils'
require 'optparse'
require 'ostruct'
require 'set'

require 'pp'

VERSION = 1.0

class ArgvParser
  #
  # Return a structure describing the options.
  #
  def self.parse(args)
    # The options specified on the command line will be collected in *options*.
    # We set default values here.
    options = OpenStruct.new
22 23 24
    options.results_dir = '.'
    options.root_path = '.'
    options.out_file = 'results.xml'
25

26 27
    opts = OptionParser.new do |o|
      o.banner = 'Usage: unity_to_junit.rb [options]'
28

29 30
      o.separator ''
      o.separator 'Specific options:'
31

32 33
      o.on('-r', '--results <dir>', 'Look for Unity Results files here.') do |results|
        # puts "results #{results}"
34 35 36
        options.results_dir = results
      end

37
      o.on('-p', '--root_path <path>', 'Prepend this path to files in results.') do |root_path|
38 39 40
        options.root_path = root_path
      end

41 42
      o.on('-o', '--output <filename>', 'XML file to generate.') do |out_file|
        # puts "out_file: #{out_file}"
43 44 45
        options.out_file = out_file
      end

46 47
      o.separator ''
      o.separator 'Common options:'
48 49

      # No argument, shows at tail.  This will print an options summary.
50 51
      o.on_tail('-h', '--help', 'Show this message') do
        puts o
52 53 54 55
        exit
      end

      # Another typical switch to print the version.
56
      o.on_tail('--version', 'Show version') do
57 58 59 60 61 62 63
        puts "unity_to_junit.rb version #{VERSION}"
        exit
      end
    end

    opts.parse!(args)
    options
64 65
  end # parse()
end # class OptparseExample
66 67 68 69

class UnityToJUnit
  include FileUtils::Verbose
  attr_reader :report, :total_tests, :failures, :ignored
70
  attr_writer :targets, :root, :out_file
71 72 73 74 75 76 77 78

  def initialize
    @report = ''
    @unit_name = ''
  end

  def run
    # Clean up result file names
79 80 81
    results = @targets.map { |target| target.tr('\\', '/') }
    # puts "Output File: #{@out_file}"
    f = File.new(@out_file, 'w')
82
    write_xml_header(f)
83
    write_suites_header(f)
84
    results.each do |result_file|
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
      lines = File.readlines(result_file).map(&:chomp)

      raise "Empty test result file: #{result_file}" if lines.empty?

      result_output = get_details(result_file, lines)
      tests, failures, ignored = parse_test_summary(lines)
      result_output[:counts][:total] = tests
      result_output[:counts][:failed] = failures
      result_output[:counts][:ignored] = ignored
      result_output[:counts][:passed] = (result_output[:counts][:total] - result_output[:counts][:failed] - result_output[:counts][:ignored])

      # use line[0] from the test output to get the test_file path and name
      test_file_str = lines[0].tr('\\', '/')
      test_file_str = test_file_str.split(':')
      test_file = if test_file_str.length < 2
                    result_file
                  else
                    test_file_str[0] + ':' + test_file_str[1]
                  end
104 105 106 107
      result_output[:source][:path] = File.dirname(test_file)
      result_output[:source][:file] = File.basename(test_file)

      # save result_output
108
      @unit_name = File.basename(test_file, '.*')
109

110 111 112 113 114
      write_suite_header(result_output[:counts], f)
      write_failures(result_output, f)
      write_tests(result_output, f)
      write_ignored(result_output, f)
      write_suite_footer(f)
115
    end
116
    write_suites_footer(f)
117 118 119
    f.close
  end

120
  def usage(err_msg = nil)
121 122
    puts "\nERROR: "
    puts err_msg if err_msg
123 124 125 126 127 128 129 130 131 132
    puts 'Usage: unity_to_junit.rb [options]'
    puts ''
    puts 'Specific options:'
    puts '    -r, --results <dir>              Look for Unity Results files here.'
    puts '    -p, --root_path <path>           Prepend this path to files in results.'
    puts '    -o, --output <filename>          XML file to generate.'
    puts ''
    puts 'Common options:'
    puts '    -h, --help                       Show this message'
    puts '        --version                    Show version'
133 134 135 136 137

    exit 1
  end

  protected
138 139 140

  def get_details(_result_file, lines)
    results = results_structure
141
    lines.each do |line|
142 143 144 145 146 147
      line = line.tr('\\', '/')
      _src_file, src_line, test_name, status, msg = line.split(/:/)
      case status
      when 'IGNORE' then results[:ignores] << { test: test_name, line: src_line, message: msg }
      when 'FAIL'   then results[:failures] << { test: test_name, line: src_line, message: msg }
      when 'PASS'   then results[:successes] << { test: test_name, line: src_line, message: msg }
148 149
      end
    end
150
    results
151 152 153
  end

  def parse_test_summary(summary)
154 155 156 157 158 159
    raise "Couldn't parse test results: #{summary}" unless summary.find { |v| v =~ /(\d+) Tests (\d+) Failures (\d+) Ignored/ }
    [Regexp.last_match(1).to_i, Regexp.last_match(2).to_i, Regexp.last_match(3).to_i]
  end

  def here
    File.expand_path(File.dirname(__FILE__))
160 161 162 163
  end

  private

164 165 166 167 168 169 170 171
  def results_structure
    {
      source: { path: '', file: '' },
      successes: [],
      failures: [],
      ignores: [],
      counts: { total: 0, passed: 0, failed: 0, ignored: 0 },
      stdout: []
172 173 174
    }
  end

175
  def write_xml_header(stream)
176 177 178
    stream.puts "<?xml version='1.0' encoding='utf-8' ?>"
  end

179 180
  def write_suites_header(stream)
    stream.puts '<testsuites>'
181 182
  end

183
  def write_suite_header(counts, stream)
184 185 186
    stream.puts "\t<testsuite errors=\"0\" skipped=\"#{counts[:ignored]}\" failures=\"#{counts[:failed]}\" tests=\"#{counts[:total]}\" name=\"unity\">"
  end

187
  def write_failures(results, stream)
188 189 190 191 192 193 194 195 196 197
    result = results[:failures]
    result.each do |item|
      filename = File.join(results[:source][:path], File.basename(results[:source][:file], '.*'))
      stream.puts "\t\t<testcase classname=\"#{@unit_name}\" name=\"#{item[:test]}\" time=\"0\">"
      stream.puts "\t\t\t<failure message=\"#{item[:message]}\" type=\"Assertion\"/>"
      stream.puts "\t\t\t<system-err>&#xD;[File] #{filename}&#xD;[Line] #{item[:line]}&#xD;</system-err>"
      stream.puts "\t\t</testcase>"
    end
  end

198
  def write_tests(results, stream)
199 200 201 202 203 204
    result = results[:successes]
    result.each do |item|
      stream.puts "\t\t<testcase classname=\"#{@unit_name}\" name=\"#{item[:test]}\" time=\"0\" />"
    end
  end

205
  def write_ignored(results, stream)
206 207 208 209 210 211 212 213 214 215 216
    result = results[:ignores]
    result.each do |item|
      filename = File.join(results[:source][:path], File.basename(results[:source][:file], '.*'))
      puts "Writing ignored tests for test harness: #{filename}"
      stream.puts "\t\t<testcase classname=\"#{@unit_name}\" name=\"#{item[:test]}\" time=\"0\">"
      stream.puts "\t\t\t<skipped message=\"#{item[:message]}\" type=\"Assertion\"/>"
      stream.puts "\t\t\t<system-err>&#xD;[File] #{filename}&#xD;[Line] #{item[:line]}&#xD;</system-err>"
      stream.puts "\t\t</testcase>"
    end
  end

217
  def write_suite_footer(stream)
218 219 220
    stream.puts "\t</testsuite>"
  end

221 222
  def write_suites_footer(stream)
    stream.puts '</testsuites>'
223
  end
224
end # UnityToJUnit
225 226

if __FILE__ == $0
227
  # parse out the command options
228 229
  options = ArgvParser.parse(ARGV)

230
  # create an instance to work with
231 232
  utj = UnityToJUnit.new
  begin
233 234
    # look in the specified or current directory for result files
    targets = "#{options.results_dir.tr('\\', '/')}**/*.test*"
235 236 237

    results = Dir[targets]
    raise "No *.testpass, *.testfail, or *.testresults files found in '#{targets}'" if results.empty?
238
    utj.targets = results
239

240 241
    # set the root path
    utj.root = options.root_path
242

243 244 245
    # set the output XML file name
    # puts "Output File from options: #{options.out_file}"
    utj.out_file = options.out_file
246

247
    # run the summarizer
248
    puts utj.run
249
  rescue StandardError => e
250 251 252
    utj.usage e.message
  end
end