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