Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix spec type inferrence #1002

Merged
merged 9 commits into from
Apr 22, 2014
Merged
8 changes: 7 additions & 1 deletion Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,17 @@ Breaking Changes for 3.0.0:
* Spec types are no longer inferred by location, they instead need to be
explicitly tagged. The old behaviour is enabled by
`config.infer_spec_type_from_file_location!`, which is still supplied
in the default generated `spec_helper.rb`. (Xavier Shay)
in the default generated `spec_helper.rb`. (Xavier Shay, Myron Marston)
* `controller` macro in controller specs no longer mutates
`:described_class` metadata. It still overrides the subject and sets
the controller, though. (Myron Marston)

Bug Fixes:

* Fix an inconsistency in the generated scaffold specs for a controller. (Andy Waite)
* Ensure `config.before(:all, :type => <type>)` hooks run before groups
of the given type, even when the type is inferred by the file
location. (Jon Rowe, Myron Marston)

### 3.0.0.beta2 / 2014-02-17
[Full Changelog](https://github.com/rspec/rspec-rails/compare/v3.0.0.beta1...v3.0.0.beta2)
Expand Down
11 changes: 10 additions & 1 deletion lib/rspec/rails.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,11 @@
require 'rspec/rails/without_filetype_infer'
require 'rspec/core'
require 'rspec/collection_matchers'
require 'rails/version'
require 'rspec/rails/extensions'
require 'rspec/rails/view_rendering'
require 'rspec/rails/adapters'
require 'rspec/rails/matchers'
require 'rspec/rails/fixture_support'
require 'rspec/rails/example'
require 'rspec/rails/vendor/capybara'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Were we relying on autoload for these before or something?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nevermind, spotted.

require 'rspec/rails/configuration'
129 changes: 69 additions & 60 deletions lib/rspec/rails/configuration.rb
Original file line number Diff line number Diff line change
@@ -1,63 +1,72 @@
RSpec::configure do |c|
def c.escaped_path(*parts)
Regexp.compile(parts.join('[\\\/]') + '[\\\/]')
end
module RSpec
module Rails
# @private
def self.initialize_configuration(config)
config.backtrace_exclusion_patterns << /vendor\//
config.backtrace_exclusion_patterns << /lib\/rspec\/rails/

controller_path_regex = c.escaped_path(%w[spec controllers])
c.include RSpec::Rails::ControllerExampleGroup,
:type => :controller,
:file_path => lambda { |file_path, metadata|
metadata[:type].nil? && controller_path_regex =~ file_path
}

helper_path_regex = c.escaped_path(%w[spec helpers])
c.include RSpec::Rails::HelperExampleGroup,
:type => :helper,
:file_path => lambda { |file_path, metadata|
metadata[:type].nil? && helper_path_regex =~ file_path
}

mailer_path_regex = c.escaped_path(%w[spec mailers])
if defined?(RSpec::Rails::MailerExampleGroup)
c.include RSpec::Rails::MailerExampleGroup,
:type => :mailer,
:file_path => lambda { |file_path, metadata|
metadata[:type].nil? && mailer_path_regex =~ file_path
}
end
config.include RSpec::Rails::ControllerExampleGroup, :type => :controller
config.include RSpec::Rails::HelperExampleGroup, :type => :helper
config.include RSpec::Rails::ModelExampleGroup, :type => :model
config.include RSpec::Rails::RequestExampleGroup, :type => :request
config.include RSpec::Rails::RoutingExampleGroup, :type => :routing
config.include RSpec::Rails::ViewExampleGroup, :type => :view
config.include RSpec::Rails::FeatureExampleGroup, :type => :feature

if defined?(RSpec::Rails::MailerExampleGroup)
config.include RSpec::Rails::MailerExampleGroup, :type => :mailer
end

# controller settings
config.add_setting :infer_base_class_for_anonymous_controllers, :default => true

# fixture support
config.include RSpec::Rails::FixtureSupport
config.add_setting :use_transactional_fixtures, :alias_with => :use_transactional_examples
config.add_setting :use_instantiated_fixtures
config.add_setting :global_fixtures
config.add_setting :fixture_path

# view rendering settings
# This allows us to expose `render_views` as a config option even though it
# breaks the convention of other options by using `render_views` as a
# command (i.e. render_views = true), where it would normally be used as a
# getter. This makes it easier for rspec-rails users because we use
# `render_views` directly in example groups, so this aligns the two APIs,
# but requires this workaround:
config.add_setting :rendering_views, :default => false

model_path_regex = c.escaped_path(%w[spec models])
c.include RSpec::Rails::ModelExampleGroup,
:type => :model,
:file_path => lambda { |file_path, metadata|
metadata[:type].nil? && model_path_regex =~ file_path
}

request_path_regex = c.escaped_path(%w[spec (requests|integration|api)])
c.include RSpec::Rails::RequestExampleGroup,
:type => :request,
:file_path => lambda { |file_path, metadata|
metadata[:type].nil? && request_path_regex =~ file_path
}

routing_path_regex = c.escaped_path(%w[spec routing])
c.include RSpec::Rails::RoutingExampleGroup,
:type => :routing,
:file_path => lambda { |file_path, metadata|
metadata[:type].nil? && routing_path_regex =~ file_path
}

view_path_regex = c.escaped_path(%w[spec views])
c.include RSpec::Rails::ViewExampleGroup,
:type => :view,
:file_path => lambda { |file_path, metadata|
metadata[:type].nil? && view_path_regex =~ file_path
}

feature_example_regex = c.escaped_path(%w[spec features])
c.include RSpec::Rails::FeatureExampleGroup,
:type => :feature,
:file_path => lambda { |file_path, metadata|
metadata[:type].nil? && feature_example_regex =~ file_path
}
def config.render_views=(val)
self.rendering_views = val
end

def config.render_views
self.rendering_views = true
end

def config.render_views?
rendering_views
end

def config.infer_spec_type_from_file_location!
{
:controller => %w[spec controllers],
:helper => %w[spec helpers],
:mailer => %w[spec mailers],
:model => %w[spec models],
:request => %w[spec (requests|integration|api)],
:routing => %w[spec routing],
:view => %w[spec views],
:feature => %w[spec features]
}.each do |type, dir_parts|
escaped_path = Regexp.compile(dir_parts.join('[\\\/]') + '[\\\/]')
define_derived_metadata(:file_path => escaped_path) do |metadata|
metadata[:type] ||= type
end
end
end
end

initialize_configuration RSpec.configuration
end
end
2 changes: 0 additions & 2 deletions lib/rspec/rails/example.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,3 @@
require 'rspec/rails/example/routing_example_group'
require 'rspec/rails/example/model_example_group'
require 'rspec/rails/example/feature_example_group'

require 'rspec/rails/infer_type_configuration'
11 changes: 3 additions & 8 deletions lib/rspec/rails/example/controller_example_group.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
RSpec.configure do |config|
config.add_setting :infer_base_class_for_anonymous_controllers, :default => true
end

module RSpec::Rails
module ControllerExampleGroup
extend ActiveSupport::Concern
Expand Down Expand Up @@ -60,7 +56,7 @@ def controller(base_class = nil, &body)
end
base_class ||= defined?(ApplicationController) ? ApplicationController : ActionController::Base

metadata[:described_class] = Class.new(base_class) do
new_controller_class = Class.new(base_class) do
def self.name
root_controller = defined?(ApplicationController) ? ApplicationController : ActionController::Base
if superclass == root_controller || superclass.abstract?
Expand All @@ -70,7 +66,8 @@ def self.name
end
end
end
metadata[:described_class].class_eval(&body)
new_controller_class.class_eval(&body)
(class << self; self; end).__send__(:define_method, :controller_class) { new_controller_class }

before do
@orig_routes = self.routes
Expand Down Expand Up @@ -155,8 +152,6 @@ def method_missing(method, *args, &block)
included do
subject { controller }

metadata[:type] = :controller

before do
self.routes = ::Rails.application.routes
end
Expand Down
2 changes: 0 additions & 2 deletions lib/rspec/rails/example/feature_example_group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ module FeatureExampleGroup
DEFAULT_HOST = "www.example.com"

included do
metadata[:type] = :feature

app = ::Rails.application
if app.respond_to?(:routes)
include app.routes.url_helpers if app.routes.respond_to?(:url_helpers)
Expand Down
2 changes: 0 additions & 2 deletions lib/rspec/rails/example/helper_example_group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ def _controller_path(example)
end

included do
metadata[:type] = :helper

before do |example|
controller.controller_path = _controller_path(example)
end
Expand Down
1 change: 0 additions & 1 deletion lib/rspec/rails/example/mailer_example_group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ module MailerExampleGroup
include ActionMailer::TestCase::Behavior

included do
metadata[:type] = :mailer
include ::Rails.application.routes.url_helpers
options = ::Rails.configuration.action_mailer.default_url_options
options.each { |key, value| default_url_options[key] = value } if options
Expand Down
4 changes: 0 additions & 4 deletions lib/rspec/rails/example/model_example_group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,5 @@ module RSpec::Rails
module ModelExampleGroup
extend ActiveSupport::Concern
include RSpec::Rails::RailsExampleGroup

included do
metadata[:type] = :model
end
end
end
2 changes: 0 additions & 2 deletions lib/rspec/rails/example/request_example_group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ def app
end

included do
metadata[:type] = :request

before do
@routes = ::Rails.application.routes
end
Expand Down
2 changes: 0 additions & 2 deletions lib/rspec/rails/example/routing_example_group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ def routes(&blk)
end

included do
metadata[:type] = :routing

before do
self.routes = ::Rails.application.routes
end
Expand Down
1 change: 0 additions & 1 deletion lib/rspec/rails/example/view_example_group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,6 @@ def _include_controller_helpers
included do
include ExampleMethods

metadata[:type] = :view
helper(*_default_helpers)

before do
Expand Down
8 changes: 0 additions & 8 deletions lib/rspec/rails/fixture_support.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,6 @@ module FixtureSupport
fixtures RSpec.configuration.global_fixtures if RSpec.configuration.global_fixtures
end
end

RSpec.configure do |c|
c.include RSpec::Rails::FixtureSupport
c.add_setting :use_transactional_fixtures, :alias_with => :use_transactional_examples
c.add_setting :use_instantiated_fixtures
c.add_setting :global_fixtures
c.add_setting :fixture_path
end
end
end
end
65 changes: 0 additions & 65 deletions lib/rspec/rails/infer_type_configuration.rb

This file was deleted.

Loading