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

Closed
ghost opened this Issue Oct 28, 2013 · 6 comments

Comments

Projects
None yet
2 participants
@ghost

ghost commented Oct 28, 2013

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
+      """
Owner

myronmarston commented Oct 28, 2013

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

@ghost

ghost commented Oct 28, 2013

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

ghost commented Oct 28, 2013

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.

Owner

myronmarston commented Oct 28, 2013

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

ghost commented Oct 28, 2013

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.

Owner

JonRowe commented Oct 28, 2013

👍 for this, I don't know why we were reimplementing this here...

@myronmarston myronmarston modified the milestones: Post 3.0, 3.0 Mar 24, 2014

myronmarston added a commit that referenced this issue Jul 31, 2014

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment