unity_test_summary.rb 4.1 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 20 21 22 23 24
  def initialize
    @report = ''
    @total_tests = 0
    @failures = 0
    @ignored = 0
  end
M
Mark VanderVoord 已提交
25

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

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

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

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

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

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

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

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

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

G
greg-williams 已提交
92 93 94
  protected

  def get_details(result_file, lines)
95 96 97
    results = { :failures => [], :ignores => [], :successes => [] }
    lines.each do |line|
      src_file,src_line,test_name,status,msg = line.split(/:/)
98
      line_out = ((@root and (@root != 0)) ? "#{@root}#{line}" : line ).gsub(/\//, "\\")
99 100 101 102
      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 已提交
103 104
      end
    end
105
    return results
G
greg-williams 已提交
106
  end
M
Mark VanderVoord 已提交
107

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

G
greg-williams 已提交
118 119 120
end

if $0 == __FILE__
121
  uts = UnityTestSummary.new
G
greg-williams 已提交
122
  begin
123 124 125 126 127 128
    #look in the specified or current directory for result files
    ARGV[0] ||= './'
    targets = "#{ARGV[0].gsub(/\\/, '/')}*.test*"
    results = Dir[targets]
    raise "No *.testpass or *.testfail files found in '#{targets}'" if results.empty?
    uts.set_targets(results)
M
Mark VanderVoord 已提交
129

130 131 132
    #set the root path
    ARGV[1] ||= File.expand_path(File.dirname(__FILE__)) + '/'
    uts.set_root_path(ARGV[1])
M
Mark VanderVoord 已提交
133

134 135
    #run the summarizer
    puts uts.run
G
greg-williams 已提交
136
  rescue Exception => e
137
    uts.usage e.message
G
greg-williams 已提交
138 139
  end
end