2011-05-08 06:57:07 -04:00
|
|
|
require 'isolation/abstract_unit'
|
|
|
|
require 'rack/test'
|
2012-10-15 19:22:12 -04:00
|
|
|
require 'active_support/json'
|
2011-05-08 06:57:07 -04:00
|
|
|
|
|
|
|
module ApplicationTests
|
2012-01-05 20:30:17 -05:00
|
|
|
class AssetsTest < ActiveSupport::TestCase
|
2011-05-08 06:57:07 -04:00
|
|
|
include ActiveSupport::Testing::Isolation
|
|
|
|
include Rack::Test::Methods
|
|
|
|
|
|
|
|
def setup
|
2012-10-14 06:03:39 -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
|
|
|
|
|
2012-06-04 18:14:42 -04:00
|
|
|
def precompile!(env = nil)
|
2011-10-03 01:35:05 -04:00
|
|
|
quietly do
|
2012-10-12 23:44:32 -04:00
|
|
|
precompile_task = "bundle exec rake assets:precompile #{env} --trace 2>&1"
|
2012-09-10 16:58:48 -04:00
|
|
|
output = Dir.chdir(app_path) { %x[ #{precompile_task} ] }
|
|
|
|
assert $?.success?, output
|
|
|
|
output
|
2012-06-04 18:14:42 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def clean_assets!
|
|
|
|
quietly do
|
2012-10-15 19:22:12 -04:00
|
|
|
assert Dir.chdir(app_path) { system('bundle exec rake assets:clobber') }
|
2011-09-23 19:56:49 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-08-30 05:09:17 -04:00
|
|
|
def assert_file_exists(filename)
|
2012-10-15 19:22:12 -04:00
|
|
|
assert Dir[filename].first, "missing #{filename}"
|
2012-08-30 05:09:17 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def assert_no_file_exists(filename)
|
2013-11-01 05:39:06 -04:00
|
|
|
assert !File.exist?(filename), "#{filename} does exist"
|
2012-08-30 05:09:17 -04:00
|
|
|
end
|
|
|
|
|
2011-05-08 06:57:07 -04:00
|
|
|
test "assets routes have higher priority" do
|
2013-04-02 17:25:30 -04:00
|
|
|
app_file "app/assets/images/rails.png", "notactuallyapng"
|
2014-04-09 13:07:31 -04:00
|
|
|
app_file "app/assets/javascripts/demo.js.erb", "a = <%= image_path('rails.png').inspect %>;"
|
2011-05-08 06:57:07 -04:00
|
|
|
|
|
|
|
app_file 'config/routes.rb', <<-RUBY
|
2013-06-09 22:47:07 -04:00
|
|
|
Rails.application.routes.draw do
|
2012-12-31 11:46:19 -05:00
|
|
|
get '*path', to: lambda { |env| [200, { "Content-Type" => "text/html" }, ["Not an asset"]] }
|
2011-05-08 06:57:07 -04:00
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
|
2014-05-16 21:00:10 -04:00
|
|
|
add_to_env_config "development", "config.assets.digest = false"
|
|
|
|
|
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"
|
2011-10-05 03:35:27 -04:00
|
|
|
assert_equal 'a = "/assets/rails.png";', last_response.body.strip
|
2011-05-08 06:57:07 -04:00
|
|
|
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-09-23 19:56:49 -04:00
|
|
|
add_to_env_config "production", "config.assets.compile = true"
|
2011-08-31 13:47:33 -04:00
|
|
|
|
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-09-23 19:56:49 -04:00
|
|
|
precompile!
|
|
|
|
|
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"
|
2012-10-15 19:22:12 -04:00
|
|
|
assert_equal "alert();\n", File.read(file)
|
2011-07-01 03:53:48 -04:00
|
|
|
end
|
2011-06-21 10:18:50 -04:00
|
|
|
end
|
|
|
|
|
2012-11-13 19:26:22 -05:00
|
|
|
def test_precompile_does_not_hit_the_database
|
|
|
|
app_file "app/assets/javascripts/application.js", "alert();"
|
|
|
|
app_file "app/assets/javascripts/foo/application.js", "alert();"
|
2013-03-25 05:10:31 -04:00
|
|
|
app_file "app/controllers/users_controller.rb", <<-eoruby
|
|
|
|
class UsersController < ApplicationController; end
|
2012-11-13 19:26:22 -05:00
|
|
|
eoruby
|
|
|
|
app_file "app/models/user.rb", <<-eoruby
|
2013-07-09 16:36:50 -04:00
|
|
|
class User < ActiveRecord::Base; raise 'should not be reached'; end
|
2012-11-13 19:26:22 -05:00
|
|
|
eoruby
|
|
|
|
|
|
|
|
ENV['RAILS_ENV'] = 'production'
|
|
|
|
ENV['DATABASE_URL'] = 'postgresql://baduser:badpass@127.0.0.1/dbname'
|
|
|
|
|
|
|
|
precompile!
|
|
|
|
|
|
|
|
files = Dir["#{app_path}/public/assets/application-*.js"]
|
|
|
|
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();".strip, File.read(file).strip
|
|
|
|
end
|
|
|
|
ensure
|
|
|
|
ENV.delete 'RAILS_ENV'
|
|
|
|
ENV.delete 'DATABASE_URL'
|
|
|
|
end
|
|
|
|
|
2011-12-10 18:12:09 -05:00
|
|
|
test "precompile application.js and application.css and all other non JS/CSS files" do
|
2011-09-08 16:44:08 -04:00
|
|
|
app_file "app/assets/javascripts/application.js", "alert();"
|
|
|
|
app_file "app/assets/stylesheets/application.css", "body{}"
|
2011-09-15 02:14:23 -04:00
|
|
|
|
|
|
|
app_file "app/assets/javascripts/someapplication.js", "alert();"
|
|
|
|
app_file "app/assets/stylesheets/someapplication.css", "body{}"
|
|
|
|
|
2011-09-08 16:44:08 -04:00
|
|
|
app_file "app/assets/javascripts/something.min.js", "alert();"
|
|
|
|
app_file "app/assets/stylesheets/something.min.css", "body{}"
|
|
|
|
|
2011-12-10 18:12:09 -05:00
|
|
|
app_file "app/assets/javascripts/something.else.js.erb", "alert();"
|
|
|
|
app_file "app/assets/stylesheets/something.else.css.erb", "body{}"
|
|
|
|
|
2011-09-08 16:44:08 -04:00
|
|
|
images_should_compile = ["a.png", "happyface.png", "happy_face.png", "happy.face.png",
|
2011-12-10 18:12:09 -05:00
|
|
|
"happy-face.png", "happy.happy_face.png", "happy_happy.face.png",
|
2012-08-30 05:09:17 -04:00
|
|
|
"happy.happy.face.png", "-happy.png", "-happy.face.png",
|
|
|
|
"_happy.face.png", "_happy.png"]
|
2011-09-23 19:56:49 -04:00
|
|
|
|
2011-09-08 16:44:08 -04:00
|
|
|
images_should_compile.each do |filename|
|
|
|
|
app_file "app/assets/images/#{filename}", "happy"
|
|
|
|
end
|
|
|
|
|
2011-09-23 19:56:49 -04:00
|
|
|
precompile!
|
2011-09-08 16:44:08 -04:00
|
|
|
|
2012-10-15 19:22:12 -04:00
|
|
|
images_should_compile = ["a-*.png", "happyface-*.png", "happy_face-*.png", "happy.face-*.png",
|
|
|
|
"happy-face-*.png", "happy.happy_face-*.png", "happy_happy.face-*.png",
|
|
|
|
"happy.happy.face-*.png", "-happy-*.png", "-happy.face-*.png",
|
|
|
|
"_happy.face-*.png", "_happy-*.png"]
|
|
|
|
|
2011-09-08 16:44:08 -04:00
|
|
|
images_should_compile.each do |filename|
|
2012-08-30 05:09:17 -04:00
|
|
|
assert_file_exists("#{app_path}/public/assets/#{filename}")
|
2011-09-08 16:44:08 -04:00
|
|
|
end
|
2011-09-15 02:14:23 -04:00
|
|
|
|
2012-10-15 19:22:12 -04:00
|
|
|
assert_file_exists("#{app_path}/public/assets/application-*.js")
|
|
|
|
assert_file_exists("#{app_path}/public/assets/application-*.css")
|
2011-09-15 02:14:23 -04:00
|
|
|
|
2012-10-15 19:22:12 -04:00
|
|
|
assert_no_file_exists("#{app_path}/public/assets/someapplication-*.js")
|
|
|
|
assert_no_file_exists("#{app_path}/public/assets/someapplication-*.css")
|
2011-09-15 02:14:23 -04:00
|
|
|
|
2012-10-15 19:22:12 -04:00
|
|
|
assert_no_file_exists("#{app_path}/public/assets/something.min-*.js")
|
|
|
|
assert_no_file_exists("#{app_path}/public/assets/something.min-*.css")
|
2011-12-10 18:12:09 -05:00
|
|
|
|
2012-10-15 19:22:12 -04:00
|
|
|
assert_no_file_exists("#{app_path}/public/assets/something.else-*.js")
|
|
|
|
assert_no_file_exists("#{app_path}/public/assets/something.else-*.css")
|
2012-08-07 13:54:08 -04:00
|
|
|
end
|
|
|
|
|
2012-08-30 05:09:17 -04:00
|
|
|
test "precompile something.js for directory containing index file" do
|
|
|
|
add_to_config "config.assets.precompile = [ 'something.js' ]"
|
|
|
|
app_file "app/assets/javascripts/something/index.js.erb", "alert();"
|
2012-08-07 13:54:08 -04:00
|
|
|
|
2012-08-30 05:09:17 -04:00
|
|
|
precompile!
|
|
|
|
|
2012-10-15 19:22:12 -04:00
|
|
|
assert_file_exists("#{app_path}/public/assets/something-*.js")
|
2011-09-08 16:44:08 -04:00
|
|
|
end
|
|
|
|
|
2013-05-08 15:10:40 -04:00
|
|
|
test 'precompile use assets defined in app env config' do
|
|
|
|
add_to_env_config 'production', 'config.assets.precompile = [ "something.js" ]'
|
|
|
|
|
|
|
|
app_file 'app/assets/javascripts/something.js.erb', 'alert();'
|
|
|
|
|
|
|
|
precompile! 'RAILS_ENV=production'
|
|
|
|
|
|
|
|
assert_file_exists("#{app_path}/public/assets/something-*.js")
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'precompile use assets defined in app config and reassigned in app env config' do
|
|
|
|
add_to_config 'config.assets.precompile = [ "something.js" ]'
|
|
|
|
add_to_env_config 'production', 'config.assets.precompile += [ "another.js" ]'
|
|
|
|
|
|
|
|
app_file 'app/assets/javascripts/something.js.erb', 'alert();'
|
|
|
|
app_file 'app/assets/javascripts/another.js.erb', 'alert();'
|
|
|
|
|
|
|
|
precompile! 'RAILS_ENV=production'
|
|
|
|
|
|
|
|
assert_file_exists("#{app_path}/public/assets/something-*.js")
|
|
|
|
assert_file_exists("#{app_path}/public/assets/another-*.js")
|
|
|
|
end
|
|
|
|
|
2011-09-06 10:24:22 -04:00
|
|
|
test "asset pipeline should use a Sprockets::Index when config.assets.digest is true" do
|
2011-09-07 00:00:36 -04:00
|
|
|
add_to_config "config.action_controller.perform_caching = false"
|
|
|
|
|
2011-09-06 10:24:22 -04:00
|
|
|
ENV["RAILS_ENV"] = "production"
|
|
|
|
require "#{app_path}/config/environment"
|
|
|
|
|
|
|
|
assert_equal Sprockets::Index, Rails.application.assets.class
|
|
|
|
end
|
|
|
|
|
2011-08-31 13:47:33 -04:00
|
|
|
test "precompile creates a manifest file with all the assets listed" do
|
2014-04-04 17:03:14 -04:00
|
|
|
app_file "app/assets/images/rails.png", "notactuallyapng"
|
2014-04-09 13:07:31 -04:00
|
|
|
app_file "app/assets/stylesheets/application.css.erb", "<%= asset_path('rails.png') %>"
|
2011-08-29 17:28:11 -04:00
|
|
|
app_file "app/assets/javascripts/application.js", "alert();"
|
|
|
|
|
2011-09-23 19:56:49 -04:00
|
|
|
precompile!
|
2012-10-15 19:22:12 -04:00
|
|
|
manifest = Dir["#{app_path}/public/assets/manifest-*.json"].first
|
2011-08-29 17:28:11 -04:00
|
|
|
|
2012-10-15 19:22:12 -04:00
|
|
|
assets = ActiveSupport::JSON.decode(File.read(manifest))
|
|
|
|
assert_match(/application-([0-z]+)\.js/, assets["assets"]["application.js"])
|
|
|
|
assert_match(/application-([0-z]+)\.css/, assets["assets"]["application.css"])
|
2011-08-29 17:28:11 -04:00
|
|
|
end
|
|
|
|
|
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();"
|
2011-09-07 00:00:36 -04:00
|
|
|
add_to_config "config.assets.prefix = '/x'"
|
2011-08-31 21:50:38 -04:00
|
|
|
|
2011-09-23 19:56:49 -04:00
|
|
|
precompile!
|
2011-08-31 21:50:38 -04:00
|
|
|
|
2012-10-15 19:22:12 -04:00
|
|
|
manifest = Dir["#{app_path}/public/x/manifest-*.json"].first
|
|
|
|
assets = ActiveSupport::JSON.decode(File.read(manifest))
|
|
|
|
assert_match(/application-([0-z]+)\.js/, assets["assets"]["application.js"])
|
2011-08-31 19:53:51 -04:00
|
|
|
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();"
|
2014-12-19 17:16:29 -05:00
|
|
|
add_to_env_config "production", "config.serve_static_files = true"
|
2011-08-29 17:28:11 -04:00
|
|
|
|
|
|
|
ENV["RAILS_ENV"] = "production"
|
2011-09-23 19:56:49 -04:00
|
|
|
precompile!
|
|
|
|
|
2012-10-15 19:22:12 -04:00
|
|
|
manifest = Dir["#{app_path}/public/assets/manifest-*.json"].first
|
|
|
|
assets = ActiveSupport::JSON.decode(File.read(manifest))
|
|
|
|
asset_path = assets["assets"]["application.js"]
|
2011-08-29 17:28:11 -04:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2013-03-26 14:04:52 -04:00
|
|
|
test "precompile properly refers files referenced with asset_path and runs in the provided RAILS_ENV" do
|
2013-04-02 17:25:30 -04:00
|
|
|
app_file "app/assets/images/rails.png", "notactuallyapng"
|
2011-07-25 04:32:03 -04:00
|
|
|
app_file "app/assets/stylesheets/application.css.erb", "<%= asset_path('rails.png') %>"
|
2011-10-04 05:24:12 -04:00
|
|
|
add_to_env_config "test", "config.assets.digest = true"
|
2011-07-25 04:32:03 -04:00
|
|
|
|
2012-06-04 18:14:42 -04:00
|
|
|
precompile!('RAILS_ENV=test')
|
|
|
|
|
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
|
|
|
|
|
2012-10-15 19:22:12 -04:00
|
|
|
test "precompile shouldn't use the digests present in manifest.json" do
|
2013-04-02 17:25:30 -04:00
|
|
|
app_file "app/assets/images/rails.png", "notactuallyapng"
|
|
|
|
|
2014-04-09 13:07:31 -04:00
|
|
|
app_file "app/assets/stylesheets/application.css.erb", "p { url: <%= asset_path('rails.png') %> }"
|
2011-09-26 12:56:38 -04:00
|
|
|
|
|
|
|
ENV["RAILS_ENV"] = "production"
|
|
|
|
precompile!
|
|
|
|
|
2012-10-15 19:22:12 -04:00
|
|
|
manifest = Dir["#{app_path}/public/assets/manifest-*.json"].first
|
|
|
|
assets = ActiveSupport::JSON.decode(File.read(manifest))
|
|
|
|
asset_path = assets["assets"]["application.css"]
|
2011-09-26 12:56:38 -04:00
|
|
|
|
2012-10-15 19:22:12 -04:00
|
|
|
app_file "app/assets/images/rails.png", "p { url: change }"
|
2011-09-26 12:56:38 -04:00
|
|
|
|
|
|
|
precompile!
|
2012-10-15 19:22:12 -04:00
|
|
|
assets = ActiveSupport::JSON.decode(File.read(manifest))
|
2011-09-26 12:56:38 -04:00
|
|
|
|
2012-10-15 19:22:12 -04:00
|
|
|
assert_not_equal asset_path, assets["assets"]["application.css"]
|
2011-09-26 12:56:38 -04:00
|
|
|
end
|
|
|
|
|
2012-10-15 19:22:12 -04:00
|
|
|
test "precompile appends the md5 hash to files referenced with asset_path and run in production with digest true" do
|
2013-04-02 17:25:30 -04:00
|
|
|
app_file "app/assets/images/rails.png", "notactuallyapng"
|
2014-04-09 13:07:31 -04:00
|
|
|
app_file "app/assets/stylesheets/application.css.erb", "<%= asset_path('rails.png') %>"
|
2011-08-15 14:31:47 -04:00
|
|
|
|
2014-05-16 21:00:10 -04:00
|
|
|
ENV["RAILS_ENV"] = "production"
|
|
|
|
precompile!
|
2012-06-04 18:14:42 -04:00
|
|
|
|
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-09-13 04:19:02 -04:00
|
|
|
test "precompile should handle utf8 filenames" do
|
2011-10-08 15:56:06 -04:00
|
|
|
filename = "レイルズ.png"
|
2013-09-13 04:44:35 -04:00
|
|
|
app_file "app/assets/images/#{filename}", "not an image really"
|
2011-10-09 12:54:54 -04:00
|
|
|
add_to_config "config.assets.precompile = [ /\.png$/, /application.(css|js)$/ ]"
|
2011-09-13 04:19:02 -04:00
|
|
|
|
2011-09-23 19:56:49 -04:00
|
|
|
precompile!
|
2012-10-15 19:22:12 -04:00
|
|
|
|
|
|
|
manifest = Dir["#{app_path}/public/assets/manifest-*.json"].first
|
|
|
|
assets = ActiveSupport::JSON.decode(File.read(manifest))
|
2013-04-02 17:25:30 -04:00
|
|
|
assert asset_path = assets["assets"].find { |(k, _)| k && k =~ /.png/ }[1]
|
2012-10-15 19:22:12 -04:00
|
|
|
|
2011-10-08 16:24:52 -04:00
|
|
|
require "#{app_path}/config/environment"
|
2011-09-13 04:19:02 -04:00
|
|
|
|
2012-10-15 19:22:12 -04:00
|
|
|
get "/assets/#{URI.parser.escape(asset_path)}"
|
2013-09-13 04:44:35 -04:00
|
|
|
assert_match "not an image really", last_response.body
|
2012-10-15 19:22:12 -04:00
|
|
|
assert_file_exists("#{app_path}/public/assets/#{asset_path}")
|
2011-09-13 04:19:02 -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"
|
|
|
|
|
2012-06-04 18:14:42 -04:00
|
|
|
clean_assets!
|
2011-06-21 12:35:48 -04:00
|
|
|
|
2012-09-28 16:20:17 -04:00
|
|
|
files = Dir["#{app_path}/public/assets/**/*", "#{app_path}/tmp/cache/assets/development/*",
|
|
|
|
"#{app_path}/tmp/cache/assets/test/*", "#{app_path}/tmp/cache/assets/production/*"]
|
2011-06-21 12:35:48 -04:00
|
|
|
assert_equal 0, files.length, "Expected no assets, but found #{files.join(', ')}"
|
|
|
|
end
|
|
|
|
|
2011-09-06 05:34:24 -04:00
|
|
|
test "assets routes are not drawn when compilation is disabled" do
|
|
|
|
app_file "app/assets/javascripts/demo.js.erb", "<%= :alert %>();"
|
2011-09-13 19:36:38 -04:00
|
|
|
add_to_config "config.assets.compile = false"
|
2011-09-06 05:34:24 -04:00
|
|
|
|
|
|
|
ENV["RAILS_ENV"] = "production"
|
|
|
|
require "#{app_path}/config/environment"
|
|
|
|
|
|
|
|
get "/assets/demo.js"
|
|
|
|
assert_equal 404, last_response.status
|
|
|
|
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
|
2013-06-09 22:47:07 -04:00
|
|
|
Rails.application.routes.draw do
|
2012-04-24 23:32:09 -04:00
|
|
|
get '/omg', :to => "omg#index"
|
2011-05-17 17:43:44 -04:00
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
|
2014-05-16 21:00:10 -04:00
|
|
|
add_to_env_config "development", "config.assets.digest = false"
|
|
|
|
|
2011-05-17 17:43:44 -04:00
|
|
|
require "#{app_path}/config/environment"
|
|
|
|
|
|
|
|
class ::OmgController < ActionController::Base
|
|
|
|
def index
|
|
|
|
flash[:cool_story] = true
|
2012-10-14 06:03:39 -04:00
|
|
|
render text: "ok"
|
2011-05-17 17:43:44 -04:00
|
|
|
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();"
|
|
|
|
|
2014-05-16 21:00:10 -04:00
|
|
|
add_to_env_config "development", "config.assets.digest = false"
|
|
|
|
|
2011-08-01 15:32:17 -04:00
|
|
|
require "#{app_path}/config/environment"
|
|
|
|
|
|
|
|
get "/assets/demo.js"
|
|
|
|
assert_match "alert();", last_response.body
|
|
|
|
assert_equal 200, last_response.status
|
|
|
|
end
|
2011-09-23 19:56:49 -04:00
|
|
|
|
|
|
|
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'
|
2012-04-08 10:28:31 -04:00
|
|
|
assert_match(/<script src="\/assets\/application-([0-z]+)\.js"><\/script>/, last_response.body)
|
|
|
|
assert_no_match(/<script src="\/assets\/xmlhr-([0-z]+)\.js"><\/script>/, last_response.body)
|
2011-09-23 19:56:49 -04:00
|
|
|
end
|
|
|
|
|
2011-10-02 06:14:31 -04:00
|
|
|
test "assets can access model information when precompiling" do
|
|
|
|
app_file "app/models/post.rb", "class Post; end"
|
|
|
|
app_file "app/assets/javascripts/application.js", "//= require_tree ."
|
|
|
|
app_file "app/assets/javascripts/xmlhr.js.erb", "<%= Post.name %>"
|
|
|
|
|
|
|
|
precompile!
|
2012-10-15 19:22:12 -04:00
|
|
|
assert_equal "Post;\n", File.read(Dir["#{app_path}/public/assets/application-*.js"].first)
|
2011-10-02 06:14:31 -04:00
|
|
|
end
|
|
|
|
|
2012-01-10 13:33:26 -05:00
|
|
|
test "initialization on the assets group should set assets_dir" do
|
|
|
|
require "#{app_path}/config/application"
|
|
|
|
Rails.application.initialize!(:assets)
|
|
|
|
assert_not_nil Rails.application.config.action_controller.assets_dir
|
|
|
|
end
|
|
|
|
|
2011-10-03 01:36:04 -04:00
|
|
|
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
|
|
|
|
|
2011-10-03 08:42:50 -04:00
|
|
|
test "digested assets are not mistakenly removed" do
|
2011-10-04 14:02:28 -04:00
|
|
|
app_file "app/assets/application.js", "alert();"
|
2011-10-03 08:42:50 -04:00
|
|
|
add_to_config "config.assets.compile = true"
|
|
|
|
|
2012-06-04 18:14:42 -04:00
|
|
|
precompile!
|
2011-10-03 08:42:50 -04:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2011-10-04 14:02:06 -04:00
|
|
|
test "digested assets are removed from configured path" do
|
|
|
|
app_file "public/production_assets/application.js", "alert();"
|
|
|
|
add_to_env_config "production", "config.assets.prefix = 'production_assets'"
|
|
|
|
|
|
|
|
ENV["RAILS_ENV"] = nil
|
2012-06-04 18:14:42 -04:00
|
|
|
|
|
|
|
clean_assets!
|
2011-10-04 14:02:06 -04:00
|
|
|
|
2012-10-15 19:22:12 -04:00
|
|
|
files = Dir["#{app_path}/public/production_assets/application-*.js"]
|
2011-10-04 14:02:06 -04:00
|
|
|
assert_equal 0, files.length, "Expected application.js asset to be removed, but still exists"
|
|
|
|
end
|
|
|
|
|
2011-11-17 06:48:12 -05:00
|
|
|
test "asset urls should use the request's protocol by default" do
|
|
|
|
app_with_assets_in_view
|
|
|
|
add_to_config "config.asset_host = 'example.com'"
|
2014-05-16 21:00:10 -04:00
|
|
|
add_to_env_config "development", "config.assets.digest = false"
|
2011-11-17 06:48:12 -05:00
|
|
|
require "#{app_path}/config/environment"
|
|
|
|
class ::PostsController < ActionController::Base; end
|
|
|
|
|
|
|
|
get '/posts', {}, {'HTTPS'=>'off'}
|
|
|
|
assert_match('src="http://example.com/assets/application.js', last_response.body)
|
|
|
|
get '/posts', {}, {'HTTPS'=>'on'}
|
|
|
|
assert_match('src="https://example.com/assets/application.js', last_response.body)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "asset urls should be protocol-relative if no request is in scope" do
|
2013-04-02 17:25:30 -04:00
|
|
|
app_file "app/assets/images/rails.png", "notreallyapng"
|
2014-04-09 13:07:31 -04:00
|
|
|
app_file "app/assets/javascripts/image_loader.js.erb", "var src='<%= image_path('rails.png') %>';"
|
2014-04-23 15:14:20 -04:00
|
|
|
add_to_config "config.assets.precompile = %w{rails.png image_loader.js}"
|
2011-11-17 06:48:12 -05:00
|
|
|
add_to_config "config.asset_host = 'example.com'"
|
2014-05-16 21:00:10 -04:00
|
|
|
add_to_env_config "development", "config.assets.digest = false"
|
2011-11-17 06:48:12 -05:00
|
|
|
precompile!
|
|
|
|
|
2014-04-04 17:03:14 -04:00
|
|
|
assert_match "src='//example.com/assets/rails.png'", File.read(Dir["#{app_path}/public/assets/image_loader-*.js"].first)
|
2011-11-17 06:48:12 -05:00
|
|
|
end
|
|
|
|
|
2011-12-12 10:52:56 -05:00
|
|
|
test "asset paths should use RAILS_RELATIVE_URL_ROOT by default" do
|
|
|
|
ENV["RAILS_RELATIVE_URL_ROOT"] = "/sub/uri"
|
2013-04-02 17:25:30 -04:00
|
|
|
app_file "app/assets/images/rails.png", "notreallyapng"
|
2014-04-09 13:07:31 -04:00
|
|
|
app_file "app/assets/javascripts/app.js.erb", "var src='<%= image_path('rails.png') %>';"
|
2014-04-23 15:14:20 -04:00
|
|
|
add_to_config "config.assets.precompile = %w{rails.png app.js}"
|
2014-05-16 21:00:10 -04:00
|
|
|
add_to_env_config "development", "config.assets.digest = false"
|
2011-12-12 10:52:56 -05:00
|
|
|
precompile!
|
|
|
|
|
2014-04-04 17:03:14 -04:00
|
|
|
assert_match "src='/sub/uri/assets/rails.png'", File.read(Dir["#{app_path}/public/assets/app-*.js"].first)
|
2012-08-30 05:09:17 -04:00
|
|
|
end
|
|
|
|
|
2012-01-12 12:46:40 -05:00
|
|
|
test "assets:cache:clean should clean cache" do
|
|
|
|
ENV["RAILS_ENV"] = "production"
|
|
|
|
precompile!
|
|
|
|
|
|
|
|
quietly do
|
2012-10-15 19:22:12 -04:00
|
|
|
Dir.chdir(app_path){ `bundle exec rake assets:clobber` }
|
2012-01-12 12:46:40 -05:00
|
|
|
end
|
|
|
|
|
2012-10-15 19:22:12 -04:00
|
|
|
assert !File.exist?("#{app_path}/tmp/cache/assets")
|
2012-01-12 12:46:40 -05:00
|
|
|
end
|
|
|
|
|
2011-09-23 19:56:49 -04:00
|
|
|
private
|
2011-10-02 06:14:31 -04:00
|
|
|
|
2011-09-23 19:56:49 -04:00
|
|
|
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
|
2013-06-09 22:47:07 -04:00
|
|
|
Rails.application.routes.draw do
|
2012-04-24 23:32:09 -04:00
|
|
|
get '/posts', :to => "posts#index"
|
2011-09-23 19:56:49 -04:00
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
end
|
2011-05-08 06:57:07 -04:00
|
|
|
end
|
|
|
|
end
|