diff options
author | thedarkone <nobody> | 2009-02-12 19:35:14 +0100 |
---|---|---|
committer | Joshua Peek <josh@joshpeek.com> | 2009-02-12 13:04:12 -0600 |
commit | 3942cb406e1d5db0ac00e03153809cc8dc4cc4db (patch) | |
tree | 3fda08b1509e81f209d5353180feec2086139a3f /actionpack/test | |
parent | b1d41bdfb06cb5f606f515965316a348d9bc9b47 (diff) | |
download | rails-3942cb406e1d5db0ac00e03153809cc8dc4cc4db.tar.gz rails-3942cb406e1d5db0ac00e03153809cc8dc4cc4db.tar.bz2 rails-3942cb406e1d5db0ac00e03153809cc8dc4cc4db.zip |
Port fast reloadable templates from rails-dev-boost.
Diffstat (limited to 'actionpack/test')
-rw-r--r-- | actionpack/test/abstract_unit.rb | 4 | ||||
-rw-r--r-- | actionpack/test/controller/view_paths_test.rb | 26 | ||||
-rw-r--r-- | actionpack/test/template/compiled_templates_test.rb | 44 | ||||
-rw-r--r-- | actionpack/test/template/render_test.rb | 29 |
4 files changed, 73 insertions, 30 deletions
diff --git a/actionpack/test/abstract_unit.rb b/actionpack/test/abstract_unit.rb index 07bd7ba71d..cdeee934d0 100644 --- a/actionpack/test/abstract_unit.rb +++ b/actionpack/test/abstract_unit.rb @@ -38,4 +38,8 @@ I18n.backend.store_translations 'pt-BR', {} ORIGINAL_LOCALES = I18n.available_locales.map(&:to_s).sort FIXTURE_LOAD_PATH = File.join(File.dirname(__FILE__), 'fixtures') +ActionView::Base.cache_template_loading = true ActionController::Base.view_paths = FIXTURE_LOAD_PATH +CACHED_VIEW_PATHS = ActionView::Base.cache_template_loading? ? + ActionController::Base.view_paths : + ActionController::Base.view_paths.map {|path| ActionView::Template::EagerPath.new(path.to_s)} diff --git a/actionpack/test/controller/view_paths_test.rb b/actionpack/test/controller/view_paths_test.rb index ac84e2dfcd..6468283270 100644 --- a/actionpack/test/controller/view_paths_test.rb +++ b/actionpack/test/controller/view_paths_test.rb @@ -41,31 +41,35 @@ class ViewLoadPathsTest < ActionController::TestCase def teardown ActiveSupport::Deprecation.behavior = @old_behavior end + + def assert_view_path_strings_are_equal(expected, actual) + assert_equal(expected.map {|path| path.sub(/\.\//, '')}, actual) + end def test_template_load_path_was_set_correctly - assert_equal [FIXTURE_LOAD_PATH], @controller.view_paths.map(&:to_s) + assert_view_path_strings_are_equal [FIXTURE_LOAD_PATH], @controller.view_paths.map(&:to_s) end def test_controller_appends_view_path_correctly @controller.append_view_path 'foo' - assert_equal [FIXTURE_LOAD_PATH, 'foo'], @controller.view_paths.map(&:to_s) + assert_view_path_strings_are_equal [FIXTURE_LOAD_PATH, 'foo'], @controller.view_paths.map(&:to_s) @controller.append_view_path(%w(bar baz)) - assert_equal [FIXTURE_LOAD_PATH, 'foo', 'bar', 'baz'], @controller.view_paths.map(&:to_s) + assert_view_path_strings_are_equal [FIXTURE_LOAD_PATH, 'foo', 'bar', 'baz'], @controller.view_paths.map(&:to_s) @controller.append_view_path(FIXTURE_LOAD_PATH) - assert_equal [FIXTURE_LOAD_PATH, 'foo', 'bar', 'baz', FIXTURE_LOAD_PATH], @controller.view_paths.map(&:to_s) + assert_view_path_strings_are_equal [FIXTURE_LOAD_PATH, 'foo', 'bar', 'baz', FIXTURE_LOAD_PATH], @controller.view_paths.map(&:to_s) end def test_controller_prepends_view_path_correctly @controller.prepend_view_path 'baz' - assert_equal ['baz', FIXTURE_LOAD_PATH], @controller.view_paths.map(&:to_s) + assert_view_path_strings_are_equal ['baz', FIXTURE_LOAD_PATH], @controller.view_paths.map(&:to_s) @controller.prepend_view_path(%w(foo bar)) - assert_equal ['foo', 'bar', 'baz', FIXTURE_LOAD_PATH], @controller.view_paths.map(&:to_s) + assert_view_path_strings_are_equal ['foo', 'bar', 'baz', FIXTURE_LOAD_PATH], @controller.view_paths.map(&:to_s) @controller.prepend_view_path(FIXTURE_LOAD_PATH) - assert_equal [FIXTURE_LOAD_PATH, 'foo', 'bar', 'baz', FIXTURE_LOAD_PATH], @controller.view_paths.map(&:to_s) + assert_view_path_strings_are_equal [FIXTURE_LOAD_PATH, 'foo', 'bar', 'baz', FIXTURE_LOAD_PATH], @controller.view_paths.map(&:to_s) end def test_template_appends_view_path_correctly @@ -73,10 +77,10 @@ class ViewLoadPathsTest < ActionController::TestCase class_view_paths = TestController.view_paths @controller.append_view_path 'foo' - assert_equal [FIXTURE_LOAD_PATH, 'foo'], @controller.view_paths.map(&:to_s) + assert_view_path_strings_are_equal [FIXTURE_LOAD_PATH, 'foo'], @controller.view_paths.map(&:to_s) @controller.append_view_path(%w(bar baz)) - assert_equal [FIXTURE_LOAD_PATH, 'foo', 'bar', 'baz'], @controller.view_paths.map(&:to_s) + assert_view_path_strings_are_equal [FIXTURE_LOAD_PATH, 'foo', 'bar', 'baz'], @controller.view_paths.map(&:to_s) assert_equal class_view_paths, TestController.view_paths end @@ -85,10 +89,10 @@ class ViewLoadPathsTest < ActionController::TestCase class_view_paths = TestController.view_paths @controller.prepend_view_path 'baz' - assert_equal ['baz', FIXTURE_LOAD_PATH], @controller.view_paths.map(&:to_s) + assert_view_path_strings_are_equal ['baz', FIXTURE_LOAD_PATH], @controller.view_paths.map(&:to_s) @controller.prepend_view_path(%w(foo bar)) - assert_equal ['foo', 'bar', 'baz', FIXTURE_LOAD_PATH], @controller.view_paths.map(&:to_s) + assert_view_path_strings_are_equal ['foo', 'bar', 'baz', FIXTURE_LOAD_PATH], @controller.view_paths.map(&:to_s) assert_equal class_view_paths, TestController.view_paths end diff --git a/actionpack/test/template/compiled_templates_test.rb b/actionpack/test/template/compiled_templates_test.rb index 55fa346fb8..a8f8455a54 100644 --- a/actionpack/test/template/compiled_templates_test.rb +++ b/actionpack/test/template/compiled_templates_test.rb @@ -2,8 +2,21 @@ require 'abstract_unit' require 'controller/fake_models' class CompiledTemplatesTest < Test::Unit::TestCase + def setup @compiled_templates = ActionView::Base::CompiledTemplates + + # first, if we are running the whole test suite with ReloadableTemplates + # try to undef all the methods through ReloadableTemplate's interfaces + unless ActionView::Base.cache_template_loading? + ActionController::Base.view_paths.each do |view_path| + view_path.paths.values.uniq!.each do |reloadable_template| + reloadable_template.undef_my_compiled_methods! + end + end + end + + # just purge anything that's left @compiled_templates.instance_methods.each do |m| @compiled_templates.send(:remove_method, m) if m =~ /^_run_/ end @@ -35,17 +48,6 @@ class CompiledTemplatesTest < Test::Unit::TestCase end end - def test_compiled_template_will_always_be_recompiled_when_template_is_not_cached - with_caching(false) do - ActionView::Template.any_instance.expects(:recompile?).times(3).returns(true) - assert_equal 0, @compiled_templates.instance_methods.size - assert_equal "Hello world!", render(:file => "#{FIXTURE_LOAD_PATH}/test/hello_world.erb") - ActionView::Template.any_instance.expects(:compile!).times(3) - 3.times { assert_equal "Hello world!", render(:file => "#{FIXTURE_LOAD_PATH}/test/hello_world.erb") } - assert_equal 1, @compiled_templates.instance_methods.size - end - end - def test_template_changes_are_not_reflected_with_cached_template_loading with_caching(true) do with_reloading(false) do @@ -63,9 +65,10 @@ class CompiledTemplatesTest < Test::Unit::TestCase with_reloading(true) do assert_equal "Hello world!", render(:file => "test/hello_world.erb") modify_template "test/hello_world.erb", "Goodbye world!" do + reset_mtime_of('test/hello_world.erb') assert_equal "Goodbye world!", render(:file => "test/hello_world.erb") - sleep(1) # Need to sleep so that the timestamp actually changes end + reset_mtime_of('test/hello_world.erb') assert_equal "Hello world!", render(:file => "test/hello_world.erb") end end @@ -74,10 +77,15 @@ class CompiledTemplatesTest < Test::Unit::TestCase private def render(*args) view_paths = ActionController::Base.view_paths - assert_equal ActionView::Template::Path, view_paths.first.class ActionView::Base.new(view_paths, {}).render(*args) end + def reset_mtime_of(template_name) + unless ActionView::Base.cache_template_loading? + ActionController::Base.view_paths.find_template(template_name).previously_last_modified = 10.seconds.ago + end + end + def modify_template(template, content) filename = "#{FIXTURE_LOAD_PATH}/#{template}" old_content = File.read(filename) @@ -100,12 +108,18 @@ class CompiledTemplatesTest < Test::Unit::TestCase end def with_reloading(reload_templates) - old_cache_template_loading = ActionView::Base.cache_template_loading + old_view_paths, old_cache_templates = ActionController::Base.view_paths, ActionView::Base.cache_template_loading begin ActionView::Base.cache_template_loading = !reload_templates + ActionController::Base.view_paths = view_paths_for(reload_templates) yield ensure - ActionView::Base.cache_template_loading = old_cache_template_loading + ActionController::Base.view_paths, ActionView::Base.cache_template_loading = old_view_paths, old_cache_templates end end + + def view_paths_for(reload_templates) + # reloadable paths are cheap to create + reload_templates ? ActionView::PathSet.new(CACHED_VIEW_PATHS.map(&:to_s)) : CACHED_VIEW_PATHS + end end diff --git a/actionpack/test/template/render_test.rb b/actionpack/test/template/render_test.rb index 34e7e82366..107c625e32 100644 --- a/actionpack/test/template/render_test.rb +++ b/actionpack/test/template/render_test.rb @@ -243,13 +243,34 @@ module RenderTestCases end end +module TemplatesSetupTeardown + def setup_view_paths_for(new_cache_template_loading) + @previous_cache_template_loading, ActionView::Base.cache_template_loading = ActionView::Base.cache_template_loading, new_cache_template_loading + view_paths = new_cache_template_loading ? CACHED_VIEW_PATHS : ActionView::Base.process_view_paths(CACHED_VIEW_PATHS.map(&:to_s)) + assert_equal(new_cache_template_loading ? ActionView::Template::EagerPath : ActionView::ReloadableTemplate::ReloadablePath, view_paths.first.class) + setup_view(view_paths) + end + + def teardown + ActionView::Base.cache_template_loading = @previous_cache_template_loading + end +end + class CachedRenderTest < Test::Unit::TestCase + include TemplatesSetupTeardown include RenderTestCases - # Ensure view path cache is primed def setup - view_paths = ActionController::Base.view_paths - assert_equal ActionView::Template::Path, view_paths.first.class - setup_view(view_paths) + setup_view_paths_for(cache_templates = true) end end + +class ReloadableRenderTest < Test::Unit::TestCase + include TemplatesSetupTeardown + include RenderTestCases + + def setup + setup_view_paths_for(cache_templates = false) + end +end + |