Skip to content

Commit e135076

Browse files
committed
Fix spec type inferrence.
- In #970 we added `infer_spec_type_from_file_location!` but forgot to remove the code in `lib/rspec/rails/configuration.rb` that made it always infer - so the opt-in API was there but it was always enabled. Here I've made it truly opt-in. - The logic of `infer_spec_type_from_file_location!` also setup the helper module inclusion via explicit `:type` metadata but was only intended to setup the implicit mapping of spec file location to type. Here I've made the module inclusion via `:type` always work w/o an explicit config option needed to enable it. - We used to mutate the `:type` metadata on inclusion of the helper module, but this caused bugs such as #825. The problem is that rspec-core uses a raw hash for the metadata and doesn't re-apply filtering/inclusion logic when the metadata hash is mutated. Instead, we use a new explicit `define_derived_metadata` API. Fixes #825 and closes #829.
1 parent fd3f5ea commit e135076

23 files changed

+134
-242
lines changed

features/directory_structure.feature

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ Feature: Directory Structure
2424
"""ruby
2525
require "spec_helper"
2626
27+
RSpec.configure do |config|
28+
config.before(:context, :type => :controller) do
29+
puts "Running a controller example group"
30+
end
31+
end
32+
2733
describe WidgetsController do
2834
it "responds successfully" do
2935
get :index
@@ -33,12 +39,19 @@ Feature: Directory Structure
3339
"""
3440
When I run `rspec spec`
3541
Then the example should pass
42+
And the output should contain "Running a controller example group"
3643

3744
Scenario: Specs in other directories must have their types specified manually
3845
Given a file named "spec/functional/widgets_controller_spec.rb" with:
3946
"""ruby
4047
require "spec_helper"
4148
49+
RSpec.configure do |config|
50+
config.before(:context, :type => :controller) do
51+
puts "Running a controller example group"
52+
end
53+
end
54+
4255
describe WidgetsController, :type => :controller do
4356
it "responds successfully" do
4457
get :index
@@ -48,6 +61,7 @@ Feature: Directory Structure
4861
"""
4962
When I run `rspec spec`
5063
Then the example should pass
64+
And the output should contain "Running a controller example group"
5165

5266
Scenario: Specs in canonical directories can override their types
5367
Given a file named "spec/routing/duckduck_routing_spec.rb" with:

lib/rspec/rails.rb

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,45 @@
1-
require 'rspec/rails/without_filetype_infer'
2-
require 'rspec/rails/configuration'
1+
require 'rspec/core'
2+
require 'rspec/collection_matchers'
3+
require 'rails/version'
4+
require 'rspec/rails/extensions'
5+
require 'rspec/rails/view_rendering'
6+
require 'rspec/rails/adapters'
7+
require 'rspec/rails/matchers'
8+
require 'rspec/rails/fixture_support'
9+
require 'rspec/rails/example'
10+
require 'rspec/rails/vendor/capybara'
11+
12+
RSpec.configure do |c|
13+
c.backtrace_exclusion_patterns << /vendor\//
14+
c.backtrace_exclusion_patterns << /lib\/rspec\/rails/
15+
16+
c.include RSpec::Rails::ControllerExampleGroup, :type => :controller
17+
c.include RSpec::Rails::HelperExampleGroup, :type => :helper
18+
c.include RSpec::Rails::ModelExampleGroup, :type => :model
19+
c.include RSpec::Rails::RequestExampleGroup, :type => :request
20+
c.include RSpec::Rails::RoutingExampleGroup, :type => :routing
21+
c.include RSpec::Rails::ViewExampleGroup, :type => :view
22+
c.include RSpec::Rails::FeatureExampleGroup, :type => :feature
23+
24+
if defined?(RSpec::Rails::MailerExampleGroup)
25+
c.include RSpec::Rails::MailerExampleGroup, :type => :mailer
26+
end
27+
28+
def c.infer_spec_type_from_file_location!
29+
{
30+
:controller => %w[spec controllers],
31+
:helper => %w[spec helpers],
32+
:mailer => %w[spec mailers],
33+
:model => %w[spec models],
34+
:request => %w[spec (requests|integration|api)],
35+
:routing => %w[spec routing],
36+
:view => %w[spec views],
37+
:feature => %w[spec features]
38+
}.each do |type, dir_parts|
39+
escaped_path = Regexp.compile(dir_parts.join('[\\\/]') + '[\\\/]')
40+
define_derived_metadata(:file_path => escaped_path) do |metadata|
41+
metadata[:type] ||= type
42+
end
43+
end
44+
end
45+
end

lib/rspec/rails/configuration.rb

Lines changed: 0 additions & 63 deletions
This file was deleted.

lib/rspec/rails/example.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,3 @@
77
require 'rspec/rails/example/routing_example_group'
88
require 'rspec/rails/example/model_example_group'
99
require 'rspec/rails/example/feature_example_group'
10-
11-
require 'rspec/rails/infer_type_configuration'

lib/rspec/rails/example/controller_example_group.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,6 @@ def method_missing(method, *args, &block)
155155
included do
156156
subject { controller }
157157

158-
metadata[:type] = :controller
159-
160158
before do
161159
self.routes = ::Rails.application.routes
162160
end

lib/rspec/rails/example/feature_example_group.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ module FeatureExampleGroup
66
DEFAULT_HOST = "www.example.com"
77

88
included do
9-
metadata[:type] = :feature
10-
119
app = ::Rails.application
1210
if app.respond_to?(:routes)
1311
include app.routes.url_helpers if app.routes.respond_to?(:url_helpers)

lib/rspec/rails/example/helper_example_group.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ def _controller_path(example)
3030
end
3131

3232
included do
33-
metadata[:type] = :helper
34-
3533
before do |example|
3634
controller.controller_path = _controller_path(example)
3735
end

lib/rspec/rails/example/mailer_example_group.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ module MailerExampleGroup
66
include ActionMailer::TestCase::Behavior
77

88
included do
9-
metadata[:type] = :mailer
109
include ::Rails.application.routes.url_helpers
1110
options = ::Rails.configuration.action_mailer.default_url_options
1211
options.each { |key, value| default_url_options[key] = value } if options

lib/rspec/rails/example/model_example_group.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,5 @@ module RSpec::Rails
22
module ModelExampleGroup
33
extend ActiveSupport::Concern
44
include RSpec::Rails::RailsExampleGroup
5-
6-
included do
7-
metadata[:type] = :model
8-
end
95
end
106
end

lib/rspec/rails/example/request_example_group.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ def app
1313
end
1414

1515
included do
16-
metadata[:type] = :request
17-
1816
before do
1917
@routes = ::Rails.application.routes
2018
end

0 commit comments

Comments
 (0)