Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Specifying pattern with no matches in rake task runs all specs #1126

Closed
ghost opened this Issue · 6 comments

2 participants

@ghost

If you specify a pattern in a rake task that does not match any spec files, rspec will be run without specifying any files. The following cucumber test demonstrates the problem:

diff --git a/features/command_line/rake_task.feature b/features/command_line/rake_task.feature
index 16514e9..3e2273a 100644
--- a/features/command_line/rake_task.feature
+++ b/features/command_line/rake_task.feature
@@ -120,3 +120,26 @@ Feature: rake task
       """
       (ruby|rbx) -S rspec ./spec/thing_spec.rb --tag fast
       """
+
+  Scenario: specify pattern that does not match any files
+    Given a file named "Rakefile" with:
+      """ruby
+      require 'rspec/core/rake_task'
+
+      RSpec::Core::RakeTask.new(:spec) do |t|
+        t.pattern = 'spec/no_matches_*_spec.rb'
+      end
+      """
+    And a file named "spec/do_not_run_spec.rb" with:
+      """ruby
+      describe "something" do
+        it "always passes" do
+          # pass
+        end
+      end
+      """
+    When I run `rake spec`
+    Then the output should match:
+      """
+      0 examples, 0 failures
+      """
@myronmarston

RSpec is doing what you told it to. What would you expect it to do instead?

@ghost

If I specify a pattern and no files match that pattern, I would expect no tests to be run. In this case, the rake task runs rspec without specifying any files, which has the result that all of the tests are run. The rake task should detect that the pattern did not match any files and not call rspec. I would expect that the result of this would be the same as running "rake spec" with no spec files present.

@ghost

The --pattern option to rspec seems to handle this situation correctly. Instead of generating the file list in RakeTask::files_to_run, could the rake task just specify a --pattern when it calls rspec? Something like this:

git diff lib/rspec/core/rake_task.rb
diff --git a/lib/rspec/core/rake_task.rb b/lib/rspec/core/rake_task.rb
index 3f34ef5..049c6a5 100644
--- a/lib/rspec/core/rake_task.rb
+++ b/lib/rspec/core/rake_task.rb
@@ -115,7 +115,11 @@ module RSpec
         cmd_parts << RUBY
         cmd_parts << ruby_opts
         cmd_parts << "-S" << rspec_path
-        cmd_parts << files_to_run
+        if ENV['SPEC']
+          cmd_parts << FileList[ ENV['SPEC'] ].sort
+        else
+          cmd_parts << "--pattern " << pattern
+        end
         cmd_parts << rspec_opts
         cmd_parts.flatten.reject(&blank).join(" ")
       end

I think that's the right way to handle this, I'll submit a pull request in a couple minutes.

@myronmarston

Thanks. I misread the cuke you posted: I was thinking it was demonstrating what the current behavior is, not what it should be. I agree that it should run no spec files if none match the pattern.

Your solution looks reasonable. Want to make a PR?

@ghost

Will do. I'm in the process of fixing the tests now, a number of the unit tests no longer apply if we're just passing the pattern argument along to rspec.

@JonRowe
Owner

:+1: for this, I don't know why we were reimplementing this here...

@myronmarston myronmarston modified the milestone: Post 3.0, 3.0
@myronmarston myronmarston referenced this issue from a commit
@kesor kesor use rspec --pattern option in rake tasks
rspec has an internal option (--pattern) to receive pattern of files to run
via the command line, use this option in rake_tasks instead of passing through
a huge list of files one by one on the command line.

Fixes #1126.
1154903
@myronmarston myronmarston closed this issue from a commit
@kesor kesor use rspec --pattern option in rake tasks
rspec has an internal option (--pattern) to receive pattern of files to run
via the command line, use this option in rake_tasks instead of passing through
a huge list of files one by one on the command line.

Fixes #1126.
eeb8730
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.