From fc12aae3b12ecb118411e4cd0d2c34da679d1ee1 Mon Sep 17 00:00:00 2001 From: Philippe Huibonhoa Date: Wed, 25 Apr 2012 21:03:38 -0700 Subject: [PATCH] Updated for better formatting with MiniTest::Reporters::DefaultReporter.new. The RubyMate reporter doesn't show dots per test. --- Support/RubyMate/run_script.rb | 192 +++++++++++++++++++++++++++------ 1 file changed, 158 insertions(+), 34 deletions(-) diff --git a/Support/RubyMate/run_script.rb b/Support/RubyMate/run_script.rb index 2702544..e96f923 100755 --- a/Support/RubyMate/run_script.rb +++ b/Support/RubyMate/run_script.rb @@ -12,8 +12,8 @@ def spork? # return false @spork ||= lambda{ + return false if ENV['SPORK_TESTUNIT'].nil? begin - return false if ENV['SPORK_TESTUNIT'].nil? spork_server = DRbObject.new_with_uri(SPORK_SERVER_URI) if spork_server.respond_to?(:run) return true @@ -44,54 +44,178 @@ def spork? end if spork? - test = ARGV.last.gsub(/--name=/, '') if ARGV.last and ARGV.last.match(/--name=/) - cmd << "-n#{test}" if test + cmd << "-n#{ARGV[1].gsub(/--name=/, '')}" script_args = [] else - script_args = ARGV + script_args = ARGV + ['-v'] end cmd << ENV["TM_FILEPATH"] -TextMate::Executor.run(cmd, :version_args => ["--version"], :script_args => script_args) do |str, type| +def start_of_test?(line) + !!(line =~ /\s*test:/) +end + +def end_of_test?(line) + !!(line =~ /[.EF]: \([\d.]+\)/) +end + +def last_line?(line) + !!(line =~ /tests.*assertions.*failures.*errors/) +end + +#test: .bulk_shipment_items distributor_return should include ship_to_distributors. :.: (0.200692) +# 1:"test: " -- throw away +# 2:".bulk_shipment_items distributor_return should include ship_to_distributors" -- test name +# 3:". :" -- throw away +# 4:"" -- any puts in the test +# 5:".:" -- throw away +# 6:"." -- test result . or E or F +# 7:"0.200692" -- duration +def test_parts_with_duration(string) + result = string.match(/\A\s*(test:\s*)(.*?)(\.\s*:)(.*)(([.EF]):)\s*\(([\d.]+)\)\s*\z/m) + + if result.nil? + nil + else + [result[2], result[4].sub(/^\s+/, ''), result[6], result[7]] + end +end + +# str = "test: schema should truth. (RedemptionTest): .\n" A +# 1:"test: " -- throw away +# 2:"schema should truth. (RedemptionTest)" -- test name +# 3:":" -- throw away +# 4:"" -- any puts in the test +# 5:"." -- test result . or E or F +def test_parts_without_duration(string) + result = string.match(/\A\s*(test:\s*)(.*?)(\.\s*:)(.*)([.EF])\s*\z/m) + + if result.nil? + nil + else + [result[2], result[4], result[5], nil] + end +end + +# str = "test: schema should truth. (RedemptionTest): .\n" A +# str = " test: schema should truth. :\t\t\t\tdebug\n" A +def format_output(string) + test_name, test_puts, test_result, test_duration = if string =~ /(([.EF]):)\s*\(([\d.]+)\)\s*\z/m + test_parts_with_duration(string) + else + test_parts_without_duration(string) + end + + if test_name.nil? + out = htmlize(string) + else + out = "  #{format_test_result(test_result)}  #{format_test_name(test_name, test_result)}" + out << ": (#{test_duration})" if test_duration + out << "
" + out << %Q!
#{htmlize(test_puts)}
! unless test_puts.strip.empty? + end + + out +end + +def format_test_name(test_name, result) + out = test_name + out = %Q!#{test_name}! unless result == '.' + out +end + +def format_test_result(result) + result.gsub(/[EF]+/, "\\&").gsub(/\.+/, "\\&") +end + +test_script_buffer = '' +test_script_output = '' +past_test_run = false #set to true once tests have been completed and now printing the errors/failures + + +class TestResultParser + + attr_reader :buffer, :in_buffer + alias :in_buffer? :in_buffer + + def initialize + @buffer = [] + @in_buffer = false + end + + def test_result_line?(line) + line =~ /^\w+#test:/ || in_buffer? + end + + def process_line(line) + if line =~ /^\w+#test:/ + @buffer = [line] + @in_buffer = true + elsif in_buffer? + @buffer << line + end + + if line =~ /= [\.EF]$/ + @in_buffer = false + + all_lines = buffer.join("\n") + match = all_lines.match(/(.*?(\. = ))(.*?)([\d\.]+ . = [\.EF])\Z/m) + + if match + test_result = match[1] + match[4] + test_puts = match[3] + + else + test_result = all_lines + end + + style = if line =~ /E|F$/ + 'color:red; font-weight:bold;' + else + 'color:green;' + end + + test_has_puts = !(test_puts.nil? || test_puts.strip == "") + + out = "
#{test_result}
" + out << "
#{test_puts}
" if test_has_puts + out + else + "" + end + end +end + +trp = TestResultParser.new + +TextMate::Executor.run(cmd, :version_args => ["--version"], :script_args => script_args) do |str, type| case type when :out if is_test_script and str =~ /\A[.EF]+\Z/ - htmlize(str).gsub(/[EF]+/, "\\&").gsub(/\.+/, "\\&") + "
" + format_test_result(htmlize(str)) + "
" elsif is_test_script - out = [str].flatten.map do |line| - if line =~ /^(\s+)(\S.*?):(\d+)(?::in\s*`(.*?)')?/ - indent, file, line, method = $1, $2, $3, $4 - url, display_name = '', 'untitled document'; - unless file == "-" - indent += " " if file.sub!(/^\[/, "") - file = File.join(ENV['TM_PROJECT_DIRECTORY'], file) unless file =~ /^\// - url = '&url=file://' + e_url(file) - display_name = File.basename(file) - end - "#{indent}" + - (method ? "method #{CGI::escapeHTML method}" : 'at top level') + - " in #{CGI::escapeHTML display_name} at line #{line}
" - elsif line =~ /\A\s*test:.*?\.\s*\(.*?\):\s*[.EF]\n?\Z/ - htmlize(line.chomp).gsub(/([EF])\Z/, "\\1").gsub(/(\.)\Z/, "\\1") + "

" - elsif line =~ /\A\s*(test:.*?\.\s*\(.*?\):\s*)(.*)\Z/m - test_header, test_output = $1, $2 - htmlize(test_header.chomp) + "

" + htmlize(test_output) + "

" - elsif line =~ /(\[[^\]]+\]\([^)]+\))\s+\[([\w\_\/\.]+)\:(\d+)\]/ - spec, file, line = $1, $2, $3, $4 - file = File.join(ENV['TM_PROJECT_DIRECTORY'], file) unless file =~ /^\// - "#{spec}:#{line}
" - elsif line =~ /(.*?:)?([\w\_ ]+).*\[([\w\_\/\.]+)\:(\d+)\]/ - method, file, line = $2, $3, $4 - file = File.join(ENV['TM_PROJECT_DIRECTORY'], file) unless file =~ /^\// - "#{File.basename(file)}:#{line}
" - elsif line =~ /^\d+ tests, \d+ assertions, (\d+) failures, (\d+) errors\b.*/ + out = "" + [str].flatten.each do |line| + out << if trp.test_result_line?(line) + trp.process_line(line) + elsif line =~ /^\d+ tests, \d+ assertions, (\d+) failures, (\d+) errors\b.*/ # test summary "#{$&}
" + elsif line =~ /([^\w\/])([\w\.\/\-@]+.rb):(\d+)(.*)/ # backtrace - provide linkage + prepend, file, line_number, append = $1, $2, $3, $4 + file = File.join(ENV['TM_PROJECT_DIRECTORY'].to_s, file) unless file =~ /^\// + color, decoration = if ENV['TM_PROJECT_DIRECTORY'].nil? or file.include?(ENV['TM_PROJECT_DIRECTORY'] + '/test') + ['blue', 'underline'] + else + ['black', 'none'] + end + "#{prepend}#{file}:#{line_number}#{append}
" else htmlize(line) end end - out.join() + + out else htmlize(str) end