aboutsummaryrefslogtreecommitdiffstats
path: root/railties/test
diff options
context:
space:
mode:
authorDmitry Polushkin <dmitry.polushkin@gmail.com>2011-10-01 20:49:14 +0100
committerDmitry Polushkin <dmitry.polushkin@gmail.com>2011-10-01 20:49:14 +0100
commit19965402b2f868c79d78269ca17cb6282f271382 (patch)
tree8635415f386595650c27469d6b6d04335121d070 /railties/test
parent9d54f8994d09db5435d6c234430ae13333928fb9 (diff)
parentec53b802dab9b676dcc9b53e542bcd840983b7a2 (diff)
downloadrails-19965402b2f868c79d78269ca17cb6282f271382.tar.gz
rails-19965402b2f868c79d78269ca17cb6282f271382.tar.bz2
rails-19965402b2f868c79d78269ca17cb6282f271382.zip
Merge branch 'master' of git://github.com/rails/rails
Diffstat (limited to 'railties/test')
-rw-r--r--railties/test/application/assets_test.rb146
-rw-r--r--railties/test/application/configuration_test.rb8
-rw-r--r--railties/test/application/middleware_test.rb8
-rw-r--r--railties/test/generators/actions_test.rb16
-rw-r--r--railties/test/initializable_test.rb13
-rw-r--r--railties/test/isolation/abstract_unit.rb9
6 files changed, 142 insertions, 58 deletions
diff --git a/railties/test/application/assets_test.rb b/railties/test/application/assets_test.rb
index 959914bcea..118ffff44b 100644
--- a/railties/test/application/assets_test.rb
+++ b/railties/test/application/assets_test.rb
@@ -21,6 +21,12 @@ module ApplicationTests
@app ||= Rails.application
end
+ def precompile!
+ capture(:stdout) do
+ Dir.chdir(app_path){ `bundle exec rake assets:precompile` }
+ end
+ end
+
test "assets routes have higher priority" do
app_file "app/assets/javascripts/demo.js.erb", "<%= :alert %>();"
@@ -38,7 +44,7 @@ module ApplicationTests
test "assets do not require compressors until it is used" do
app_file "app/assets/javascripts/demo.js.erb", "<%= :alert %>();"
- app_file "config/initializers/compile.rb", "Rails.application.config.assets.compile = true"
+ add_to_env_config "production", "config.assets.compile = true"
ENV["RAILS_ENV"] = "production"
require "#{app_path}/config/environment"
@@ -54,11 +60,12 @@ module ApplicationTests
app_file "app/assets/javascripts/foo/application.js", "alert();"
ENV["RAILS_ENV"] = nil
- capture(:stdout) do
- Dir.chdir(app_path){ `bundle exec rake assets:precompile` }
- end
+ precompile!
+
files = Dir["#{app_path}/public/assets/application-*.js"]
+ files << Dir["#{app_path}/public/assets/application.js"].first
files << Dir["#{app_path}/public/assets/foo/application-*.js"].first
+ files << Dir["#{app_path}/public/assets/foo/application.js"].first
files.each do |file|
assert_not_nil file, "Expected application.js asset to be generated, but none found"
assert_equal "alert()", File.read(file)
@@ -68,6 +75,10 @@ module ApplicationTests
test "precompile application.js and application.css and all other files not ending with .js or .css by default" do
app_file "app/assets/javascripts/application.js", "alert();"
app_file "app/assets/stylesheets/application.css", "body{}"
+
+ app_file "app/assets/javascripts/someapplication.js", "alert();"
+ app_file "app/assets/stylesheets/someapplication.css", "body{}"
+
app_file "app/assets/javascripts/something.min.js", "alert();"
app_file "app/assets/stylesheets/something.min.css", "body{}"
@@ -76,19 +87,23 @@ module ApplicationTests
"happy.happy.face.png", "happy", "happy.face", "-happyface",
"-happy.png", "-happy.face.png", "_happyface", "_happy.face.png",
"_happy.png"]
+
images_should_compile.each do |filename|
app_file "app/assets/images/#{filename}", "happy"
end
- capture(:stdout) do
- Dir.chdir(app_path){ `bundle exec rake assets:precompile` }
- end
+ precompile!
images_should_compile.each do |filename|
assert File.exists?("#{app_path}/public/assets/#{filename}")
end
+
assert File.exists?("#{app_path}/public/assets/application.js")
assert File.exists?("#{app_path}/public/assets/application.css")
+
+ assert !File.exists?("#{app_path}/public/assets/someapplication.js")
+ assert !File.exists?("#{app_path}/public/assets/someapplication.css")
+
assert !File.exists?("#{app_path}/public/assets/something.min.js")
assert !File.exists?("#{app_path}/public/assets/something.min.css")
end
@@ -109,10 +124,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
- Dir.chdir(app_path){ `bundle exec rake assets:precompile` }
- end
-
+ precompile!
manifest = "#{app_path}/public/assets/manifest.yml"
assets = YAML.load_file(manifest)
@@ -126,12 +138,8 @@ module ApplicationTests
# digest is default in false, we must enable it for test environment
add_to_config "config.assets.digest = true"
add_to_config "config.assets.manifest = '#{app_path}/shared'"
- FileUtils.mkdir "#{app_path}/shared"
-
- capture(:stdout) do
- Dir.chdir(app_path){ `bundle exec rake assets:precompile` }
- end
+ precompile!
manifest = "#{app_path}/shared/manifest.yml"
assets = YAML.load_file(manifest)
@@ -139,16 +147,13 @@ module ApplicationTests
assert_match(/application-([0-z]+)\.css/, assets["application.css"])
end
-
test "the manifest file should be saved by default in the same assets folder" do
app_file "app/assets/javascripts/application.js", "alert();"
# digest is default in false, we must enable it for test environment
add_to_config "config.assets.digest = true"
add_to_config "config.assets.prefix = '/x'"
- capture(:stdout) do
- Dir.chdir(app_path){ `bundle exec rake assets:precompile` }
- end
+ precompile!
manifest = "#{app_path}/public/x/manifest.yml"
assets = YAML.load_file(manifest)
@@ -160,9 +165,7 @@ module ApplicationTests
app_file "app/assets/javascripts/application.js", "alert();"
add_to_config "config.assets.digest = false"
- capture(:stdout) do
- Dir.chdir(app_path){ `bundle exec rake assets:precompile` }
- end
+ precompile!
assert File.exists?("#{app_path}/public/assets/application.js")
assert File.exists?("#{app_path}/public/assets/application.css")
@@ -176,12 +179,11 @@ module ApplicationTests
test "assets do not require any assets group gem when manifest file is present" do
app_file "app/assets/javascripts/application.js", "alert();"
- app_file "config/initializers/serve_static_assets.rb", "Rails.application.config.serve_static_assets = true"
+ add_to_env_config "production", "config.serve_static_assets = true"
ENV["RAILS_ENV"] = "production"
- capture(:stdout) do
- Dir.chdir(app_path){ `bundle exec rake assets:precompile` }
- end
+ precompile!
+
manifest = "#{app_path}/public/assets/manifest.yml"
assets = YAML.load_file(manifest)
asset_path = assets["application.js"]
@@ -205,9 +207,7 @@ module ApplicationTests
RUBY
ENV["RAILS_ENV"] = "production"
- capture(:stdout) do
- Dir.chdir(app_path){ `bundle exec rake assets:precompile` }
- end
+ precompile!
# Create file after of precompile
app_file "app/assets/javascripts/app.js", "alert();"
@@ -231,9 +231,7 @@ module ApplicationTests
RUBY
ENV["RAILS_ENV"] = "development"
- capture(:stdout) do
- Dir.chdir(app_path){ `bundle exec rake assets:precompile` }
- end
+ precompile!
# Create file after of precompile
app_file "app/assets/javascripts/app.js", "alert();"
@@ -258,6 +256,24 @@ module ApplicationTests
assert_match(/\/assets\/rails-([0-z]+)\.png/, File.read(file))
end
+ test "precompile shouldn't use the digests present in manifest.yml" do
+ app_file "app/assets/stylesheets/application.css.erb", "<%= asset_path('rails.png') %>"
+
+ ENV["RAILS_ENV"] = "production"
+ precompile!
+
+ manifest = "#{app_path}/public/assets/manifest.yml"
+ assets = YAML.load_file(manifest)
+ asset_path = assets["application.css"]
+
+ app_file "app/assets/images/rails.png", "image changed"
+
+ precompile!
+ assets = YAML.load_file(manifest)
+
+ assert_not_equal asset_path, assets["application.css"]
+ end
+
test "precompile appends the md5 hash to files referenced with asset_path and run in production as default even using RAILS_GROUPS=assets" do
app_file "app/assets/stylesheets/application.css.erb", "<%= asset_path('rails.png') %>"
add_to_config "config.assets.compile = true"
@@ -270,28 +286,11 @@ module ApplicationTests
assert_match(/\/assets\/rails-([0-z]+)\.png/, File.read(file))
end
- test "precompile ignore asset_host" do
- app_file "app/assets/javascripts/application.css.erb", "<%= asset_path 'rails.png' %>"
- add_to_config "config.action_controller.asset_host = Proc.new { |source, request| 'http://www.example.com/' }"
-
- capture(:stdout) do
- Dir.chdir(app_path){ `bundle exec rake assets:precompile` }
- end
-
- file = Dir["#{app_path}/public/assets/application.css"].first
- content = File.read(file)
- assert_match(/\/assets\/rails.png/, content)
- assert_no_match(/www\.example\.com/, content)
- end
-
test "precompile should handle utf8 filenames" do
app_file "app/assets/images/レイルズ.png", "not a image really"
add_to_config "config.assets.precompile = [ /\.png$$/, /application.(css|js)$/ ]"
- capture(:stdout) do
- Dir.chdir(app_path){ `bundle exec rake assets:precompile` }
- end
-
+ precompile!
assert File.exists?("#{app_path}/public/assets/レイルズ.png")
manifest = "#{app_path}/public/assets/manifest.yml"
@@ -363,5 +362,50 @@ module ApplicationTests
assert_match "alert();", last_response.body
assert_equal 200, last_response.status
end
+
+ test "assets are concatenated when debug is off and compile is off either if debug_assets param is provided" do
+ app_with_assets_in_view
+
+ # config.assets.debug and config.assets.compile are false for production environment
+ ENV["RAILS_ENV"] = "production"
+ precompile!
+
+ require "#{app_path}/config/environment"
+
+ class ::PostsController < ActionController::Base ; end
+
+ # the debug_assets params isn't used if compile is off
+ get '/posts?debug_assets=true'
+ assert_match(/<script src="\/assets\/application-([0-z]+)\.js" type="text\/javascript"><\/script>/, last_response.body)
+ assert_no_match(/<script src="\/assets\/xmlhr-([0-z]+)\.js" type="text\/javascript"><\/script>/, last_response.body)
+ end
+
+ test "assets aren't concatened when compile is true is on and debug_assets params is true" do
+ app_with_assets_in_view
+ add_to_env_config "production", "config.assets.compile = true"
+ add_to_env_config "production", "config.assets.allow_debugging = true"
+
+ ENV["RAILS_ENV"] = "production"
+ require "#{app_path}/config/environment"
+
+ class ::PostsController < ActionController::Base ; end
+
+ get '/posts?debug_assets=true'
+ assert_match(/<script src="\/assets\/application-([0-z]+)\.js\?body=1" type="text\/javascript"><\/script>/, last_response.body)
+ assert_match(/<script src="\/assets\/xmlhr-([0-z]+)\.js\?body=1" type="text\/javascript"><\/script>/, last_response.body)
+ end
+
+ private
+ def app_with_assets_in_view
+ app_file "app/assets/javascripts/application.js", "//= require_tree ."
+ app_file "app/assets/javascripts/xmlhr.js", "function f1() { alert(); }"
+ app_file "app/views/posts/index.html.erb", "<%= javascript_include_tag 'application' %>"
+
+ app_file "config/routes.rb", <<-RUBY
+ AppTemplate::Application.routes.draw do
+ match '/posts', :to => "posts#index"
+ end
+ RUBY
+ end
end
end
diff --git a/railties/test/application/configuration_test.rb b/railties/test/application/configuration_test.rb
index 448982f9de..97ad47ac14 100644
--- a/railties/test/application/configuration_test.rb
+++ b/railties/test/application/configuration_test.rb
@@ -306,7 +306,7 @@ module ApplicationTests
require "#{app_path}/config/environment"
require "mail"
- ActionMailer::Base
+ _ = ActionMailer::Base
assert_equal [::MyMailInterceptor], ::Mail.send(:class_variable_get, "@@delivery_interceptors")
end
@@ -319,7 +319,7 @@ module ApplicationTests
require "#{app_path}/config/environment"
require "mail"
- ActionMailer::Base
+ _ = ActionMailer::Base
assert_equal [::MyMailInterceptor, ::MyOtherMailInterceptor], ::Mail.send(:class_variable_get, "@@delivery_interceptors")
end
@@ -332,7 +332,7 @@ module ApplicationTests
require "#{app_path}/config/environment"
require "mail"
- ActionMailer::Base
+ _ = ActionMailer::Base
assert_equal [::MyMailObserver], ::Mail.send(:class_variable_get, "@@delivery_notification_observers")
end
@@ -345,7 +345,7 @@ module ApplicationTests
require "#{app_path}/config/environment"
require "mail"
- ActionMailer::Base
+ _ = ActionMailer::Base
assert_equal [::MyMailObserver, ::MyOtherMailObserver], ::Mail.send(:class_variable_get, "@@delivery_notification_observers")
end
diff --git a/railties/test/application/middleware_test.rb b/railties/test/application/middleware_test.rb
index bed5ba503f..093cb6ca2a 100644
--- a/railties/test/application/middleware_test.rb
+++ b/railties/test/application/middleware_test.rb
@@ -69,6 +69,14 @@ module ApplicationTests
assert middleware.include?("Rack::SSL")
end
+ test "Rack::SSL is configured with options when given" do
+ add_to_config "config.force_ssl = true"
+ add_to_config "config.ssl_options = { :host => 'example.com' }"
+ boot!
+
+ assert_equal AppTemplate::Application.middleware.first.args, [{:host => 'example.com'}]
+ end
+
test "removing Active Record omits its middleware" do
use_frameworks []
boot!
diff --git a/railties/test/generators/actions_test.rb b/railties/test/generators/actions_test.rb
index 94e9abb3cc..e621f7f6f7 100644
--- a/railties/test/generators/actions_test.rb
+++ b/railties/test/generators/actions_test.rb
@@ -189,6 +189,22 @@ class ActionsTest < Rails::Generators::TestCase
action :rake, 'log:clear', :env => 'production'
end
+ def test_rake_with_rails_env_variable_should_run_rake_command_in_env
+ generator.expects(:run).once.with('rake log:clear RAILS_ENV=production', :verbose => false)
+ old_env, ENV["RAILS_ENV"] = ENV["RAILS_ENV"], "production"
+ action :rake, 'log:clear'
+ ensure
+ ENV["RAILS_ENV"] = old_env
+ end
+
+ def test_env_option_should_win_over_rails_env_variable_when_running_rake
+ generator.expects(:run).once.with('rake log:clear RAILS_ENV=production', :verbose => false)
+ old_env, ENV["RAILS_ENV"] = ENV["RAILS_ENV"], "staging"
+ action :rake, 'log:clear', :env => 'production'
+ ensure
+ ENV["RAILS_ENV"] = old_env
+ end
+
def test_rake_with_sudo_option_should_run_rake_command_with_sudo
generator.expects(:run).once.with('sudo rake log:clear RAILS_ENV=development', :verbose => false)
action :rake, 'log:clear', :sudo => true
diff --git a/railties/test/initializable_test.rb b/railties/test/initializable_test.rb
index 72c35879c5..1dbcc249ab 100644
--- a/railties/test/initializable_test.rb
+++ b/railties/test/initializable_test.rb
@@ -61,7 +61,7 @@ module InitializableTests
class Instance
include Rails::Initializable
- initializer :one do
+ initializer :one, :group => :assets do
$arr << 1
end
@@ -69,7 +69,7 @@ module InitializableTests
$arr << 2
end
- initializer :three do
+ initializer :three, :group => :all do
$arr << 3
end
@@ -211,12 +211,19 @@ module InitializableTests
instance.run_initializers
assert_equal [1, 2, 3, 4], $arr
end
+
+ test "running locals with groups" do
+ $arr = []
+ instance = Instance.new
+ instance.run_initializers(:assets)
+ assert_equal [1, 3], $arr
+ end
end
class WithArgsTest < ActiveSupport::TestCase
test "running initializers with args" do
$with_arg = nil
- WithArgs.new.run_initializers('foo')
+ WithArgs.new.run_initializers(nil, 'foo')
assert_equal 'foo', $with_arg
end
end
diff --git a/railties/test/isolation/abstract_unit.rb b/railties/test/isolation/abstract_unit.rb
index 4a6bdb0320..06b658e7bd 100644
--- a/railties/test/isolation/abstract_unit.rb
+++ b/railties/test/isolation/abstract_unit.rb
@@ -224,6 +224,15 @@ module TestHelpers
end
end
+ def add_to_env_config(env, str)
+ environment = File.read("#{app_path}/config/environments/#{env}.rb")
+ if environment =~ /(\n\s*end\s*)\Z/
+ File.open("#{app_path}/config/environments/#{env}.rb", 'w') do |f|
+ f.puts $` + "\n#{str}\n" + $1
+ end
+ end
+ end
+
def remove_from_config(str)
file = "#{app_path}/config/application.rb"
contents = File.read(file)