2017-08-14 13:08:09 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-06 13:16:09 -04:00
|
|
|
require "isolation/abstract_unit"
|
|
|
|
require "rack/test"
|
|
|
|
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
|
|
|
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)
|
2015-09-27 17:34:13 -04:00
|
|
|
with_env env.to_h do
|
|
|
|
quietly do
|
2016-01-23 14:25:02 -05:00
|
|
|
precompile_task = "bin/rails assets:precompile --trace 2>&1"
|
2015-09-27 17:34:13 -04:00
|
|
|
output = Dir.chdir(app_path) { %x[ #{precompile_task} ] }
|
|
|
|
assert $?.success?, output
|
|
|
|
output
|
|
|
|
end
|
2012-06-04 18:14:42 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-09-27 17:34:13 -04:00
|
|
|
def with_env(env)
|
|
|
|
env.each { |k, v| ENV[k.to_s] = v }
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
env.each_key { |k| ENV.delete k.to_s }
|
|
|
|
end
|
|
|
|
|
2012-06-04 18:14:42 -04:00
|
|
|
def clean_assets!
|
|
|
|
quietly do
|
2016-08-06 13:16:09 -04:00
|
|
|
assert Dir.chdir(app_path) { system("bin/rails 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)
|
2015-09-27 17:34:13 -04:00
|
|
|
globbed = Dir[filename]
|
|
|
|
assert globbed.one?, "Found #{globbed.size} files matching #{filename}. All files in the directory: #{Dir.entries(File.dirname(filename)).inspect}"
|
2012-08-30 05:09:17 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def assert_no_file_exists(filename)
|
2018-05-12 22:26:10 -04:00
|
|
|
assert_not 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
|
|
|
|
2016-08-06 13:16:09 -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"
|
|
|
|
|
2017-09-08 20:48:15 -04:00
|
|
|
require "#{app_path}/config/environment"
|
2011-05-17 17:43:44 -04:00
|
|
|
|
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"
|
2015-07-29 19:16:57 -04:00
|
|
|
add_to_env_config "production", "config.assets.precompile = []"
|
2011-08-31 13:47:33 -04:00
|
|
|
|
2015-09-27 17:34:13 -04:00
|
|
|
# Load app env
|
|
|
|
app "production"
|
2011-07-06 20:38:54 -04:00
|
|
|
|
2018-04-17 18:21:34 -04:00
|
|
|
assert_not defined?(Uglifier)
|
2011-07-06 20:38:54 -04:00
|
|
|
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
|
2015-09-27 17:34:13 -04:00
|
|
|
app_file "app/assets/config/manifest.js", "//= link_tree ../javascripts"
|
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-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
|
2015-09-27 17:34:13 -04:00
|
|
|
app_file "app/assets/config/manifest.js", "//= link_tree ../javascripts"
|
2012-11-13 19:26:22 -05:00
|
|
|
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
|
|
|
|
|
2015-09-27 17:34:13 -04:00
|
|
|
precompile! \
|
2016-08-06 13:16:09 -04:00
|
|
|
RAILS_ENV: "production",
|
|
|
|
DATABASE_URL: "postgresql://baduser:badpass@127.0.0.1/dbname"
|
2012-11-13 19:26:22 -05:00
|
|
|
|
|
|
|
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
|
|
|
|
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!
|
|
|
|
|
2015-12-16 13:30:58 -05:00
|
|
|
assert_file_exists("#{app_path}/public/assets/something/index-*.js")
|
2011-09-08 16:44:08 -04:00
|
|
|
end
|
|
|
|
|
2016-08-06 13:16:09 -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();"
|
2013-05-08 15:10:40 -04:00
|
|
|
|
2016-08-06 13:16:09 -04:00
|
|
|
precompile! RAILS_ENV: "production"
|
2013-05-08 15:10:40 -04:00
|
|
|
|
|
|
|
assert_file_exists("#{app_path}/public/assets/something-*.js")
|
|
|
|
end
|
|
|
|
|
2016-08-06 13:16:09 -04:00
|
|
|
test "sprockets cache is not shared between environments" do
|
2016-03-02 12:21:38 -05:00
|
|
|
app_file "app/assets/images/rails.png", "notactuallyapng"
|
2016-06-30 18:39:37 -04:00
|
|
|
app_file "app/assets/stylesheets/application.css.erb", "body { background: '<%= asset_path('rails.png') %>'; }"
|
2016-08-06 13:16:09 -04:00
|
|
|
add_to_env_config "production", 'config.assets.prefix = "production_assets"'
|
2016-03-02 12:21:38 -05:00
|
|
|
|
|
|
|
precompile!
|
|
|
|
|
|
|
|
assert_file_exists("#{app_path}/public/assets/application-*.css")
|
|
|
|
|
|
|
|
file = Dir["#{app_path}/public/assets/application-*.css"].first
|
|
|
|
assert_match(/assets\/rails-([0-z]+)\.png/, File.read(file))
|
|
|
|
|
2016-08-06 13:16:09 -04:00
|
|
|
precompile! RAILS_ENV: "production"
|
2016-03-02 12:21:38 -05:00
|
|
|
|
|
|
|
assert_file_exists("#{app_path}/public/production_assets/application-*.css")
|
|
|
|
|
|
|
|
file = Dir["#{app_path}/public/production_assets/application-*.css"].first
|
|
|
|
assert_match(/production_assets\/rails-([0-z]+)\.png/, File.read(file))
|
|
|
|
end
|
|
|
|
|
2016-08-06 13:16:09 -04:00
|
|
|
test "precompile use assets defined in app config and reassigned in app env config" do
|
2015-08-05 09:14:09 -04:00
|
|
|
add_to_config 'config.assets.precompile = [ "something_manifest.js" ]'
|
2016-08-06 13:16:09 -04:00
|
|
|
add_to_env_config "production", 'config.assets.precompile += [ "another_manifest.js" ]'
|
2015-08-05 09:14:09 -04:00
|
|
|
|
2016-08-06 13:16:09 -04:00
|
|
|
app_file "app/assets/config/something_manifest.js", "//= link something.js"
|
|
|
|
app_file "app/assets/config/another_manifest.js", "//= link another.js"
|
2013-05-08 15:10:40 -04:00
|
|
|
|
2016-08-06 13:16:09 -04:00
|
|
|
app_file "app/assets/javascripts/something.js.erb", "alert();"
|
|
|
|
app_file "app/assets/javascripts/another.js.erb", "alert();"
|
2013-05-08 15:10:40 -04:00
|
|
|
|
2016-08-06 13:16:09 -04:00
|
|
|
precompile! RAILS_ENV: "production"
|
2013-05-08 15:10:40 -04:00
|
|
|
|
2015-09-27 17:34:13 -04:00
|
|
|
assert_file_exists("#{app_path}/public/assets/something_manifest-*.js")
|
2013-05-08 15:10:40 -04:00
|
|
|
assert_file_exists("#{app_path}/public/assets/something-*.js")
|
2015-09-27 17:34:13 -04:00
|
|
|
assert_file_exists("#{app_path}/public/assets/another_manifest-*.js")
|
2013-05-08 15:10:40 -04:00
|
|
|
assert_file_exists("#{app_path}/public/assets/another-*.js")
|
|
|
|
end
|
|
|
|
|
2015-08-05 09:14:09 -04:00
|
|
|
test "asset pipeline should use a Sprockets::CachedEnvironment when config.assets.digest is true" do
|
2011-09-07 00:00:36 -04:00
|
|
|
add_to_config "config.action_controller.perform_caching = false"
|
2015-04-26 12:55:05 -04:00
|
|
|
add_to_env_config "production", "config.assets.compile = true"
|
2011-09-07 00:00:36 -04:00
|
|
|
|
2015-09-27 17:34:13 -04:00
|
|
|
# Load app env
|
|
|
|
app "production"
|
2011-09-06 10:24:22 -04:00
|
|
|
|
2015-08-05 09:14:09 -04:00
|
|
|
assert_equal Sprockets::CachedEnvironment, Rails.application.assets.class
|
2011-09-06 10:24:22 -04:00
|
|
|
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!
|
2011-08-29 17:28:11 -04:00
|
|
|
|
2015-09-27 17:34:13 -04:00
|
|
|
manifest = Dir["#{app_path}/public/assets/.sprockets-manifest-*.json"].first
|
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
|
|
|
|
2015-03-29 20:56:00 -04:00
|
|
|
manifest = Dir["#{app_path}/public/x/.sprockets-manifest-*.json"].first
|
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"])
|
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();"
|
2015-11-04 16:31:16 -05:00
|
|
|
add_to_env_config "production", "config.public_file_server.enabled = true"
|
2011-08-29 17:28:11 -04:00
|
|
|
|
2016-08-06 13:16:09 -04:00
|
|
|
precompile! RAILS_ENV: "production"
|
2011-09-23 19:56:49 -04:00
|
|
|
|
2015-03-29 20:56:00 -04:00
|
|
|
manifest = Dir["#{app_path}/public/assets/.sprockets-manifest-*.json"].first
|
2012-10-15 19:22:12 -04:00
|
|
|
assets = ActiveSupport::JSON.decode(File.read(manifest))
|
|
|
|
asset_path = assets["assets"]["application.js"]
|
2011-08-29 17:28:11 -04:00
|
|
|
|
2015-09-27 17:34:13 -04:00
|
|
|
# Load app env
|
|
|
|
app "production"
|
2011-08-29 17:28:11 -04:00
|
|
|
|
|
|
|
# Checking if Uglifier is defined we can know if Sprockets was reached or not
|
2018-04-17 18:21:34 -04:00
|
|
|
assert_not defined?(Uglifier)
|
2011-08-29 17:28:11 -04:00
|
|
|
get "/assets/#{asset_path}"
|
|
|
|
assert_match "alert()", last_response.body
|
2018-04-17 18:21:34 -04:00
|
|
|
assert_not defined?(Uglifier)
|
2011-08-29 17:28:11 -04:00
|
|
|
end
|
|
|
|
|
2015-09-27 17:34:13 -04:00
|
|
|
test "precompile properly refers files referenced with asset_path" do
|
2013-04-02 17:25:30 -04:00
|
|
|
app_file "app/assets/images/rails.png", "notactuallyapng"
|
2015-08-05 09:14:09 -04:00
|
|
|
app_file "app/assets/stylesheets/application.css.erb", "p { background-image: url(<%= asset_path('rails.png') %>) }"
|
2011-07-25 04:32:03 -04:00
|
|
|
|
2015-09-27 17:34:13 -04:00
|
|
|
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-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"
|
|
|
|
|
2015-08-05 09:14:09 -04:00
|
|
|
app_file "app/assets/stylesheets/application.css.erb", "p { background-image: url(<%= asset_path('rails.png') %>) }"
|
2011-09-26 12:56:38 -04:00
|
|
|
|
2016-08-06 13:16:09 -04:00
|
|
|
precompile! RAILS_ENV: "production"
|
2011-09-26 12:56:38 -04:00
|
|
|
|
2015-03-29 20:56:00 -04:00
|
|
|
manifest = Dir["#{app_path}/public/assets/.sprockets-manifest-*.json"].first
|
2012-10-15 19:22:12 -04:00
|
|
|
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!
|
|
|
|
|
2015-09-27 17:34:13 -04:00
|
|
|
assets = ActiveSupport::JSON.decode(File.read(manifest))
|
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
|
|
|
|
|
2016-05-17 15:44:57 -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"
|
2015-08-05 09:14:09 -04:00
|
|
|
app_file "app/assets/stylesheets/application.css.erb", "p { background-image: url(<%= asset_path('rails.png') %>) }"
|
2011-08-15 14:31:47 -04:00
|
|
|
|
2016-08-06 13:16:09 -04:00
|
|
|
precompile! RAILS_ENV: "production"
|
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"
|
2015-09-27 17:34:13 -04:00
|
|
|
app_file "app/assets/config/manifest.js", "//= link_tree ../images"
|
2015-08-05 09:14:09 -04:00
|
|
|
add_to_config "config.assets.precompile = %w(manifest.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
|
|
|
|
2015-03-29 20:56:00 -04:00
|
|
|
manifest = Dir["#{app_path}/public/assets/.sprockets-manifest-*.json"].first
|
2012-10-15 19:22:12 -04:00
|
|
|
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
|
|
|
|
2015-09-27 17:34:13 -04:00
|
|
|
# Load app env
|
|
|
|
app "development"
|
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
|
|
|
|
2016-04-10 02:36:41 -04:00
|
|
|
files = Dir["#{app_path}/public/assets/**/*"]
|
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
|
|
|
|
2015-09-27 17:34:13 -04:00
|
|
|
# Load app env
|
|
|
|
app "production"
|
2011-09-06 05:34:24 -04:00
|
|
|
|
|
|
|
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"
|
|
|
|
|
2015-09-27 17:34:13 -04:00
|
|
|
# Load app env
|
|
|
|
app "development"
|
2011-05-17 17:43:44 -04:00
|
|
|
|
|
|
|
class ::OmgController < ActionController::Base
|
|
|
|
def index
|
|
|
|
flash[:cool_story] = true
|
2016-05-21 08:49:38 -04:00
|
|
|
render plain: "ok"
|
2011-05-17 17:43:44 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
get "/omg"
|
2016-08-06 13:16:09 -04:00
|
|
|
assert_equal "ok", last_response.body
|
2011-05-17 17:43:44 -04:00
|
|
|
|
|
|
|
get "/assets/demo.js"
|
|
|
|
assert_match "alert()", last_response.body
|
2016-12-24 12:29:52 -05:00
|
|
|
assert_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"
|
|
|
|
|
2015-09-27 17:34:13 -04:00
|
|
|
# Load app env
|
|
|
|
app "development"
|
2011-08-01 15:32:17 -04:00
|
|
|
|
|
|
|
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
|
2016-08-06 13:16:09 -04:00
|
|
|
precompile! RAILS_ENV: "production"
|
2011-09-23 19:56:49 -04:00
|
|
|
|
2015-09-27 17:34:13 -04:00
|
|
|
# Load app env
|
|
|
|
app "production"
|
2011-09-23 19:56:49 -04:00
|
|
|
|
|
|
|
class ::PostsController < ActionController::Base ; end
|
|
|
|
|
|
|
|
# the debug_assets params isn't used if compile is off
|
2016-08-06 13:16:09 -04:00
|
|
|
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!
|
2015-09-27 17:34:13 -04:00
|
|
|
|
2015-12-17 11:15:45 -05:00
|
|
|
assert_match(/Post;/, 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"
|
2015-09-27 17:34:13 -04:00
|
|
|
|
|
|
|
# Load app env
|
|
|
|
app "development"
|
|
|
|
|
2011-11-17 06:48:12 -05:00
|
|
|
class ::PostsController < ActionController::Base; end
|
|
|
|
|
2017-08-12 07:31:46 -04:00
|
|
|
get "/posts", {}, { "HTTPS" => "off" }
|
2015-12-16 13:30:58 -05:00
|
|
|
assert_match('src="http://example.com/assets/application.self.js', last_response.body)
|
2017-08-12 07:31:46 -04:00
|
|
|
get "/posts", {}, { "HTTPS" => "on" }
|
2015-12-16 13:30:58 -05:00
|
|
|
assert_match('src="https://example.com/assets/application.self.js', last_response.body)
|
2011-11-17 06:48:12 -05:00
|
|
|
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"
|
2015-09-27 17:34:13 -04:00
|
|
|
|
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"
|
2015-09-27 17:34:13 -04:00
|
|
|
|
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
|
|
|
|
|
2011-09-23 19:56:49 -04:00
|
|
|
private
|
2011-10-02 06:14:31 -04:00
|
|
|
|
2016-08-06 13:55:02 -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' %>"
|
2011-09-23 19:56:49 -04:00
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
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
|
2016-08-06 13:55:02 -04:00
|
|
|
end
|
2011-05-08 06:57:07 -04:00
|
|
|
end
|
|
|
|
end
|