RSpec on Rails rake task for Recent Specs

June 5, 2007

As our number of specs grow I started to miss the test:recent functionality, so I created a simple little rake task to make it work with RSpec. Drop this in lib/tasks, I called it spec_recent.rake.

require 'spec/rake/spectask'
# Grab recently touched specs
def recent_specs(touched_since)
  recent_specs = FileList['app/**/*.rb'].map do |path|
    if File.mtime(path) > touched_since
      spec = File.join('spec', File.dirname(path).split("/")[1..-1].join('/'),
        "#{File.basename(path, '.rb')}_spec.rb")
      spec if File.exists?(spec)
    end
  end.compact
  recent_specs += FileList['spec/**/*_spec.rb'].select do |path| 
    File.mtime(path) > touched_since 
  end.uniq
end

desc 'Run recent specs'
Spec::Rake::SpecTask.new("spec:recent") do |t|
  t.spec_opts = ["--format","specdoc","--color"]
  t.spec_files = recent_specs(Time.now - 600) # 10 min.
end

then you just type rake spec:recent and it will run recent specs (last 10 min). Currently it's just looking for spec files that have changed, or spec files that directly correlate to files that have changed in app. It's not perfect, it won't understand that app/models/foo.rb may have a spec in spec/models/foo_some_test_spec.rb , but it'll figure out subdirectories. So if app/controllers/admin/my_controller.rb is modified it will look for spec/controllers/admin/my_controller_spec.rb.

I submitted it to the RSpec rubyforge, so if anyone wants to improve on it, please do so!

Comments

There are no comments for this post.

Add comment

About

I'm a programmer and shutterbug living on the West Coast of Canada.

Blog Archive

Recently Heard

Whisky & Green Teaby SupergrassPlayed on 2008/11/18 at 10:27AM
No Sant (Flytronix Mix) (feat. Lena Fiagbe)by Wasis DiopPlayed on 2008/11/18 at 10:21AM
Imperial Hippyby Transglobal UndergroundPlayed on 2008/11/18 at 10:20AM
Powered by Viviti