unity_test_summary.rb 4.2 KB
Newer Older
M
Mark VanderVoord 已提交
1 2 3 4 5 6
# ==========================================
#   Unity Project - A Test Framework for C
#   Copyright (c) 2007 Mike Karlesky, Mark VanderVoord, Greg Williams
#   [Released under MIT License. Please refer to license.txt for details]
# ==========================================

7
# !/usr/bin/ruby
M
Mark VanderVoord 已提交
8 9 10 11 12 13 14 15 16 17 18
#
# unity_test_summary.rb
#
require 'fileutils'
require 'set'

class UnityTestSummary
  include FileUtils::Verbose

  attr_reader :report, :total_tests, :failures, :ignored

19
  def initialize(_opts = {})
M
Mark VanderVoord 已提交
20 21 22 23 24 25 26 27
    @report = ''
    @total_tests = 0
    @failures = 0
    @ignored = 0
  end

  def run
    # Clean up result file names
28
    results = @targets.map { |target| target.tr('\\', '/') }
M
Mark VanderVoord 已提交
29 30 31 32 33 34

    # Dig through each result file, looking for details on pass/fail:
    failure_output = []
    ignore_output = []

    results.each do |result_file|
35 36
      lines = File.readlines(result_file).map(&:chomp)
      if lines.empty?
M
Mark VanderVoord 已提交
37 38 39 40 41
        raise "Empty test result file: #{result_file}"
      else
        output = get_details(result_file, lines)
        failure_output << output[:failures] unless output[:failures].empty?
        ignore_output  << output[:ignores]  unless output[:ignores].empty?
42
        tests, failures, ignored = parse_test_summary(lines)
M
Mark VanderVoord 已提交
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
        @total_tests += tests
        @failures += failures
        @ignored += ignored
      end
    end

    if @ignored > 0
      @report += "\n"
      @report += "--------------------------\n"
      @report += "UNITY IGNORED TEST SUMMARY\n"
      @report += "--------------------------\n"
      @report += ignore_output.flatten.join("\n")
    end

    if @failures > 0
      @report += "\n"
      @report += "--------------------------\n"
      @report += "UNITY FAILED TEST SUMMARY\n"
      @report += "--------------------------\n"
      @report += failure_output.flatten.join("\n")
    end

    @report += "\n"
    @report += "--------------------------\n"
    @report += "OVERALL UNITY TEST SUMMARY\n"
    @report += "--------------------------\n"
    @report += "#{@total_tests} TOTAL TESTS #{@failures} TOTAL FAILURES #{@ignored} IGNORED\n"
    @report += "\n"
  end

  def set_targets(target_array)
    @targets = target_array
  end

  def set_root_path(path)
    @root = path
  end

81
  def usage(err_msg = nil)
M
Mark VanderVoord 已提交
82 83 84
    puts "\nERROR: "
    puts err_msg if err_msg
    puts "\nUsage: unity_test_summary.rb result_file_directory/ root_path/"
85 86 87 88
    puts '     result_file_directory - The location of your results files.'
    puts '                             Defaults to current directory if not specified.'
    puts '                             Should end in / if specified.'
    puts '     root_path - Helpful for producing more verbose output if using relative paths.'
M
Mark VanderVoord 已提交
89 90 91 92 93
    exit 1
  end

  protected

94 95
  def get_details(_result_file, lines)
    results = { failures: [], ignores: [], successes: [] }
M
Mark VanderVoord 已提交
96
    lines.each do |line|
97 98 99 100 101 102
      src_file, src_line, test_name, status, msg = line.split(/:/)
      line_out = (@root && (@root != 0) ? "#{@root}#{line}" : line).gsub(/\//, '\\')
      case status
      when 'IGNORE' then results[:ignores]   << line_out
      when 'FAIL'   then results[:failures]  << line_out
      when 'PASS'   then results[:successes] << line_out
M
Mark VanderVoord 已提交
103 104
      end
    end
105
    results
M
Mark VanderVoord 已提交
106 107 108 109
  end

  def parse_test_summary(summary)
    if summary.find { |v| v =~ /(\d+) Tests (\d+) Failures (\d+) Ignored/ }
110
      [Regexp.last_match(1).to_i, Regexp.last_match(2).to_i, Regexp.last_match(3).to_i]
M
Mark VanderVoord 已提交
111 112 113 114 115
    else
      raise "Couldn't parse test results: #{summary}"
    end
  end

116 117 118
  def here
    File.expand_path(File.dirname(__FILE__))
  end
M
Mark VanderVoord 已提交
119 120
end

121
if $PROGRAM_NAME == __FILE__
M
Mark VanderVoord 已提交
122

123 124 125
  # parse out the command options
  opts, args = ARGV.partition { |v| v =~ /^--\w+/ }
  opts.map! { |v| v[2..-1].to_sym }
M
Mark VanderVoord 已提交
126

127
  # create an instance to work with
M
Mark VanderVoord 已提交
128 129 130
  uts = UnityTestSummary.new(opts)

  begin
131
    # look in the specified or current directory for result files
M
Mark VanderVoord 已提交
132
    args[0] ||= './'
133
    targets = "#{ARGV[0].tr('\\', '/')}**/*.test*"
M
Mark VanderVoord 已提交
134 135 136 137
    results = Dir[targets]
    raise "No *.testpass, *.testfail, or *.testresults files found in '#{targets}'" if results.empty?
    uts.set_targets(results)

138
    # set the root path
M
Mark VanderVoord 已提交
139 140 141
    args[1] ||= Dir.pwd + '/'
    uts.set_root_path(ARGV[1])

142
    # run the summarizer
M
Mark VanderVoord 已提交
143 144 145 146 147
    puts uts.run
  rescue Exception => e
    uts.usage e.message
  end
end