Csabi's blog

Software Craftsmanship, Agile, and other Ideas about Programming

Subscribe to RSS feed

Posts tagged with "shUnit"

Ruby's Watchr Configuration for ShUnit2

, , , ...

require "erb"

watch('tests/.*Test\.sh') do |md|
  clear_console
  run "#{md[0]}"
end

watch('source/(.*)\.sh') do |md|   # runs test when source code changes
  clear_console
  testpath = "tests/" + md[1] + "Test.sh"
  run "#{testpath}"
end

def clear_console
  puts "\e[H\e[2J"  #clear console
end

def run cmd
  result = `#{cmd} 2>&1`
  if result.match(/FAILED\s+\(failures=/)
    notify_failed cmd, result
  elsif result.match(/ERROR/) or result.match(/command not found/)
    notify "#{cmd}", result, "pending.png", 6000
  elsif result.match(/\nOK\n/)
    notify "#{cmd}", "<font size=4 color=lightgreen><b><i>Tests Passed Successfuly</i></b></font>", "success.png", 1000
  else
    notify "#{cmd}", "Unknown error: 
" + result, "pending.png", 3000
  end
  puts result
end

def notify_failed cmd, result
    ft = result.match(/.*(test.*)\nASSERT:(.*?)\n/m)
    first_failing_test = defined?(ft[1]) ? ft[1] : "no match"
    first_failure_message = defined?(ft[2]) ? ft[2] : "Critical"
 
    notify "#{cmd}", "<b>" + first_failing_test + "</b> failed!\n" +
      "<font size=3 color=pink><b><i>" + ERB::Util.html_escape(first_failure_message) + "</i></b></font>", "failure.png", 6000
end

def notify title, msg, img, show_time
  images_dir='~/.autotest/images'
  system "notify-send '#{title}' '#{msg}' -i #{images_dir}/#{img} -t #{show_time}"
end

Using Ruby's Watcher for Continuous Bash (shUnit) Testing

, , , ...

So, did you ever TDD a Bash script? ... Very well, I thought so. But, what about continuous testing with shUnit? You know, the cool automatic tests we run when programming in Ruby wink

Yeees! You can do that with any language with the help of Ruby's Watchr gem. If you are on Linux, you will want libnotify also. So, how do you do it? Take a look at the script below, adjust the parameters to your needs and run watcher like this: 'watchr autotest_file_you_just_wrote'

watch('tests/.*Test\.sh') do |md|
  clear_console
  run "#{md[0]}"
end

watch('source/(.*)\.sh') do |md|   # runs test when source code changes
  clear_console
  testpath = "tests/" + md[1] + "Test.sh"
  run "#{testpath}"
end

def clear_console
  puts "\e[H\e[2J"  #clear console
end

def run cmd
  result = `#{cmd}`
  if result.match(/No tests failed/)
    notify "#{cmd}", "<font size=4 color=lightgreen><b><i>Tests Passed Successfuly</i></b></font>", "success.png"
  else
    first_failing_test = result.match(/^.*:\s+(.*)\s+E/)[1]
    first_failure_message = result.match(/^(.*)failed/)[1]
    notify "#{cmd}", "<b>" + first_failing_test + "</b> failed!\n" +
      "<font size=4 color=pink><b><i>" + first_failure_message + "</i></b></font>", "failure.png"
  end
  puts result
end

def notify title, msg, img
  images_dir='~/.autotest/images'
  system "notify-send '#{title}' '#{msg}' -i #{images_dir}/#{img} -t 3000"
end

Have fun smile