unity_test_summary.rb 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. # !/usr/bin/ruby
  7. #
  8. # unity_test_summary.rb
  9. #
  10. require 'fileutils'
  11. require 'set'
  12. class UnityTestSummary
  13. include FileUtils::Verbose
  14. attr_reader :report, :total_tests, :failures, :ignored
  15. attr_writer :targets, :root
  16. def initialize(_opts = {})
  17. @report = ''
  18. @total_tests = 0
  19. @failures = 0
  20. @ignored = 0
  21. end
  22. def run
  23. # Clean up result file names
  24. results = @targets.map { |target| target.tr('\\', '/') }
  25. # Dig through each result file, looking for details on pass/fail:
  26. failure_output = []
  27. ignore_output = []
  28. results.each do |result_file|
  29. lines = File.readlines(result_file).map(&:chomp)
  30. raise "Empty test result file: #{result_file}" if lines.empty?
  31. output = get_details(result_file, lines)
  32. failure_output << output[:failures] unless output[:failures].empty?
  33. ignore_output << output[:ignores] unless output[:ignores].empty?
  34. tests, failures, ignored = parse_test_summary(lines)
  35. @total_tests += tests
  36. @failures += failures
  37. @ignored += ignored
  38. end
  39. if @ignored > 0
  40. @report += "\n"
  41. @report += "--------------------------\n"
  42. @report += "UNITY IGNORED TEST SUMMARY\n"
  43. @report += "--------------------------\n"
  44. @report += ignore_output.flatten.join("\n")
  45. end
  46. if @failures > 0
  47. @report += "\n"
  48. @report += "--------------------------\n"
  49. @report += "UNITY FAILED TEST SUMMARY\n"
  50. @report += "--------------------------\n"
  51. @report += failure_output.flatten.join("\n")
  52. end
  53. @report += "\n"
  54. @report += "--------------------------\n"
  55. @report += "OVERALL UNITY TEST SUMMARY\n"
  56. @report += "--------------------------\n"
  57. @report += "#{@total_tests} TOTAL TESTS #{@failures} TOTAL FAILURES #{@ignored} IGNORED\n"
  58. @report += "\n"
  59. end
  60. def usage(err_msg = nil)
  61. puts "\nERROR: "
  62. puts err_msg if err_msg
  63. puts "\nUsage: unity_test_summary.rb result_file_directory/ root_path/"
  64. puts ' result_file_directory - The location of your results files.'
  65. puts ' Defaults to current directory if not specified.'
  66. puts ' Should end in / if specified.'
  67. puts ' root_path - Helpful for producing more verbose output if using relative paths.'
  68. exit 1
  69. end
  70. protected
  71. def get_details(_result_file, lines)
  72. results = { failures: [], ignores: [], successes: [] }
  73. lines.each do |line|
  74. _src_file, _src_line, _test_name, status, _msg = line.split(/:/)
  75. line_out = (@root && (@root != 0) ? "#{@root}#{line}" : line).gsub(/\//, '\\')
  76. case status
  77. when 'IGNORE' then results[:ignores] << line_out
  78. when 'FAIL' then results[:failures] << line_out
  79. when 'PASS' then results[:successes] << line_out
  80. end
  81. end
  82. results
  83. end
  84. def parse_test_summary(summary)
  85. raise "Couldn't parse test results: #{summary}" unless summary.find { |v| v =~ /(\d+) Tests (\d+) Failures (\d+) Ignored/ }
  86. [Regexp.last_match(1).to_i, Regexp.last_match(2).to_i, Regexp.last_match(3).to_i]
  87. end
  88. def here
  89. File.expand_path(File.dirname(__FILE__))
  90. end
  91. end
  92. if $0 == __FILE__
  93. # parse out the command options
  94. opts, args = ARGV.partition { |v| v =~ /^--\w+/ }
  95. opts.map! { |v| v[2..-1].to_sym }
  96. # create an instance to work with
  97. uts = UnityTestSummary.new(opts)
  98. begin
  99. # look in the specified or current directory for result files
  100. args[0] ||= './'
  101. targets = "#{ARGV[0].tr('\\', '/')}**/*.test*"
  102. results = Dir[targets]
  103. raise "No *.testpass, *.testfail, or *.testresults files found in '#{targets}'" if results.empty?
  104. uts.targets = results
  105. # set the root path
  106. args[1] ||= Dir.pwd + '/'
  107. uts.root = ARGV[1]
  108. # run the summarizer
  109. puts uts.run
  110. rescue StandardError => e
  111. uts.usage e.message
  112. end
  113. end