From 502d5e24e28b3634910495d0fb71cb20b1426aee Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Wed, 29 Aug 2012 14:23:15 -0500 Subject: Add automatic template digests to all CacheHelper#cache calls (originally spiked in the cache_digests plugin) *DHH* --- actionpack/test/template/digestor_test.rb | 128 ++++++++++++++++++++++++++++++ 1 file changed, 128 insertions(+) create mode 100644 actionpack/test/template/digestor_test.rb (limited to 'actionpack/test/template') diff --git a/actionpack/test/template/digestor_test.rb b/actionpack/test/template/digestor_test.rb new file mode 100644 index 0000000000..067ab500f5 --- /dev/null +++ b/actionpack/test/template/digestor_test.rb @@ -0,0 +1,128 @@ +require 'abstract_unit' +require 'fileutils' + +class FixtureTemplate + attr_reader :source + + def initialize(template_path) + @source = File.read(template_path) + rescue Errno::ENOENT + raise ActionView::MissingTemplate.new([], "", [], true, []) + end +end + +class FixtureFinder + FIXTURES_DIR = "#{File.dirname(__FILE__)}/../fixtures/digestor" + TMP_DIR = "#{File.dirname(__FILE__)}/../tmp" + + def find(logical_name, keys, partial, options) + FixtureTemplate.new("#{TMP_DIR}/#{partial ? logical_name.gsub(%r|/([^/]+)$|, '/_\1') : logical_name}.#{options[:formats].first}.erb") + end +end + +class TemplateDigestorTest < ActionView::TestCase + def setup + FileUtils.cp_r FixtureFinder::FIXTURES_DIR, FixtureFinder::TMP_DIR + end + + def teardown + FileUtils.rm_r FixtureFinder::TMP_DIR + 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 + + 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 + + def test_logging_of_missing_template + assert_logged "Couldn't find template for digesting: messages/something_missing.html" do + digest("messages/show") + end + end + + def test_nested_template_directory + assert_digest_difference("messages/show") do + change_template("messages/actions/_move") + end + end + + 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 + + def test_collection_derived_from_record_dependency + assert_digest_difference("messages/show") do + change_template("events/_event") + end + end + + + private + def assert_logged(message) + log = StringIO.new + ActionView::Digestor.logger = Logger.new(log) + + yield + + log.rewind + assert_match message, log.read + + ActionView::Digestor.logger = nil + end + + def assert_digest_difference(template_name) + previous_digest = digest(template_name) + ActionView::Digestor.cache.clear + + yield + + assert previous_digest != digest(template_name), "digest didn't change" + ActionView::Digestor.cache.clear + end + + def digest(template_name) + ActionView::Digestor.digest(template_name, :html, FixtureFinder.new) + end + + def change_template(template_name) + File.open("#{FixtureFinder::TMP_DIR}/#{template_name}.html.erb", "w") do |f| + f.write "\nTHIS WAS CHANGED!" + end + end +end -- cgit v1.2.3