2012-08-29 15:23:15 -04:00
|
|
|
require 'abstract_unit'
|
|
|
|
require 'fileutils'
|
|
|
|
|
|
|
|
class FixtureTemplate
|
2012-12-16 17:38:46 -05:00
|
|
|
attr_reader :source, :handler
|
2012-09-05 12:51:24 -04:00
|
|
|
|
2012-08-29 15:23:15 -04:00
|
|
|
def initialize(template_path)
|
|
|
|
@source = File.read(template_path)
|
2013-02-20 10:42:30 -05:00
|
|
|
@handler = ActionView::Template.handler_for_extension(:erb)
|
2012-08-29 15:23:15 -04:00
|
|
|
rescue Errno::ENOENT
|
|
|
|
raise ActionView::MissingTemplate.new([], "", [], true, [])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class FixtureFinder
|
|
|
|
FIXTURES_DIR = "#{File.dirname(__FILE__)}/../fixtures/digestor"
|
2012-09-05 12:51:24 -04:00
|
|
|
|
2013-07-15 09:59:18 -04:00
|
|
|
attr_reader :details
|
|
|
|
|
|
|
|
def initialize
|
|
|
|
@details = {}
|
|
|
|
end
|
|
|
|
|
|
|
|
def details_key
|
|
|
|
details.hash
|
|
|
|
end
|
|
|
|
|
2013-04-25 10:14:22 -04:00
|
|
|
def find(logical_name, keys, partial, options)
|
2012-10-17 14:30:08 -04:00
|
|
|
FixtureTemplate.new("digestor/#{partial ? logical_name.gsub(%r|/([^/]+)$|, '/_\1') : logical_name}.#{options[:formats].first}.erb")
|
2012-08-29 15:23:15 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class TemplateDigestorTest < ActionView::TestCase
|
|
|
|
def setup
|
2012-10-17 14:30:08 -04:00
|
|
|
@cwd = Dir.pwd
|
|
|
|
@tmp_dir = Dir.mktmpdir
|
|
|
|
|
|
|
|
FileUtils.cp_r FixtureFinder::FIXTURES_DIR, @tmp_dir
|
|
|
|
Dir.chdir @tmp_dir
|
2012-08-29 15:23:15 -04:00
|
|
|
end
|
2012-09-05 12:51:24 -04:00
|
|
|
|
2012-08-29 15:23:15 -04:00
|
|
|
def teardown
|
2012-10-17 14:30:08 -04:00
|
|
|
Dir.chdir @cwd
|
|
|
|
FileUtils.rm_r @tmp_dir
|
2012-08-29 15:23:15 -04:00
|
|
|
ActionView::Digestor.cache.clear
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_top_level_change_reflected
|
|
|
|
assert_digest_difference("messages/show") do
|
|
|
|
change_template("messages/show")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_explicit_dependency
|
|
|
|
assert_digest_difference("messages/show") do
|
|
|
|
change_template("messages/_message")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-12-21 16:51:41 -05:00
|
|
|
def test_explicit_dependency_in_multiline_erb_tag
|
|
|
|
assert_digest_difference("messages/show") do
|
|
|
|
change_template("messages/_form")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-08-29 15:23:15 -04:00
|
|
|
def test_second_level_dependency
|
|
|
|
assert_digest_difference("messages/show") do
|
|
|
|
change_template("comments/_comments")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_second_level_dependency_within_same_directory
|
|
|
|
assert_digest_difference("messages/show") do
|
|
|
|
change_template("messages/_header")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_third_level_dependency
|
|
|
|
assert_digest_difference("messages/show") do
|
|
|
|
change_template("comments/_comment")
|
|
|
|
end
|
|
|
|
end
|
2012-10-27 10:03:18 -04:00
|
|
|
|
2012-10-11 12:36:58 -04:00
|
|
|
def test_directory_depth_dependency
|
|
|
|
assert_digest_difference("level/below/index") do
|
|
|
|
change_template("level/below/_header")
|
|
|
|
end
|
|
|
|
end
|
2012-08-29 15:23:15 -04:00
|
|
|
|
|
|
|
def test_logging_of_missing_template
|
|
|
|
assert_logged "Couldn't find template for digesting: messages/something_missing.html" do
|
|
|
|
digest("messages/show")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-04-29 18:42:16 -04:00
|
|
|
def test_logging_of_missing_template_ending_with_number
|
|
|
|
assert_logged "Couldn't find template for digesting: messages/something_missing_1.html" do
|
|
|
|
digest("messages/show")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-08-29 15:23:15 -04:00
|
|
|
def test_nested_template_directory
|
|
|
|
assert_digest_difference("messages/show") do
|
|
|
|
change_template("messages/actions/_move")
|
|
|
|
end
|
|
|
|
end
|
2012-09-05 12:51:24 -04:00
|
|
|
|
2013-07-07 12:44:16 -04:00
|
|
|
def test_recursion_in_renders
|
|
|
|
assert digest("level/recursion") # assert recursion is possible
|
|
|
|
assert_not_nil digest("level/recursion") # assert digest is stored
|
|
|
|
end
|
|
|
|
|
2013-07-19 10:45:14 -04:00
|
|
|
def test_chaining_the_top_template_on_recursion
|
2013-07-07 12:44:16 -04:00
|
|
|
assert digest("level/recursion") # assert recursion is possible
|
|
|
|
|
|
|
|
assert_digest_difference("level/recursion") do
|
|
|
|
change_template("level/recursion")
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_not_nil digest("level/recursion") # assert digest is stored
|
|
|
|
end
|
|
|
|
|
2013-07-19 10:45:14 -04:00
|
|
|
def test_chaining_the_partial_template_on_recursion
|
2013-07-07 12:44:16 -04:00
|
|
|
assert digest("level/recursion") # assert recursion is possible
|
|
|
|
|
|
|
|
assert_digest_difference("level/recursion") do
|
|
|
|
change_template("level/_recursion")
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_not_nil digest("level/recursion") # assert digest is stored
|
|
|
|
end
|
|
|
|
|
2012-08-29 15:23:15 -04:00
|
|
|
def test_dont_generate_a_digest_for_missing_templates
|
|
|
|
assert_equal '', digest("nothing/there")
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_collection_dependency
|
|
|
|
assert_digest_difference("messages/index") do
|
|
|
|
change_template("messages/_message")
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_digest_difference("messages/index") do
|
|
|
|
change_template("events/_event")
|
|
|
|
end
|
|
|
|
end
|
2012-09-05 12:51:24 -04:00
|
|
|
|
2012-08-29 15:23:15 -04:00
|
|
|
def test_collection_derived_from_record_dependency
|
|
|
|
assert_digest_difference("messages/show") do
|
|
|
|
change_template("events/_event")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-07-15 09:59:18 -04:00
|
|
|
def test_details_are_included_in_cache_key
|
|
|
|
# Cache the template digest.
|
|
|
|
old_digest = digest("events/_event")
|
|
|
|
|
|
|
|
# Change the template; the cached digest remains unchanged.
|
|
|
|
change_template("events/_event")
|
|
|
|
|
|
|
|
# The details are changed, so a new cache key is generated.
|
|
|
|
finder.details[:foo] = "bar"
|
|
|
|
|
|
|
|
# The cache is busted.
|
|
|
|
assert_not_equal old_digest, digest("events/_event")
|
|
|
|
end
|
|
|
|
|
2012-08-30 07:01:25 -04:00
|
|
|
def test_extra_whitespace_in_render_partial
|
|
|
|
assert_digest_difference("messages/edit") do
|
|
|
|
change_template("messages/_form")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_extra_whitespace_in_render_named_partial
|
|
|
|
assert_digest_difference("messages/edit") do
|
|
|
|
change_template("messages/_header")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_extra_whitespace_in_render_record
|
|
|
|
assert_digest_difference("messages/edit") do
|
|
|
|
change_template("messages/_message")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_extra_whitespace_in_render_with_parenthesis
|
|
|
|
assert_digest_difference("messages/edit") do
|
|
|
|
change_template("events/_event")
|
|
|
|
end
|
|
|
|
end
|
2012-08-29 15:23:15 -04:00
|
|
|
|
2012-08-30 07:13:47 -04:00
|
|
|
def test_old_style_hash_in_render_invocation
|
|
|
|
assert_digest_difference("messages/edit") do
|
|
|
|
change_template("comments/_comment")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-01-08 13:20:47 -05:00
|
|
|
def test_dependencies_via_options_results_in_different_digest
|
|
|
|
digest_plain = digest("comments/_comment")
|
|
|
|
digest_fridge = digest("comments/_comment", dependencies: ["fridge"])
|
|
|
|
digest_phone = digest("comments/_comment", dependencies: ["phone"])
|
|
|
|
digest_fridge_phone = digest("comments/_comment", dependencies: ["fridge", "phone"])
|
|
|
|
|
|
|
|
assert_not_equal digest_plain, digest_fridge
|
|
|
|
assert_not_equal digest_plain, digest_phone
|
|
|
|
assert_not_equal digest_plain, digest_fridge_phone
|
|
|
|
assert_not_equal digest_fridge, digest_phone
|
|
|
|
assert_not_equal digest_fridge, digest_fridge_phone
|
|
|
|
assert_not_equal digest_phone, digest_fridge_phone
|
|
|
|
end
|
|
|
|
|
2013-08-05 16:36:47 -04:00
|
|
|
def test_cache_template_loading
|
|
|
|
resolver_before = ActionView::Resolver.caching
|
|
|
|
ActionView::Resolver.caching = false
|
|
|
|
assert_digest_difference("messages/edit", true) do
|
|
|
|
change_template("comments/_comment")
|
|
|
|
end
|
|
|
|
ActionView::Resolver.caching = resolver_before
|
|
|
|
end
|
|
|
|
|
2012-08-29 15:23:15 -04:00
|
|
|
private
|
|
|
|
def assert_logged(message)
|
2012-09-05 12:51:24 -04:00
|
|
|
old_logger = ActionView::Base.logger
|
2012-08-29 15:23:15 -04:00
|
|
|
log = StringIO.new
|
2012-09-05 12:51:24 -04:00
|
|
|
ActionView::Base.logger = Logger.new(log)
|
2012-08-29 15:23:15 -04:00
|
|
|
|
2012-09-05 12:51:24 -04:00
|
|
|
begin
|
|
|
|
yield
|
|
|
|
|
|
|
|
log.rewind
|
|
|
|
assert_match message, log.read
|
|
|
|
ensure
|
|
|
|
ActionView::Base.logger = old_logger
|
|
|
|
end
|
2012-08-29 15:23:15 -04:00
|
|
|
end
|
|
|
|
|
2013-08-05 16:36:47 -04:00
|
|
|
def assert_digest_difference(template_name, persistent = false)
|
2012-08-29 15:23:15 -04:00
|
|
|
previous_digest = digest(template_name)
|
2013-08-05 16:36:47 -04:00
|
|
|
ActionView::Digestor.cache.clear unless persistent
|
2012-08-29 15:23:15 -04:00
|
|
|
|
|
|
|
yield
|
|
|
|
|
|
|
|
assert previous_digest != digest(template_name), "digest didn't change"
|
|
|
|
ActionView::Digestor.cache.clear
|
|
|
|
end
|
2012-09-05 12:51:24 -04:00
|
|
|
|
2013-01-08 13:20:47 -05:00
|
|
|
def digest(template_name, options={})
|
2013-07-15 09:59:18 -04:00
|
|
|
ActionView::Digestor.digest(template_name, :html, finder, options)
|
|
|
|
end
|
|
|
|
|
|
|
|
def finder
|
|
|
|
@finder ||= FixtureFinder.new
|
2012-08-29 15:23:15 -04:00
|
|
|
end
|
2012-09-05 12:51:24 -04:00
|
|
|
|
2012-08-29 15:23:15 -04:00
|
|
|
def change_template(template_name)
|
2012-10-17 14:30:08 -04:00
|
|
|
File.open("digestor/#{template_name}.html.erb", "w") do |f|
|
2012-08-29 15:23:15 -04:00
|
|
|
f.write "\nTHIS WAS CHANGED!"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|