135375f98Sopenharmony_ci# ==========================================
235375f98Sopenharmony_ci#   Unity Project - A Test Framework for C
335375f98Sopenharmony_ci#   Copyright (c) 2007 Mike Karlesky, Mark VanderVoord, Greg Williams
435375f98Sopenharmony_ci#   [Released under MIT License. Please refer to license.txt for details]
535375f98Sopenharmony_ci# ==========================================
635375f98Sopenharmony_ci
735375f98Sopenharmony_ci# !/usr/bin/ruby
835375f98Sopenharmony_ci#
935375f98Sopenharmony_ci# unity_test_summary.rb
1035375f98Sopenharmony_ci#
1135375f98Sopenharmony_cirequire 'fileutils'
1235375f98Sopenharmony_cirequire 'set'
1335375f98Sopenharmony_ci
1435375f98Sopenharmony_ciclass UnityTestSummary
1535375f98Sopenharmony_ci  include FileUtils::Verbose
1635375f98Sopenharmony_ci
1735375f98Sopenharmony_ci  attr_reader :report, :total_tests, :failures, :ignored
1835375f98Sopenharmony_ci  attr_writer :targets, :root
1935375f98Sopenharmony_ci
2035375f98Sopenharmony_ci  def initialize(_opts = {})
2135375f98Sopenharmony_ci    @report = ''
2235375f98Sopenharmony_ci    @total_tests = 0
2335375f98Sopenharmony_ci    @failures = 0
2435375f98Sopenharmony_ci    @ignored = 0
2535375f98Sopenharmony_ci  end
2635375f98Sopenharmony_ci
2735375f98Sopenharmony_ci  def run
2835375f98Sopenharmony_ci    # Clean up result file names
2935375f98Sopenharmony_ci    results = @targets.map { |target| target.tr('\\', '/') }
3035375f98Sopenharmony_ci
3135375f98Sopenharmony_ci    # Dig through each result file, looking for details on pass/fail:
3235375f98Sopenharmony_ci    failure_output = []
3335375f98Sopenharmony_ci    ignore_output = []
3435375f98Sopenharmony_ci
3535375f98Sopenharmony_ci    results.each do |result_file|
3635375f98Sopenharmony_ci      lines = File.readlines(result_file).map(&:chomp)
3735375f98Sopenharmony_ci
3835375f98Sopenharmony_ci      raise "Empty test result file: #{result_file}" if lines.empty?
3935375f98Sopenharmony_ci
4035375f98Sopenharmony_ci      output = get_details(result_file, lines)
4135375f98Sopenharmony_ci      failure_output << output[:failures] unless output[:failures].empty?
4235375f98Sopenharmony_ci      ignore_output  << output[:ignores]  unless output[:ignores].empty?
4335375f98Sopenharmony_ci      tests, failures, ignored = parse_test_summary(lines)
4435375f98Sopenharmony_ci      @total_tests += tests
4535375f98Sopenharmony_ci      @failures += failures
4635375f98Sopenharmony_ci      @ignored += ignored
4735375f98Sopenharmony_ci    end
4835375f98Sopenharmony_ci
4935375f98Sopenharmony_ci    if @ignored > 0
5035375f98Sopenharmony_ci      @report += "\n"
5135375f98Sopenharmony_ci      @report += "--------------------------\n"
5235375f98Sopenharmony_ci      @report += "UNITY IGNORED TEST SUMMARY\n"
5335375f98Sopenharmony_ci      @report += "--------------------------\n"
5435375f98Sopenharmony_ci      @report += ignore_output.flatten.join("\n")
5535375f98Sopenharmony_ci    end
5635375f98Sopenharmony_ci
5735375f98Sopenharmony_ci    if @failures > 0
5835375f98Sopenharmony_ci      @report += "\n"
5935375f98Sopenharmony_ci      @report += "--------------------------\n"
6035375f98Sopenharmony_ci      @report += "UNITY FAILED TEST SUMMARY\n"
6135375f98Sopenharmony_ci      @report += "--------------------------\n"
6235375f98Sopenharmony_ci      @report += failure_output.flatten.join("\n")
6335375f98Sopenharmony_ci    end
6435375f98Sopenharmony_ci
6535375f98Sopenharmony_ci    @report += "\n"
6635375f98Sopenharmony_ci    @report += "--------------------------\n"
6735375f98Sopenharmony_ci    @report += "OVERALL UNITY TEST SUMMARY\n"
6835375f98Sopenharmony_ci    @report += "--------------------------\n"
6935375f98Sopenharmony_ci    @report += "#{@total_tests} TOTAL TESTS #{@failures} TOTAL FAILURES #{@ignored} IGNORED\n"
7035375f98Sopenharmony_ci    @report += "\n"
7135375f98Sopenharmony_ci  end
7235375f98Sopenharmony_ci
7335375f98Sopenharmony_ci  def usage(err_msg = nil)
7435375f98Sopenharmony_ci    puts "\nERROR: "
7535375f98Sopenharmony_ci    puts err_msg if err_msg
7635375f98Sopenharmony_ci    puts "\nUsage: unity_test_summary.rb result_file_directory/ root_path/"
7735375f98Sopenharmony_ci    puts '     result_file_directory - The location of your results files.'
7835375f98Sopenharmony_ci    puts '                             Defaults to current directory if not specified.'
7935375f98Sopenharmony_ci    puts '                             Should end in / if specified.'
8035375f98Sopenharmony_ci    puts '     root_path - Helpful for producing more verbose output if using relative paths.'
8135375f98Sopenharmony_ci    exit 1
8235375f98Sopenharmony_ci  end
8335375f98Sopenharmony_ci
8435375f98Sopenharmony_ci  protected
8535375f98Sopenharmony_ci
8635375f98Sopenharmony_ci  def get_details(_result_file, lines)
8735375f98Sopenharmony_ci    results = { failures: [], ignores: [], successes: [] }
8835375f98Sopenharmony_ci    lines.each do |line|
8935375f98Sopenharmony_ci      status_match = line.match(/^[^:]+:[^:]+:\w+(?:\([^)]*\))?:([^:]+):?/)
9035375f98Sopenharmony_ci      next unless status_match
9135375f98Sopenharmony_ci
9235375f98Sopenharmony_ci      status = status_match.captures[0]
9335375f98Sopenharmony_ci
9435375f98Sopenharmony_ci      line_out = (@root && (@root != 0) ? "#{@root}#{line}" : line).gsub(/\//, '\\')
9535375f98Sopenharmony_ci      case status
9635375f98Sopenharmony_ci      when 'IGNORE' then results[:ignores]   << line_out
9735375f98Sopenharmony_ci      when 'FAIL'   then results[:failures]  << line_out
9835375f98Sopenharmony_ci      when 'PASS'   then results[:successes] << line_out
9935375f98Sopenharmony_ci      end
10035375f98Sopenharmony_ci    end
10135375f98Sopenharmony_ci    results
10235375f98Sopenharmony_ci  end
10335375f98Sopenharmony_ci
10435375f98Sopenharmony_ci  def parse_test_summary(summary)
10535375f98Sopenharmony_ci    raise "Couldn't parse test results: #{summary}" unless summary.find { |v| v =~ /(\d+) Tests (\d+) Failures (\d+) Ignored/ }
10635375f98Sopenharmony_ci
10735375f98Sopenharmony_ci    [Regexp.last_match(1).to_i, Regexp.last_match(2).to_i, Regexp.last_match(3).to_i]
10835375f98Sopenharmony_ci  end
10935375f98Sopenharmony_ciend
11035375f98Sopenharmony_ci
11135375f98Sopenharmony_ciif $0 == __FILE__
11235375f98Sopenharmony_ci
11335375f98Sopenharmony_ci  # parse out the command options
11435375f98Sopenharmony_ci  opts, args = ARGV.partition { |v| v =~ /^--\w+/ }
11535375f98Sopenharmony_ci  opts.map! { |v| v[2..].to_sym }
11635375f98Sopenharmony_ci
11735375f98Sopenharmony_ci  # create an instance to work with
11835375f98Sopenharmony_ci  uts = UnityTestSummary.new(opts)
11935375f98Sopenharmony_ci
12035375f98Sopenharmony_ci  begin
12135375f98Sopenharmony_ci    # look in the specified or current directory for result files
12235375f98Sopenharmony_ci    args[0] ||= './'
12335375f98Sopenharmony_ci    targets = "#{ARGV[0].tr('\\', '/')}**/*.test*"
12435375f98Sopenharmony_ci    results = Dir[targets]
12535375f98Sopenharmony_ci
12635375f98Sopenharmony_ci    raise "No *.testpass, *.testfail, or *.testresults files found in '#{targets}'" if results.empty?
12735375f98Sopenharmony_ci
12835375f98Sopenharmony_ci    uts.targets = results
12935375f98Sopenharmony_ci
13035375f98Sopenharmony_ci    # set the root path
13135375f98Sopenharmony_ci    args[1] ||= "#{Dir.pwd}/"
13235375f98Sopenharmony_ci    uts.root = ARGV[1]
13335375f98Sopenharmony_ci
13435375f98Sopenharmony_ci    # run the summarizer
13535375f98Sopenharmony_ci    puts uts.run
13635375f98Sopenharmony_ci  rescue StandardError => e
13735375f98Sopenharmony_ci    uts.usage e.message
13835375f98Sopenharmony_ci  end
13935375f98Sopenharmony_ciend
140