xref: /freebsd/contrib/ntp/sntp/unity/auto/unity_test_summary.rb (revision b626f5a73a48f44a31a200291b141e1da408a2ff)
1276da39aSCy Schubert# ==========================================
2276da39aSCy Schubert#   Unity Project - A Test Framework for C
3276da39aSCy Schubert#   Copyright (c) 2007 Mike Karlesky, Mark VanderVoord, Greg Williams
4276da39aSCy Schubert#   [Released under MIT License. Please refer to license.txt for details]
5276da39aSCy Schubert# ==========================================
6276da39aSCy Schubert
7276da39aSCy Schubert#!/usr/bin/ruby
8276da39aSCy Schubert#
9276da39aSCy Schubert# unity_test_summary.rb
10276da39aSCy Schubert#
11276da39aSCy Schubertrequire 'fileutils'
12276da39aSCy Schubertrequire 'set'
13276da39aSCy Schubert
14276da39aSCy Schubertclass UnityTestSummary
15276da39aSCy Schubert  include FileUtils::Verbose
16276da39aSCy Schubert
17276da39aSCy Schubert  attr_reader :report, :total_tests, :failures, :ignored
18276da39aSCy Schubert
19*9034852cSGleb Smirnoff  def initialize(opts = {})
20276da39aSCy Schubert    @report = ''
21276da39aSCy Schubert    @total_tests = 0
22276da39aSCy Schubert    @failures = 0
23276da39aSCy Schubert    @ignored = 0
24*9034852cSGleb Smirnoff
25*9034852cSGleb Smirnoff
26276da39aSCy Schubert  end
27276da39aSCy Schubert
28276da39aSCy Schubert  def run
29276da39aSCy Schubert    # Clean up result file names
30276da39aSCy Schubert    results = @targets.map {|target| target.gsub(/\\/,'/')}
31276da39aSCy Schubert
32276da39aSCy Schubert    # Dig through each result file, looking for details on pass/fail:
33276da39aSCy Schubert    failure_output = []
34276da39aSCy Schubert    ignore_output = []
35276da39aSCy Schubert
36276da39aSCy Schubert    results.each do |result_file|
37276da39aSCy Schubert      lines = File.readlines(result_file).map { |line| line.chomp }
38276da39aSCy Schubert      if lines.length == 0
39276da39aSCy Schubert        raise "Empty test result file: #{result_file}"
40276da39aSCy Schubert      else
41276da39aSCy Schubert        output = get_details(result_file, lines)
42276da39aSCy Schubert        failure_output << output[:failures] unless output[:failures].empty?
43276da39aSCy Schubert        ignore_output  << output[:ignores]  unless output[:ignores].empty?
44276da39aSCy Schubert        tests,failures,ignored = parse_test_summary(lines)
45276da39aSCy Schubert        @total_tests += tests
46276da39aSCy Schubert        @failures += failures
47276da39aSCy Schubert        @ignored += ignored
48276da39aSCy Schubert      end
49276da39aSCy Schubert    end
50276da39aSCy Schubert
51276da39aSCy Schubert    if @ignored > 0
52276da39aSCy Schubert      @report += "\n"
53276da39aSCy Schubert      @report += "--------------------------\n"
54276da39aSCy Schubert      @report += "UNITY IGNORED TEST SUMMARY\n"
55276da39aSCy Schubert      @report += "--------------------------\n"
56276da39aSCy Schubert      @report += ignore_output.flatten.join("\n")
57276da39aSCy Schubert    end
58276da39aSCy Schubert
59276da39aSCy Schubert    if @failures > 0
60276da39aSCy Schubert      @report += "\n"
61276da39aSCy Schubert      @report += "--------------------------\n"
62276da39aSCy Schubert      @report += "UNITY FAILED TEST SUMMARY\n"
63276da39aSCy Schubert      @report += "--------------------------\n"
64276da39aSCy Schubert      @report += failure_output.flatten.join("\n")
65276da39aSCy Schubert    end
66276da39aSCy Schubert
67276da39aSCy Schubert    @report += "\n"
68276da39aSCy Schubert    @report += "--------------------------\n"
69276da39aSCy Schubert    @report += "OVERALL UNITY TEST SUMMARY\n"
70276da39aSCy Schubert    @report += "--------------------------\n"
71276da39aSCy Schubert    @report += "#{@total_tests} TOTAL TESTS #{@failures} TOTAL FAILURES #{@ignored} IGNORED\n"
72276da39aSCy Schubert    @report += "\n"
73276da39aSCy Schubert  end
74276da39aSCy Schubert
75276da39aSCy Schubert  def set_targets(target_array)
76276da39aSCy Schubert    @targets = target_array
77276da39aSCy Schubert  end
78276da39aSCy Schubert
79276da39aSCy Schubert  def set_root_path(path)
80276da39aSCy Schubert    @root = path
81276da39aSCy Schubert  end
82276da39aSCy Schubert
83276da39aSCy Schubert  def usage(err_msg=nil)
84*9034852cSGleb Smirnoff    puts "\nERROR: "
85276da39aSCy Schubert    puts err_msg if err_msg
86*9034852cSGleb Smirnoff    puts "\nUsage: unity_test_summary.rb result_file_directory/ root_path/"
87*9034852cSGleb Smirnoff    puts "     result_file_directory - The location of your results files."
88*9034852cSGleb Smirnoff    puts "                             Defaults to current directory if not specified."
89*9034852cSGleb Smirnoff    puts "                             Should end in / if specified."
90*9034852cSGleb Smirnoff    puts "     root_path - Helpful for producing more verbose output if using relative paths."
91276da39aSCy Schubert    exit 1
92276da39aSCy Schubert  end
93276da39aSCy Schubert
94276da39aSCy Schubert  protected
95276da39aSCy Schubert
96276da39aSCy Schubert  def get_details(result_file, lines)
97276da39aSCy Schubert    results = { :failures => [], :ignores => [], :successes => [] }
98276da39aSCy Schubert    lines.each do |line|
99276da39aSCy Schubert      src_file,src_line,test_name,status,msg = line.split(/:/)
100*9034852cSGleb Smirnoff      line_out = ((@root && (@root != 0)) ? "#{@root}#{line}" : line ).gsub(/\//, "\\")
101276da39aSCy Schubert      case(status)
102276da39aSCy Schubert        when 'IGNORE' then results[:ignores]   << line_out
103276da39aSCy Schubert        when 'FAIL'   then results[:failures]  << line_out
104276da39aSCy Schubert        when 'PASS'   then results[:successes] << line_out
105276da39aSCy Schubert      end
106276da39aSCy Schubert    end
107276da39aSCy Schubert    return results
108276da39aSCy Schubert  end
109276da39aSCy Schubert
110276da39aSCy Schubert  def parse_test_summary(summary)
111*9034852cSGleb Smirnoff    if summary.find { |v| v =~ /(\d+) Tests (\d+) Failures (\d+) Ignored/ }
112276da39aSCy Schubert      [$1.to_i,$2.to_i,$3.to_i]
113276da39aSCy Schubert    else
114276da39aSCy Schubert      raise "Couldn't parse test results: #{summary}"
115276da39aSCy Schubert    end
116276da39aSCy Schubert  end
117276da39aSCy Schubert
118276da39aSCy Schubert  def here; File.expand_path(File.dirname(__FILE__)); end
119276da39aSCy Schubert
120276da39aSCy Schubertend
121276da39aSCy Schubert
122276da39aSCy Schubertif $0 == __FILE__
123*9034852cSGleb Smirnoff
124*9034852cSGleb Smirnoff  #parse out the command options
125*9034852cSGleb Smirnoff  opts, args = ARGV.partition {|v| v =~ /^--\w+/}
126*9034852cSGleb Smirnoff  opts.map! {|v| v[2..-1].to_sym }
127*9034852cSGleb Smirnoff
128*9034852cSGleb Smirnoff  #create an instance to work with
129*9034852cSGleb Smirnoff  uts = UnityTestSummary.new(opts)
130*9034852cSGleb Smirnoff
131276da39aSCy Schubert  begin
132*9034852cSGleb Smirnoff    #look in the specified or current directory for result files
133*9034852cSGleb Smirnoff    args[0] ||= './'
134*9034852cSGleb Smirnoff    targets = "#{ARGV[0].gsub(/\\/, '/')}**/*.test*"
135*9034852cSGleb Smirnoff    results = Dir[targets]
136*9034852cSGleb Smirnoff    raise "No *.testpass, *.testfail, or *.testresults files found in '#{targets}'" if results.empty?
137*9034852cSGleb Smirnoff    uts.set_targets(results)
138*9034852cSGleb Smirnoff
139*9034852cSGleb Smirnoff    #set the root path
140*9034852cSGleb Smirnoff    args[1] ||= Dir.pwd + '/'
141*9034852cSGleb Smirnoff    uts.set_root_path(ARGV[1])
142*9034852cSGleb Smirnoff
143*9034852cSGleb Smirnoff    #run the summarizer
144*9034852cSGleb Smirnoff    puts uts.run
145276da39aSCy Schubert  rescue Exception => e
146*9034852cSGleb Smirnoff    uts.usage e.message
147276da39aSCy Schubert  end
148276da39aSCy Schubertend
149*9034852cSGleb Smirnoff
150