Monday, July 12, 2010

Extremely Simple Alternative to AutoTest

I am reading the Rails 3 in Action by Yehuda. To run the sample code I customized the stakeout.rb to output the results of the test when specs get run whenever the file is changed.

1. Copy the source to a directory that is in your path:

#!/usr/bin/env ruby
if ARGV.size < 2
puts "Usage: stakeout.rb [files to watch]+"
exit 1
end

command = ARGV.shift
files = {}

ARGV.each do |arg|
Dir[arg].each { |file|
files[file] = File.mtime(file)
}
end

loop do

sleep 1

changed_file, last_changed = files.find { |file, last_changed|
File.mtime(file) > last_changed
}

if changed_file
files[changed_file] = File.mtime(changed_file)
puts "=> #{changed_file} changed, running #{command}"
puts system(command)
puts "=> done"
end

end

2. chmod +x stakeout.rb

3. To run:

stakeout.rb "spec bacon_spec.rb" **/*

This will run the spec called back_spec.rb whenever any files found recursively from the current file is changed.

Reference:

Faster TDD with Stakeout.rb