1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/actionview/test/template/log_subscriber_test.rb

301 lines
11 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
require "abstract_unit"
require "active_support/log_subscriber/test_helper"
require "action_view/log_subscriber"
require "controller/fake_models"
class AVLogSubscriberTest < ActiveSupport::TestCase
include ActiveSupport::LogSubscriber::TestHelper
def setup
2010-03-02 17:40:59 -05:00
super
ActionView::LookupContext::DetailsKey.clear
2019-01-23 19:11:54 -05:00
view_paths = ActionController::Base.view_paths
lookup_context = ActionView::LookupContext.new(view_paths, {}, ["test"])
@view = ActionView::Base.with_empty_template_cache.with_context(lookup_context)
ActionView::LogSubscriber.attach_to :action_view
2015-08-22 00:03:53 -04:00
unless Rails.respond_to?(:root)
@defined_root = true
def Rails.root; :defined_root; end # Minitest `stub` expects the method to be defined.
end
end
def teardown
super
Clear Resolvers' cache after AVLogSubscriberTest tests Addresses the build failure below: https://buildkite.com/rails/rails/builds/70853#29647167-4db2-4810-810e-d2c3a4e6dcf8 * This pull request addresses this error. ``` $ bin/test test/template/log_subscriber_test.rb test/template/test_case_test.rb -n "/^(?:AVLogSubscriberTest#(?:test_render_collection_template)|ActionView::HelperInclusionTest#(?:test_helper_class_that_is_being_tested_is_always_included_in_view_instance))$/" --seed 54118 Run options: -n "/^(?:AVLogSubscriberTest#(?:test_render_collection_template)|ActionView::HelperInclusionTest#(?:test_helper_class_that_is_being_tested_is_always_included_in_view_instance))$/" --seed 54118 .E Error: ActionView::HelperInclusionTest#test_helper_class_that_is_being_tested_is_always_included_in_view_instance: ActionView::Template::Error: undefined method `_test__customer_erb___3224798146126036163_10260' for #<#<Class:0x00005653f1b35768>:0x00005653f1b348e0> /home/yahonda/src/github.com/rails/rails/actionview/lib/action_view/base.rb:276:in `_run' /home/yahonda/src/github.com/rails/rails/actionview/lib/action_view/template.rb:182:in `block in render' /home/yahonda/src/github.com/rails/rails/activesupport/lib/active_support/notifications.rb:205:in `instrument' /home/yahonda/src/github.com/rails/rails/actionview/lib/action_view/template.rb:384:in `instrument_render_template' /home/yahonda/src/github.com/rails/rails/actionview/lib/action_view/template.rb:180:in `render' /home/yahonda/src/github.com/rails/rails/actionview/lib/action_view/renderer/collection_renderer.rb:186:in `block in collection_with_template' /home/yahonda/src/github.com/rails/rails/actionview/lib/action_view/renderer/collection_renderer.rb:66:in `block in each_with_info' /home/yahonda/src/github.com/rails/rails/actionview/lib/action_view/renderer/collection_renderer.rb:66:in `each' /home/yahonda/src/github.com/rails/rails/actionview/lib/action_view/renderer/collection_renderer.rb:66:in `each_with_info' /home/yahonda/src/github.com/rails/rails/actionview/lib/action_view/renderer/collection_renderer.rb:177:in `each' /home/yahonda/src/github.com/rails/rails/actionview/lib/action_view/renderer/collection_renderer.rb:177:in `map' /home/yahonda/src/github.com/rails/rails/actionview/lib/action_view/renderer/collection_renderer.rb:177:in `collection_with_template' /home/yahonda/src/github.com/rails/rails/actionview/lib/action_view/renderer/collection_renderer.rb:159:in `block (2 levels) in render_collection' /home/yahonda/src/github.com/rails/rails/actionview/lib/action_view/renderer/partial_renderer/collection_caching.rb:21:in `cache_collection_render' /home/yahonda/src/github.com/rails/rails/actionview/lib/action_view/renderer/collection_renderer.rb:158:in `block in render_collection' /home/yahonda/src/github.com/rails/rails/activesupport/lib/active_support/notifications.rb:203:in `block in instrument' /home/yahonda/src/github.com/rails/rails/activesupport/lib/active_support/notifications/instrumenter.rb:24:in `instrument' /home/yahonda/src/github.com/rails/rails/activesupport/lib/active_support/notifications.rb:203:in `instrument' /home/yahonda/src/github.com/rails/rails/actionview/lib/action_view/renderer/collection_renderer.rb:144:in `render_collection' /home/yahonda/src/github.com/rails/rails/actionview/lib/action_view/renderer/collection_renderer.rb:116:in `render_collection_with_partial' /home/yahonda/src/github.com/rails/rails/actionview/lib/action_view/renderer/renderer.rb:75:in `render_partial_to_object' /home/yahonda/src/github.com/rails/rails/actionview/lib/action_view/renderer/renderer.rb:27:in `render_to_object' /home/yahonda/src/github.com/rails/rails/actionview/lib/action_view/renderer/renderer.rb:22:in `render' /home/yahonda/src/github.com/rails/rails/actionview/lib/action_view/helpers/rendering_helper.rb:38:in `block in render' /home/yahonda/src/github.com/rails/rails/actionview/lib/action_view/base.rb:306:in `in_rendering_context' /home/yahonda/src/github.com/rails/rails/actionview/lib/action_view/helpers/rendering_helper.rb:34:in `render' /home/yahonda/src/github.com/rails/rails/actionview/lib/action_view/test_case.rb:205:in `render' /home/yahonda/src/github.com/rails/rails/actionview/test/template/test_case_test.rb:126:in `render_from_helper' /home/yahonda/src/github.com/rails/rails/actionview/test/fixtures/test/_from_helper.erb:1:in `__home_yahonda_src_github_com_rails_rails_actionview_test_fixtures_test__from_helper_erb___900298654238864924_10340' /home/yahonda/src/github.com/rails/rails/actionview/lib/action_view/base.rb:276:in `_run' /home/yahonda/src/github.com/rails/rails/actionview/lib/action_view/template.rb:182:in `block in render' /home/yahonda/src/github.com/rails/rails/activesupport/lib/active_support/notifications.rb:205:in `instrument' /home/yahonda/src/github.com/rails/rails/actionview/lib/action_view/template.rb:384:in `instrument_render_template' /home/yahonda/src/github.com/rails/rails/actionview/lib/action_view/template.rb:180:in `render' /home/yahonda/src/github.com/rails/rails/actionview/lib/action_view/renderer/partial_renderer.rb:285:in `block in render_partial_template' /home/yahonda/src/github.com/rails/rails/activesupport/lib/active_support/notifications.rb:203:in `block in instrument' /home/yahonda/src/github.com/rails/rails/activesupport/lib/active_support/notifications/instrumenter.rb:24:in `instrument' /home/yahonda/src/github.com/rails/rails/activesupport/lib/active_support/notifications.rb:203:in `instrument' /home/yahonda/src/github.com/rails/rails/actionview/lib/action_view/renderer/partial_renderer.rb:280:in `render_partial_template' /home/yahonda/src/github.com/rails/rails/actionview/lib/action_view/renderer/partial_renderer.rb:271:in `render' /home/yahonda/src/github.com/rails/rails/actionview/lib/action_view/renderer/renderer.rb:84:in `render_partial_to_object' /home/yahonda/src/github.com/rails/rails/actionview/lib/action_view/renderer/renderer.rb:27:in `render_to_object' /home/yahonda/src/github.com/rails/rails/actionview/lib/action_view/renderer/renderer.rb:22:in `render' /home/yahonda/src/github.com/rails/rails/actionview/lib/action_view/helpers/rendering_helper.rb:38:in `block in render' /home/yahonda/src/github.com/rails/rails/actionview/lib/action_view/base.rb:306:in `in_rendering_context' /home/yahonda/src/github.com/rails/rails/actionview/lib/action_view/helpers/rendering_helper.rb:34:in `render' /home/yahonda/src/github.com/rails/rails/actionview/lib/action_view/test_case.rb:205:in `render' /home/yahonda/src/github.com/rails/rails/actionview/lib/action_view/test_case.rb:122:in `render' /home/yahonda/src/github.com/rails/rails/actionview/test/template/test_case_test.rb:136:in `block in <class:HelperInclusionTest>' bin/test test/template/test_case_test.rb:132 Finished in 0.076637s, 26.0970 runs/s, 39.1454 assertions/s. 2 runs, 3 assertions, 0 failures, 1 errors, 0 skips $ ``` Refer #36189 for the similar fix
2020-07-29 23:11:19 -04:00
ActionController::Base.view_paths.map(&:clear_cache)
ActiveSupport::LogSubscriber.log_subscribers.clear
2015-08-22 00:03:53 -04:00
# We need to undef `root`, RenderTestCases don't want this to be defined
Rails.instance_eval { undef :root } if defined?(@defined_root)
end
def set_logger(logger)
2012-01-18 12:05:36 -05:00
ActionView::Base.logger = logger
end
def set_cache_controller
controller = ActionController::Base.new
controller.perform_caching = true
controller.cache_store = ActiveSupport::Cache::MemoryStore.new
@view.controller = controller
end
def set_view_cache_dependencies
def @view.view_cache_dependencies; []; end
2017-05-18 12:12:32 -04:00
def @view.combined_fragment_cache_key(*); "ahoy `controller` dependency"; end
end
def test_render_template_template
Rails.stub(:root, File.expand_path(FIXTURE_LOAD_PATH)) do
@view.render(template: "test/hello_world")
wait
assert_equal 1, @logger.logged(:debug).size
assert_equal 1, @logger.logged(:info).size
assert_match(/Rendering test\/hello_world\.erb/, @logger.logged(:debug).last)
assert_match(/Rendered test\/hello_world\.erb/, @logger.logged(:info).last)
end
end
def test_render_template_with_layout
Rails.stub(:root, File.expand_path(FIXTURE_LOAD_PATH)) do
@view.render(template: "test/hello_world", layout: "layouts/yield")
wait
assert_equal 2, @logger.logged(:debug).size
assert_equal 2, @logger.logged(:info).size
assert_match(/Rendering layout layouts\/yield\.erb/, @logger.logged(:debug).first)
assert_match(/Rendering test\/hello_world\.erb within layouts\/yield/, @logger.logged(:debug).last)
assert_match(/Rendered test\/hello_world\.erb within layouts\/yield/, @logger.logged(:info).first)
assert_match(/Rendered layout layouts\/yield\.erb/, @logger.logged(:info).last)
end
end
def test_render_file_template
2015-08-22 00:03:53 -04:00
Rails.stub(:root, File.expand_path(FIXTURE_LOAD_PATH)) do
@view.render(file: "#{FIXTURE_LOAD_PATH}/test/hello_world.erb")
2015-08-22 00:03:53 -04:00
wait
assert_equal 1, @logger.logged(:debug).size
assert_equal 1, @logger.logged(:info).size
assert_match(/Rendering test\/hello_world\.erb/, @logger.logged(:debug).last)
2015-08-22 00:03:53 -04:00
assert_match(/Rendered test\/hello_world\.erb/, @logger.logged(:info).last)
end
end
def test_render_text_template
2015-08-22 00:03:53 -04:00
Rails.stub(:root, File.expand_path(FIXTURE_LOAD_PATH)) do
@view.render(plain: "TEXT")
2015-08-22 00:03:53 -04:00
wait
assert_equal 1, @logger.logged(:debug).size
assert_equal 1, @logger.logged(:info).size
assert_match(/Rendering text template/, @logger.logged(:debug).last)
2015-08-22 00:03:53 -04:00
assert_match(/Rendered text template/, @logger.logged(:info).last)
end
end
def test_render_inline_template
2015-08-22 00:03:53 -04:00
Rails.stub(:root, File.expand_path(FIXTURE_LOAD_PATH)) do
2016-08-06 13:36:34 -04:00
@view.render(inline: "<%= 'TEXT' %>")
2015-08-22 00:03:53 -04:00
wait
assert_equal 1, @logger.logged(:debug).size
assert_equal 1, @logger.logged(:info).size
assert_match(/Rendering inline template/, @logger.logged(:debug).last)
2015-08-22 00:03:53 -04:00
assert_match(/Rendered inline template/, @logger.logged(:info).last)
end
end
def test_render_partial_with_implicit_path
2015-08-22 00:03:53 -04:00
Rails.stub(:root, File.expand_path(FIXTURE_LOAD_PATH)) do
@view.render(Customer.new("david"), greeting: "hi")
2015-08-22 00:03:53 -04:00
wait
assert_equal 1, @logger.logged(:debug).size
assert_match(/Rendered customers\/_customer\.html\.erb/, @logger.logged(:debug).last)
2015-08-22 00:03:53 -04:00
end
end
def test_render_partial_with_cache_missed
2015-08-22 00:03:53 -04:00
Rails.stub(:root, File.expand_path(FIXTURE_LOAD_PATH)) do
set_view_cache_dependencies
set_cache_controller
@view.render(partial: "test/cached_customer", locals: { cached_customer: Customer.new("david") })
2015-08-22 00:03:53 -04:00
wait
assert_equal 1, @logger.logged(:debug).size
assert_match(/Rendered test\/_cached_customer\.erb (.*) \[cache miss\]/, @logger.logged(:debug).last)
end
end
def test_render_partial_with_cache_hitted
Rails.stub(:root, File.expand_path(FIXTURE_LOAD_PATH)) do
set_view_cache_dependencies
set_cache_controller
# Second render should hit cache.
@view.render(partial: "test/cached_customer", locals: { cached_customer: Customer.new("david") })
@view.render(partial: "test/cached_customer", locals: { cached_customer: Customer.new("david") })
wait
assert_equal 2, @logger.logged(:debug).size
assert_match(/Rendered test\/_cached_customer\.erb (.*) \[cache hit\]/, @logger.logged(:debug).last)
end
end
def test_render_partial_as_layout
Rails.stub(:root, File.expand_path(FIXTURE_LOAD_PATH)) do
set_view_cache_dependencies
set_cache_controller
@view.render(layout: "layouts/yield_only") { "hello" }
assert_equal 1, @logger.logged(:debug).size
assert_match(/Rendered layouts\/_yield_only\.erb/, @logger.logged(:debug).first)
end
end
def test_render_partial_with_layout
Rails.stub(:root, File.expand_path(FIXTURE_LOAD_PATH)) do
set_view_cache_dependencies
set_cache_controller
@view.render(partial: "partial", layout: "layouts/yield_only")
assert_equal 1, @logger.logged(:debug).size
assert_match(/Rendered test\/_partial\.html\.erb within layouts\/_yield_only/, @logger.logged(:debug).first)
end
end
def test_render_uncached_outer_partial_with_inner_cached_partial_wont_mix_cache_hits_or_misses
Rails.stub(:root, File.expand_path(FIXTURE_LOAD_PATH)) do
set_view_cache_dependencies
set_cache_controller
@view.render(partial: "test/nested_cached_customer", locals: { cached_customer: Customer.new("Stan") })
wait
*, cached_inner, uncached_outer = @logger.logged(:debug)
assert_match(/Rendered test\/_cached_customer\.erb (.*) \[cache miss\]/, cached_inner)
assert_match(/Rendered test\/_nested_cached_customer\.erb \(Duration: .*?ms \| Allocations: .*?\)$/, uncached_outer)
# Second render hits the cache for the _cached_customer partial. Outer template's log shouldn't be affected.
@view.render(partial: "test/nested_cached_customer", locals: { cached_customer: Customer.new("Stan") })
wait
*, cached_inner, uncached_outer = @logger.logged(:debug)
assert_match(/Rendered test\/_cached_customer\.erb (.*) \[cache hit\]/, cached_inner)
assert_match(/Rendered test\/_nested_cached_customer\.erb \(Duration: .*?ms \| Allocations: .*?\)$/, uncached_outer)
end
end
def test_render_cached_outer_partial_with_cached_inner_partial
Rails.stub(:root, File.expand_path(FIXTURE_LOAD_PATH)) do
set_view_cache_dependencies
set_cache_controller
@view.render(partial: "test/cached_nested_cached_customer", locals: { cached_customer: Customer.new("Stan") })
wait
*, cached_inner, cached_outer = @logger.logged(:debug)
assert_match(/Rendered test\/_cached_customer\.erb (.*) \[cache miss\]/, cached_inner)
assert_match(/Rendered test\/_cached_nested_cached_customer\.erb (.*) \[cache miss\]/, cached_outer)
# One render: inner partial skipped, because the outer has been cached.
assert_difference -> { @logger.logged(:debug).size }, +1 do
@view.render(partial: "test/cached_nested_cached_customer", locals: { cached_customer: Customer.new("Stan") })
wait
end
assert_match(/Rendered test\/_cached_nested_cached_customer\.erb (.*) \[cache hit\]/, @logger.logged(:debug).last)
end
end
def test_render_partial_with_cache_hitted_and_missed
Rails.stub(:root, File.expand_path(FIXTURE_LOAD_PATH)) do
set_view_cache_dependencies
set_cache_controller
@view.render(partial: "test/cached_customer", locals: { cached_customer: Customer.new("david") })
wait
assert_match(/Rendered test\/_cached_customer\.erb (.*) \[cache miss\]/, @logger.logged(:debug).last)
@view.render(partial: "test/cached_customer", locals: { cached_customer: Customer.new("david") })
wait
assert_match(/Rendered test\/_cached_customer\.erb (.*) \[cache hit\]/, @logger.logged(:debug).last)
@view.render(partial: "test/cached_customer", locals: { cached_customer: Customer.new("Stan") })
wait
assert_match(/Rendered test\/_cached_customer\.erb (.*) \[cache miss\]/, @logger.logged(:debug).last)
2015-08-22 00:03:53 -04:00
end
end
def test_render_collection_template
2015-08-22 00:03:53 -04:00
Rails.stub(:root, File.expand_path(FIXTURE_LOAD_PATH)) do
set_cache_controller
2016-08-06 13:36:34 -04:00
@view.render(partial: "test/customer", collection: [ Customer.new("david"), Customer.new("mary") ])
2015-08-22 00:03:53 -04:00
wait
assert_equal 1, @logger.logged(:debug).size
assert_match(/Rendered collection of test\/_customer.erb \[2 times\]/, @logger.logged(:debug).last)
2015-08-22 00:03:53 -04:00
end
end
def test_render_collection_template_with_layout
Rails.stub(:root, File.expand_path(FIXTURE_LOAD_PATH)) do
set_cache_controller
@view.render(partial: "test/customer", layout: "layouts/yield_only", collection: [ Customer.new("david"), Customer.new("mary") ])
wait
assert_equal 1, @logger.logged(:debug).size
assert_match(/Rendered collection of test\/_customer.erb within layouts\/_yield_only \[2 times\]/, @logger.logged(:debug).last)
end
end
def test_render_collection_with_implicit_path
2015-08-22 00:03:53 -04:00
Rails.stub(:root, File.expand_path(FIXTURE_LOAD_PATH)) do
set_cache_controller
2016-08-06 13:36:34 -04:00
@view.render([ Customer.new("david"), Customer.new("mary") ], greeting: "hi")
2015-08-22 00:03:53 -04:00
wait
assert_equal 1, @logger.logged(:debug).size
assert_match(/Rendered collection of customers\/_customer\.html\.erb \[2 times\]/, @logger.logged(:debug).last)
2015-08-22 00:03:53 -04:00
end
end
def test_render_collection_template_without_path
2015-08-22 00:03:53 -04:00
Rails.stub(:root, File.expand_path(FIXTURE_LOAD_PATH)) do
set_cache_controller
2016-08-06 13:36:34 -04:00
@view.render([ GoodCustomer.new("david"), Customer.new("mary") ], greeting: "hi")
2015-08-22 00:03:53 -04:00
wait
assert_equal 1, @logger.logged(:debug).size
assert_match(/Rendered collection of templates/, @logger.logged(:debug).last)
end
end
def test_render_collection_with_cached_set
Rails.stub(:root, File.expand_path(FIXTURE_LOAD_PATH)) do
set_view_cache_dependencies
set_cache_controller
@view.render(partial: "customers/customer", collection: [ Customer.new("david"), Customer.new("mary") ], cached: true,
locals: { greeting: "hi" })
wait
assert_equal 1, @logger.logged(:debug).size
assert_match(/Rendered collection of customers\/_customer\.html\.erb \[0 \/ 2 cache hits\]/, @logger.logged(:debug).last)
2015-08-22 00:03:53 -04:00
end
end
end