xref: /freebsd/contrib/ntp/sntp/unity/auto/unity_test_summary.rb (revision 13ea0450a9c8742119d36f3bf8f47accdce46e54)
1# ==========================================
2#   Unity Project - A Test Framework for C
3#   Copyright (c) 2007 Mike Karlesky, Mark VanderVoord, Greg Williams
4#   [Released under MIT License. Please refer to license.txt for details]
5# ==========================================
6
7#!/usr/bin/ruby
8#
9# unity_test_summary.rb
10#
11require 'fileutils'
12require 'set'
13
14class UnityTestSummary
15  include FileUtils::Verbose
16
17  attr_reader :report, :total_tests, :failures, :ignored
18
19  def initialize(opts = {})
20    @report = ''
21    @total_tests = 0
22    @failures = 0
23    @ignored = 0
24
25
26  end
27
28  def run
29    # Clean up result file names
30    results = @targets.map {|target| target.gsub(/\\/,'/')}
31
32    # Dig through each result file, looking for details on pass/fail:
33    failure_output = []
34    ignore_output = []
35
36    results.each do |result_file|
37      lines = File.readlines(result_file).map { |line| line.chomp }
38      if lines.length == 0
39        raise "Empty test result file: #{result_file}"
40      else
41        output = get_details(result_file, lines)
42        failure_output << output[:failures] unless output[:failures].empty?
43        ignore_output  << output[:ignores]  unless output[:ignores].empty?
44        tests,failures,ignored = parse_test_summary(lines)
45        @total_tests += tests
46        @failures += failures
47        @ignored += ignored
48      end
49    end
50
51    if @ignored > 0
52      @report += "\n"
53      @report += "--------------------------\n"
54      @report += "UNITY IGNORED TEST SUMMARY\n"
55      @report += "--------------------------\n"
56      @report += ignore_output.flatten.join("\n")
57    end
58
59    if @failures > 0
60      @report += "\n"
61      @report += "--------------------------\n"
62      @report += "UNITY FAILED TEST SUMMARY\n"
63      @report += "--------------------------\n"
64      @report += failure_output.flatten.join("\n")
65    end
66
67    @report += "\n"
68    @report += "--------------------------\n"
69    @report += "OVERALL UNITY TEST SUMMARY\n"
70    @report += "--------------------------\n"
71    @report += "#{@total_tests} TOTAL TESTS #{@failures} TOTAL FAILURES #{@ignored} IGNORED\n"
72    @report += "\n"
73  end
74
75  def set_targets(target_array)
76    @targets = target_array
77  end
78
79  def set_root_path(path)
80    @root = path
81  end
82
83  def usage(err_msg=nil)
84    puts "\nERROR: "
85    puts err_msg if err_msg
86    puts "\nUsage: unity_test_summary.rb result_file_directory/ root_path/"
87    puts "     result_file_directory - The location of your results files."
88    puts "                             Defaults to current directory if not specified."
89    puts "                             Should end in / if specified."
90    puts "     root_path - Helpful for producing more verbose output if using relative paths."
91    exit 1
92  end
93
94  protected
95
96  def get_details(result_file, lines)
97    results = { :failures => [], :ignores => [], :successes => [] }
98    lines.each do |line|
99      src_file,src_line,test_name,status,msg = line.split(/:/)
100      line_out = ((@root && (@root != 0)) ? "#{@root}#{line}" : line ).gsub(/\//, "\\")
101      case(status)
102        when 'IGNORE' then results[:ignores]   << line_out
103        when 'FAIL'   then results[:failures]  << line_out
104        when 'PASS'   then results[:successes] << line_out
105      end
106    end
107    return results
108  end
109
110  def parse_test_summary(summary)
111    if summary.find { |v| v =~ /(\d+) Tests (\d+) Failures (\d+) Ignored/ }
112      [$1.to_i,$2.to_i,$3.to_i]
113    else
114      raise "Couldn't parse test results: #{summary}"
115    end
116  end
117
118  def here; File.expand_path(File.dirname(__FILE__)); end
119
120end
121
122if $0 == __FILE__
123
124  #parse out the command options
125  opts, args = ARGV.partition {|v| v =~ /^--\w+/}
126  opts.map! {|v| v[2..-1].to_sym }
127
128  #create an instance to work with
129  uts = UnityTestSummary.new(opts)
130
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)
138
139    #set the root path
140    args[1] ||= Dir.pwd + '/'
141    uts.set_root_path(ARGV[1])
142
143    #run the summarizer
144    puts uts.run
145  rescue Exception => e
146    uts.usage e.message
147  end
148end
149
150