From 635aa91224b5743c5736f916db18e80713650b86 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Mon, 5 Oct 2009 09:41:08 -0500 Subject: More robust console test --- railties/test/application/console_test.rb | 52 +++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 railties/test/application/console_test.rb (limited to 'railties/test/application') diff --git a/railties/test/application/console_test.rb b/railties/test/application/console_test.rb new file mode 100644 index 0000000000..e8a4a4e158 --- /dev/null +++ b/railties/test/application/console_test.rb @@ -0,0 +1,52 @@ +require 'isolation/abstract_unit' + +class ConsoleTest < Test::Unit::TestCase + include ActiveSupport::Testing::Isolation + + def setup + build_app + boot_rails + + # Load steps taken from rails/commands/console.rb + require "#{rails_root}/config/environment" + require 'rails/console_app' + require 'rails/console_with_helpers' + end + + def test_app_method_should_return_integration_session + console_session = app + assert_not_nil console_session + assert_instance_of ActionController::Integration::Session, console_session + end + + def test_new_session_should_return_integration_session + session = new_session + assert_not_nil session + assert_instance_of ActionController::Integration::Session, session + end + + def test_reload_should_fire_preparation_callbacks + a = b = c = nil + + # TODO: These should be defined on the initializer + ActionDispatch::Callbacks.to_prepare { a = b = c = 1 } + ActionDispatch::Callbacks.to_prepare { b = c = 2 } + ActionDispatch::Callbacks.to_prepare { c = 3 } + + # Hide Reloading... output + silence_stream(STDOUT) do + reload! + end + + assert_equal 1, a + assert_equal 2, b + assert_equal 3, c + end + + def test_access_to_helpers + assert_not_nil helper + assert_instance_of ActionView::Base, helper + assert_equal 'Once upon a time in a world...', + helper.truncate('Once upon a time in a world far far away') + end +end -- cgit v1.2.3 From 444ba150bd344793ebd0bd5efa6efd93fee89a7c Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Mon, 5 Oct 2009 09:42:35 -0500 Subject: Put test in place for deprecated dispatcher --- railties/test/application/load_test.rb | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'railties/test/application') diff --git a/railties/test/application/load_test.rb b/railties/test/application/load_test.rb index 5158abdbb4..305cd7f273 100644 --- a/railties/test/application/load_test.rb +++ b/railties/test/application/load_test.rb @@ -43,6 +43,13 @@ module ApplicationTests assert Rails.application.new.is_a?(Rails::Application) end + # Passenger still uses AC::Dispatcher, so we need to + # keep it working for now + test "deprecated ActionController::Dispatcher still works" do + rackup + assert ActionController::Dispatcher.new.is_a?(Rails::Application) + end + test "the config object is available on the application object" do rackup assert_equal 'UTC', Rails.application.config.time_zone -- cgit v1.2.3 From 20d6938453f439531a13e2ef1fd0905edf56294c Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Mon, 5 Oct 2009 10:36:05 -0500 Subject: Rewrite FCGI handler test --- railties/test/application/fcgi_dispatcher_test.rb | 290 ++++++++++++++++++++++ 1 file changed, 290 insertions(+) create mode 100644 railties/test/application/fcgi_dispatcher_test.rb (limited to 'railties/test/application') diff --git a/railties/test/application/fcgi_dispatcher_test.rb b/railties/test/application/fcgi_dispatcher_test.rb new file mode 100644 index 0000000000..b8ab9f8996 --- /dev/null +++ b/railties/test/application/fcgi_dispatcher_test.rb @@ -0,0 +1,290 @@ +require 'isolation/abstract_unit' +require 'mocha' + +begin + +begin + require 'fcgi' +rescue LoadError + gem 'fcgi', '0.8.7' + require 'fcgi' +end + +class RailsFCGIHandlerTest < Test::Unit::TestCase + include ActiveSupport::Testing::Isolation + + def setup + build_app + boot_rails + + require "#{rails_root}/config/environment" + require 'rails/fcgi_handler' + + @log = StringIO.new + @handler = RailsFCGIHandler.new(@log) + end + + def test_process_restart + request = mock + FCGI.stubs(:each).yields(request) + + @handler.expects(:process_request).once + @handler.expects(:dispatcher_error).never + + @handler.expects(:when_ready).returns(:restart) + @handler.expects(:close_connection).with(request) + @handler.expects(:reload!).never + @handler.expects(:restart!) + + @handler.process! + end + + def test_process_exit + request = mock + FCGI.stubs(:each).yields(request) + + @handler.expects(:process_request).once + @handler.expects(:dispatcher_error).never + + @handler.expects(:when_ready).returns(:exit) + @handler.expects(:close_connection).with(request) + @handler.expects(:reload!).never + @handler.expects(:restart!).never + + @handler.process! + end + + def test_process_with_system_exit_exception + request = mock + FCGI.stubs(:each).yields(request) + + @handler.expects(:process_request).once.raises(SystemExit) + @handler.stubs(:dispatcher_log) + @handler.expects(:dispatcher_log).with(:info, regexp_matches(/^stopping/)) + @handler.expects(:dispatcher_error).never + + @handler.expects(:when_ready).never + @handler.expects(:close_connection).never + @handler.expects(:reload!).never + @handler.expects(:restart!).never + + @handler.process! + end + + def test_restart_handler_outside_request + @handler.expects(:dispatcher_log).with(:info, "asked to restart ASAP") + @handler.expects(:restart!).once + + @handler.send(:restart_handler, nil) + assert_equal nil, @handler.when_ready + end + + def test_install_signal_handler_should_log_on_bad_signal + @handler.stubs(:trap).raises(ArgumentError) + + @handler.expects(:dispatcher_log).with(:warn, "Ignoring unsupported signal CHEESECAKE.") + @handler.send(:install_signal_handler, "CHEESECAKE", nil) + end + + def test_reload + @handler.expects(:restore!) + @handler.expects(:dispatcher_log).with(:info, "reloaded") + + @handler.send(:reload!) + assert_nil @handler.when_ready + end + + def test_reload_runs_gc_when_gc_request_period_set + @handler.expects(:run_gc!) + @handler.expects(:restore!) + @handler.expects(:dispatcher_log).with(:info, "reloaded") + @handler.gc_request_period = 10 + @handler.send(:reload!) + end + + def test_reload_doesnt_run_gc_if_gc_request_period_isnt_set + @handler.expects(:run_gc!).never + @handler.expects(:restore!) + @handler.expects(:dispatcher_log).with(:info, "reloaded") + @handler.send(:reload!) + end + + def test_restart! + @handler.expects(:dispatcher_log).with(:info, "restarted") + @handler.expects(:exec).returns('restarted') + assert_equal 'restarted', @handler.send(:restart!) + end + + def test_restore! + $".expects(:replace) + ActionController::Routing::Routes.expects(:reload) + @handler.send(:restore!) + end + + def test_uninterrupted_processing + request = mock + FCGI.expects(:each).yields(request) + @handler.expects(:process_request).with(request) + + @handler.process! + + assert_nil @handler.when_ready + end +end + +class RailsFCGIHandlerSignalsTest < Test::Unit::TestCase + include ActiveSupport::Testing::Isolation + + def setup + build_app + boot_rails + + require "#{rails_root}/config/environment" + require 'rails/fcgi_handler' + + ::RailsFCGIHandler.class_eval do + attr_accessor :signal + alias_method :old_gc_countdown, :gc_countdown + def gc_countdown + signal ? Process.kill(signal, $$) : old_gc_countdown + end + end + + @log = StringIO.new + @handler = RailsFCGIHandler.new(@log) + @dispatcher = mock + Dispatcher.stubs(:new).returns(@dispatcher) + end + + def test_interrupted_via_HUP_when_not_in_request + request = mock + FCGI.expects(:each).once.yields(request) + @handler.expects(:signal).times(2).returns('HUP') + + @handler.expects(:reload!).once + @handler.expects(:close_connection).never + @handler.expects(:exit).never + + @handler.process! + assert_equal :reload, @handler.when_ready + end + + def test_interrupted_via_USR1_when_not_in_request + request = mock + FCGI.expects(:each).once.yields(request) + @handler.expects(:signal).times(2).returns('USR1') + @handler.expects(:exit_handler).never + + @handler.expects(:reload!).never + @handler.expects(:close_connection).with(request).once + @handler.expects(:exit).never + + @handler.process! + assert_nil @handler.when_ready + end + + def test_restart_via_USR2_when_in_request + request = mock + FCGI.expects(:each).once.yields(request) + @handler.expects(:signal).times(2).returns('USR2') + @handler.expects(:exit_handler).never + + @handler.expects(:reload!).never + @handler.expects(:close_connection).with(request).once + @handler.expects(:exit).never + @handler.expects(:restart!).once + + @handler.process! + assert_equal :restart, @handler.when_ready + end + + def test_interrupted_via_TERM + request = mock + FCGI.expects(:each).once.yields(request) + ::Rack::Handler::FastCGI.expects(:serve).once.returns('TERM') + + @handler.expects(:reload!).never + @handler.expects(:close_connection).never + + @handler.process! + assert_nil @handler.when_ready + end + + def test_runtime_exception_in_fcgi + error = RuntimeError.new('foo') + FCGI.expects(:each).times(2).raises(error) + @handler.expects(:dispatcher_error).with(error, regexp_matches(/^retrying/)) + @handler.expects(:dispatcher_error).with(error, regexp_matches(/^stopping/)) + @handler.process! + end + + def test_runtime_error_in_dispatcher + request = mock + error = RuntimeError.new('foo') + FCGI.expects(:each).once.yields(request) + ::Rack::Handler::FastCGI.expects(:serve).once.raises(error) + @handler.expects(:dispatcher_error).with(error, regexp_matches(/^unhandled/)) + @handler.process! + end + + def test_signal_exception_in_fcgi + error = SignalException.new('USR2') + FCGI.expects(:each).once.raises(error) + @handler.expects(:dispatcher_error).with(error, regexp_matches(/^stopping/)) + @handler.process! + end + + def test_signal_exception_in_dispatcher + request = mock + error = SignalException.new('USR2') + FCGI.expects(:each).once.yields(request) + ::Rack::Handler::FastCGI.expects(:serve).once.raises(error) + @handler.expects(:dispatcher_error).with(error, regexp_matches(/^stopping/)) + @handler.process! + end +end + +class RailsFCGIHandlerPeriodicGCTest < Test::Unit::TestCase + include ActiveSupport::Testing::Isolation + + def setup + build_app + boot_rails + + require "#{rails_root}/config/environment" + require 'rails/fcgi_handler' + + @log = StringIO.new + end + + def teardown + GC.enable + end + + def test_normal_gc + @handler = RailsFCGIHandler.new(@log) + assert_nil @handler.gc_request_period + + # When GC is enabled, GC.disable disables and returns false. + assert_equal false, GC.disable + end + + def test_periodic_gc + @handler = RailsFCGIHandler.new(@log, 10) + assert_equal 10, @handler.gc_request_period + + request = mock + FCGI.expects(:each).times(10).yields(request) + + @handler.expects(:run_gc!).never + 9.times { @handler.process! } + @handler.expects(:run_gc!).once + @handler.process! + + assert_nil @handler.when_ready + end +end + +rescue LoadError + $stderr.puts 'Skipping fcgi tests. `gem install fcgi` and try again.' +end -- cgit v1.2.3 From 570f055c44a0b6da973f63689f8fedbef9fe32d3 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Mon, 5 Oct 2009 11:16:24 -0500 Subject: Yank FCGI Handler from core http://github.com/rails/fcgi_handler --- railties/test/application/fcgi_dispatcher_test.rb | 290 ---------------------- 1 file changed, 290 deletions(-) delete mode 100644 railties/test/application/fcgi_dispatcher_test.rb (limited to 'railties/test/application') diff --git a/railties/test/application/fcgi_dispatcher_test.rb b/railties/test/application/fcgi_dispatcher_test.rb deleted file mode 100644 index b8ab9f8996..0000000000 --- a/railties/test/application/fcgi_dispatcher_test.rb +++ /dev/null @@ -1,290 +0,0 @@ -require 'isolation/abstract_unit' -require 'mocha' - -begin - -begin - require 'fcgi' -rescue LoadError - gem 'fcgi', '0.8.7' - require 'fcgi' -end - -class RailsFCGIHandlerTest < Test::Unit::TestCase - include ActiveSupport::Testing::Isolation - - def setup - build_app - boot_rails - - require "#{rails_root}/config/environment" - require 'rails/fcgi_handler' - - @log = StringIO.new - @handler = RailsFCGIHandler.new(@log) - end - - def test_process_restart - request = mock - FCGI.stubs(:each).yields(request) - - @handler.expects(:process_request).once - @handler.expects(:dispatcher_error).never - - @handler.expects(:when_ready).returns(:restart) - @handler.expects(:close_connection).with(request) - @handler.expects(:reload!).never - @handler.expects(:restart!) - - @handler.process! - end - - def test_process_exit - request = mock - FCGI.stubs(:each).yields(request) - - @handler.expects(:process_request).once - @handler.expects(:dispatcher_error).never - - @handler.expects(:when_ready).returns(:exit) - @handler.expects(:close_connection).with(request) - @handler.expects(:reload!).never - @handler.expects(:restart!).never - - @handler.process! - end - - def test_process_with_system_exit_exception - request = mock - FCGI.stubs(:each).yields(request) - - @handler.expects(:process_request).once.raises(SystemExit) - @handler.stubs(:dispatcher_log) - @handler.expects(:dispatcher_log).with(:info, regexp_matches(/^stopping/)) - @handler.expects(:dispatcher_error).never - - @handler.expects(:when_ready).never - @handler.expects(:close_connection).never - @handler.expects(:reload!).never - @handler.expects(:restart!).never - - @handler.process! - end - - def test_restart_handler_outside_request - @handler.expects(:dispatcher_log).with(:info, "asked to restart ASAP") - @handler.expects(:restart!).once - - @handler.send(:restart_handler, nil) - assert_equal nil, @handler.when_ready - end - - def test_install_signal_handler_should_log_on_bad_signal - @handler.stubs(:trap).raises(ArgumentError) - - @handler.expects(:dispatcher_log).with(:warn, "Ignoring unsupported signal CHEESECAKE.") - @handler.send(:install_signal_handler, "CHEESECAKE", nil) - end - - def test_reload - @handler.expects(:restore!) - @handler.expects(:dispatcher_log).with(:info, "reloaded") - - @handler.send(:reload!) - assert_nil @handler.when_ready - end - - def test_reload_runs_gc_when_gc_request_period_set - @handler.expects(:run_gc!) - @handler.expects(:restore!) - @handler.expects(:dispatcher_log).with(:info, "reloaded") - @handler.gc_request_period = 10 - @handler.send(:reload!) - end - - def test_reload_doesnt_run_gc_if_gc_request_period_isnt_set - @handler.expects(:run_gc!).never - @handler.expects(:restore!) - @handler.expects(:dispatcher_log).with(:info, "reloaded") - @handler.send(:reload!) - end - - def test_restart! - @handler.expects(:dispatcher_log).with(:info, "restarted") - @handler.expects(:exec).returns('restarted') - assert_equal 'restarted', @handler.send(:restart!) - end - - def test_restore! - $".expects(:replace) - ActionController::Routing::Routes.expects(:reload) - @handler.send(:restore!) - end - - def test_uninterrupted_processing - request = mock - FCGI.expects(:each).yields(request) - @handler.expects(:process_request).with(request) - - @handler.process! - - assert_nil @handler.when_ready - end -end - -class RailsFCGIHandlerSignalsTest < Test::Unit::TestCase - include ActiveSupport::Testing::Isolation - - def setup - build_app - boot_rails - - require "#{rails_root}/config/environment" - require 'rails/fcgi_handler' - - ::RailsFCGIHandler.class_eval do - attr_accessor :signal - alias_method :old_gc_countdown, :gc_countdown - def gc_countdown - signal ? Process.kill(signal, $$) : old_gc_countdown - end - end - - @log = StringIO.new - @handler = RailsFCGIHandler.new(@log) - @dispatcher = mock - Dispatcher.stubs(:new).returns(@dispatcher) - end - - def test_interrupted_via_HUP_when_not_in_request - request = mock - FCGI.expects(:each).once.yields(request) - @handler.expects(:signal).times(2).returns('HUP') - - @handler.expects(:reload!).once - @handler.expects(:close_connection).never - @handler.expects(:exit).never - - @handler.process! - assert_equal :reload, @handler.when_ready - end - - def test_interrupted_via_USR1_when_not_in_request - request = mock - FCGI.expects(:each).once.yields(request) - @handler.expects(:signal).times(2).returns('USR1') - @handler.expects(:exit_handler).never - - @handler.expects(:reload!).never - @handler.expects(:close_connection).with(request).once - @handler.expects(:exit).never - - @handler.process! - assert_nil @handler.when_ready - end - - def test_restart_via_USR2_when_in_request - request = mock - FCGI.expects(:each).once.yields(request) - @handler.expects(:signal).times(2).returns('USR2') - @handler.expects(:exit_handler).never - - @handler.expects(:reload!).never - @handler.expects(:close_connection).with(request).once - @handler.expects(:exit).never - @handler.expects(:restart!).once - - @handler.process! - assert_equal :restart, @handler.when_ready - end - - def test_interrupted_via_TERM - request = mock - FCGI.expects(:each).once.yields(request) - ::Rack::Handler::FastCGI.expects(:serve).once.returns('TERM') - - @handler.expects(:reload!).never - @handler.expects(:close_connection).never - - @handler.process! - assert_nil @handler.when_ready - end - - def test_runtime_exception_in_fcgi - error = RuntimeError.new('foo') - FCGI.expects(:each).times(2).raises(error) - @handler.expects(:dispatcher_error).with(error, regexp_matches(/^retrying/)) - @handler.expects(:dispatcher_error).with(error, regexp_matches(/^stopping/)) - @handler.process! - end - - def test_runtime_error_in_dispatcher - request = mock - error = RuntimeError.new('foo') - FCGI.expects(:each).once.yields(request) - ::Rack::Handler::FastCGI.expects(:serve).once.raises(error) - @handler.expects(:dispatcher_error).with(error, regexp_matches(/^unhandled/)) - @handler.process! - end - - def test_signal_exception_in_fcgi - error = SignalException.new('USR2') - FCGI.expects(:each).once.raises(error) - @handler.expects(:dispatcher_error).with(error, regexp_matches(/^stopping/)) - @handler.process! - end - - def test_signal_exception_in_dispatcher - request = mock - error = SignalException.new('USR2') - FCGI.expects(:each).once.yields(request) - ::Rack::Handler::FastCGI.expects(:serve).once.raises(error) - @handler.expects(:dispatcher_error).with(error, regexp_matches(/^stopping/)) - @handler.process! - end -end - -class RailsFCGIHandlerPeriodicGCTest < Test::Unit::TestCase - include ActiveSupport::Testing::Isolation - - def setup - build_app - boot_rails - - require "#{rails_root}/config/environment" - require 'rails/fcgi_handler' - - @log = StringIO.new - end - - def teardown - GC.enable - end - - def test_normal_gc - @handler = RailsFCGIHandler.new(@log) - assert_nil @handler.gc_request_period - - # When GC is enabled, GC.disable disables and returns false. - assert_equal false, GC.disable - end - - def test_periodic_gc - @handler = RailsFCGIHandler.new(@log, 10) - assert_equal 10, @handler.gc_request_period - - request = mock - FCGI.expects(:each).times(10).yields(request) - - @handler.expects(:run_gc!).never - 9.times { @handler.process! } - @handler.expects(:run_gc!).once - @handler.process! - - assert_nil @handler.when_ready - end -end - -rescue LoadError - $stderr.puts 'Skipping fcgi tests. `gem install fcgi` and try again.' -end -- cgit v1.2.3 From 6d6ae0841c94d3c0ce0c91311028ff7396c44a4a Mon Sep 17 00:00:00 2001 From: Carl Lerche Date: Wed, 7 Oct 2009 15:21:19 -0700 Subject: Start moving the initializers into the application object --- railties/test/application/initializable_test.rb | 26 +++++++++++++++++++ railties/test/application/initializer_test.rb | 33 +++++++++++++++++++++++++ railties/test/application/load_test.rb | 4 +-- 3 files changed, 61 insertions(+), 2 deletions(-) create mode 100644 railties/test/application/initializable_test.rb create mode 100644 railties/test/application/initializer_test.rb (limited to 'railties/test/application') diff --git a/railties/test/application/initializable_test.rb b/railties/test/application/initializable_test.rb new file mode 100644 index 0000000000..38eaec63c0 --- /dev/null +++ b/railties/test/application/initializable_test.rb @@ -0,0 +1,26 @@ +require "isolation/abstract_unit" + +module ApplicationTests + class InitializerTest < Test::Unit::TestCase + include ActiveSupport::Testing::Isolation + + def setup + build_app + boot_rails + end + + test "initializers only ever run once" do + class MyApp < Rails::Application + initializer :counter do + $counter += 1 + end + end + + $counter = 0 + MyApp.initializers[:counter].run + MyApp.initializers[:counter].run + + assert_equal 1, $counter + end + end +end \ No newline at end of file diff --git a/railties/test/application/initializer_test.rb b/railties/test/application/initializer_test.rb new file mode 100644 index 0000000000..9583e1606c --- /dev/null +++ b/railties/test/application/initializer_test.rb @@ -0,0 +1,33 @@ +require "isolation/abstract_unit" + +module ApplicationTests + class InitializerTest < Test::Unit::TestCase + include ActiveSupport::Testing::Isolation + + def setup + build_app + boot_rails + end + + test "initializing an application initializes rails" do + class MyApp < Rails::Application ; end + + if RUBY_VERSION < '1.9' + $KCODE = '' + MyApp.new + assert_equal 'UTF8', $KCODE + else + Encoding.default_external = Encoding::US_ASCII + MyApp.new + assert_equal Encoding::UTF_8, Encoding.default_external + end + end + + test "initializing an application adds the application paths to the load path" do + class MyApp < Rails::Application ; end + + MyApp.new + assert $:.include?("#{app_path}/app/models") + end + end +end \ No newline at end of file diff --git a/railties/test/application/load_test.rb b/railties/test/application/load_test.rb index 305cd7f273..5c3d35fb16 100644 --- a/railties/test/application/load_test.rb +++ b/railties/test/application/load_test.rb @@ -40,14 +40,14 @@ module ApplicationTests test "Rails.application is available after config.ru has been racked up" do rackup - assert Rails.application.new.is_a?(Rails::Application) + assert Rails.application.new < Rails::Application end # Passenger still uses AC::Dispatcher, so we need to # keep it working for now test "deprecated ActionController::Dispatcher still works" do rackup - assert ActionController::Dispatcher.new.is_a?(Rails::Application) + assert ActionController::Dispatcher.new < Rails::Application end test "the config object is available on the application object" do -- cgit v1.2.3 From e4d7e5090c928e5b64350be9a1fb3aaf6f968e4c Mon Sep 17 00:00:00 2001 From: Carl Lerche Date: Thu, 8 Oct 2009 12:14:57 -0700 Subject: Moving more initializers into the application object --- railties/test/application/initializable_test.rb | 26 ------- railties/test/application/initializer_test.rb | 25 +++++++ railties/test/application/plugins_test.rb | 90 +++++++++++++++++++++++++ 3 files changed, 115 insertions(+), 26 deletions(-) delete mode 100644 railties/test/application/initializable_test.rb create mode 100644 railties/test/application/plugins_test.rb (limited to 'railties/test/application') diff --git a/railties/test/application/initializable_test.rb b/railties/test/application/initializable_test.rb deleted file mode 100644 index 38eaec63c0..0000000000 --- a/railties/test/application/initializable_test.rb +++ /dev/null @@ -1,26 +0,0 @@ -require "isolation/abstract_unit" - -module ApplicationTests - class InitializerTest < Test::Unit::TestCase - include ActiveSupport::Testing::Isolation - - def setup - build_app - boot_rails - end - - test "initializers only ever run once" do - class MyApp < Rails::Application - initializer :counter do - $counter += 1 - end - end - - $counter = 0 - MyApp.initializers[:counter].run - MyApp.initializers[:counter].run - - assert_equal 1, $counter - end - end -end \ No newline at end of file diff --git a/railties/test/application/initializer_test.rb b/railties/test/application/initializer_test.rb index 9583e1606c..1b6af718c0 100644 --- a/railties/test/application/initializer_test.rb +++ b/railties/test/application/initializer_test.rb @@ -29,5 +29,30 @@ module ApplicationTests MyApp.new assert $:.include?("#{app_path}/app/models") end + + test "adding an unknown framework raises an error" do + class MyApp < Rails::Application + config.frameworks << :action_foo + end + + assert_raises RuntimeError do + MyApp.new + end + end + + test "eager loading loads parent classes before children" do + app_file "lib/zoo.rb", <<-ZOO + class Zoo ; include ReptileHouse ; end + ZOO + app_file "lib/zoo/reptile_house.rb", <<-ZOO + module Zoo::ReptileHouse ; end + ZOO + + Rails::Initializer.run do |config| + config.eager_load_paths = "#{app_path}/lib" + end + + assert Zoo + end end end \ No newline at end of file diff --git a/railties/test/application/plugins_test.rb b/railties/test/application/plugins_test.rb new file mode 100644 index 0000000000..2a433ea016 --- /dev/null +++ b/railties/test/application/plugins_test.rb @@ -0,0 +1,90 @@ +require "isolation/abstract_unit" + +module ApplicationTests + class PluginTest < Test::Unit::TestCase + include ActiveSupport::Testing::Isolation + + def assert_plugins(list_of_names, array_of_plugins, message=nil) + assert_equal list_of_names.map { |n| n.to_s }, array_of_plugins.map { |p| p.name }, message + end + + def setup + build_app + boot_rails + @failure_tip = "It's likely someone has added a new plugin fixture without updating this list" + # Tmp hax to get tests working + FileUtils.cp_r "#{File.dirname(__FILE__)}/../fixtures/plugins", "#{app_path}/vendor" + end + + test "all plugins are loaded when registered plugin list is untouched" do + Rails::Initializer.run { } + assert_plugins [ + :a, :acts_as_chunky_bacon, :engine, :gemlike, :plugin_with_no_lib_dir, :stubby + ], Rails.application.config.loaded_plugins, @failure_tip + end + + test "all plugins loaded after all" do + Rails::Initializer.run do |config| + config.plugins = [:stubby, :all, :acts_as_chunky_bacon] + end + assert_plugins [:stubby, :a, :engine, :gemlike, :plugin_with_no_lib_dir, :acts_as_chunky_bacon], Rails.application.config.loaded_plugins, @failure_tip + end + + test "plugin names may be strings" do + plugin_names = ['stubby', 'acts_as_chunky_bacon', :a, :plugin_with_no_lib_dir] + Rails::Initializer.run do |config| + config.plugins = ['stubby', 'acts_as_chunky_bacon', :a, :plugin_with_no_lib_dir] + end + + assert_plugins plugin_names, Rails.application.config.loaded_plugins, @failure_tip + end + + test "all plugins loaded when all is used" do + Rails::Initializer.run do |config| + config.plugins = [:stubby, :acts_as_chunky_bacon, :all] + end + + assert_plugins [:stubby, :acts_as_chunky_bacon, :a, :engine, :gemlike, :plugin_with_no_lib_dir], Rails.application.config.loaded_plugins, @failure_tip + end + + test "all loaded plugins are added to the load paths" do + Rails::Initializer.run do |config| + config.plugins = [:stubby, :acts_as_chunky_bacon] + end + + assert $LOAD_PATH.include?("#{app_path}/vendor/plugins/default/stubby/lib") + assert $LOAD_PATH.include?("#{app_path}/vendor/plugins/default/acts/acts_as_chunky_bacon/lib") + end + + test "registering a plugin name that does not exist raises a load error" do + assert_raise(LoadError) do + Rails::Initializer.run do |config| + config.plugins = [:stubby, :acts_as_a_non_existant_plugin] + end + end + end + + test "load error messages mention missing plugins and no others" do + valid_plugins = [:stubby, :acts_as_chunky_bacon] + invalid_plugins = [:non_existant_plugin1, :non_existant_plugin2] + + begin + Rails::Initializer.run do |config| + config.plugins = [:stubby, :acts_as_chunky_bacon, :non_existant_plugin1, :non_existant_plugin2] + end + flunk "Expected a LoadError but did not get one" + rescue LoadError => e + assert_plugins valid_plugins, Rails.application.config.loaded_plugins, @failure_tip + + invalid_plugins.each do |plugin| + assert_match(/#{plugin.to_s}/, e.message, "LoadError message should mention plugin '#{plugin}'") + end + + valid_plugins.each do |plugin| + assert_no_match(/#{plugin.to_s}/, e.message, "LoadError message should not mention '#{plugin}'") + end + end + end + + end +end \ No newline at end of file -- cgit v1.2.3 From ed77e84c4b00bf1d2269af6a07575c30039d942d Mon Sep 17 00:00:00 2001 From: Carl Lerche Date: Thu, 8 Oct 2009 15:08:08 -0700 Subject: Ported over more initializers --- railties/test/application/initializer_test.rb | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'railties/test/application') diff --git a/railties/test/application/initializer_test.rb b/railties/test/application/initializer_test.rb index 1b6af718c0..6de07b4122 100644 --- a/railties/test/application/initializer_test.rb +++ b/railties/test/application/initializer_test.rb @@ -54,5 +54,12 @@ module ApplicationTests assert Zoo end + + test "load environment with global" do + app_file "config/environments/development.rb", "$initialize_test_set_from_env = 'success'" + assert_nil $initialize_test_set_from_env + Rails::Initializer.run { } + assert_equal "success", $initialize_test_set_from_env + end end end \ No newline at end of file -- cgit v1.2.3 From 5c42157cd83b022d05738c6759e38536f74fc572 Mon Sep 17 00:00:00 2001 From: Carl Lerche Date: Thu, 8 Oct 2009 15:30:17 -0700 Subject: Even more initializers ported over --- railties/test/application/initializer_test.rb | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'railties/test/application') diff --git a/railties/test/application/initializer_test.rb b/railties/test/application/initializer_test.rb index 6de07b4122..c57ed08fdc 100644 --- a/railties/test/application/initializer_test.rb +++ b/railties/test/application/initializer_test.rb @@ -55,6 +55,13 @@ module ApplicationTests assert Zoo end + test "setting another default locale" do + Rails::Initializer.run do |config| + config.i18n.default_locale = :de + end + assert_equal :de, I18n.default_locale + end + test "load environment with global" do app_file "config/environments/development.rb", "$initialize_test_set_from_env = 'success'" assert_nil $initialize_test_set_from_env -- cgit v1.2.3 From 992c2db76cd6cd6aa9a6ba3711a6ea1ad8910062 Mon Sep 17 00:00:00 2001 From: Carl Lerche Date: Thu, 8 Oct 2009 18:12:28 -0700 Subject: Finish porting over the initializers to the app object and fix all the tests --- railties/test/application/generators_test.rb | 89 +++++++++++++++++ railties/test/application/initializer_test.rb | 132 +++++++++++++++++++++++++- railties/test/application/plugins_test.rb | 11 +++ 3 files changed, 227 insertions(+), 5 deletions(-) create mode 100644 railties/test/application/generators_test.rb (limited to 'railties/test/application') diff --git a/railties/test/application/generators_test.rb b/railties/test/application/generators_test.rb new file mode 100644 index 0000000000..0d6eb4147a --- /dev/null +++ b/railties/test/application/generators_test.rb @@ -0,0 +1,89 @@ +require "isolation/abstract_unit" + +module ApplicationTests + class GeneratorsTest < Test::Unit::TestCase + include ActiveSupport::Testing::Isolation + + def setup + require "rails/generators" + build_app + boot_rails + end + + test "generators default values" do + Rails::Initializer.run do |c| + assert_equal(true, c.generators.colorize_logging) + assert_equal({}, c.generators.aliases) + assert_equal({}, c.generators.options) + end + end + + test "generators set rails options" do + Rails::Initializer.run do |c| + c.generators.orm = :datamapper + c.generators.test_framework = :rspec + expected = { :rails => { :orm => :datamapper, :test_framework => :rspec } } + assert_equal(expected, c.generators.options) + end + end + + test "generators set rails aliases" do + Rails::Initializer.run do |c| + c.generators.aliases = { :rails => { :test_framework => "-w" } } + expected = { :rails => { :test_framework => "-w" } } + assert_equal expected, c.generators.aliases + end + end + + test "generators aliases and options on initialization" do + Rails::Initializer.run do |c| + c.generators.rails :aliases => { :test_framework => "-w" } + c.generators.orm :datamapper + c.generators.test_framework :rspec + end + + assert_equal :rspec, Rails::Generators.options[:rails][:test_framework] + assert_equal "-w", Rails::Generators.aliases[:rails][:test_framework] + end + + test "generators no color on initialization" do + Rails::Initializer.run do |c| + c.generators.colorize_logging = false + end + + assert_equal Thor::Base.shell, Thor::Shell::Basic + end + + test "generators with hashes for options and aliases" do + Rails::Initializer.run do |c| + c.generators do |g| + g.orm :datamapper, :migration => false + g.plugin :aliases => { :generator => "-g" }, + :generator => true + end + + expected = { + :rails => { :orm => :datamapper }, + :plugin => { :generator => true }, + :datamapper => { :migration => false } + } + + assert_equal expected, c.generators.options + assert_equal({ :plugin => { :generator => "-g" } }, c.generators.aliases) + end + end + + test "generators with hashes are deep merged" do + Rails::Initializer.run do |c| + c.generators do |g| + g.orm :datamapper, :migration => false + g.plugin :aliases => { :generator => "-g" }, + :generator => true + end + end + + assert Rails::Generators.aliases.size >= 1 + assert Rails::Generators.options.size >= 1 + end + end +end \ No newline at end of file diff --git a/railties/test/application/initializer_test.rb b/railties/test/application/initializer_test.rb index c57ed08fdc..f46bf2b656 100644 --- a/railties/test/application/initializer_test.rb +++ b/railties/test/application/initializer_test.rb @@ -55,6 +55,59 @@ module ApplicationTests assert Zoo end + test "load environment with global" do + app_file "config/environments/development.rb", "$initialize_test_set_from_env = 'success'" + assert_nil $initialize_test_set_from_env + Rails::Initializer.run { } + assert_equal "success", $initialize_test_set_from_env + end + + test "action_controller load paths set only if action controller in use" do + assert_nothing_raised NameError do + Rails::Initializer.run do |config| + config.frameworks = [] + end + end + end + + test "action_pack is added to the load path if action_controller is required" do + Rails::Initializer.run do |config| + config.frameworks = [:action_controller] + end + + assert $:.include?("#{framework_path}/actionpack/lib") + end + + test "action_pack is added to the load path if action_view is required" do + Rails::Initializer.run do |config| + config.frameworks = [:action_view] + end + + assert $:.include?("#{framework_path}/actionpack/lib") + end + + test "after_initialize block works correctly" do + Rails::Initializer.run do |config| + config.after_initialize { $test_after_initialize_block1 = "success" } + config.after_initialize { $test_after_initialize_block2 = "congratulations" } + end + + assert_equal "success", $test_after_initialize_block1 + assert_equal "congratulations", $test_after_initialize_block2 + end + + test "after_initialize block works correctly when no block is passed" do + Rails::Initializer.run do |config| + config.after_initialize { $test_after_initialize_block1 = "success" } + config.after_initialize # don't pass a block, this is what we're testing! + config.after_initialize { $test_after_initialize_block2 = "congratulations" } + end + + assert_equal "success", $test_after_initialize_block1 + assert_equal "congratulations", $test_after_initialize_block2 + end + + # i18n test "setting another default locale" do Rails::Initializer.run do |config| config.i18n.default_locale = :de @@ -62,11 +115,80 @@ module ApplicationTests assert_equal :de, I18n.default_locale end - test "load environment with global" do - app_file "config/environments/development.rb", "$initialize_test_set_from_env = 'success'" - assert_nil $initialize_test_set_from_env - Rails::Initializer.run { } - assert_equal "success", $initialize_test_set_from_env + test "no config locales dir present should return empty load path" do + FileUtils.rm_rf "#{app_path}/config/locales" + Rails::Initializer.run do |c| + assert_equal [], c.i18n.load_path + end + end + + test "config locales dir present should be added to load path" do + Rails::Initializer.run do |c| + assert_equal ["#{app_path}/config/locales/en.yml"], c.i18n.load_path + end + end + + test "config defaults should be added with config settings" do + Rails::Initializer.run do |c| + c.i18n.load_path << "my/other/locale.yml" + end + + assert_equal [ + "#{app_path}/config/locales/en.yml", "my/other/locale.yml" + ], Rails.application.config.i18n.load_path + end + + # DB middleware + test "database middleware doesn't initialize when session store is not active_record" do + Rails::Initializer.run do |config| + config.action_controller.session_store = :cookie_store + end + + assert !Rails.application.config.middleware.include?(ActiveRecord::SessionStore) + end + + test "database middleware doesn't initialize when activerecord is not in frameworks" do + Rails::Initializer.run do |c| + c.frameworks = [] + end + assert_equal [], Rails.application.config.middleware + end + + test "database middleware initializes when session store is active record" do + Rails::Initializer.run do |c| + c.action_controller.session_store = :active_record_store + end + + expects = [ActiveRecord::ConnectionAdapters::ConnectionManagement, ActiveRecord::QueryCache, ActiveRecord::SessionStore] + middleware = Rails.application.config.middleware.map { |m| m.klass } + assert_equal expects, middleware & expects + end + + test "ensure database middleware doesn't use action_controller on initializing" do + Rails::Initializer.run do |c| + c.frameworks -= [:action_controller] + c.action_controller.session_store = :active_record_store + end + + assert !Rails.application.config.middleware.include?(ActiveRecord::SessionStore) + end + + # Pathview test + test "load view paths doesn't perform anything when action_view not in frameworks" do + Rails::Initializer.run do |c| + c.frameworks -= [:action_view] + end + assert_equal nil, ActionMailer::Base.template_root + assert_equal [], ActionController::Base.view_paths + end + + # Rails root test + test "Rails.root == RAILS_ROOT" do + assert_equal RAILS_ROOT, Rails.root.to_s + end + + test "Rails.root should be a Pathname" do + assert_instance_of Pathname, Rails.root end end end \ No newline at end of file diff --git a/railties/test/application/plugins_test.rb b/railties/test/application/plugins_test.rb index 2a433ea016..81e7f4d88c 100644 --- a/railties/test/application/plugins_test.rb +++ b/railties/test/application/plugins_test.rb @@ -23,6 +23,17 @@ module ApplicationTests ], Rails.application.config.loaded_plugins, @failure_tip end + test "no plugins are loaded if the configuration has an empty plugin list" do + Rails::Initializer.run { |c| c.plugins = [] } + assert_plugins [], Rails.application.config.loaded_plugins + end + + test "only the specified plugins are located in the order listed" do + plugin_names = [:plugin_with_no_lib_dir, :acts_as_chunky_bacon] + Rails::Initializer.run { |c| c.plugins = plugin_names } + assert_plugins plugin_names, Rails.application.config.loaded_plugins + end + test "all plugins loaded after all" do Rails::Initializer.run do |config| config.plugins = [:stubby, :all, :acts_as_chunky_bacon] -- cgit v1.2.3