From c7760809bfc8e19362272b71b23a294d48195d65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Thu, 7 Oct 2010 21:30:19 +0200 Subject: Allow cache to be temporarily disabled through lookup_context. --- actionpack/test/template/lookup_context_test.rb | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'actionpack/test/template/lookup_context_test.rb') diff --git a/actionpack/test/template/lookup_context_test.rb b/actionpack/test/template/lookup_context_test.rb index cc71cb42d0..55d581e512 100644 --- a/actionpack/test/template/lookup_context_test.rb +++ b/actionpack/test/template/lookup_context_test.rb @@ -163,4 +163,25 @@ class LookupContextTest < ActiveSupport::TestCase template = @lookup_context.find("foo", "test", true) assert_equal "Bar", template.source end + + test "can disable the cache on demand" do + @lookup_context.view_paths = ActionView::FixtureResolver.new("test/_foo.erb" => "Foo") + old_template = @lookup_context.find("foo", "test", true) + + template = @lookup_context.find("foo", "test", true) + assert_equal template, old_template + + assert @lookup_context.cache + template = @lookup_context.disable_cache do + assert !@lookup_context.cache + @lookup_context.find("foo", "test", true) + end + assert @lookup_context.cache + + assert_not_equal template, old_template + end + + test "can have cache disabled on initialization" do + assert !ActionView::LookupContext.new(FIXTURE_LOAD_PATH, :cache => false).cache + end end \ No newline at end of file -- cgit v1.2.3 From 38d78f99d52801d8392a7229b40edae74cc3d142 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Sun, 10 Oct 2010 09:24:17 +0200 Subject: Resolvers now consider timestamps. Before this patch, every request in development caused the template to be compiled, regardless if it was updated in the filesystem or not. This patch now checks the timestamp and only compiles it again if any change was done. While this probably won't show any difference for current setups, but it will be useful for asset template handlers (like SASS), as compiling their templates is slower than ERb, Haml, etc. --- actionpack/test/template/lookup_context_test.rb | 45 +++++++++++++++++++++++++ 1 file changed, 45 insertions(+) (limited to 'actionpack/test/template/lookup_context_test.rb') diff --git a/actionpack/test/template/lookup_context_test.rb b/actionpack/test/template/lookup_context_test.rb index 55d581e512..23dfc1ba75 100644 --- a/actionpack/test/template/lookup_context_test.rb +++ b/actionpack/test/template/lookup_context_test.rb @@ -184,4 +184,49 @@ class LookupContextTest < ActiveSupport::TestCase test "can have cache disabled on initialization" do assert !ActionView::LookupContext.new(FIXTURE_LOAD_PATH, :cache => false).cache end +end + +class LookupContextWithFalseCaching < ActiveSupport::TestCase + def setup + @resolver = ActionView::FixtureResolver.new("test/_foo.erb" => ["Foo", Time.utc(2000)]) + @resolver.stubs(:caching?).returns(false) + @lookup_context = ActionView::LookupContext.new(@resolver, {}) + end + + test "templates are always found in the resolver but timestamp is checked before being compiled" do + template = @lookup_context.find("foo", "test", true) + assert_equal "Foo", template.source + + # Now we are going to change the template, but it won't change the returned template + # since the timestamp is the same. + @resolver.hash["test/_foo.erb"][0] = "Bar" + template = @lookup_context.find("foo", "test", true) + assert_equal "Foo", template.source + + # Now update the timestamp. + @resolver.hash["test/_foo.erb"][1] = Time.now.utc + template = @lookup_context.find("foo", "test", true) + assert_equal "Bar", template.source + end + + test "if no template was found in the second lookup, give it higher preference" do + template = @lookup_context.find("foo", "test", true) + assert_equal "Foo", template.source + + @resolver.hash.clear + assert_raise ActionView::MissingTemplate do + @lookup_context.find("foo", "test", true) + end + end + + test "if no template was cached in the first lookup, do not use the cache in the second" do + @resolver.hash.clear + assert_raise ActionView::MissingTemplate do + @lookup_context.find("foo", "test", true) + end + + @resolver.hash["test/_foo.erb"] = ["Foo", Time.utc(2000)] + template = @lookup_context.find("foo", "test", true) + assert_equal "Foo", template.source + end end \ No newline at end of file -- cgit v1.2.3 From 5ec27189b8b433145baa7270cf4219c5041f6a4c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Sun, 10 Oct 2010 23:11:50 +0200 Subject: Do not allow templates coming from Fallback resolvers to store a virtual path. --- actionpack/test/template/lookup_context_test.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'actionpack/test/template/lookup_context_test.rb') diff --git a/actionpack/test/template/lookup_context_test.rb b/actionpack/test/template/lookup_context_test.rb index 23dfc1ba75..850589b13b 100644 --- a/actionpack/test/template/lookup_context_test.rb +++ b/actionpack/test/template/lookup_context_test.rb @@ -100,8 +100,8 @@ class LookupContextTest < ActiveSupport::TestCase @lookup_context.with_fallbacks do assert_equal 3, @lookup_context.view_paths.size - assert @lookup_context.view_paths.include?(ActionView::FileSystemResolver.new("")) - assert @lookup_context.view_paths.include?(ActionView::FileSystemResolver.new("/")) + assert @lookup_context.view_paths.include?(ActionView::FallbackFileSystemResolver.new("")) + assert @lookup_context.view_paths.include?(ActionView::FallbackFileSystemResolver.new("/")) end end -- cgit v1.2.3 From 01ab6f961bff150d50c99f03fa3946f48ac29b17 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Thu, 14 Oct 2010 09:47:49 +0200 Subject: Remove :cache => true on lookup templates initialization. --- actionpack/test/template/lookup_context_test.rb | 4 ---- 1 file changed, 4 deletions(-) (limited to 'actionpack/test/template/lookup_context_test.rb') diff --git a/actionpack/test/template/lookup_context_test.rb b/actionpack/test/template/lookup_context_test.rb index 850589b13b..6d3b26e131 100644 --- a/actionpack/test/template/lookup_context_test.rb +++ b/actionpack/test/template/lookup_context_test.rb @@ -180,10 +180,6 @@ class LookupContextTest < ActiveSupport::TestCase assert_not_equal template, old_template end - - test "can have cache disabled on initialization" do - assert !ActionView::LookupContext.new(FIXTURE_LOAD_PATH, :cache => false).cache - end end class LookupContextWithFalseCaching < ActiveSupport::TestCase -- cgit v1.2.3 From 8624065bffd203e1f590ca431d468f9b38e9fb7f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Wed, 24 Nov 2010 12:38:52 +0100 Subject: Allow template handlers to store temp data. --- actionpack/test/template/lookup_context_test.rb | 29 +++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) (limited to 'actionpack/test/template/lookup_context_test.rb') diff --git a/actionpack/test/template/lookup_context_test.rb b/actionpack/test/template/lookup_context_test.rb index 6d3b26e131..c9dd27cf2a 100644 --- a/actionpack/test/template/lookup_context_test.rb +++ b/actionpack/test/template/lookup_context_test.rb @@ -180,6 +180,16 @@ class LookupContextTest < ActiveSupport::TestCase assert_not_equal template, old_template end + + test "data can be stored in cached templates" do + template = @lookup_context.find("hello_world", "test") + template.data["cached"] = "data" + assert_equal "Hello world!", template.source + + template = @lookup_context.find("hello_world", "test") + assert_equal "data", template.data["cached"] + assert_equal "Hello world!", template.source + end end class LookupContextWithFalseCaching < ActiveSupport::TestCase @@ -205,7 +215,7 @@ class LookupContextWithFalseCaching < ActiveSupport::TestCase assert_equal "Bar", template.source end - test "if no template was found in the second lookup, give it higher preference" do + test "if no template was found in the second lookup, with no cache, raise error" do template = @lookup_context.find("foo", "test", true) assert_equal "Foo", template.source @@ -215,7 +225,7 @@ class LookupContextWithFalseCaching < ActiveSupport::TestCase end end - test "if no template was cached in the first lookup, do not use the cache in the second" do + test "if no template was cached in the first lookup, retrieval should work in the second call" do @resolver.hash.clear assert_raise ActionView::MissingTemplate do @lookup_context.find("foo", "test", true) @@ -225,4 +235,19 @@ class LookupContextWithFalseCaching < ActiveSupport::TestCase template = @lookup_context.find("foo", "test", true) assert_equal "Foo", template.source end + + test "data can be stored as long as template was not updated" do + template = @lookup_context.find("foo", "test", true) + template.data["cached"] = "data" + assert_equal "Foo", template.source + + template = @lookup_context.find("foo", "test", true) + assert_equal "data", template.data["cached"] + assert_equal "Foo", template.source + + @resolver.hash["test/_foo.erb"][1] = Time.now.utc + template = @lookup_context.find("foo", "test", true) + assert_nil template.data["cached"] + assert_equal "Foo", template.source + end end \ No newline at end of file -- cgit v1.2.3 From 6c5a3bb3125735760e92f49c3824d757ef87c61e Mon Sep 17 00:00:00 2001 From: artemave Date: Sun, 12 Dec 2010 20:40:40 +0000 Subject: all tests pass --- actionpack/test/template/lookup_context_test.rb | 48 ++++++++++++------------- 1 file changed, 24 insertions(+), 24 deletions(-) (limited to 'actionpack/test/template/lookup_context_test.rb') diff --git a/actionpack/test/template/lookup_context_test.rb b/actionpack/test/template/lookup_context_test.rb index c9dd27cf2a..a629b3c046 100644 --- a/actionpack/test/template/lookup_context_test.rb +++ b/actionpack/test/template/lookup_context_test.rb @@ -80,18 +80,18 @@ class LookupContextTest < ActiveSupport::TestCase end test "find templates using the given view paths and configured details" do - template = @lookup_context.find("hello_world", "test") + template = @lookup_context.find("hello_world", %w(test)) assert_equal "Hello world!", template.source @lookup_context.locale = :da - template = @lookup_context.find("hello_world", "test") + template = @lookup_context.find("hello_world", %w(test)) assert_equal "Hey verden", template.source end test "found templates respects given formats if one cannot be found from template or handler" do ActionView::Template::Handlers::ERB.expects(:default_format).returns(nil) @lookup_context.formats = [:text] - template = @lookup_context.find("hello_world", "test") + template = @lookup_context.find("hello_world", %w(test)) assert_equal [:text], template.formats end @@ -137,44 +137,44 @@ class LookupContextTest < ActiveSupport::TestCase test "gives the key forward to the resolver, so it can be used as cache key" do @lookup_context.view_paths = ActionView::FixtureResolver.new("test/_foo.erb" => "Foo") - template = @lookup_context.find("foo", "test", true) + template = @lookup_context.find("foo", %w(test), true) assert_equal "Foo", template.source # Now we are going to change the template, but it won't change the returned template # since we will hit the cache. @lookup_context.view_paths.first.hash["test/_foo.erb"] = "Bar" - template = @lookup_context.find("foo", "test", true) + template = @lookup_context.find("foo", %w(test), true) assert_equal "Foo", template.source # This time we will change the locale. The updated template should be picked since # lookup_context generated a new key after we changed the locale. @lookup_context.locale = :da - template = @lookup_context.find("foo", "test", true) + template = @lookup_context.find("foo", %w(test), true) assert_equal "Bar", template.source # Now we will change back the locale and it will still pick the old template. # This is expected because lookup_context will reuse the previous key for :en locale. @lookup_context.locale = :en - template = @lookup_context.find("foo", "test", true) + template = @lookup_context.find("foo", %w(test), true) assert_equal "Foo", template.source # Finally, we can expire the cache. And the expected template will be used. @lookup_context.view_paths.first.clear_cache - template = @lookup_context.find("foo", "test", true) + template = @lookup_context.find("foo", %w(test), true) assert_equal "Bar", template.source end test "can disable the cache on demand" do @lookup_context.view_paths = ActionView::FixtureResolver.new("test/_foo.erb" => "Foo") - old_template = @lookup_context.find("foo", "test", true) + old_template = @lookup_context.find("foo", %w(test), true) - template = @lookup_context.find("foo", "test", true) + template = @lookup_context.find("foo", %w(test), true) assert_equal template, old_template assert @lookup_context.cache template = @lookup_context.disable_cache do assert !@lookup_context.cache - @lookup_context.find("foo", "test", true) + @lookup_context.find("foo", %w(test), true) end assert @lookup_context.cache @@ -182,11 +182,11 @@ class LookupContextTest < ActiveSupport::TestCase end test "data can be stored in cached templates" do - template = @lookup_context.find("hello_world", "test") + template = @lookup_context.find("hello_world", %w(test)) template.data["cached"] = "data" assert_equal "Hello world!", template.source - template = @lookup_context.find("hello_world", "test") + template = @lookup_context.find("hello_world", %w(test)) assert_equal "data", template.data["cached"] assert_equal "Hello world!", template.source end @@ -200,54 +200,54 @@ class LookupContextWithFalseCaching < ActiveSupport::TestCase end test "templates are always found in the resolver but timestamp is checked before being compiled" do - template = @lookup_context.find("foo", "test", true) + template = @lookup_context.find("foo", %w(test), true) assert_equal "Foo", template.source # Now we are going to change the template, but it won't change the returned template # since the timestamp is the same. @resolver.hash["test/_foo.erb"][0] = "Bar" - template = @lookup_context.find("foo", "test", true) + template = @lookup_context.find("foo", %w(test), true) assert_equal "Foo", template.source # Now update the timestamp. @resolver.hash["test/_foo.erb"][1] = Time.now.utc - template = @lookup_context.find("foo", "test", true) + template = @lookup_context.find("foo", %w(test), true) assert_equal "Bar", template.source end test "if no template was found in the second lookup, with no cache, raise error" do - template = @lookup_context.find("foo", "test", true) + template = @lookup_context.find("foo", %w(test), true) assert_equal "Foo", template.source @resolver.hash.clear assert_raise ActionView::MissingTemplate do - @lookup_context.find("foo", "test", true) + @lookup_context.find("foo", %w(test), true) end end test "if no template was cached in the first lookup, retrieval should work in the second call" do @resolver.hash.clear assert_raise ActionView::MissingTemplate do - @lookup_context.find("foo", "test", true) + @lookup_context.find("foo", %w(test), true) end @resolver.hash["test/_foo.erb"] = ["Foo", Time.utc(2000)] - template = @lookup_context.find("foo", "test", true) + template = @lookup_context.find("foo", %w(test), true) assert_equal "Foo", template.source end test "data can be stored as long as template was not updated" do - template = @lookup_context.find("foo", "test", true) + template = @lookup_context.find("foo", %w(test), true) template.data["cached"] = "data" assert_equal "Foo", template.source - template = @lookup_context.find("foo", "test", true) + template = @lookup_context.find("foo", %w(test), true) assert_equal "data", template.data["cached"] assert_equal "Foo", template.source @resolver.hash["test/_foo.erb"][1] = Time.now.utc - template = @lookup_context.find("foo", "test", true) + template = @lookup_context.find("foo", %w(test), true) assert_nil template.data["cached"] assert_equal "Foo", template.source end -end \ No newline at end of file +end -- cgit v1.2.3 From 4c44f0468a0b6d2dd9b67d801da4336ad9a169a0 Mon Sep 17 00:00:00 2001 From: Nick Sutterer Date: Thu, 30 Dec 2010 14:11:14 +0100 Subject: added tests for the MissingTemplate exception message. --- actionpack/test/template/lookup_context_test.rb | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'actionpack/test/template/lookup_context_test.rb') diff --git a/actionpack/test/template/lookup_context_test.rb b/actionpack/test/template/lookup_context_test.rb index a629b3c046..4258f88071 100644 --- a/actionpack/test/template/lookup_context_test.rb +++ b/actionpack/test/template/lookup_context_test.rb @@ -251,3 +251,24 @@ class LookupContextWithFalseCaching < ActiveSupport::TestCase assert_equal "Foo", template.source end end + +class TestMissingTemplate < ActiveSupport::TestCase + def setup + @lookup_context = ActionView::LookupContext.new("/Path/to/views", {}) + @details = "{:handlers=>[:erb, :rjs, :builder], :formats=>[:html, :text, :js, :css, :ics, :csv, :xml, :rss, :atom, :yaml, :multipart_form, :url_encoded_form, :json], :locale=>[:en, :en]}" + end + + test "if no template was found we get a helpful error message including the inheritance chain" do + e = assert_raise ActionView::MissingTemplate do + @lookup_context.find("foo", %w(parent child)) + end + assert_equal "Missing template parent/foo, child/foo with #{@details}. Searched in:\n * \"/Path/to/views\"\n", e.message + end + + test "if no partial was found we get a helpful error message including the inheritance chain" do + e = assert_raise ActionView::MissingTemplate do + @lookup_context.find("foo", %w(parent child), true) + end + assert_equal "Missing partial parent/foo, child/foo with #{@details}. Searched in:\n * \"/Path/to/views\"\n", e.message + end +end -- cgit v1.2.3 From 8e5d91062f25f8c97a3bd8c6dc7dc45e38b54388 Mon Sep 17 00:00:00 2001 From: Piotr Sarnacki Date: Fri, 31 Dec 2010 11:17:37 +0100 Subject: Don't be so picky on MissingTemplate error details, this fails randomly on 1.8.7 because of not ordered hash --- actionpack/test/template/lookup_context_test.rb | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) (limited to 'actionpack/test/template/lookup_context_test.rb') diff --git a/actionpack/test/template/lookup_context_test.rb b/actionpack/test/template/lookup_context_test.rb index 4258f88071..f7a684779c 100644 --- a/actionpack/test/template/lookup_context_test.rb +++ b/actionpack/test/template/lookup_context_test.rb @@ -255,20 +255,19 @@ end class TestMissingTemplate < ActiveSupport::TestCase def setup @lookup_context = ActionView::LookupContext.new("/Path/to/views", {}) - @details = "{:handlers=>[:erb, :rjs, :builder], :formats=>[:html, :text, :js, :css, :ics, :csv, :xml, :rss, :atom, :yaml, :multipart_form, :url_encoded_form, :json], :locale=>[:en, :en]}" end test "if no template was found we get a helpful error message including the inheritance chain" do e = assert_raise ActionView::MissingTemplate do @lookup_context.find("foo", %w(parent child)) end - assert_equal "Missing template parent/foo, child/foo with #{@details}. Searched in:\n * \"/Path/to/views\"\n", e.message + assert_match %r{Missing template parent/foo, child/foo with .* Searched in:\n \* "/Path/to/views"\n}, e.message end - + test "if no partial was found we get a helpful error message including the inheritance chain" do e = assert_raise ActionView::MissingTemplate do @lookup_context.find("foo", %w(parent child), true) end - assert_equal "Missing partial parent/foo, child/foo with #{@details}. Searched in:\n * \"/Path/to/views\"\n", e.message + assert_match %r{Missing partial parent/foo, child/foo with .* Searched in:\n \* "/Path/to/views"\n}, e.message end end -- cgit v1.2.3 From 262b2ea8cda20999ddf8c4bf13b7a70453e996d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Wed, 19 Jan 2011 23:42:10 +0100 Subject: Solve SystemStackError when changing locale inside ActionMailer [#5329 state:resolved] --- actionpack/test/template/lookup_context_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'actionpack/test/template/lookup_context_test.rb') diff --git a/actionpack/test/template/lookup_context_test.rb b/actionpack/test/template/lookup_context_test.rb index f7a684779c..f3b1335000 100644 --- a/actionpack/test/template/lookup_context_test.rb +++ b/actionpack/test/template/lookup_context_test.rb @@ -73,7 +73,7 @@ class LookupContextTest < ActiveSupport::TestCase assert_equal :pt, I18n.locale assert_equal :pt, @lookup_context.locale ensure - I18n.config = I18n.config.i18n_config + I18n.config = I18n.config.original_config end assert_equal :pt, I18n.locale -- cgit v1.2.3 From 6b1018526fb304727ee4191afc2d8a5e29e49eea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Sun, 28 Nov 2010 22:40:32 +0100 Subject: Use Mime::Type references. --- actionpack/test/template/lookup_context_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'actionpack/test/template/lookup_context_test.rb') diff --git a/actionpack/test/template/lookup_context_test.rb b/actionpack/test/template/lookup_context_test.rb index f3b1335000..8d063e66b0 100644 --- a/actionpack/test/template/lookup_context_test.rb +++ b/actionpack/test/template/lookup_context_test.rb @@ -47,7 +47,7 @@ class LookupContextTest < ActiveSupport::TestCase end test "handles */* formats" do - @lookup_context.formats = [:"*/*"] + @lookup_context.formats = ["*/*"] assert_equal Mime::SET, @lookup_context.formats end -- cgit v1.2.3