Skip to content

Commit 33c3c73

Browse files
committed
Centralized config API extensions into a method.
This lets us re-use that method when we isolate config in our specs.
1 parent 98398d5 commit 33c3c73

File tree

9 files changed

+119
-126
lines changed

9 files changed

+119
-126
lines changed

lib/rspec/rails.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,11 @@
1-
require 'rspec/rails/without_filetype_infer'
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'
211
require 'rspec/rails/configuration'

lib/rspec/rails/configuration.rb

Lines changed: 108 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,111 @@
1-
RSpec::configure do |c|
1+
module RSpec
2+
module Rails
3+
# @private
4+
def self.add_rspec_rails_config_api_to(config)
5+
# controller settings
6+
config.add_setting :infer_base_class_for_anonymous_controllers, :default => true
7+
8+
# fixture support
9+
config.include RSpec::Rails::FixtureSupport
10+
config.add_setting :use_transactional_fixtures, :alias_with => :use_transactional_examples
11+
config.add_setting :use_instantiated_fixtures
12+
config.add_setting :global_fixtures
13+
config.add_setting :fixture_path
14+
15+
# view rendering settings
16+
# This allows us to expose `render_views` as a config option even though it
17+
# breaks the convention of other options by using `render_views` as a
18+
# command (i.e. render_views = true), where it would normally be used as a
19+
# getter. This makes it easier for rspec-rails users because we use
20+
# `render_views` directly in example groups, so this aligns the two APIs,
21+
# but requires this workaround:
22+
config.add_setting :rendering_views, :default => false
23+
24+
def config.render_views=(val)
25+
self.rendering_views = val
26+
end
27+
28+
def config.render_views
29+
self.rendering_views = true
30+
end
31+
32+
def config.render_views?
33+
rendering_views
34+
end
35+
36+
def config.infer_spec_type_from_file_location!
37+
def self.escaped_path(*parts)
38+
Regexp.compile(parts.join('[\\\/]') + '[\\\/]')
39+
end
40+
41+
controller_path_regex = self.escaped_path(%w[spec controllers])
42+
self.include RSpec::Rails::ControllerExampleGroup,
43+
:type => :controller,
44+
:file_path => lambda { |file_path, metadata|
45+
metadata[:type].nil? && controller_path_regex =~ file_path
46+
}
47+
48+
helper_path_regex = self.escaped_path(%w[spec helpers])
49+
self.include RSpec::Rails::HelperExampleGroup,
50+
:type => :helper,
51+
:file_path => lambda { |file_path, metadata|
52+
metadata[:type].nil? && helper_path_regex =~ file_path
53+
}
54+
55+
mailer_path_regex = self.escaped_path(%w[spec mailers])
56+
if defined?(RSpec::Rails::MailerExampleGroup)
57+
self.include RSpec::Rails::MailerExampleGroup,
58+
:type => :mailer,
59+
:file_path => lambda { |file_path, metadata|
60+
metadata[:type].nil? && mailer_path_regex =~ file_path
61+
}
62+
end
63+
64+
model_path_regex = self.escaped_path(%w[spec models])
65+
self.include RSpec::Rails::ModelExampleGroup,
66+
:type => :model,
67+
:file_path => lambda { |file_path, metadata|
68+
metadata[:type].nil? && model_path_regex =~ file_path
69+
}
70+
71+
request_path_regex = self.escaped_path(%w[spec (requests|integration|api)])
72+
self.include RSpec::Rails::RequestExampleGroup,
73+
:type => :request,
74+
:file_path => lambda { |file_path, metadata|
75+
metadata[:type].nil? && request_path_regex =~ file_path
76+
}
77+
78+
routing_path_regex = self.escaped_path(%w[spec routing])
79+
self.include RSpec::Rails::RoutingExampleGroup,
80+
:type => :routing,
81+
:file_path => lambda { |file_path, metadata|
82+
metadata[:type].nil? && routing_path_regex =~ file_path
83+
}
84+
85+
view_path_regex = self.escaped_path(%w[spec views])
86+
self.include RSpec::Rails::ViewExampleGroup,
87+
:type => :view,
88+
:file_path => lambda { |file_path, metadata|
89+
metadata[:type].nil? && view_path_regex =~ file_path
90+
}
91+
92+
feature_example_regex = self.escaped_path(%w[spec features])
93+
self.include RSpec::Rails::FeatureExampleGroup,
94+
:type => :feature,
95+
:file_path => lambda { |file_path, metadata|
96+
metadata[:type].nil? && feature_example_regex =~ file_path
97+
}
98+
end
99+
end
100+
end
101+
end
102+
103+
RSpec.configure do |c|
104+
RSpec::Rails.add_rspec_rails_config_api_to(c)
105+
106+
c.backtrace_exclusion_patterns << /vendor\//
107+
c.backtrace_exclusion_patterns << /lib\/rspec\/rails/
108+
2109
def c.escaped_path(*parts)
3110
Regexp.compile(parts.join('[\\\/]') + '[\\\/]')
4111
end

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 & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
RSpec.configure do |config|
2-
config.add_setting :infer_base_class_for_anonymous_controllers, :default => true
3-
end
4-
51
module RSpec::Rails
62
module ControllerExampleGroup
73
extend ActiveSupport::Concern

lib/rspec/rails/fixture_support.rb

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,6 @@ module FixtureSupport
2626
fixtures RSpec.configuration.global_fixtures if RSpec.configuration.global_fixtures
2727
end
2828
end
29-
30-
RSpec.configure do |c|
31-
c.include RSpec::Rails::FixtureSupport
32-
c.add_setting :use_transactional_fixtures, :alias_with => :use_transactional_examples
33-
c.add_setting :use_instantiated_fixtures
34-
c.add_setting :global_fixtures
35-
c.add_setting :fixture_path
36-
end
3729
end
3830
end
3931
end

lib/rspec/rails/infer_type_configuration.rb

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

lib/rspec/rails/view_rendering.rb

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,5 @@
11
require 'action_view/testing/resolvers'
22

3-
RSpec.configure do |config|
4-
# This allows us to expose `render_views` as a config option even though it
5-
# breaks the convention of other options by using `render_views` as a
6-
# command (i.e. render_views = true), where it would normally be used as a
7-
# getter. This makes it easier for rspec-rails users because we use
8-
# `render_views` directly in example groups, so this aligns the two APIs,
9-
# but requires this workaround:
10-
config.add_setting :rendering_views, :default => false
11-
12-
def config.render_views=(val)
13-
self.rendering_views = val
14-
end
15-
16-
def config.render_views
17-
self.rendering_views = true
18-
end
19-
20-
def config.render_views?
21-
rendering_views
22-
end
23-
end
24-
253
module RSpec
264
module Rails
275
module ViewRendering

lib/rspec/rails/without_filetype_infer.rb

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

spec/support/helpers.rb

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,7 @@ def metadata_with(additional_metadata)
1111
def with_isolated_config
1212
original_config = RSpec.configuration
1313
RSpec.configuration = RSpec::Core::Configuration.new
14-
RSpec.configure do |c|
15-
c.include RSpec::Rails::FixtureSupport
16-
c.add_setting :use_transactional_fixtures, :alias_with => :use_transactional_examples
17-
c.add_setting :use_instantiated_fixtures
18-
c.add_setting :global_fixtures
19-
c.add_setting :fixture_path
20-
end
14+
RSpec::Rails.add_rspec_rails_config_api_to(RSpec.configuration)
2115
yield
2216
ensure
2317
RSpec.configuration = original_config

0 commit comments

Comments
 (0)