提交 a38c749d 编写于 作者: Y Yehuda Katz

Sync 'rails/rails/master'

上级 42b32938
*2.3.0 [Edge]* *2.3.0 [Edge]*
* Added :silence option to BenchmarkHelper#benchmark and turned log_level into a hash parameter and deprecated the old use [DHH]
* Fixed the AssetTagHelper cache to use the computed asset host as part of the cache key instead of just assuming the its a string #1299 [DHH]
* Make ActionController#render(string) work as a shortcut for render :file/:template/:action => string. [#1435] [Pratik Naik] Examples: * Make ActionController#render(string) work as a shortcut for render :file/:template/:action => string. [#1435] [Pratik Naik] Examples:
# Instead of render(:action => 'other_action') # Instead of render(:action => 'other_action')
......
...@@ -91,7 +91,6 @@ def reload_application ...@@ -91,7 +91,6 @@ def reload_application
run_callbacks :prepare_dispatch run_callbacks :prepare_dispatch
Routing::Routes.reload Routing::Routes.reload
ActionView::Helpers::AssetTagHelper::AssetTag::Cache.clear
end end
# Cleanup the application by clearing out loaded classes so they can # Cleanup the application by clearing out loaded classes so they can
......
...@@ -81,8 +81,8 @@ class MultiPartNeededException < Exception ...@@ -81,8 +81,8 @@ class MultiPartNeededException < Exception
end end
# Create and initialize a new Session instance. # Create and initialize a new Session instance.
def initialize(app) def initialize(app = nil)
@application = app @application = app || ActionController::Dispatcher.new
reset! reset!
end end
...@@ -591,7 +591,6 @@ def reset! ...@@ -591,7 +591,6 @@ def reset!
# can use this method to open multiple sessions that ought to be tested # can use this method to open multiple sessions that ought to be tested
# simultaneously. # simultaneously.
def open_session(application = nil) def open_session(application = nil)
application ||= ActionController::Dispatcher.new
session = Integration::Session.new(application) session = Integration::Session.new(application)
# delegate the fixture accessors back to the test instance # delegate the fixture accessors back to the test instance
......
...@@ -60,8 +60,8 @@ def self.included(base) #:nodoc: ...@@ -60,8 +60,8 @@ def self.included(base) #:nodoc:
module ClassMethods module ClassMethods
def call_with_exception(env, exception) #:nodoc: def call_with_exception(env, exception) #:nodoc:
request = env["actioncontroller.rescue.request"] request = env["actioncontroller.rescue.request"] ||= Request.new(env)
response = env["actioncontroller.rescue.response"] response = env["actioncontroller.rescue.response"] ||= Response.new
new.process(request, response, :rescue_action, exception) new.process(request, response, :rescue_action, exception)
end end
end end
......
...@@ -18,12 +18,33 @@ module BenchmarkHelper ...@@ -18,12 +18,33 @@ module BenchmarkHelper
# That would add something like "Process data files (345.2ms)" to the log, # That would add something like "Process data files (345.2ms)" to the log,
# which you can then use to compare timings when optimizing your code. # which you can then use to compare timings when optimizing your code.
# #
# You may give an optional logger level as the second argument # You may give an optional logger level as the :level option.
# (:debug, :info, :warn, :error); the default value is :info. # (:debug, :info, :warn, :error); the default value is :info.
def benchmark(message = "Benchmarking", level = :info) #
# <% benchmark "Low-level files", :level => :debug do %>
# <%= lowlevel_files_operation %>
# <% end %>
#
# Finally, you can pass true as the third argument to silence all log activity
# inside the block. This is great for boiling down a noisy block to just a single statement:
#
# <% benchmark "Process data files", :level => :info, :silence => true do %>
# <%= expensive_and_chatty_files_operation %>
# <% end %>
def benchmark(message = "Benchmarking", options = {})
if controller.logger if controller.logger
ms = Benchmark.ms { yield } if options.is_a?(Symbol)
controller.logger.send(level, '%s (%.1fms)' % [message, ms]) ActiveSupport::Deprecation.warn("use benchmark('#{message}', :level => :#{options}) instead", caller)
options = { :level => options, :silence => false }
else
options.assert_valid_keys(:level, :silence)
options[:level] ||= :info
end
result = nil
ms = Benchmark.ms { result = options[:silence] ? controller.logger.silence { yield } : yield }
controller.logger.send(options[:level], '%s (%.1fms)' % [ message, ms ])
result
else else
yield yield
end end
......
...@@ -32,11 +32,6 @@ def test_reloads_routes_before_dispatch_if_in_loading_mode ...@@ -32,11 +32,6 @@ def test_reloads_routes_before_dispatch_if_in_loading_mode
dispatch(false) dispatch(false)
end end
def test_clears_asset_tag_cache_before_dispatch_if_in_loading_mode
ActionView::Helpers::AssetTagHelper::AssetTag::Cache.expects(:clear).once
dispatch(false)
end
def test_leaves_dependencies_after_dispatch_if_not_in_loading_mode def test_leaves_dependencies_after_dispatch_if_not_in_loading_mode
ActionController::Routing::Routes.expects(:reload).never ActionController::Routing::Routes.expects(:reload).never
ActiveSupport::Dependencies.expects(:clear).never ActiveSupport::Dependencies.expects(:clear).never
......
...@@ -10,6 +10,10 @@ def update ...@@ -10,6 +10,10 @@ def update
SessionUploadTest.last_request_type = ActionController::Base.param_parsers[request.content_type] SessionUploadTest.last_request_type = ActionController::Base.param_parsers[request.content_type]
render :text => "got here" render :text => "got here"
end end
def read
render :text => "File: #{params[:uploaded_data].read}"
end
end end
class SessionUploadTest < ActionController::IntegrationTest class SessionUploadTest < ActionController::IntegrationTest
...@@ -19,21 +23,43 @@ class << self ...@@ -19,21 +23,43 @@ class << self
attr_accessor :last_request_type attr_accessor :last_request_type
end end
# def setup def test_upload_and_read_file
# @session = ActionController::Integration::Session.new with_test_routing do
# end post '/read', :uploaded_data => fixture_file_upload(FILES_DIR + "/hello.txt", "text/plain")
def test_post_with_upload assert_equal "File: Hello", response.body
uses_mocha "test_post_with_upload" do end
ActiveSupport::Dependencies.stubs(:load?).returns(false) end
# The lint wrapper is used in integration tests
# instead of a normal StringIO class
InputWrapper = Rack::Lint::InputWrapper
def test_post_with_upload_with_unrewindable_input
InputWrapper.any_instance.expects(:rewind).raises(Errno::ESPIPE)
with_test_routing do
post '/read', :uploaded_data => fixture_file_upload(FILES_DIR + "/hello.txt", "text/plain")
assert_equal "File: Hello", response.body
end
end
def test_post_with_upload_with_params_parsing
with_test_routing do
params = { :uploaded_data => fixture_file_upload(FILES_DIR + "/mona_lisa.jpg", "image/jpg") }
post '/update', params, :location => 'blah'
assert_equal(:multipart_form, SessionUploadTest.last_request_type)
end
end
private
def with_test_routing
with_routing do |set| with_routing do |set|
set.draw do |map| set.draw do |map|
map.update 'update', :controller => "upload_test", :action => "update", :method => :post map.update 'update', :controller => "upload_test", :action => "update", :method => :post
map.read 'read', :controller => "upload_test", :action => "read", :method => :post
end end
params = { :uploaded_data => fixture_file_upload(FILES_DIR + "/mona_lisa.jpg", "image/jpg") } yield
post '/update', params, :location => 'blah'
assert_equal(:multipart_form, SessionUploadTest.last_request_type)
end end
end end
end
end end
...@@ -390,6 +390,13 @@ def test_rescue_dispatcher_exceptions ...@@ -390,6 +390,13 @@ def test_rescue_dispatcher_exceptions
assert_equal "no way", @response.body assert_equal "no way", @response.body
end end
def test_rescue_dispatcher_exceptions_without_request_set
@request.env['REQUEST_URI'] = '/no_way'
response = RescueController.call_with_exception(@request.env, ActionController::RoutingError.new("Route not found"))
assert_kind_of ActionController::Response, response
assert_equal "no way", response.body
end
protected protected
def with_all_requests_local(local = true) def with_all_requests_local(local = true)
old_local, ActionController::Base.consider_all_requests_local = old_local, ActionController::Base.consider_all_requests_local =
......
Hello
\ No newline at end of file
...@@ -38,8 +38,6 @@ def host_with_port() 'localhost' end ...@@ -38,8 +38,6 @@ def host_with_port() 'localhost' end
@controller.request = @request @controller.request = @request
ActionView::Helpers::AssetTagHelper::reset_javascript_include_default ActionView::Helpers::AssetTagHelper::reset_javascript_include_default
AssetTag::Cache.clear
AssetCollection::Cache.clear
end end
def teardown def teardown
...@@ -281,6 +279,26 @@ def test_should_not_modify_source_string ...@@ -281,6 +279,26 @@ def test_should_not_modify_source_string
assert_equal copy, source assert_equal copy, source
end end
def test_caching_image_path_with_caching_and_proc_asset_host_using_request
ENV['RAILS_ASSET_ID'] = ''
ActionController::Base.asset_host = Proc.new do |source, request|
if request.ssl?
"#{request.protocol}#{request.host_with_port}"
else
"#{request.protocol}assets#{source.length}.example.com"
end
end
ActionController::Base.perform_caching = true
@controller.request.stubs(:ssl?).returns(false)
assert_equal "http://assets15.example.com/images/xml.png", image_path("xml.png")
@controller.request.stubs(:ssl?).returns(true)
assert_equal "http://localhost/images/xml.png", image_path("xml.png")
end
def test_caching_javascript_include_tag_when_caching_on def test_caching_javascript_include_tag_when_caching_on
ENV["RAILS_ASSET_ID"] = "" ENV["RAILS_ASSET_ID"] = ""
ActionController::Base.asset_host = 'http://a0.example.com' ActionController::Base.asset_host = 'http://a0.example.com'
......
...@@ -4,32 +4,25 @@ ...@@ -4,32 +4,25 @@
class BenchmarkHelperTest < ActionView::TestCase class BenchmarkHelperTest < ActionView::TestCase
tests ActionView::Helpers::BenchmarkHelper tests ActionView::Helpers::BenchmarkHelper
class MockLogger def teardown
attr_reader :logged controller.logger.send(:clear_buffer)
def initialize
@logged = []
end
def method_missing(method, *args)
@logged << [method, args]
end
end end
def controller def controller
@controller ||= Struct.new(:logger).new(MockLogger.new) logger = ActiveSupport::BufferedLogger.new(StringIO.new)
logger.auto_flushing = false
@controller ||= Struct.new(:logger).new(logger)
end end
def test_without_block def test_without_block
assert_raise(LocalJumpError) { benchmark } assert_raise(LocalJumpError) { benchmark }
assert controller.logger.logged.empty? assert buffer.empty?
end end
def test_defaults def test_defaults
i_was_run = false i_was_run = false
benchmark { i_was_run = true } benchmark { i_was_run = true }
assert i_was_run assert i_was_run
assert 1, controller.logger.logged.size
assert_last_logged assert_last_logged
end end
...@@ -37,24 +30,57 @@ def test_with_message ...@@ -37,24 +30,57 @@ def test_with_message
i_was_run = false i_was_run = false
benchmark('test_run') { i_was_run = true } benchmark('test_run') { i_was_run = true }
assert i_was_run assert i_was_run
assert 1, controller.logger.logged.size
assert_last_logged 'test_run' assert_last_logged 'test_run'
end end
def test_with_message_and_level def test_with_message_and_deprecated_level
i_was_run = false i_was_run = false
benchmark('debug_run', :debug) { i_was_run = true }
assert_deprecated do
benchmark('debug_run', :debug) { i_was_run = true }
end
assert i_was_run assert i_was_run
assert 1, controller.logger.logged.size assert_last_logged 'debug_run'
assert_last_logged 'debug_run', :debug end
def test_within_level
controller.logger.level = ActiveSupport::BufferedLogger::DEBUG
benchmark('included_debug_run', :level => :debug) { }
assert_last_logged 'included_debug_run'
end
def test_outside_level
controller.logger.level = ActiveSupport::BufferedLogger::ERROR
benchmark('skipped_debug_run', :level => :debug) { }
assert_no_match(/skipped_debug_run/, buffer.last)
ensure
controller.logger.level = ActiveSupport::BufferedLogger::DEBUG
end end
def test_without_silencing
benchmark('debug_run', :silence => false) do
controller.logger.info "not silenced!"
end
assert_equal 2, buffer.size
end
def test_with_silencing
benchmark('debug_run', :silence => true) do
controller.logger.info "silenced!"
end
assert_equal 1, buffer.size
end
private private
def assert_last_logged(message = 'Benchmarking', level = :info) def buffer
last = controller.logger.logged.last controller.logger.send(:buffer)
assert 2, last.size end
assert_equal level, last.first
assert 1, last[1].size def assert_last_logged(message = 'Benchmarking')
assert last[1][0] =~ /^#{message} \(.*\)$/ assert_match(/^#{message} \(.*\)$/, buffer.last)
end end
end end
...@@ -14,6 +14,15 @@ class ApplicationController < ActionController::Base; end ...@@ -14,6 +14,15 @@ class ApplicationController < ActionController::Base; end
Test::Unit.run = false Test::Unit.run = false
class ConsoleAppTest < Test::Unit::TestCase class ConsoleAppTest < Test::Unit::TestCase
def test_app_method_should_return_integration_session
assert_nothing_thrown do
console_session = app
assert_not_nil console_session
assert_instance_of ActionController::Integration::Session,
console_session
end
end
uses_mocha 'console reload test' do uses_mocha 'console reload test' do
def test_reload_should_fire_preparation_callbacks def test_reload_should_fire_preparation_callbacks
a = b = c = nil a = b = c = nil
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册