2011-05-08 06:57:07 -04:00
|
|
|
require 'isolation/abstract_unit'
|
2011-06-21 10:18:50 -04:00
|
|
|
require 'active_support/core_ext/kernel/reporting'
|
2011-05-08 06:57:07 -04:00
|
|
|
require 'rack/test'
|
|
|
|
|
|
|
|
module ApplicationTests
|
2011-06-21 10:18:50 -04:00
|
|
|
class AssetsTest < Test::Unit::TestCase
|
2011-05-08 06:57:07 -04:00
|
|
|
include ActiveSupport::Testing::Isolation
|
|
|
|
include Rack::Test::Methods
|
|
|
|
|
|
|
|
def setup
|
2011-07-25 04:32:03 -04:00
|
|
|
build_app(:initializers => true)
|
2011-05-08 06:57:07 -04:00
|
|
|
boot_rails
|
|
|
|
end
|
|
|
|
|
2011-06-06 08:54:05 -04:00
|
|
|
def teardown
|
|
|
|
teardown_app
|
|
|
|
end
|
|
|
|
|
2011-05-17 16:51:14 -04:00
|
|
|
def app
|
|
|
|
@app ||= Rails.application
|
|
|
|
end
|
|
|
|
|
2011-05-08 06:57:07 -04:00
|
|
|
test "assets routes have higher priority" do
|
|
|
|
app_file "app/assets/javascripts/demo.js.erb", "<%= :alert %>();"
|
|
|
|
|
|
|
|
app_file 'config/routes.rb', <<-RUBY
|
|
|
|
AppTemplate::Application.routes.draw do
|
|
|
|
match '*path', :to => lambda { |env| [200, { "Content-Type" => "text/html" }, "Not an asset"] }
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
|
2011-05-17 17:43:44 -04:00
|
|
|
require "#{app_path}/config/environment"
|
|
|
|
|
2011-05-08 06:57:07 -04:00
|
|
|
get "/assets/demo.js"
|
|
|
|
assert_match "alert()", last_response.body
|
|
|
|
end
|
2011-05-17 16:51:14 -04:00
|
|
|
|
2011-07-06 20:38:54 -04:00
|
|
|
test "assets do not require compressors until it is used" do
|
|
|
|
app_file "app/assets/javascripts/demo.js.erb", "<%= :alert %>();"
|
2011-08-31 13:47:33 -04:00
|
|
|
app_file "config/initializers/compile.rb", "Rails.application.config.assets.compile = true"
|
|
|
|
|
2011-07-06 20:38:54 -04:00
|
|
|
ENV["RAILS_ENV"] = "production"
|
|
|
|
require "#{app_path}/config/environment"
|
|
|
|
|
|
|
|
assert !defined?(Uglifier)
|
|
|
|
get "/assets/demo.js"
|
|
|
|
assert_match "alert()", last_response.body
|
|
|
|
assert defined?(Uglifier)
|
|
|
|
end
|
|
|
|
|
2011-08-15 14:35:47 -04:00
|
|
|
test "precompile creates the file, gives it the original asset's content and run in production as default" do
|
2011-06-21 10:18:50 -04:00
|
|
|
app_file "app/assets/javascripts/application.js", "alert();"
|
2011-07-01 03:53:48 -04:00
|
|
|
app_file "app/assets/javascripts/foo/application.js", "alert();"
|
2011-06-21 10:18:50 -04:00
|
|
|
|
2011-08-15 14:31:47 -04:00
|
|
|
ENV["RAILS_ENV"] = nil
|
2011-06-21 10:18:50 -04:00
|
|
|
capture(:stdout) do
|
|
|
|
Dir.chdir(app_path){ `bundle exec rake assets:precompile` }
|
|
|
|
end
|
2011-08-16 11:37:56 -04:00
|
|
|
files = Dir["#{app_path}/public/assets/application-*.js"]
|
|
|
|
files << Dir["#{app_path}/public/assets/foo/application-*.js"].first
|
2011-07-01 03:53:48 -04:00
|
|
|
files.each do |file|
|
|
|
|
assert_not_nil file, "Expected application.js asset to be generated, but none found"
|
2011-08-15 14:31:47 -04:00
|
|
|
assert_equal "alert()", File.read(file)
|
2011-07-01 03:53:48 -04:00
|
|
|
end
|
2011-06-21 10:18:50 -04:00
|
|
|
end
|
|
|
|
|
2011-08-31 13:47:33 -04:00
|
|
|
test "precompile creates a manifest file with all the assets listed" do
|
2011-08-29 17:28:11 -04:00
|
|
|
app_file "app/assets/stylesheets/application.css.erb", "<%= asset_path('rails.png') %>"
|
|
|
|
app_file "app/assets/javascripts/application.js", "alert();"
|
2011-08-31 19:53:51 -04:00
|
|
|
# digest is default in false, we must enable it for test environment
|
|
|
|
app_file "config/initializers/compile.rb", "Rails.application.config.assets.digest = true"
|
2011-08-29 17:28:11 -04:00
|
|
|
|
|
|
|
capture(:stdout) do
|
|
|
|
Dir.chdir(app_path){ `bundle exec rake assets:precompile` }
|
|
|
|
end
|
|
|
|
|
|
|
|
manifest = "#{app_path}/public/assets/manifest.yml"
|
|
|
|
|
|
|
|
assets = YAML.load_file(manifest)
|
2011-09-03 22:53:13 -04:00
|
|
|
assert_match(/application-([0-z]+)\.js/, assets["application.js"])
|
|
|
|
assert_match(/application-([0-z]+)\.css/, assets["application.css"])
|
2011-08-29 17:28:11 -04:00
|
|
|
end
|
|
|
|
|
2011-08-30 17:30:53 -04:00
|
|
|
test "precompile creates a manifest file in a custom path with all the assets listed" do
|
|
|
|
app_file "app/assets/stylesheets/application.css.erb", "<%= asset_path('rails.png') %>"
|
|
|
|
app_file "app/assets/javascripts/application.js", "alert();"
|
|
|
|
FileUtils.mkdir "#{app_path}/shared"
|
|
|
|
app_file "config/initializers/manifest.rb", "Rails.application.config.assets.manifest = '#{app_path}/shared'"
|
2011-08-31 19:53:51 -04:00
|
|
|
# digest is default in false, we must enable it for test environment
|
|
|
|
app_file "config/initializers/compile.rb", "Rails.application.config.assets.digest = true"
|
2011-08-30 17:30:53 -04:00
|
|
|
|
|
|
|
capture(:stdout) do
|
|
|
|
Dir.chdir(app_path){ `bundle exec rake assets:precompile` }
|
|
|
|
end
|
|
|
|
|
|
|
|
manifest = "#{app_path}/shared/manifest.yml"
|
|
|
|
|
|
|
|
assets = YAML.load_file(manifest)
|
2011-09-03 22:53:13 -04:00
|
|
|
assert_match(/application-([0-z]+)\.js/, assets["application.js"])
|
|
|
|
assert_match(/application-([0-z]+)\.css/, assets["application.css"])
|
2011-08-30 17:30:53 -04:00
|
|
|
end
|
|
|
|
|
2011-08-31 19:53:51 -04:00
|
|
|
|
2011-08-31 21:50:38 -04:00
|
|
|
test "the manifest file should be saved by default in the same assets folder" do
|
|
|
|
app_file "app/assets/javascripts/application.js", "alert();"
|
|
|
|
app_file "config/initializers/manifest.rb", "Rails.application.config.assets.prefix = '/x'"
|
2011-08-31 19:53:51 -04:00
|
|
|
# digest is default in false, we must enable it for test environment
|
|
|
|
app_file "config/initializers/compile.rb", "Rails.application.config.assets.digest = true"
|
2011-08-31 21:50:38 -04:00
|
|
|
|
|
|
|
capture(:stdout) do
|
|
|
|
Dir.chdir(app_path){ `bundle exec rake assets:precompile` }
|
|
|
|
end
|
|
|
|
|
|
|
|
manifest = "#{app_path}/public/x/manifest.yml"
|
|
|
|
assets = YAML.load_file(manifest)
|
2011-09-03 22:53:13 -04:00
|
|
|
assert_match(/application-([0-z]+)\.js/, assets["application.js"])
|
2011-08-31 21:50:38 -04:00
|
|
|
end
|
|
|
|
|
2011-08-31 19:53:51 -04:00
|
|
|
test "precompile does not append asset digests when config.assets.digest is false" do
|
|
|
|
app_file "app/assets/stylesheets/application.css.erb", "<%= asset_path('rails.png') %>"
|
|
|
|
app_file "app/assets/javascripts/application.js", "alert();"
|
|
|
|
app_file "config/initializers/compile.rb", "Rails.application.config.assets.digest = false"
|
|
|
|
|
|
|
|
capture(:stdout) do
|
|
|
|
Dir.chdir(app_path){ `bundle exec rake assets:precompile` }
|
|
|
|
end
|
|
|
|
|
|
|
|
assert File.exists?("#{app_path}/public/assets/application.js")
|
|
|
|
assert File.exists?("#{app_path}/public/assets/application.css")
|
|
|
|
|
|
|
|
manifest = "#{app_path}/public/assets/manifest.yml"
|
|
|
|
|
|
|
|
assets = YAML.load_file(manifest)
|
|
|
|
assert_equal "application.js", assets["application.js"]
|
|
|
|
assert_equal "application.css", assets["application.css"]
|
|
|
|
end
|
|
|
|
|
2011-08-31 13:47:33 -04:00
|
|
|
test "assets do not require any assets group gem when manifest file is present" do
|
2011-08-29 17:28:11 -04:00
|
|
|
app_file "app/assets/javascripts/application.js", "alert();"
|
|
|
|
|
|
|
|
ENV["RAILS_ENV"] = "production"
|
|
|
|
capture(:stdout) do
|
|
|
|
Dir.chdir(app_path){ `bundle exec rake assets:precompile` }
|
|
|
|
end
|
|
|
|
manifest = "#{app_path}/public/assets/manifest.yml"
|
|
|
|
assets = YAML.load_file(manifest)
|
|
|
|
asset_path = assets["application.js"]
|
|
|
|
|
|
|
|
require "#{app_path}/config/environment"
|
|
|
|
|
|
|
|
# Checking if Uglifier is defined we can know if Sprockets was reached or not
|
|
|
|
assert !defined?(Uglifier)
|
|
|
|
get "/assets/#{asset_path}"
|
|
|
|
assert_match "alert()", last_response.body
|
|
|
|
assert !defined?(Uglifier)
|
|
|
|
end
|
|
|
|
|
2011-08-31 13:47:33 -04:00
|
|
|
test "assets raise AssetNotPrecompiledError when manifest file is present and requested file isn't precompiled" do
|
2011-08-29 17:28:11 -04:00
|
|
|
app_file "app/views/posts/index.html.erb", "<%= javascript_include_tag 'app' %>"
|
|
|
|
|
|
|
|
app_file "config/routes.rb", <<-RUBY
|
|
|
|
AppTemplate::Application.routes.draw do
|
|
|
|
match '/posts', :to => "posts#index"
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
|
|
|
|
ENV["RAILS_ENV"] = "production"
|
2011-08-31 13:47:33 -04:00
|
|
|
capture(:stdout) do
|
|
|
|
Dir.chdir(app_path){ `bundle exec rake assets:precompile` }
|
|
|
|
end
|
|
|
|
|
|
|
|
# Create file after of precompile
|
|
|
|
app_file "app/assets/javascripts/app.js", "alert();"
|
|
|
|
|
2011-08-29 17:28:11 -04:00
|
|
|
require "#{app_path}/config/environment"
|
|
|
|
class ::PostsController < ActionController::Base ; end
|
|
|
|
|
|
|
|
get '/posts'
|
2011-09-03 22:53:13 -04:00
|
|
|
assert_match(/AssetNotPrecompiledError/, last_response.body)
|
|
|
|
assert_match(/app.js isn't precompiled/, last_response.body)
|
2011-08-29 17:28:11 -04:00
|
|
|
end
|
|
|
|
|
2011-08-15 14:31:47 -04:00
|
|
|
test "precompile appends the md5 hash to files referenced with asset_path and run in the provided RAILS_ENV" do
|
2011-07-25 04:32:03 -04:00
|
|
|
app_file "app/assets/stylesheets/application.css.erb", "<%= asset_path('rails.png') %>"
|
2011-08-31 13:47:33 -04:00
|
|
|
# digest is default in false, we must enable it for test environment
|
|
|
|
app_file "config/initializers/compile.rb", "Rails.application.config.assets.digest = true"
|
2011-07-25 04:32:03 -04:00
|
|
|
|
2011-08-15 14:31:47 -04:00
|
|
|
# capture(:stdout) do
|
|
|
|
Dir.chdir(app_path){ `bundle exec rake assets:precompile RAILS_ENV=test` }
|
|
|
|
# end
|
2011-08-16 11:37:56 -04:00
|
|
|
file = Dir["#{app_path}/public/assets/application-*.css"].first
|
2011-09-03 22:53:13 -04:00
|
|
|
assert_match(/\/assets\/rails-([0-z]+)\.png/, File.read(file))
|
2011-08-15 14:31:47 -04:00
|
|
|
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') %>"
|
2011-08-31 13:47:33 -04:00
|
|
|
app_file "config/initializers/compile.rb", "Rails.application.config.assets.compile = true"
|
2011-08-15 14:31:47 -04:00
|
|
|
|
|
|
|
ENV["RAILS_ENV"] = nil
|
2011-07-25 04:32:03 -04:00
|
|
|
capture(:stdout) do
|
2011-08-15 14:31:47 -04:00
|
|
|
Dir.chdir(app_path){ `bundle exec rake assets:precompile RAILS_GROUPS=assets` }
|
2011-07-25 04:32:03 -04:00
|
|
|
end
|
2011-08-16 11:37:56 -04:00
|
|
|
file = Dir["#{app_path}/public/assets/application-*.css"].first
|
2011-09-03 22:53:13 -04:00
|
|
|
assert_match(/\/assets\/rails-([0-z]+)\.png/, File.read(file))
|
2011-07-25 04:32:03 -04:00
|
|
|
end
|
|
|
|
|
2011-06-21 12:35:48 -04:00
|
|
|
test "assets are cleaned up properly" do
|
|
|
|
app_file "public/assets/application.js", "alert();"
|
|
|
|
app_file "public/assets/application.css", "a { color: green; }"
|
|
|
|
app_file "public/assets/subdir/broken.png", "not really an image file"
|
|
|
|
|
|
|
|
capture(:stdout) do
|
|
|
|
Dir.chdir(app_path){ `bundle exec rake assets:clean` }
|
|
|
|
end
|
|
|
|
|
2011-08-04 19:17:03 -04:00
|
|
|
files = Dir["#{app_path}/public/assets/**/*", "#{app_path}/tmp/cache/*"]
|
2011-06-21 12:35:48 -04:00
|
|
|
assert_equal 0, files.length, "Expected no assets, but found #{files.join(', ')}"
|
|
|
|
end
|
|
|
|
|
2011-05-17 16:51:14 -04:00
|
|
|
test "does not stream session cookies back" do
|
2011-05-17 17:43:44 -04:00
|
|
|
app_file "app/assets/javascripts/demo.js.erb", "<%= :alert %>();"
|
|
|
|
|
|
|
|
app_file "config/routes.rb", <<-RUBY
|
|
|
|
AppTemplate::Application.routes.draw do
|
|
|
|
match '/omg', :to => "omg#index"
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
|
|
|
|
require "#{app_path}/config/environment"
|
|
|
|
|
|
|
|
class ::OmgController < ActionController::Base
|
|
|
|
def index
|
|
|
|
flash[:cool_story] = true
|
|
|
|
render :text => "ok"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
get "/omg"
|
|
|
|
assert_equal 'ok', last_response.body
|
|
|
|
|
|
|
|
get "/assets/demo.js"
|
|
|
|
assert_match "alert()", last_response.body
|
|
|
|
assert_equal nil, last_response.headers["Set-Cookie"]
|
2011-05-17 16:51:14 -04:00
|
|
|
end
|
2011-08-01 15:32:17 -04:00
|
|
|
|
|
|
|
test "files in any assets/ directories are not added to Sprockets" do
|
|
|
|
%w[app lib vendor].each do |dir|
|
|
|
|
app_file "#{dir}/assets/#{dir}_test.erb", "testing"
|
|
|
|
end
|
|
|
|
|
|
|
|
app_file "app/assets/javascripts/demo.js", "alert();"
|
|
|
|
|
|
|
|
require "#{app_path}/config/environment"
|
|
|
|
|
|
|
|
get "/assets/demo.js"
|
|
|
|
assert_match "alert();", last_response.body
|
|
|
|
assert_equal 200, last_response.status
|
|
|
|
end
|
2011-05-08 06:57:07 -04:00
|
|
|
end
|
|
|
|
end
|