Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to write a custom rake task for RSpec?

Tags:

ruby

rspec2

In my Rake file:

require 'rspec/core/rake_task'

desc 'Default: run specs.'
task :default => :spec

desc "Run specs"
RSpec::Core::RakeTask.new do |task|
    task.pattern = "**/spec/*_spec.rb"
    task.rspec_opts = Dir.glob("[0-9][0-9][0-9]_*").collect { |x| "-I#{x}" }.sort
    task.rspec_opts << '-r ./rspec_config'
    task.rspec_opts << '--color'
    task.rspec_opts << '-f documentation'
end

In rspec_config.rb

RSpec.configure {|c| c.fail_fast = true}

My file structure:

|-- 001_hello
|   |-- hello1.rb
|   `-- spec
|       `-- hello_spec.rb
|-- 002_hello
|   |-- hello2.rb
|   `-- spec
|       `-- hello_spec.rb
|-- 003_hello
|   |-- hello3.rb
|   `-- spec
|       `-- hello_spec.rb
|-- Rakefile
`-- rspec_config.rb

when rake task will run it will do the operation sequentially on the above file structure. How to ensure that if '001_hello' fails then it should not run '002_hello'?

Currently it run on reverse order ie. '003_hello' then '002_hello' then '001_hello'.

like image 420
suvankar Avatar asked Sep 13 '11 06:09

suvankar


1 Answers

You need to modify the task pattern to get files to run in a particular order. For instance:

RSpec::Core::RakeTask.new do |task|
  task.pattern = Dir['[0-9][0-9][0-9]_*/spec/*_spec.rb'].sort
  task.rspec_opts = Dir.glob("[0-9][0-9][0-9]_*").collect { |x| "-I#{x}" }
  task.rspec_opts << '-r ./rspec_config --color -f d'
end

This will run all files matching ###_*/spec/*_spec.rb in alphabetical order.

like image 136
Pan Thomakos Avatar answered Oct 03 '22 08:10

Pan Thomakos