2017-07-23 11:36:41 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-05-15 10:17:28 -04:00
|
|
|
$:.unshift File.expand_path("lib", __dir__)
|
2013-04-16 18:06:11 -04:00
|
|
|
|
2017-05-15 10:17:28 -04:00
|
|
|
ENV["TMPDIR"] = File.expand_path("tmp", __dir__)
|
2013-04-16 18:06:11 -04:00
|
|
|
|
2016-08-06 12:50:17 -04:00
|
|
|
require "active_support/core_ext/kernel/reporting"
|
2013-04-16 18:06:11 -04:00
|
|
|
|
|
|
|
# These are the normal settings that will be set up by Railties
|
|
|
|
# TODO: Have these tests support other combinations of these values
|
|
|
|
silence_warnings do
|
2017-01-11 03:48:00 -05:00
|
|
|
Encoding.default_internal = Encoding::UTF_8
|
|
|
|
Encoding.default_external = Encoding::UTF_8
|
2013-04-16 18:06:11 -04:00
|
|
|
end
|
|
|
|
|
2016-08-06 12:50:17 -04:00
|
|
|
require "active_support/testing/autorun"
|
2019-08-02 00:24:21 -04:00
|
|
|
require "active_support/testing/method_call_assertions"
|
2016-08-06 12:50:17 -04:00
|
|
|
require "action_controller"
|
|
|
|
require "action_view"
|
|
|
|
require "action_view/testing/resolvers"
|
|
|
|
require "active_support/dependencies"
|
|
|
|
require "active_model"
|
2013-04-16 18:06:11 -04:00
|
|
|
|
2020-05-02 16:18:19 -04:00
|
|
|
module ActionViewTestSuiteUtils
|
|
|
|
def self.require_helpers(helpers_dirs)
|
|
|
|
Array(helpers_dirs).each do |helpers_dir|
|
|
|
|
Dir.glob("#{helpers_dir}/**/*_helper.rb") do |helper_file|
|
|
|
|
require helper_file
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
ActionViewTestSuiteUtils.require_helpers("#{__dir__}/fixtures/helpers")
|
|
|
|
ActionViewTestSuiteUtils.require_helpers("#{__dir__}/fixtures/alternate_helpers")
|
|
|
|
|
2013-04-16 18:06:11 -04:00
|
|
|
ActiveSupport::Dependencies.hook!
|
|
|
|
|
|
|
|
Thread.abort_on_exception = true
|
|
|
|
|
|
|
|
# Show backtraces for deprecated behavior for quicker cleanup.
|
|
|
|
ActiveSupport::Deprecation.debug = true
|
|
|
|
|
2013-12-16 13:04:07 -05:00
|
|
|
# Disable available locale checks to avoid warnings running the test suite.
|
|
|
|
I18n.enforce_available_locales = false
|
|
|
|
|
2014-10-27 12:28:53 -04:00
|
|
|
ORIGINAL_LOCALES = I18n.available_locales.map(&:to_s).sort
|
2013-04-16 18:06:11 -04:00
|
|
|
|
2017-05-15 10:17:28 -04:00
|
|
|
FIXTURE_LOAD_PATH = File.expand_path("fixtures", __dir__)
|
2013-04-16 18:06:11 -04:00
|
|
|
|
|
|
|
module RenderERBUtils
|
|
|
|
def view
|
|
|
|
@view ||= begin
|
|
|
|
path = ActionView::FileSystemResolver.new(FIXTURE_LOAD_PATH)
|
|
|
|
view_paths = ActionView::PathSet.new([path])
|
2019-01-23 17:19:50 -05:00
|
|
|
view = ActionView::Base.with_empty_template_cache
|
|
|
|
view.with_view_paths(view_paths)
|
2013-04-16 18:06:11 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def render_erb(string)
|
2020-12-30 07:24:58 -05:00
|
|
|
@virtual_path = nil
|
|
|
|
|
2013-04-16 18:06:11 -04:00
|
|
|
template = ActionView::Template.new(
|
|
|
|
string.strip,
|
|
|
|
"test template",
|
2019-02-01 19:10:02 -05:00
|
|
|
ActionView::Template.handler_for_extension(:erb),
|
2019-02-25 18:14:53 -05:00
|
|
|
format: :html, locals: [])
|
2013-04-16 18:06:11 -04:00
|
|
|
|
2019-01-23 17:19:50 -05:00
|
|
|
view = ActionView::Base.with_empty_template_cache
|
|
|
|
template.render(view.empty, {}).strip
|
2013-04-16 18:06:11 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class RoutedRackApp
|
|
|
|
attr_reader :routes
|
|
|
|
|
|
|
|
def initialize(routes, &blk)
|
|
|
|
@routes = routes
|
|
|
|
@stack = ActionDispatch::MiddlewareStack.new(&blk).build(@routes)
|
|
|
|
end
|
|
|
|
|
|
|
|
def call(env)
|
|
|
|
@stack.call(env)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class BasicController
|
2020-07-28 05:57:36 -04:00
|
|
|
attr_accessor :request, :response
|
2013-04-16 18:06:11 -04:00
|
|
|
|
|
|
|
def config
|
|
|
|
@config ||= ActiveSupport::InheritableOptions.new(ActionController::Base.config).tap do |config|
|
|
|
|
# VIEW TODO: View tests should not require a controller
|
2017-05-15 10:17:28 -04:00
|
|
|
public_dir = File.expand_path("fixtures/public", __dir__)
|
2013-04-16 18:06:11 -04:00
|
|
|
config.assets_dir = public_dir
|
|
|
|
config.javascripts_dir = "#{public_dir}/javascripts"
|
|
|
|
config.stylesheets_dir = "#{public_dir}/stylesheets"
|
2016-08-06 13:44:11 -04:00
|
|
|
config.assets = ActiveSupport::InheritableOptions.new(prefix: "assets")
|
2013-04-16 18:06:11 -04:00
|
|
|
config
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class ActionDispatch::IntegrationTest < ActiveSupport::TestCase
|
|
|
|
def self.build_app(routes = nil)
|
2018-09-24 17:49:26 -04:00
|
|
|
routes ||= ActionDispatch::Routing::RouteSet.new.tap { |rs|
|
2018-09-25 13:21:40 -04:00
|
|
|
rs.draw { }
|
2018-09-24 17:49:26 -04:00
|
|
|
}
|
|
|
|
RoutedRackApp.new(routes) do |middleware|
|
2015-08-08 10:27:23 -04:00
|
|
|
middleware.use ActionDispatch::ShowExceptions, ActionDispatch::PublicExceptions.new("#{FIXTURE_LOAD_PATH}/public")
|
|
|
|
middleware.use ActionDispatch::DebugExceptions
|
|
|
|
middleware.use ActionDispatch::Callbacks
|
|
|
|
middleware.use ActionDispatch::Cookies
|
|
|
|
middleware.use ActionDispatch::Flash
|
|
|
|
middleware.use Rack::Head
|
2013-04-16 18:06:11 -04:00
|
|
|
yield(middleware) if block_given?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
self.app = build_app
|
|
|
|
|
|
|
|
def with_routing(&block)
|
|
|
|
temporary_routes = ActionDispatch::Routing::RouteSet.new
|
|
|
|
old_app, self.class.app = self.class.app, self.class.build_app(temporary_routes)
|
|
|
|
|
|
|
|
yield temporary_routes
|
|
|
|
ensure
|
|
|
|
self.class.app = old_app
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-01-31 23:12:37 -05:00
|
|
|
ActionView::RoutingUrlFor.include(ActionDispatch::Routing::UrlFor)
|
2013-07-16 09:25:25 -04:00
|
|
|
|
2013-04-16 18:06:11 -04:00
|
|
|
module ActionController
|
|
|
|
class Base
|
|
|
|
self.view_paths = FIXTURE_LOAD_PATH
|
|
|
|
|
|
|
|
def self.test_routes(&block)
|
|
|
|
routes = ActionDispatch::Routing::RouteSet.new
|
|
|
|
routes.draw(&block)
|
|
|
|
include routes.url_helpers
|
2018-09-24 17:49:26 -04:00
|
|
|
routes
|
2013-04-16 18:06:11 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class TestCase
|
|
|
|
include ActionDispatch::TestProcess
|
|
|
|
|
2018-09-24 17:49:26 -04:00
|
|
|
def self.with_routes(&block)
|
|
|
|
routes = ActionDispatch::Routing::RouteSet.new
|
|
|
|
routes.draw(&block)
|
|
|
|
include Module.new {
|
|
|
|
define_method(:setup) do
|
|
|
|
super()
|
|
|
|
@routes = routes
|
Address `ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true`
This pull request fixes #38697
It is caused by `@controller.singleton_class.include @routes.url_helpers` when `@controller` is nil in `ActionController::TestCase`.
* Without this commit
```ruby
% cd actionview
% PARALLEL_WORKERS=1 bin/test test/actionpack/controller/layout_test.rb test/template/url_helper_test.rb --seed 16702 -n "/^(?:LayoutSetInResponseTest#(?:test_layout_symbol_set_in_controller_returning_nil_falls_back_to_default)|UrlHelperTest#(?:test_url_for_with_array_and_only_path_set_to_false))$/"
Run options: --seed 16702 -n "/^(?:LayoutSetInResponseTest#(?:test_layout_symbol_set_in_controller_returning_nil_falls_back_to_default)|UrlHelperTest#(?:test_url_for_with_array_and_only_path_set_to_false))$/"
.E
Error:
UrlHelperTest#test_url_for_with_array_and_only_path_set_to_false:
ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true
/Users/yahonda/src/github.com/rails/rails/actionpack/lib/action_dispatch/http/url.rb:64:in `full_url_for'
/Users/yahonda/src/github.com/rails/rails/actionpack/lib/action_dispatch/http/url.rb:54:in `url_for'
/Users/yahonda/src/github.com/rails/rails/actionpack/lib/action_dispatch/routing/route_set.rb:333:in `block in <class:RouteSet>'
/Users/yahonda/src/github.com/rails/rails/actionpack/lib/action_dispatch/routing/route_set.rb:838:in `url_for'
/Users/yahonda/src/github.com/rails/rails/actionpack/lib/action_dispatch/routing/route_set.rb:270:in `call'
/Users/yahonda/src/github.com/rails/rails/actionpack/lib/action_dispatch/routing/route_set.rb:213:in `call'
/Users/yahonda/src/github.com/rails/rails/actionpack/lib/action_dispatch/routing/route_set.rb:326:in `block in define_url_helper'
/Users/yahonda/src/github.com/rails/rails/actionpack/lib/action_dispatch/routing/polymorphic_routes.rb:233:in `polymorphic_method'
/Users/yahonda/src/github.com/rails/rails/actionpack/lib/action_dispatch/routing/polymorphic_routes.rb:116:in `polymorphic_url'
/Users/yahonda/src/github.com/rails/rails/actionview/lib/action_view/routing_url_for.rb:104:in `url_for'
/Users/yahonda/src/github.com/rails/rails/actionview/test/template/url_helper_test.rb:102:in `test_url_for_with_array_and_only_path_set_to_false'
bin/test test/template/url_helper_test.rb:100
Finished in 0.042275s, 47.3093 runs/s, 47.3093 assertions/s.
2 runs, 2 assertions, 0 failures, 1 errors, 0 skips
%
```
2020-08-12 06:25:04 -04:00
|
|
|
@controller.singleton_class.include @routes.url_helpers if @controller
|
2018-09-24 17:49:26 -04:00
|
|
|
end
|
|
|
|
}
|
|
|
|
routes
|
|
|
|
end
|
|
|
|
|
|
|
|
def with_routes(&block)
|
|
|
|
@routes = ActionDispatch::Routing::RouteSet.new
|
|
|
|
@routes.draw(&block)
|
|
|
|
@routes
|
|
|
|
end
|
2013-04-16 18:06:11 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
module ActionDispatch
|
|
|
|
class DebugExceptions
|
|
|
|
private
|
2016-08-06 13:55:02 -04:00
|
|
|
remove_method :stderr_logger
|
2016-09-14 04:57:52 -04:00
|
|
|
# Silence logger
|
2016-08-06 13:55:02 -04:00
|
|
|
def stderr_logger
|
|
|
|
nil
|
|
|
|
end
|
2013-04-16 18:06:11 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-08-22 00:03:53 -04:00
|
|
|
class ActiveSupport::TestCase
|
2021-01-25 14:50:41 -05:00
|
|
|
if Process.respond_to?(:fork) && !Gem.win_platform?
|
|
|
|
parallelize
|
|
|
|
else
|
|
|
|
parallelize(with: :threads)
|
|
|
|
end
|
2019-08-02 00:24:11 -04:00
|
|
|
|
2019-08-02 00:24:21 -04:00
|
|
|
include ActiveSupport::Testing::MethodCallAssertions
|
|
|
|
|
2019-08-02 00:24:11 -04:00
|
|
|
private
|
|
|
|
# Skips the current run on Rubinius using Minitest::Assertions#skip
|
|
|
|
def rubinius_skip(message = "")
|
|
|
|
skip message if RUBY_ENGINE == "rbx"
|
|
|
|
end
|
|
|
|
|
|
|
|
# Skips the current run on JRuby using Minitest::Assertions#skip
|
|
|
|
def jruby_skip(message = "")
|
|
|
|
skip message if defined?(JRUBY_VERSION)
|
|
|
|
end
|
2015-08-22 00:03:53 -04:00
|
|
|
end
|
2019-03-24 13:59:28 -04:00
|
|
|
|
|
|
|
require_relative "../../tools/test_common"
|