Nullcreations.net | Ramblings & Photography of Jerrett Taylor

RSpec on Rails rake task for Recent Specs

June 6th, 2007 | 0 comments

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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!

0 Comments

Musings

04.14.08

Trying to decide where to travel to? Wikitravel.org Random Page has the answers!

02.23.08

sd's embedded_actions has a new home on github

02.18.08

Very cool.

02.17.08

Open Source Food? - looks like a nice recipe site!

02.15.08

For anyone using capistrano with more than one target for deployment (staging, production), check out cap multistage

02.15.08

Wow, I just realized how bad my code blocks break my blog in safari. I should fix that!

01.27.08

Mike has created another nice netbeans theme - go check it out, if you use netbeans!

01.21.08

A handy tool, Rubular is a web based regex editor

01.15.08

Dan put together a sweet code editor plugin for mce with syntax highlighting etc.

12.13.07

On OSX? Add RubyImporter to Spotlight and search your source!

12.04.07

sd has released a new version of embedded_actions

11.27.07

RejectConf videos are online thanks to confreaks!

11.19.07

Good news - we upgraded a project at work to rails 2.0 rc2 and the speed to run specs dropped from 30-35 seconds to 10-15 seconds!

11.08.07

Eigenclass.org has a great big list of Ruby 1.9 changes that is being kept up to date!

11.06.07

Rein is working on a trac replacement in ruby called eskort , and it looks cool. More info on his goals