Skip to content

Instantly share code, notes, and snippets.

@arafatm
Created February 17, 2010 04:55
Show Gist options
  • Save arafatm/306315 to your computer and use it in GitHub Desktop.
Save arafatm/306315 to your computer and use it in GitHub Desktop.
#! /usr/bin/ruby
# Output test results to screen statusline
# Author: Arafat M (http://github.com/arafatm)
#
# WARNING: This code has not been tested. Quick and dirty to see if I could
# get it to work
#
# Shamelessly copied code and concepts from:
#
# http://gist.github.com/raw/276317/45b7ca8a20f0585acc46bc75fade09a260155a61/tests.watchr
# http://github.com/blackwinter/zentest/blob/master/lib/autotest.rb
# http://github.com/scudco/autotest-screen/blob/master/lib/autotest/screen.rb
watch("test/unit/(.*)test\.rb" ) do |md|
run_with_screen(md[0])
end
watch('app/models/.*\.rb') do |md|
run_with_screen("test/unit/#{File.basename(md[0]).split('.')[0]}_test.rb")
end
# Signal Handling
Signal.trap('QUIT') { run_all_tests } # ctrl-\
Signal.trap('INT') { abort("\n") } # ctrl-c
STATUS = '%{= ky}%-Lw%{=r}%20>%n %t%{= ky}%+Lw %{= ky}%-=| %{= kw} %c%{-} %{=r} %H'
COLORS = {
:black => 'dd',
:green => 'gw',
:yellow => 'yk',
:red => 'rw'
}
def run_all_tests
tests = 0
assertions = 0
failures = 0
errors = 0
system 'clear'
cmd = "rake test"
output = `#{cmd}`
output.split("\n").each do |line|
if line =~ /\d+ tests, \d+ assertions, \d+ failures, \d+ errors/
results = line.scan(/(\d+)\s*tests?,\s*(\d+)\s*assertions?,\s*(\d+)\s*failures?,\s*(\d+)\s*errors?/).first
tests = tests + results[0].to_i
assertions = assertions + results[1].to_i
failures = failures + results[2].to_i
errors = errors + results[3].to_i
end
end
if failures > 0
color = COLORS[:red]
msg = "#{failures} / #{assertions}"
puts output
else
color = COLORS[:green]
msg = "ALL: #{tests} : #{assertions}"
end
statusline = STATUS + "%{= #{color}} #{msg}"
output_results(statusline)
end
def run_with_screen(file)
#system 'clear'
cmd = "ruby #{file}"
output = `#{cmd}`
results = output.scan(/(\d+)\s*tests?,\s*(\d+)\s*assertions?,\s*(\d+)\s*failures?,\s*(\d+)\s*errors?/).first
tests, assertions, failures, errors = results.map{|r| r.to_i}
if failures > 0
color = COLORS[:red]
msg = "#{failures} / #{assertions}"
puts output
else
color = COLORS[:green]
msg = "#{tests} : #{assertions}"
end
statusline = STATUS + "%{= #{color}} #{msg}"
output_results(statusline)
end
def output_results(msg)
system %(screen -X eval 'hardstatus alwayslastline "#{msg}"')
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment