diff options
-rw-r--r-- | actionpack/lib/action_controller/test_case.rb | 20 | ||||
-rw-r--r-- | actionpack/lib/sprockets/assets.rake | 72 | ||||
-rw-r--r-- | actionpack/test/controller/test_test.rb | 16 | ||||
-rw-r--r-- | activesupport/test/rescuable_test.rb | 2 | ||||
-rw-r--r-- | railties/test/application/assets_test.rb | 27 |
5 files changed, 90 insertions, 47 deletions
diff --git a/actionpack/lib/action_controller/test_case.rb b/actionpack/lib/action_controller/test_case.rb index a83fa74795..6913c1ef4a 100644 --- a/actionpack/lib/action_controller/test_case.rb +++ b/actionpack/lib/action_controller/test_case.rb @@ -333,9 +333,21 @@ module ActionController module ClassMethods # Sets the controller class name. Useful if the name can't be inferred from test class. - # Expects +controller_class+ as a constant. Example: <tt>tests WidgetController</tt>. + # Normalizes +controller_class+ before using. Examples: + # + # tests WidgetController + # tests :widget + # tests 'widget' + # def tests(controller_class) - self.controller_class = controller_class + case controller_class + when String, Symbol + self.controller_class = "#{controller_class.to_s.underscore}_controller".camelize.constantize + when Class + self.controller_class = controller_class + else + raise ArgumentError, "controller class must be a String, Symbol, or Class" + end end def controller_class=(new_class) @@ -352,9 +364,7 @@ module ActionController end def determine_default_controller_class(name) - name.sub(/Test$/, '').constantize - rescue NameError - nil + name.sub(/Test$/, '').safe_constantize end def prepare_controller_class(new_class) diff --git a/actionpack/lib/sprockets/assets.rake b/actionpack/lib/sprockets/assets.rake index 7b86d84a27..cc1e70d114 100644 --- a/actionpack/lib/sprockets/assets.rake +++ b/actionpack/lib/sprockets/assets.rake @@ -1,53 +1,51 @@ -namespace :assets do - desc "Compile all the assets named in config.assets.precompile" - task :precompile do - # We need to do this dance because RAILS_GROUPS is used - # too early in the boot process and changing here is already too late. - if ENV["RAILS_GROUPS"].to_s.empty? || ENV["RAILS_ENV"].to_s.empty? - ENV["RAILS_GROUPS"] ||= "assets" - ENV["RAILS_ENV"] ||= "production" - ruby $0, *ARGV - else - require "fileutils" - Rake::Task["tmp:cache:clear"].invoke - Rake::Task["assets:environment"].invoke +require "fileutils" - unless Rails.application.config.assets.enabled - raise "Cannot precompile assets if sprockets is disabled. Please set config.assets.enabled to true" - end +namespace :assets do + def invoke_precompile + args = [$0, "assets:internal_precompile"] + args << "--trace" if Rake.application.options.trace + ruby *args + end - # Ensure that action view is loaded and the appropriate sprockets hooks get executed - _ = ActionView::Base + desc "Compile all the assets named in config.assets.precompile" + task :precompile => "assets:clean" do + ENV["RAILS_GROUPS"] ||= "assets" + ENV["RAILS_ENV"] ||= "production" + invoke_precompile + end - config = Rails.application.config - config.assets.compile = true - config.assets.digest = false if ENV["RAILS_ASSETS_NONDIGEST"] + task :internal_precompile => "assets:environment" do + unless Rails.application.config.assets.enabled + raise "Cannot precompile assets if sprockets is disabled. Please set config.assets.enabled to true" + end - env = Rails.application.assets + # Ensure that action view is loaded and the appropriate sprockets hooks get executed + _ = ActionView::Base - # Always compile files and avoid use of existing precompiled assets - config.assets.compile = true - config.assets.digests = {} + config = Rails.application.config + config.assets.compile = true + config.assets.digest = false if ENV["RAILS_ASSETS_NONDIGEST"] + config.assets.digests = {} - target = File.join(Rails.public_path, config.assets.prefix) - static_compiler = Sprockets::StaticCompiler.new(env, target, :digest => config.assets.digest) + env = Rails.application.assets + target = File.join(Rails.public_path, config.assets.prefix) + static_compiler = Sprockets::StaticCompiler.new(env, target, :digest => config.assets.digest) - manifest = static_compiler.precompile(config.assets.precompile) - manifest_path = config.assets.manifest || target - FileUtils.mkdir_p(manifest_path) + manifest = static_compiler.precompile(config.assets.precompile) + manifest_path = config.assets.manifest || target + FileUtils.mkdir_p(manifest_path) - unless ENV["RAILS_ASSETS_NONDIGEST"] - File.open("#{manifest_path}/manifest.yml", 'wb') do |f| - YAML.dump(manifest, f) - end - ENV["RAILS_ASSETS_NONDIGEST"] = "true" - ruby $0, *ARGV + unless ENV["RAILS_ASSETS_NONDIGEST"] + File.open("#{manifest_path}/manifest.yml", 'wb') do |f| + YAML.dump(manifest, f) end + ENV["RAILS_ASSETS_NONDIGEST"] = "true" + invoke_precompile end end desc "Remove compiled assets" - task :clean => ['assets:environment', 'tmp:cache:clear'] do + task :clean => "tmp:cache:clear" do config = Rails.application.config public_asset_path = File.join(Rails.public_path, config.assets.prefix) rm_rf public_asset_path, :secure => true diff --git a/actionpack/test/controller/test_test.rb b/actionpack/test/controller/test_test.rb index ca87cda2a3..b64e275363 100644 --- a/actionpack/test/controller/test_test.rb +++ b/actionpack/test/controller/test_test.rb @@ -774,6 +774,22 @@ class CrazyNameTest < ActionController::TestCase end end +class CrazySymbolNameTest < ActionController::TestCase + tests :content + + def test_set_controller_class_using_symbol + assert_equal ContentController, self.class.controller_class + end +end + +class CrazyStringNameTest < ActionController::TestCase + tests 'content' + + def test_set_controller_class_using_string + assert_equal ContentController, self.class.controller_class + end +end + class NamedRoutesControllerTest < ActionController::TestCase tests ContentController diff --git a/activesupport/test/rescuable_test.rb b/activesupport/test/rescuable_test.rb index bf4f5265ef..c28ffa50f2 100644 --- a/activesupport/test/rescuable_test.rb +++ b/activesupport/test/rescuable_test.rb @@ -70,7 +70,7 @@ class CoolStargate < Stargate end -class RescueableTest < Test::Unit::TestCase +class RescuableTest < Test::Unit::TestCase def setup @stargate = Stargate.new @cool_stargate = CoolStargate.new diff --git a/railties/test/application/assets_test.rb b/railties/test/application/assets_test.rb index ba59d3faa7..e0e36063c8 100644 --- a/railties/test/application/assets_test.rb +++ b/railties/test/application/assets_test.rb @@ -22,7 +22,7 @@ module ApplicationTests end def precompile! - capture(:stdout) do + quietly do Dir.chdir(app_path){ `bundle exec rake assets:precompile` } end end @@ -249,7 +249,7 @@ module ApplicationTests # digest is default in false, we must enable it for test environment add_to_config "config.assets.digest = true" - capture(:stdout) do + quietly do Dir.chdir(app_path){ `bundle exec rake assets:precompile RAILS_ENV=test` } end file = Dir["#{app_path}/public/assets/application.css"].first @@ -281,7 +281,7 @@ module ApplicationTests add_to_config "config.assets.compile = true" ENV["RAILS_ENV"] = nil - capture(:stdout) do + quietly do Dir.chdir(app_path){ `bundle exec rake assets:precompile RAILS_GROUPS=assets` } end file = Dir["#{app_path}/public/assets/application-*.css"].first @@ -306,7 +306,7 @@ module ApplicationTests app_file "public/assets/application.css", "a { color: green; }" app_file "public/assets/subdir/broken.png", "not really an image file" - capture(:stdout) do + quietly do Dir.chdir(app_path){ `bundle exec rake assets:clean` } end @@ -419,6 +419,25 @@ module ApplicationTests assert_equal "NoPost;\n", File.read("#{app_path}/public/assets/application.js") end + test "enhancements to assets:precompile should only run once" do + app_file "lib/tasks/enhance.rake", "Rake::Task['assets:precompile'].enhance { puts 'enhancement' }" + output = precompile! + assert_equal 1, output.scan("enhancement").size + end + + test "digested assets are not mistakenly removed" do + app_file "public/assets/application.js", "alert();" + add_to_config "config.assets.compile = true" + add_to_config "config.assets.digest = true" + + quietly do + Dir.chdir(app_path){ `bundle exec rake assets:clean assets:precompile` } + end + + files = Dir["#{app_path}/public/assets/application-*.js"] + assert_equal 1, files.length, "Expected digested application.js asset to be generated, but none found" + end + private def app_with_assets_in_view |