Prevent rspec from loading tests files from a directoryacc

690 views
Skip to first unread message

Nalesso Moreira

unread,
Nov 12, 2013, 11:24:18 PM11/12/13
to rs...@googlegroups.com
Hi there,
My test suite is failing because rspec is loading the tests of a rails dummy
app located in spec/dummy/spec/**/*.rb.

Here is a better explanation:

Given a dir spec
And I have a rails app in spec/dummy
When I run rspec command
Then rspec should not load tests in spec/dummy/spec/**/*.rb
And all my tests mustt pass

Does anybody know how I could sort of add_exception like autotest
does not to fetch/load this particular dir?

With regards,
Nalesso Moreira

David Chelimsky

unread,
Nov 13, 2013, 6:24:01 AM11/13/13
to rs...@googlegroups.com
There's currently no exception feature, but rspec loads all files matching the glob pattern "spec/**/*_spec.rb" by default (run "rspec --help" to see the command line options). This can be overridden on the command line or in .rspec (or ~/.rspec). So given:

$ ls spec
dummy foo bar
$ rspec --pattern "spec/{foo,bar}/**/*_spec.rb"

Depending on your shell and your glob fu, there might be a way to exclude the spec/dummy directory using a negation, but I haven't figured out how to do that yet.

HTH,
David




--
You received this message because you are subscribed to the Google Groups "rspec" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rspec+un...@googlegroups.com.
To post to this group, send email to rs...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rspec/0a23b642-d0df-4493-9f53-d78752989e5a%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Nalesso Moreira

unread,
Nov 14, 2013, 11:06:18 AM11/14/13
to rs...@googlegroups.com
Hi David,
Thanks for your response.

Do you think rspec-core should provide an options like this?
I am going to submit a PR on github with this feature implemented.

With regards,
Nalesso Antonio

Nalesso Moreira

unread,
Nov 14, 2013, 11:12:34 AM11/14/13
to rs...@googlegroups.com
Rationale would be like so:

RSpec.configure do |c|
  c.add_path_exception /dummy/
  c.add_path_exceptions [ /dummy/, /app/ ]
end

WDYT?

Thanks,
Nalesso Antonio

David Chelimsky

unread,
Nov 14, 2013, 11:44:31 AM11/14/13
to rs...@googlegroups.com
Seams reasonable, but the devil is in the details. Go ahead and submit a PR and see what the team thinks.

Cheers,
David

Sent from my iPhone

Myron Marston

unread,
Nov 14, 2013, 12:25:27 PM11/14/13
to rs...@googlegroups.com
In my opinion, this adds complexity for what is very much a niche use case.  As far as I know, you're the first user to ever report an issue like this :).  Also, you can already achieve this using a snippet like this:

RSpec.configure do |c|
  c.filter_run_excluding :example_group => lambda { |metadata|
    metadata[:file_path].include?('dummy')
  }
end

David Chelimsky

unread,
Nov 14, 2013, 1:54:40 PM11/14/13
to rs...@googlegroups.com
Thanks Myron - forgot that mechanism exists for that :)


Nalesso Moreira

unread,
Nov 14, 2013, 7:23:02 PM11/14/13
to rs...@googlegroups.com
Hi Myron,
thanks for your response, somehow not very helpful.
This does not seam to be the best solution, as these files
are under the directory spec/dummy/spec ( i.e a rails app called dummy using rspec ),
I would have to add a tag to any group example that I do not want rspec to run.
I believe this seams a bit redundant!?

The implementation I have in mind is to tell rspec not to require any file that matches
the path exception added.

Rspec::Core::Configuration#gather_directories returns the files that match a pattern
and sorts them.
My ideas is to add four more methods, and modify gather_directories to be like:

def initialize
 @path_exceptions = false
end

attr_reader :path_exceptions

def gather_directories(path)
 stripped = "{#{pattern.gsub(/\s*,\s*/, ',')}}"
 files       = pattern =~ /^#{Regexp.escape path}/ ? Dir[stripped] : Dir["#{path}/#{stripped}"]
 except_for!(files)
end

def except_for!(files)
 files = files.select { |file| file !~ path_exceptions } if path_exceptions
 files.sort
end

def add_exception = path
  @path_exceptions = path
 end
 alias :add_exceptions :add_exception

And you would add exceptions like so:

RSpec.configure do |c|
  c.add_exception = /dummy/

 # OR

 c.add_exceptions = /dummy|app|test|sinatra/
end

In order not to use another loop in this minor implementation,
we would require users to follow that convention to specify more
than one path as an exception. ( i.e dummy|test|this, Ruby Regexp OR )
I got it working here, but as you Myron Marston does not seem to like this
implementation I would not even bother to open up a PR.
It's a minimal complexity added.
AFAIK anything has to start from somewhere, so here I am proposing this feature.

I am looking forward to hearing from you all.

With regards,
Nalesso Antonio

David Chelimsky

unread,
Nov 14, 2013, 7:28:29 PM11/14/13
to rs...@googlegroups.com
On Thu, Nov 14, 2013 at 7:23 PM, Nalesso Moreira <nbi...@gmail.com> wrote:
Hi Myron,
thanks for your response, somehow not very helpful.
This does not seam to be the best solution, as these files
are under the directory spec/dummy/spec ( i.e a rails app called dummy using rspec ),
I would have to add a tag to any group example that I do not want rspec to run.

Myron's suggestion will exclude any examples in files that have "dummy" in their path without having to tag anything. Did you try it?
 

Nalesso Moreira

unread,
Nov 14, 2013, 8:49:32 PM11/14/13
to rs...@googlegroups.com
Hi David,
thanks for your reply.
Yes I did try, indeed it did not work.
BTW I am using rspec 2.14.7.

Thanks,
Nalesso Antonio

David Chelimsky

unread,
Nov 14, 2013, 10:47:47 PM11/14/13
to rs...@googlegroups.com
On Thu, Nov 14, 2013 at 8:49 PM, Nalesso Moreira <nbi...@gmail.com> wrote:
Hi David,
thanks for your reply.
Yes I did try, indeed it did not work.

I just did it and it worked as expected, so there's something different about either your spec_helper.rb or your environment. Happy to peek if you post the full spec_helper.rb (including Myron's suggestion) to a gist.
 

Nalesso Moreira

unread,
Nov 18, 2013, 9:01:53 AM11/18/13
to rs...@googlegroups.com
Hi David,
I am sorry for the late reply.
Here is the spec_helper as well as the entire app.

With regards,
Nalesso Antonio

David Chelimsky

unread,
Nov 19, 2013, 12:20:41 AM11/19/13
to rs...@googlegroups.com
I won't have time to look at this for a couple of days, but the first line of spec_helper.rb is suspect as it's calling to a different version of the rspec executable than the one that ships with the gem.


Message has been deleted

Nalesso Moreira

unread,
Nov 20, 2013, 8:15:55 AM11/20/13
to rs...@googlegroups.com

Hi David
Yes, it is requiring rspec with my feature, which works pretty well.
I have removed that and still did not work.

Thanks for your time.
Nalesso Antonio

David Chelimsky

unread,
Nov 20, 2013, 8:26:56 AM11/20/13
to rs...@googlegroups.com
The filter based on metadata won't work in your case because the error is happening when the file is loaded, not when it's deciding which of the loaded examples to run. So your original suggestion, that it should be handled at the command line, is a better direction. I'd recommend that you submit an issue to rspec-core explaining specifically that there is a file that you don't even want loaded, so you'd like an explicit exclusion mechanism.

Cheers,
David


Nalesso Moreira

unread,
Nov 25, 2013, 9:37:01 AM11/25/13
to rs...@googlegroups.com
You're absolutely right, It all made sense now.
I didn't think about this.
thanks very much indeed David.
I will fill up an issue on rspec-core with a PR and see what they think about it.

With regards,
Antonio Nalesso
Reply all
Reply to author
Forward
0 new messages