Les Hill github twitter facebook linked in archives
Posted August 24, 2008

The usual autotest workflow goes something like this:

  • Edit and save
  • Autotest runs associated specs
  • Are there failures? Fix and start over
  • Autotest runs the entire suite

Sometimes though, you want autotest to just ignore most of your specs and focus on a few specs.

Last week, while Kevin, Rick, and Yossef (OG) were here, they shared an autotest tweak that does exactly that.

The tweak allows you to specify a regular expression to limit the files which autotest watches; for example, to autotest only files matching *user*.rb you would start autotest like so (atest is an alias):

% atest user

Here is the (lightly modified) code to do this; in your .autotest file add:

 1 if ENV['AUTOTEST'] and not ENV['AUTOTEST'].empty?
 2   only_these_files_re = Regexp.new(ENV['AUTOTEST'])
 3 
 4   Autotest.send(:alias_method, :real_find_files, :find_files)
 5   Autotest.send(:define_method, :find_files) do |*args|
 6     real_find_files.reject do |k, v|
 7       !only_these_files_re.match(k)
 8     end
 9   end
10 end

And in your .bash_profile add:

# Autotest
function fn_autotest() {
  AUTOTEST=$1 autotest
}
alias atest='fn_autotest'

Now use the alias to invoke autotest. For standard autotest behavior:

% atest

To limit what autotest is watching, pass a regular expression (which can be a simple string):

% atest user.*html

Thanks to Tom Preston-Werner for the CSS layout, Webby for the blog renderer, and GitHub Pages for the blog hosting.