Making Minispec and Autotest Play Nice

As I’m moving into Ruby 1.9 development, I’m really liking minispec. It’s much like rspec, but very lightweight and comes with the language.

Autotest is also incredibly useful, but it doesn’t find specs by default. It very much likes there to be a ‘test’ directory, with files named like ‘test_foo.rb’.

If you use rspec, it registers its own mappings for autotest and you never have to worry.

Autotest’s source recommends implementing an ‘autotest/discover.rb’ in your project, which it finds and runs automagically for you.

Through some sniffing around the rspec codebase, I came up with this ‘discover.rb’ for a gem I’m working on. It finds ‘spec/lib/foo_spec.rb’ and looks for an implementation class of ‘lib/foo.rb’. It’s really just a super-simplification of rspec’s discovery strategy.

Autotest.add_hook :initialize do |at|
  at.clear_mappings
  at.add_mapping(%r%^lib/(.*)\.rb$%) { |_, m|
    ["spec/lib/#{m[1]}_spec.rb"]
  }
end

class Autotest::MiniSpec < Autotest
end

The other part required to make autotest happy is a nice ‘.autotest’ file in the project.

require 'autotest/restart'

Autotest.add_hook :initialize do |at|
  at.testlib = 'minitest/spec'

  at.add_exception 'coverage.info'
  at.add_exception 'coverage'
end

There’s probably a nice way to combine these, given that they both are patching the initialize hook.

And for good measure, here’s the ‘spec/lib/spec_helper.rb’ I’m including in all the specs:

$LOAD_PATH.unshift(File.dirname(__FILE__))
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))

require 'minitest/autorun'

# Try to make the output nice and fancy with 'turn'
begin
  require 'turn'
rescue LoadError
  puts "Could not find gem 'turn', continuing without prettiness."
end
Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s