Csabi's blog

Software Craftsmanship, Agile, and other Ideas about Programming

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

Ruby Plasmoid and Qt::Timer (time events)Ruby's Watchr Configuration for ShUnit2

Write a comment

New comments have been disabled for this post.