Merge pull request #1352 from bf4/railties

Fix generators (@dgynn); load Railtie only with Rails, ensures caching configured
This commit is contained in:
Benjamin Fleischer
2016-01-18 23:24:40 -05:00
15 changed files with 216 additions and 61 deletions

View File

@@ -0,0 +1,57 @@
# Execute this test in isolation
require 'support/isolated_unit'
class RailtieTest < ActiveSupport::TestCase
include ActiveSupport::Testing::Isolation
class WithRails < RailtieTest
setup do
require 'rails'
require 'active_model_serializers'
make_basic_app
end
test 'mixes ActionController::Serialization into ActionController::Base' do
assert ActionController.const_defined?(:Serialization),
"ActionController::Serialization should be defined, but isn't"
assert ::ActionController::Base.included_modules.include?(::ActionController::Serialization),
"ActionController::Serialization should be included in ActionController::Base, but isn't"
end
test 'sets the ActiveModelSerializers.logger to Rails.logger' do
refute_nil Rails.logger
refute_nil ActiveModelSerializers.logger
assert_equal Rails.logger, ActiveModelSerializers.logger
end
test 'it is configured for caching' do
assert_equal ActionController::Base.cache_store, ActiveModelSerializers.config.cache_store
assert_equal Rails.configuration.action_controller.perform_caching, ActiveModelSerializers.config.perform_caching
end
end
class WithoutRails < RailtieTest
setup do
require 'active_model_serializers'
make_basic_app
end
test 'does not mix ActionController::Serialization into ActionController::Base' do
refute ActionController.const_defined?(:Serialization),
'ActionController::Serialization should not be defined, but is'
end
test 'has its own logger at ActiveModelSerializers.logger' do
refute_nil Rails.logger
refute_nil ActiveModelSerializers.logger
refute_equal Rails.logger, ActiveModelSerializers.logger
end
test 'it is not configured for caching' do
refute_nil ActionController::Base.cache_store
assert_nil ActiveModelSerializers.config.cache_store
refute Rails.configuration.action_controller.perform_caching
refute ActiveModelSerializers.config.perform_caching
end
end
end

View File

@@ -1,4 +1,5 @@
require 'test_helper'
require 'generators/rails/resource_override'
class ResourceGeneratorTest < Rails::Generators::TestCase
destination File.expand_path('../../../tmp/generators', __FILE__)

View File

@@ -1,5 +1,6 @@
require 'test_helper'
require 'generators/serializer/serializer_generator'
require 'generators/rails/resource_override'
require 'generators/rails/serializer_generator'
class SerializerGeneratorTest < Rails::Generators::TestCase
destination File.expand_path('../../../tmp/generators', __FILE__)

View File

@@ -0,0 +1,77 @@
# https://github.com/rails/rails/blob/v5.0.0.beta1/railties/test/isolation/abstract_unit.rb
# Usage Example:
#
# require 'support/isolated_unit'
#
# class RailtieTest < ActiveSupport::TestCase
# include ActiveSupport::Testing::Isolation
#
# class WithRailsDefinedOnLoad < RailtieTest
# setup do
# require 'rails'
# require 'active_model_serializers'
# make_basic_app
# end
#
# # some tests
# end
#
# class WithoutRailsDefinedOnLoad < RailtieTest
# setup do
# require 'active_model_serializers'
# make_basic_app
# end
#
# # some tests
# end
# end
#
# Note:
# It is important to keep this file as light as possible
# the goal for tests that require this is to test booting up
# rails from an empty state, so anything added here could
# hide potential failures
#
# It is also good to know what is the bare minimum to get
# Rails booted up.
require 'bundler/setup' unless defined?(Bundler)
require 'active_support'
require 'active_support/core_ext/string/access'
# These files do not require any others and are needed
# to run the tests
require 'active_support/testing/autorun'
require 'active_support/testing/isolation'
module TestHelpers
module Generation
# Make a very basic app, without creating the whole directory structure.
# Is faster and simpler than generating a Rails app in a temp directory
def make_basic_app
require 'rails'
require 'action_controller/railtie'
@app = Class.new(Rails::Application) do
config.eager_load = false
config.session_store :cookie_store, key: '_myapp_session'
config.active_support.deprecation = :log
config.active_support.test_order = :parallel
ActiveSupport::TestCase.respond_to?(:test_order=) && ActiveSupport::TestCase.test_order = :parallel
config.root = File.dirname(__FILE__)
config.log_level = :info
# Set a fake logger to avoid creating the log directory automatically
fake_logger = Logger.new(nil)
config.logger = fake_logger
end
@app.respond_to?(:secrets) && @app.secrets.secret_key_base = '3b7cd727ee24e8444053437c36cc66c4'
yield @app if block_given?
@app.initialize!
end
end
end
class ActiveSupport::TestCase
include TestHelpers::Generation
end

View File

@@ -17,6 +17,7 @@ require 'action_controller'
require 'action_controller/test_case'
require 'action_controller/railtie'
require 'active_support/json'
require 'active_model_serializers'
require 'fileutils'
FileUtils.mkdir_p(File.expand_path('../../tmp/cache', __FILE__))
@@ -42,9 +43,6 @@ end
require 'minitest/reporters'
Minitest::Reporters.use!
require 'active_model_serializers'
require 'active_model/serializer/railtie'
require 'support/stream_capture'
require 'support/rails_app'
@@ -59,7 +57,7 @@ require 'fixtures/active_record'
require 'fixtures/poro'
ActiveSupport.on_load(:active_model_serializers) do
ActiveSupport.on_load(:action_controller) do
$action_controller_logger = ActiveModelSerializers.logger
ActiveModelSerializers.logger = Logger.new(IO::NULL)
end