suvankar
suvankar

Reputation: 1558

how to write a custom rake task for RSpec?

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'.

Upvotes: 5

Views: 5428

Answers (2)

Pan Thomakos
Pan Thomakos

Reputation: 34350

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.

Upvotes: 6

Simone
Simone

Reputation: 11797

I don't understand if the execution order is actually a problem for you.

Anyway, if the application should exit upon error, why don't you just raise an exception?

EDIT
Since the execution order is meaningful to you, I think that

task.rspec_opts = Dir.glob("[0-9][0-9][0-9]_*").collect { |x| "-I#{x}" }.sort

won't do what you expect.

Probably the file are then included in no particular order, so you should programmatically invoke each file individually and check it.

HTH

Upvotes: 1

Related Questions