unity_test_summary.rb 4.2 KB
Newer Older
G
greg-williams 已提交
1 2 3 4 5 6 7 8 9 10
#!/usr/bin/ruby
#
# unity_test_summary.rb
#
require 'fileutils'
require 'set'

class UnityTestSummary
  include FileUtils::Verbose

11 12 13 14 15 16 17 18 19
  attr_reader :report, :total_tests, :failures, :ignored
  
  def initialize
    @report = ''
    @total_tests = 0
    @failures = 0
    @ignored = 0
  end
  
G
greg-williams 已提交
20 21 22 23
  def run
    # Clean up result file names
    results = @targets.map {|target| target.gsub(/\\/,'/')}

24
    # Dig through each result file, looking for details on pass/fail:   
G
greg-williams 已提交
25 26 27 28 29 30
    failure_output = ""
    ignore_output = ""
    
    results.each do |result_file|
      lines = File.readlines(result_file).map { |line| line.chomp }
      if lines.length == 0
31
        raise "Empty test result file: #{result_file}"
G
greg-williams 已提交
32 33 34 35 36 37
      else
        summary_line = -2
        output = get_details(result_file, lines)
        failure_output += output[:failures] if !output[:failures].empty?
        ignore_output += output[:ignores] if !output[:ignores].empty?
        tests,failures,ignored = parse_test_summary(lines[summary_line])
38 39 40
        @total_tests += tests
        @failures += failures
        @ignored += ignored
G
greg-williams 已提交
41 42 43
      end
    end
    
44 45 46 47 48 49
    if @ignored > 0
      @report += "\n"
      @report += "--------------------------\n"
      @report += "UNITY IGNORED TEST SUMMARY\n"
      @report += "--------------------------\n"
      @report += ignore_output
G
greg-williams 已提交
50 51
    end
    
52 53 54 55 56 57
    if @failures > 0
      @report += "\n"
      @report += "--------------------------\n"
      @report += "UNITY FAILED TEST SUMMARY\n"
      @report += "--------------------------\n"
      @report += failure_output
G
greg-williams 已提交
58 59
    end
  
60 61 62 63 64 65
    @report += "\n"
    @report += "--------------------------\n"
    @report += "OVERALL UNITY TEST SUMMARY\n"
    @report += "--------------------------\n"
    @report += "TOTAL TESTS: #{@total_tests} TOTAL FAILURES: #{@failures} IGNORED: #{@ignored}\n"
    @report += "\n"
G
greg-williams 已提交
66
  end
67
  
G
greg-williams 已提交
68 69 70 71 72 73 74 75
  def set_targets(target_array)
    @targets = target_array
  end
  
  def set_root_path(path)
    @root = path
  end

76 77 78 79 80 81
  def usage(err_msg=nil)
    puts err_msg if err_msg
    puts "Usage: unity_test_summary.rb"
    exit 1
  end
  
G
greg-williams 已提交
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
  protected
  
  @@targets=nil
  @@path=nil
  @@root=nil

  def get_details(result_file, lines)
    fail_lines = [] # indices of lines with failures
    ignore_lines = [] # indices of lines with ignores
    lines.each_with_index do |line,i|
      if (i < (lines.length - 2) && !(line =~ /PASS$/))
        if line =~ /(^.*\.c):(\d+)/
          if line =~ /IGNORED$/
            ignore_lines << i
          else
            fail_lines << i
          end
        elsif line =~ /IGNORED$/
          ignore_lines << i        
        end
      end
    end
    
    failures = []
    fail_lines.each do |fail_line|
      if lines[fail_line] =~ /\w:/
        src_file,src_line,test_name,msg = lines[fail_line].split(/:/)
        src_file = "#{@root}#{src_file}" unless @root == nil || @root.length == 0
        detail = "#{src_file}:#{src_line}:#{test_name}:: #{msg}"
        failures << detail.gsub(/\//, "\\")
      end
    end
    if failures.length == 0
      failure_results = ""
    else
      failure_results = failures.join("\n") + "\n"
    end
    
    ignores = []
    ignore_lines.each do |ignore_line|
      if lines[ignore_line] =~ /\w:/
        src_file,src_line,test_name,msg = lines[ignore_line].split(/:/)
        src_file = "#{@root}#{src_file}" unless @root == nil || @root.length == 0
        detail = "#{src_file}:#{src_line}:#{test_name}:: #{msg}"
        ignores << detail.gsub(/\//, "\\")
      end
    end
    if ignores.length == 0
      ignore_results = ""
    else
      ignore_results = ignores.join("\n") + "\n"
    end
    
    results = {:failures => failure_results, :ignores => ignore_results}
  end
  
  def parse_test_summary(summary)
    if summary =~ /(\d+) Tests (\d+) Failures (\d+) Ignored/
      [$1.to_i,$2.to_i,$3.to_i]
    else
      raise "Couldn't parse test results: #{summary}"
    end
  end

  def here; File.expand_path(File.dirname(__FILE__)); end
  
end

if $0 == __FILE__
  script = UnityTestSummary.new
  begin
    script.run
  rescue Exception => e
    script.usage e.message
  end
end