aboutsummaryrefslogtreecommitdiffstats
path: root/railties/test
diff options
context:
space:
mode:
authorMikel Lindsaar <raasdnil@gmail.com>2010-01-17 12:46:51 +1100
committerMikel Lindsaar <raasdnil@gmail.com>2010-01-17 12:46:51 +1100
commit6f663addaa7ed40f1133687d7a2be0958bf0c059 (patch)
tree7beb7f092c1979d1fd5ea6e7aaaf4f59c2f4abf4 /railties/test
parent8834b2612b7ddda70ee6a685eb0063d3daa8e63d (diff)
parent3e94032227d450d479f511070c51f37f53d0ecc4 (diff)
downloadrails-6f663addaa7ed40f1133687d7a2be0958bf0c059.tar.gz
rails-6f663addaa7ed40f1133687d7a2be0958bf0c059.tar.bz2
rails-6f663addaa7ed40f1133687d7a2be0958bf0c059.zip
Merge branch 'master' of github.com:lifo/docrails
Diffstat (limited to 'railties/test')
-rw-r--r--railties/test/abstract_unit.rb1
-rw-r--r--railties/test/application/generators_test.rb2
-rw-r--r--railties/test/application/initializer_test.rb11
-rw-r--r--railties/test/application/metal_test.rb86
-rw-r--r--railties/test/application/middleware_test.rb80
-rw-r--r--railties/test/application/notifications_test.rb46
-rw-r--r--railties/test/application/test_test.rb63
-rw-r--r--railties/test/fixtures/metal/multiplemetals/app/metal/metal_a.rb5
-rw-r--r--railties/test/fixtures/metal/multiplemetals/app/metal/metal_b.rb5
-rw-r--r--railties/test/fixtures/metal/pluralmetal/app/metal/legacy_routes.rb5
-rw-r--r--railties/test/fixtures/metal/singlemetal/app/metal/foo_metal.rb5
-rw-r--r--railties/test/fixtures/metal/subfolders/app/metal/Folder/metal_a.rb7
-rw-r--r--railties/test/fixtures/metal/subfolders/app/metal/Folder/metal_b.rb7
-rw-r--r--railties/test/generators/app_generator_test.rb22
-rw-r--r--railties/test/generators/generators_test_helper.rb6
-rw-r--r--railties/test/initializer/path_test.rb8
-rw-r--r--railties/test/metal_test.rb101
-rw-r--r--railties/test/plugins/configuration_test.rb9
-rw-r--r--railties/test/plugins/vendored_test.rb8
-rw-r--r--railties/test/rails_info_controller_test.rb1
-rw-r--r--railties/test/subscriber_test.rb130
21 files changed, 451 insertions, 157 deletions
diff --git a/railties/test/abstract_unit.rb b/railties/test/abstract_unit.rb
index 2d6983076a..77ef82856a 100644
--- a/railties/test/abstract_unit.rb
+++ b/railties/test/abstract_unit.rb
@@ -17,7 +17,6 @@ require 'fileutils'
require 'active_support'
require 'active_support/core_ext/logger'
-require 'active_support/test_case'
require 'action_controller'
require 'rails/all'
diff --git a/railties/test/application/generators_test.rb b/railties/test/application/generators_test.rb
index 0c858d6394..e1e51c318c 100644
--- a/railties/test/application/generators_test.rb
+++ b/railties/test/application/generators_test.rb
@@ -52,8 +52,8 @@ module ApplicationTests
config.generators.test_framework :rspec
RUBY
- require "#{app_path}/config/environment"
# Initialize the application
+ require "#{app_path}/config/environment"
require "rails/generators"
Rails::Generators.configure!
diff --git a/railties/test/application/initializer_test.rb b/railties/test/application/initializer_test.rb
index 3fd0b0e5df..754c0f1839 100644
--- a/railties/test/application/initializer_test.rb
+++ b/railties/test/application/initializer_test.rb
@@ -83,6 +83,17 @@ module ApplicationTests
assert_equal "congratulations", $test_after_initialize_block2
end
+ test "after_initialize runs after frameworks have been initialized" do
+ $activerecord_configurations = nil
+ add_to_config <<-RUBY
+ config.after_initialize { $activerecord_configurations = ActiveRecord::Base.configurations }
+ RUBY
+
+ require "#{app_path}/config/environment"
+ assert $activerecord_configurations
+ assert $activerecord_configurations['development']
+ end
+
# i18n
test "setting another default locale" do
add_to_config <<-RUBY
diff --git a/railties/test/application/metal_test.rb b/railties/test/application/metal_test.rb
new file mode 100644
index 0000000000..225bede117
--- /dev/null
+++ b/railties/test/application/metal_test.rb
@@ -0,0 +1,86 @@
+require 'isolation/abstract_unit'
+
+module ApplicationTests
+ class MetalTest < Test::Unit::TestCase
+ include ActiveSupport::Testing::Isolation
+
+ def setup
+ build_app
+ boot_rails
+
+ require 'rack/test'
+ extend Rack::Test::Methods
+ end
+
+ def app
+ @app ||= begin
+ require "#{app_path}/config/environment"
+ Rails.application
+ end
+ end
+
+ test "single metal endpoint" do
+ app_file 'app/metal/foo_metal.rb', <<-RUBY
+ class FooMetal
+ def self.call(env)
+ [200, { "Content-Type" => "text/html"}, ["FooMetal"]]
+ end
+ end
+ RUBY
+
+ get "/"
+ assert_equal 200, last_response.status
+ assert_equal "FooMetal", last_response.body
+ end
+
+ test "multiple metal endpoints" do
+ app_file 'app/metal/metal_a.rb', <<-RUBY
+ class MetalA
+ def self.call(env)
+ [404, { "Content-Type" => "text/html", "X-Cascade" => "pass" }, ["Metal A"]]
+ end
+ end
+ RUBY
+
+ app_file 'app/metal/metal_b.rb', <<-RUBY
+ class MetalB
+ def self.call(env)
+ [200, { "Content-Type" => "text/html"}, ["Metal B"]]
+ end
+ end
+ RUBY
+
+ get "/"
+ assert_equal 200, last_response.status
+ assert_equal "Metal B", last_response.body
+ end
+
+ test "pass through to application" do
+ app_file 'app/metal/foo_metal.rb', <<-RUBY
+ class FooMetal
+ def self.call(env)
+ [404, { "Content-Type" => "text/html", "X-Cascade" => "pass" }, ["Not Found"]]
+ end
+ end
+ RUBY
+
+ controller :foo, <<-RUBY
+ class FooController < ActionController::Base
+ def index
+ render :text => "foo"
+ end
+ end
+ RUBY
+
+ app_file 'config/routes.rb', <<-RUBY
+ AppTemplate::Application.routes.draw do |map|
+ match ':controller(/:action)'
+ end
+ RUBY
+
+ get "/foo"
+ assert_equal 200, last_response.status
+ assert_equal "foo", last_response.body
+ end
+ end
+end
diff --git a/railties/test/application/middleware_test.rb b/railties/test/application/middleware_test.rb
new file mode 100644
index 0000000000..7b3077bb6e
--- /dev/null
+++ b/railties/test/application/middleware_test.rb
@@ -0,0 +1,80 @@
+require 'isolation/abstract_unit'
+
+module ApplicationTests
+ class MiddlewareTest < Test::Unit::TestCase
+ include ActiveSupport::Testing::Isolation
+
+ def setup
+ build_app
+ boot_rails
+ FileUtils.rm_rf "#{app_path}/config/environments"
+ end
+
+ test "default middleware stack" do
+ boot!
+
+ assert_equal [
+ "ActionDispatch::Static",
+ "Rack::Lock",
+ "Rack::Runtime",
+ "ActionDispatch::ShowExceptions",
+ "ActionDispatch::Callbacks",
+ "ActionDispatch::Session::CookieStore",
+ "ActionDispatch::Flash",
+ "ActionDispatch::Cascade",
+ "ActionDispatch::ParamsParser",
+ "Rack::MethodOverride",
+ "ActionDispatch::Head",
+ "ActiveRecord::ConnectionAdapters::ConnectionManagement",
+ "ActiveRecord::QueryCache"
+ ], middleware
+ end
+
+ test "removing activerecord omits its middleware" do
+ use_frameworks []
+ boot!
+ assert !middleware.include?("ActiveRecord::ConnectionAdapters::ConnectionManagement")
+ assert !middleware.include?("ActiveRecord::QueryCache")
+ end
+
+ test "removes lock if allow concurrency is set" do
+ add_to_config "config.action_controller.allow_concurrency = true"
+ boot!
+ assert !middleware.include?("Rack::Lock")
+ end
+
+ test "removes static asset server if serve_static_assets is disabled" do
+ add_to_config "config.serve_static_assets = false"
+ boot!
+ assert !middleware.include?("ActionDispatch::Static")
+ end
+
+ test "use middleware" do
+ use_frameworks []
+ add_to_config "config.middleware.use Rack::Config"
+ boot!
+ assert_equal "Rack::Config", middleware.last
+ end
+
+ test "insert middleware after" do
+ add_to_config "config.middleware.insert_after ActionDispatch::Static, Rack::Config"
+ boot!
+ assert_equal "Rack::Config", middleware.second
+ end
+
+ test "insert middleware before" do
+ add_to_config "config.middleware.insert_before ActionDispatch::Static, Rack::Config"
+ boot!
+ assert_equal "Rack::Config", middleware.first
+ end
+
+ private
+ def boot!
+ require "#{app_path}/config/environment"
+ end
+
+ def middleware
+ AppTemplate::Application.instance.middleware.active.map(&:klass).map(&:name)
+ end
+ end
+end
diff --git a/railties/test/application/notifications_test.rb b/railties/test/application/notifications_test.rb
index b57e829cca..1eb0777db8 100644
--- a/railties/test/application/notifications_test.rb
+++ b/railties/test/application/notifications_test.rb
@@ -12,28 +12,64 @@ module ApplicationTests
end
end
+ class MockLogger
+ def method_missing(*args)
+ @logged ||= []
+ @logged << args.last
+ end
+
+ def logged
+ @logged.compact.map { |l| l.to_s.strip }
+ end
+ end
+
class NotificationsTest < Test::Unit::TestCase
include ActiveSupport::Testing::Isolation
def setup
build_app
boot_rails
+ end
+
+ def instrument(*args, &block)
+ ActiveSupport::Notifications.instrument(*args, &block)
+ end
+
+ def wait
+ ActiveSupport::Notifications.notifier.wait
+ end
+
+ test "new queue is set" do
+ # We don't want to load all frameworks, so remove them and clean up environments.
+ use_frameworks []
FileUtils.rm_rf("#{app_path}/config/environments")
- require "active_support/notifications"
- @events = []
add_to_config <<-RUBY
config.notifications.notifier = ActiveSupport::Notifications::Notifier.new(ApplicationTests::MyQueue.new)
RUBY
- end
- test "new queue is set" do
- use_frameworks []
require "#{app_path}/config/environment"
assert_raise RuntimeError do
ActiveSupport::Notifications.publish('foo')
end
end
+
+ test "rails subscribers are added" do
+ add_to_config <<-RUBY
+ config.colorize_logging = false
+ RUBY
+
+ require "#{app_path}/config/environment"
+
+ ActiveRecord::Base.logger = logger = MockLogger.new
+
+ # Mimic an ActiveRecord notifications
+ instrument "active_record.sql", :name => "SQL", :sql => "SHOW tables"
+ wait
+
+ assert_equal 1, logger.logged.size
+ assert_match /SHOW tables/, logger.logged.last
+ end
end
end
diff --git a/railties/test/application/test_test.rb b/railties/test/application/test_test.rb
new file mode 100644
index 0000000000..37175783d8
--- /dev/null
+++ b/railties/test/application/test_test.rb
@@ -0,0 +1,63 @@
+require 'isolation/abstract_unit'
+
+module ApplicationTests
+ class TestTest < Test::Unit::TestCase
+ include ActiveSupport::Testing::Isolation
+
+ def setup
+ build_app
+ boot_rails
+ end
+
+ test "truth" do
+ app_file 'test/unit/foo_test.rb', <<-RUBY
+ require 'test_helper'
+
+ class FooTest < ActiveSupport::TestCase
+ def test_truth
+ assert true
+ end
+ end
+ RUBY
+
+ run_test 'unit/foo_test.rb'
+ end
+
+ test "integration test" do
+ controller 'posts', <<-RUBY
+ class PostsController < ActionController::Base
+ end
+ RUBY
+
+ app_file 'app/views/posts/index.html.erb', <<-HTML
+ Posts#index
+ HTML
+
+ app_file 'test/integration/posts_test.rb', <<-RUBY
+ require 'test_helper'
+
+ class PostsTest < ActionController::IntegrationTest
+ def test_index
+ get '/posts'
+ assert_response :success
+ assert_template "index"
+ end
+ end
+ RUBY
+
+ run_test 'integration/posts_test.rb'
+ end
+
+ private
+ def run_test(name)
+ result = ruby '-Itest', "#{app_path}/test/#{name}"
+ assert_equal 0, $?.to_i, result
+ end
+
+ def ruby(*args)
+ Dir.chdir(app_path) do
+ `RUBYLIB='#{$:.join(':')}' #{Gem.ruby} #{args.join(' ')}`
+ end
+ end
+ end
+end
diff --git a/railties/test/fixtures/metal/multiplemetals/app/metal/metal_a.rb b/railties/test/fixtures/metal/multiplemetals/app/metal/metal_a.rb
deleted file mode 100644
index 4ca4ddd447..0000000000
--- a/railties/test/fixtures/metal/multiplemetals/app/metal/metal_a.rb
+++ /dev/null
@@ -1,5 +0,0 @@
-class MetalA
- def self.call(env)
- [404, { "Content-Type" => "text/html"}, ["Metal A"]]
- end
-end
diff --git a/railties/test/fixtures/metal/multiplemetals/app/metal/metal_b.rb b/railties/test/fixtures/metal/multiplemetals/app/metal/metal_b.rb
deleted file mode 100644
index 80e69fe0b0..0000000000
--- a/railties/test/fixtures/metal/multiplemetals/app/metal/metal_b.rb
+++ /dev/null
@@ -1,5 +0,0 @@
-class MetalB
- def self.call(env)
- [200, { "Content-Type" => "text/html"}, ["Metal B"]]
- end
-end
diff --git a/railties/test/fixtures/metal/pluralmetal/app/metal/legacy_routes.rb b/railties/test/fixtures/metal/pluralmetal/app/metal/legacy_routes.rb
deleted file mode 100644
index 0cd3737c32..0000000000
--- a/railties/test/fixtures/metal/pluralmetal/app/metal/legacy_routes.rb
+++ /dev/null
@@ -1,5 +0,0 @@
-class LegacyRoutes < Rails::Rack::Metal
- def self.call(env)
- [301, { "Location" => "http://example.com"}, []]
- end
-end
diff --git a/railties/test/fixtures/metal/singlemetal/app/metal/foo_metal.rb b/railties/test/fixtures/metal/singlemetal/app/metal/foo_metal.rb
deleted file mode 100644
index 5f5b087592..0000000000
--- a/railties/test/fixtures/metal/singlemetal/app/metal/foo_metal.rb
+++ /dev/null
@@ -1,5 +0,0 @@
-class FooMetal < Rails::Rack::Metal
- def self.call(env)
- [200, { "Content-Type" => "text/html"}, ["Hi"]]
- end
-end
diff --git a/railties/test/fixtures/metal/subfolders/app/metal/Folder/metal_a.rb b/railties/test/fixtures/metal/subfolders/app/metal/Folder/metal_a.rb
deleted file mode 100644
index 25b3bb0abc..0000000000
--- a/railties/test/fixtures/metal/subfolders/app/metal/Folder/metal_a.rb
+++ /dev/null
@@ -1,7 +0,0 @@
-module Folder
- class MetalA < Rails::Rack::Metal
- def self.call(env)
- [200, { "Content-Type" => "text/html"}, ["Hi"]]
- end
- end
-end
diff --git a/railties/test/fixtures/metal/subfolders/app/metal/Folder/metal_b.rb b/railties/test/fixtures/metal/subfolders/app/metal/Folder/metal_b.rb
deleted file mode 100644
index 7583363f71..0000000000
--- a/railties/test/fixtures/metal/subfolders/app/metal/Folder/metal_b.rb
+++ /dev/null
@@ -1,7 +0,0 @@
-module Folder
- class MetalB < Rails::Rack::Metal
- def self.call(env)
- [200, { "Content-Type" => "text/html"}, ["Hi"]]
- end
- end
-end
diff --git a/railties/test/generators/app_generator_test.rb b/railties/test/generators/app_generator_test.rb
index 7dd798db75..62ea07f14e 100644
--- a/railties/test/generators/app_generator_test.rb
+++ b/railties/test/generators/app_generator_test.rb
@@ -55,13 +55,19 @@ class AppGeneratorTest < GeneratorsTestCase
end
def test_invalid_application_name_raises_an_error
- content = capture(:stderr){ Rails::Generators::AppGenerator.start [File.join(destination_root, "43-things")] }
+ content = capture(:stderr){ run_generator [File.join(destination_root, "43-things")] }
assert_equal "Invalid application name 43-things. Please give a name which does not start with numbers.\n", content
end
def test_invalid_application_name_is_fixed
- silence(:stdout){ Rails::Generators::AppGenerator.start [File.join(destination_root, "things-43")] }
- assert_file "things-43/config/environment.rb", /Things43::Application/
+ run_generator [File.join(destination_root, "things-43")]
+ assert_file "things-43/config/environment.rb", /Things43::Application\.initialize!/
+ assert_file "things-43/config/application.rb", /^module Things43$/
+ end
+
+ def test_application_names_are_not_singularized
+ run_generator [File.join(destination_root, "hats")]
+ assert_file "hats/config/environment.rb", /Hats::Application\.initialize!/
end
def test_config_database_is_added_by_default
@@ -138,7 +144,7 @@ class AppGeneratorTest < GeneratorsTestCase
template = %{ say "It works!" }
template.instance_eval "def read; self; end" # Make the string respond to read
- generator([destination_root], :template => path, :database => "sqlite3").expects(:open).with(path).returns(template)
+ generator([destination_root], :template => path).expects(:open).with(path).returns(template)
assert_match /It works!/, silence(:stdout){ generator.invoke }
end
@@ -162,14 +168,16 @@ class AppGeneratorTest < GeneratorsTestCase
end
def test_dev_option
- run_generator [destination_root, "--dev"]
+ generator([destination_root], :dev => true).expects(:run).with("gem bundle")
+ silence(:stdout){ generator.invoke }
rails_path = File.expand_path('../../..', Rails.root)
- dev_gem = %(gem "rails", :path => #{rails_path.inspect})
+ dev_gem = %(directory #{rails_path.inspect}, :glob => "{*/,}*.gemspec")
assert_file 'Gemfile', /^#{Regexp.escape(dev_gem)}$/
end
def test_edge_option
- run_generator [destination_root, "--edge"]
+ generator([destination_root], :edge => true).expects(:run).with("gem bundle")
+ silence(:stdout){ generator.invoke }
edge_gem = %(gem "rails", :git => "git://github.com/rails/rails.git")
assert_file 'Gemfile', /^#{Regexp.escape(edge_gem)}$/
end
diff --git a/railties/test/generators/generators_test_helper.rb b/railties/test/generators/generators_test_helper.rb
index fcd0989fd7..54953b76c8 100644
--- a/railties/test/generators/generators_test_helper.rb
+++ b/railties/test/generators/generators_test_helper.rb
@@ -1,5 +1,3 @@
-# TODO: Fix this RAILS_ENV stuff
-RAILS_ENV = 'test' unless defined?(RAILS_ENV)
require 'abstract_unit'
module Rails
@@ -25,4 +23,8 @@ class GeneratorsTestCase < Rails::Generators::TestCase
rescue
# Do nothing.
end
+
+ def test_truth
+ # Don't cry test/unit
+ end
end \ No newline at end of file
diff --git a/railties/test/initializer/path_test.rb b/railties/test/initializer/path_test.rb
index bfb1887d11..328dda6d2c 100644
--- a/railties/test/initializer/path_test.rb
+++ b/railties/test/initializer/path_test.rb
@@ -63,7 +63,7 @@ module InitializerTests
end
test "environments has a glob equal to the current environment" do
- assert_equal "#{RAILS_ENV}.rb", @paths.config.environments.glob
+ assert_equal "#{Rails.env}.rb", @paths.config.environments.glob
end
test "load path includes each of the paths in config.paths as long as the directories exist" do
@@ -85,17 +85,17 @@ module InitializerTests
end
test "controller paths include builtin in development mode" do
- RAILS_ENV.replace "development"
+ Rails.env.replace "development"
assert Rails::Configuration.new.paths.app.controllers.paths.any? { |p| p =~ /builtin/ }
end
test "controller paths does not have builtin_directories in test mode" do
- RAILS_ENV.replace "test"
+ Rails.env.replace "test"
assert !Rails::Configuration.new.paths.app.controllers.paths.any? { |p| p =~ /builtin/ }
end
test "controller paths does not have builtin_directories in production mode" do
- RAILS_ENV.replace "production"
+ Rails.env.replace "production"
assert !Rails::Configuration.new.paths.app.controllers.paths.any? { |p| p =~ /builtin/ }
end
diff --git a/railties/test/metal_test.rb b/railties/test/metal_test.rb
deleted file mode 100644
index 91f55c2b5e..0000000000
--- a/railties/test/metal_test.rb
+++ /dev/null
@@ -1,101 +0,0 @@
-require 'abstract_unit'
-
-class MetalTest < Test::Unit::TestCase
- def test_metals_should_return_list_of_found_metal_apps
- use_appdir("singlemetal") do
- assert_equal(["FooMetal"], found_metals_as_string_array)
- end
- end
-
- def test_metals_should_respect_class_name_conventions
- use_appdir("pluralmetal") do
- assert_equal(["LegacyRoutes"], found_metals_as_string_array)
- end
- end
-
- def test_metals_should_return_alphabetical_list_of_found_metal_apps
- use_appdir("multiplemetals") do
- assert_equal(["MetalA", "MetalB"], found_metals_as_string_array)
- end
- end
-
- def test_metals_load_order_should_be_overriden_by_requested_metals
- use_appdir("multiplemetals") do
- Rails::Rack::Metal.requested_metals = ["MetalB", "MetalA"]
- assert_equal(["MetalB", "MetalA"], found_metals_as_string_array)
- end
- end
-
- def test_metals_not_listed_should_not_load
- use_appdir("multiplemetals") do
- Rails::Rack::Metal.requested_metals = ["MetalB"]
- assert_equal(["MetalB"], found_metals_as_string_array)
- end
- end
-
- def test_metal_finding_should_work_with_subfolders
- use_appdir("subfolders") do
- assert_equal(["Folder::MetalA", "Folder::MetalB"], found_metals_as_string_array)
- end
- end
-
- def test_metal_finding_with_requested_metals_should_work_with_subfolders
- use_appdir("subfolders") do
- Rails::Rack::Metal.requested_metals = ["Folder::MetalB"]
- assert_equal(["Folder::MetalB"], found_metals_as_string_array)
- end
- end
-
- def test_metal_finding_should_work_with_multiple_metal_paths_in_185_and_below
- use_appdir("singlemetal") do
- engine_metal_path = "#{File.dirname(__FILE__)}/fixtures/plugins/engines/engine/app/metal"
- Rails::Rack::Metal.metal_paths << engine_metal_path
- $LOAD_PATH << engine_metal_path
- assert_equal(["FooMetal", "EngineMetal"], found_metals_as_string_array)
- end
- end
-
- def test_metal_default_pass_through_on_404
- use_appdir("multiplemetals") do
- result = Rails::Rack::Metal.new(app).call({})
- assert_equal 200, result.first
- assert_equal ["Metal B"], result.last
- end
- end
-
- def test_metal_pass_through_on_417
- use_appdir("multiplemetals") do
- Rails::Rack::Metal.pass_through_on = 417
- result = Rails::Rack::Metal.new(app).call({})
- assert_equal 404, result.first
- assert_equal ["Metal A"], result.last
- end
- end
-
- def test_metal_pass_through_on_404_and_200
- use_appdir("multiplemetals") do
- Rails::Rack::Metal.pass_through_on = [404, 200]
- result = Rails::Rack::Metal.new(app).call({})
- assert_equal 402, result.first
- assert_equal ["End of the Line"], result.last
- end
- end
-
- private
-
- def app
- lambda{|env|[402,{},["End of the Line"]]}
- end
-
- def use_appdir(root)
- dir = "#{File.dirname(__FILE__)}/fixtures/metal/#{root}"
- Rails::Rack::Metal.metal_paths = ["#{dir}/app/metal"]
- Rails::Rack::Metal.requested_metals = nil
- $LOAD_PATH << "#{dir}/app/metal"
- yield
- end
-
- def found_metals_as_string_array
- Rails::Rack::Metal.metals.map { |m| m.to_s }
- end
-end
diff --git a/railties/test/plugins/configuration_test.rb b/railties/test/plugins/configuration_test.rb
index 25bf24eb3b..09f8943af9 100644
--- a/railties/test/plugins/configuration_test.rb
+++ b/railties/test/plugins/configuration_test.rb
@@ -32,5 +32,14 @@ module PluginsTest
assert_equal "hello", MyApp.config.foo.greetings
assert_equal "bar", MyApp.config.foo.bar
end
+
+ test "plugin can add subscribers" do
+ begin
+ class Foo < Rails::Railtie; subscriber(Rails::Subscriber.new); end
+ assert_kind_of Rails::Subscriber, Rails::Subscriber.subscribers[:foo]
+ ensure
+ Rails::Subscriber.subscribers.clear
+ end
+ end
end
end
diff --git a/railties/test/plugins/vendored_test.rb b/railties/test/plugins/vendored_test.rb
index 9a2d40cad8..b3b85891b2 100644
--- a/railties/test/plugins/vendored_test.rb
+++ b/railties/test/plugins/vendored_test.rb
@@ -191,5 +191,11 @@ module PluginsTest
boot_rails
assert_equal [:a, :c, :b], $arr
end
+
+ test "plugin order array is strings" do
+ add_to_config "config.plugins = %w( c_plugin all )"
+ boot_rails
+ assert_equal [:c, :a, :b], $arr
+ end
end
-end \ No newline at end of file
+end
diff --git a/railties/test/rails_info_controller_test.rb b/railties/test/rails_info_controller_test.rb
index 435bd34925..edab27465e 100644
--- a/railties/test/rails_info_controller_test.rb
+++ b/railties/test/rails_info_controller_test.rb
@@ -1,6 +1,5 @@
require 'abstract_unit'
require 'action_controller'
-require 'action_controller/test_case'
require 'rails/info'
require 'rails/info_controller'
diff --git a/railties/test/subscriber_test.rb b/railties/test/subscriber_test.rb
new file mode 100644
index 0000000000..ac34939510
--- /dev/null
+++ b/railties/test/subscriber_test.rb
@@ -0,0 +1,130 @@
+require 'abstract_unit'
+require 'rails/subscriber/test_helper'
+
+class MySubscriber < Rails::Subscriber
+ attr_reader :event
+
+ def some_event(event)
+ @event = event
+ info event.name
+ end
+
+ def foo(event)
+ debug "debug"
+ info "info"
+ warn "warn"
+ end
+
+ def bar(event)
+ info "#{color("cool", :red)}, #{color("isn't it?", :blue, true)}"
+ end
+end
+
+module SubscriberTest
+ def setup
+ super
+ @subscriber = MySubscriber.new
+ Rails::Subscriber.instance_variable_set(:@log_tailer, nil)
+ end
+
+ def teardown
+ super
+ Rails::Subscriber.subscribers.clear
+ Rails::Subscriber.instance_variable_set(:@log_tailer, nil)
+ end
+
+ def instrument(*args, &block)
+ ActiveSupport::Notifications.instrument(*args, &block)
+ end
+
+ def test_proxies_method_to_rails_logger
+ @subscriber.foo(nil)
+ assert_equal %w(debug), @logger.logged(:debug)
+ assert_equal %w(info), @logger.logged(:info)
+ assert_equal %w(warn), @logger.logged(:warn)
+ end
+
+ def test_set_color_for_messages
+ Rails::Subscriber.colorize_logging = true
+ @subscriber.bar(nil)
+ assert_equal "\e[31mcool\e[0m, \e[1m\e[34misn't it?\e[0m", @logger.logged(:info).last
+ end
+
+ def test_does_not_set_color_if_colorize_logging_is_set_to_false
+ @subscriber.bar(nil)
+ assert_equal "cool, isn't it?", @logger.logged(:info).last
+ end
+
+ def test_event_is_sent_to_the_registered_class
+ Rails::Subscriber.add :my_subscriber, @subscriber
+ instrument "my_subscriber.some_event"
+ wait
+ assert_equal %w(my_subscriber.some_event), @logger.logged(:info)
+ end
+
+ def test_event_is_an_active_support_notifications_event
+ Rails::Subscriber.add :my_subscriber, @subscriber
+ instrument "my_subscriber.some_event"
+ wait
+ assert_kind_of ActiveSupport::Notifications::Event, @subscriber.event
+ end
+
+ def test_does_not_send_the_event_if_it_doesnt_match_the_class
+ Rails::Subscriber.add :my_subscriber, @subscriber
+ instrument "my_subscriber.unknown_event"
+ wait
+ # If we get here, it means that NoMethodError was raised.
+ end
+
+ def test_does_not_send_the_event_if_logger_is_nil
+ Rails.logger = nil
+ @subscriber.expects(:some_event).never
+ Rails::Subscriber.add :my_subscriber, @subscriber
+ instrument "my_subscriber.some_event"
+ wait
+ end
+
+ def test_flushes_loggers
+ Rails::Subscriber.add :my_subscriber, @subscriber
+ Rails::Subscriber.flush_all!
+ assert_equal 1, @logger.flush_count
+ end
+
+ def test_flushes_loggers_when_action_dispatch_callback_is_received
+ Rails::Subscriber.add :my_subscriber, @subscriber
+ instrument "action_dispatch.callback"
+ wait
+ assert_equal 1, @logger.flush_count
+ end
+
+ def test_flushes_the_same_logger_just_once
+ Rails::Subscriber.add :my_subscriber, @subscriber
+ Rails::Subscriber.add :another, @subscriber
+ instrument "action_dispatch.callback"
+ wait
+ assert_equal 1, @logger.flush_count
+ end
+
+ def test_tails_logs_when_action_dispatch_callback_is_received
+ log_tailer = mock()
+ log_tailer.expects(:tail!)
+ Rails::Subscriber.log_tailer = log_tailer
+
+ Rails::Subscriber.add :my_subscriber, @subscriber
+ instrument "action_dispatch.callback"
+ wait
+ ensure
+ Rails::Subscriber.log_tailer = nil
+ end
+
+ class SyncSubscriberTest < ActiveSupport::TestCase
+ include Rails::Subscriber::SyncTestHelper
+ include SubscriberTest
+ end
+
+ class AsyncSubscriberTest < ActiveSupport::TestCase
+ include Rails::Subscriber::AsyncTestHelper
+ include SubscriberTest
+ end
+
+end \ No newline at end of file