unity_test_summary.rb 4.3 KB
Newer Older
1 2 3 4
# ==========================================
#   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]
M
Mark VanderVoord 已提交
5
# ==========================================
6

G
greg-williams 已提交
7 8 9 10 11 12 13 14 15 16
#!/usr/bin/ruby
#
# unity_test_summary.rb
#
require 'fileutils'
require 'set'

class UnityTestSummary
  include FileUtils::Verbose

17
  attr_reader :report, :total_tests, :failures, :ignored
M
Mark VanderVoord 已提交
18

19
  def initialize(opts = {})
20 21 22 23
    @report = ''
    @total_tests = 0
    @failures = 0
    @ignored = 0
24 25


26
  end
M
Mark VanderVoord 已提交
27

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

    # Dig through each result file, looking for details on pass/fail:
33 34
    failure_output = []
    ignore_output = []
M
Mark VanderVoord 已提交
35

G
greg-williams 已提交
36 37 38
    results.each do |result_file|
      lines = File.readlines(result_file).map { |line| line.chomp }
      if lines.length == 0
39
        raise "Empty test result file: #{result_file}"
G
greg-williams 已提交
40 41
      else
        output = get_details(result_file, lines)
42 43
        failure_output << output[:failures] unless output[:failures].empty?
        ignore_output  << output[:ignores]  unless output[:ignores].empty?
44
        tests,failures,ignored = parse_test_summary(lines)
45 46 47
        @total_tests += tests
        @failures += failures
        @ignored += ignored
G
greg-williams 已提交
48 49
      end
    end
M
Mark VanderVoord 已提交
50

51 52 53 54 55
    if @ignored > 0
      @report += "\n"
      @report += "--------------------------\n"
      @report += "UNITY IGNORED TEST SUMMARY\n"
      @report += "--------------------------\n"
56
      @report += ignore_output.flatten.join("\n")
G
greg-williams 已提交
57
    end
M
Mark VanderVoord 已提交
58

59 60 61 62 63
    if @failures > 0
      @report += "\n"
      @report += "--------------------------\n"
      @report += "UNITY FAILED TEST SUMMARY\n"
      @report += "--------------------------\n"
64
      @report += failure_output.flatten.join("\n")
G
greg-williams 已提交
65
    end
M
Mark VanderVoord 已提交
66

67 68 69 70
    @report += "\n"
    @report += "--------------------------\n"
    @report += "OVERALL UNITY TEST SUMMARY\n"
    @report += "--------------------------\n"
71
    @report += "#{@total_tests} TOTAL TESTS #{@failures} TOTAL FAILURES #{@ignored} IGNORED\n"
72
    @report += "\n"
G
greg-williams 已提交
73
  end
M
Mark VanderVoord 已提交
74

G
greg-williams 已提交
75 76 77
  def set_targets(target_array)
    @targets = target_array
  end
M
Mark VanderVoord 已提交
78

G
greg-williams 已提交
79 80 81 82
  def set_root_path(path)
    @root = path
  end

83
  def usage(err_msg=nil)
84
    puts "\nERROR: "
85
    puts err_msg if err_msg
86 87
    puts "\nUsage: unity_test_summary.rb result_file_directory/ root_path/"
    puts "     result_file_directory - The location of your results files."
88 89 90
    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."
91 92
    exit 1
  end
M
Mark VanderVoord 已提交
93

G
greg-williams 已提交
94 95 96
  protected

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

G
greg-williams 已提交
110
  def parse_test_summary(summary)
111
    if summary.find { |v| v =~ /(\d+) Tests (\d+) Failures (\d+) Ignored/ }
G
greg-williams 已提交
112 113 114 115 116 117 118
      [$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
M
Mark VanderVoord 已提交
119

G
greg-williams 已提交
120 121 122
end

if $0 == __FILE__
123 124 125 126 127 128 129 130

  #parse out the command options
  opts, args = ARGV.partition {|v| v =~ /^--\w+/}
  opts.map! {|v| v[2..-1].to_sym }

  #create an instance to work with
  uts = UnityTestSummary.new(opts)

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

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

143 144
    #run the summarizer
    puts uts.run
G
greg-williams 已提交
145
  rescue Exception => e
146
    uts.usage e.message
G
greg-williams 已提交
147 148
  end
end