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"
|
2011-08-25 05:40:10 -04:00
|
|
|
|
|
|
|
module ApplicationTests
|
2012-01-05 20:30:17 -05:00
|
|
|
class AssetDebuggingTest < ActiveSupport::TestCase
|
2011-08-25 05:40:10 -04:00
|
|
|
include ActiveSupport::Testing::Isolation
|
|
|
|
include Rack::Test::Methods
|
|
|
|
|
|
|
|
def setup
|
2017-09-08 20:48:15 -04:00
|
|
|
build_app(initializers: true)
|
2011-08-25 05:40:10 -04:00
|
|
|
|
|
|
|
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-10-14 06:03:39 -04:00
|
|
|
get '/posts', to: "posts#index"
|
2011-08-25 05:40:10 -04:00
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
|
|
|
|
app_file "app/controllers/posts_controller.rb", <<-RUBY
|
|
|
|
class PostsController < ActionController::Base
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
|
|
|
|
ENV["RAILS_ENV"] = "production"
|
|
|
|
end
|
|
|
|
|
|
|
|
def teardown
|
|
|
|
teardown_app
|
|
|
|
end
|
|
|
|
|
2011-08-31 14:09:35 -04:00
|
|
|
test "assets are concatenated when debug is off and compile is off either if debug_assets param is provided" do
|
|
|
|
# config.assets.debug and config.assets.compile are false for production environment
|
|
|
|
ENV["RAILS_ENV"] = "production"
|
2017-09-03 12:55:26 -04:00
|
|
|
rails "assets:precompile", "--trace"
|
2015-09-27 17:34:13 -04:00
|
|
|
|
|
|
|
# Load app env
|
|
|
|
app "production"
|
2011-08-25 05:40:10 -04:00
|
|
|
|
2011-08-31 14:09:35 -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-08-25 05:40:10 -04:00
|
|
|
end
|
|
|
|
|
2015-12-16 13:30:58 -05:00
|
|
|
test "assets aren't concatenated when compile is true is on and debug_assets params is true" do
|
2012-10-09 12:51:29 -04:00
|
|
|
add_to_env_config "production", "config.assets.compile = true"
|
2011-08-25 05:40:10 -04:00
|
|
|
|
2015-09-27 17:34:13 -04:00
|
|
|
# Load app env
|
|
|
|
app "production"
|
2011-08-25 05:40:10 -04:00
|
|
|
|
2011-08-31 14:09:35 -04:00
|
|
|
class ::PostsController < ActionController::Base ; end
|
|
|
|
|
2016-08-06 13:16:09 -04:00
|
|
|
get "/posts?debug_assets=true"
|
2015-12-16 13:30:58 -05:00
|
|
|
assert_match(/<script src="\/assets\/application(\.self)?-([0-z]+)\.js\?body=1"><\/script>/, last_response.body)
|
|
|
|
assert_match(/<script src="\/assets\/xmlhr(\.self)?-([0-z]+)\.js\?body=1"><\/script>/, last_response.body)
|
2011-08-25 05:40:10 -04:00
|
|
|
end
|
2016-06-15 15:16:21 -04:00
|
|
|
|
2016-08-18 16:24:13 -04:00
|
|
|
test "public path and tag methods are not over-written by the asset pipeline" do
|
2016-06-15 15:16:21 -04:00
|
|
|
contents = "doesnotexist"
|
|
|
|
cases = {
|
2016-08-30 16:04:23 -04:00
|
|
|
asset_path: %r{/#{contents}},
|
|
|
|
image_path: %r{/images/#{contents}},
|
|
|
|
video_path: %r{/videos/#{contents}},
|
|
|
|
audio_path: %r{/audios/#{contents}},
|
|
|
|
font_path: %r{/fonts/#{contents}},
|
|
|
|
javascript_path: %r{/javascripts/#{contents}},
|
|
|
|
stylesheet_path: %r{/stylesheets/#{contents}},
|
|
|
|
image_tag: %r{<img src="/images/#{contents}"},
|
|
|
|
favicon_link_tag: %r{<link rel="shortcut icon" type="image/x-icon" href="/images/#{contents}" />},
|
|
|
|
stylesheet_link_tag: %r{<link rel="stylesheet" media="screen" href="/stylesheets/#{contents}.css" />},
|
|
|
|
javascript_include_tag: %r{<script src="/javascripts/#{contents}.js">},
|
|
|
|
audio_tag: %r{<audio src="/audios/#{contents}"></audio>},
|
2018-03-14 07:18:06 -04:00
|
|
|
video_tag: %r{<video src="/videos/#{contents}"></video>},
|
|
|
|
image_submit_tag: %r{<input type="image" src="/images/#{contents}" />}
|
2016-06-15 15:16:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
cases.each do |(view_method, tag_match)|
|
2016-08-30 16:04:23 -04:00
|
|
|
app_file "app/views/posts/index.html.erb", "<%= #{view_method} '#{contents}', skip_pipeline: true %>"
|
2016-06-15 15:16:21 -04:00
|
|
|
|
|
|
|
app "development"
|
|
|
|
|
|
|
|
class ::PostsController < ActionController::Base ; end
|
|
|
|
|
2016-08-18 16:24:20 -04:00
|
|
|
get "/posts?debug_assets=true"
|
2016-06-15 15:16:21 -04:00
|
|
|
|
|
|
|
body = last_response.body
|
2016-08-30 16:04:23 -04:00
|
|
|
assert_match(tag_match, body, "Expected `#{view_method}` to produce a match to #{tag_match}, but did not: #{body}")
|
2016-06-15 15:16:21 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test "public url methods are not over-written by the asset pipeline" do
|
|
|
|
contents = "doesnotexist"
|
|
|
|
cases = {
|
2016-08-30 16:04:23 -04:00
|
|
|
asset_url: %r{http://example.org/#{contents}},
|
|
|
|
image_url: %r{http://example.org/images/#{contents}},
|
|
|
|
video_url: %r{http://example.org/videos/#{contents}},
|
|
|
|
audio_url: %r{http://example.org/audios/#{contents}},
|
|
|
|
font_url: %r{http://example.org/fonts/#{contents}},
|
|
|
|
javascript_url: %r{http://example.org/javascripts/#{contents}},
|
|
|
|
stylesheet_url: %r{http://example.org/stylesheets/#{contents}},
|
2016-06-15 15:16:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
cases.each do |(view_method, tag_match)|
|
2016-08-30 16:04:23 -04:00
|
|
|
app_file "app/views/posts/index.html.erb", "<%= #{view_method} '#{contents}', skip_pipeline: true %>"
|
2016-06-15 15:16:21 -04:00
|
|
|
|
|
|
|
app "development"
|
|
|
|
|
|
|
|
class ::PostsController < ActionController::Base ; end
|
|
|
|
|
2016-08-18 16:24:20 -04:00
|
|
|
get "/posts?debug_assets=true"
|
2016-06-15 15:16:21 -04:00
|
|
|
|
|
|
|
body = last_response.body
|
2016-08-30 16:04:23 -04:00
|
|
|
assert_match(tag_match, body, "Expected `#{view_method}` to produce a match to #{tag_match}, but did not: #{body}")
|
2016-06-15 15:16:21 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-30 11:26:13 -04:00
|
|
|
test "{ skip_pipeline: true } does not use the asset pipeline" do
|
2016-06-15 15:16:21 -04:00
|
|
|
cases = {
|
2016-08-21 15:08:24 -04:00
|
|
|
/\/assets\/application-.*.\.js/ => {},
|
2016-08-30 11:26:13 -04:00
|
|
|
/application.js/ => { skip_pipeline: true },
|
2016-08-21 15:08:24 -04:00
|
|
|
}
|
|
|
|
cases.each do |(tag_match, options_hash)|
|
2016-08-30 16:04:23 -04:00
|
|
|
app_file "app/views/posts/index.html.erb", "<%= asset_path('application.js', #{options_hash}) %>"
|
2016-08-21 15:08:24 -04:00
|
|
|
|
|
|
|
app "development"
|
|
|
|
|
|
|
|
class ::PostsController < ActionController::Base ; end
|
|
|
|
|
|
|
|
get "/posts?debug_assets=true"
|
|
|
|
|
|
|
|
body = last_response.body.strip
|
2016-08-30 16:04:23 -04:00
|
|
|
assert_match(tag_match, body, "Expected `asset_path` with `#{options_hash}` to produce a match to #{tag_match}, but did not: #{body}")
|
2016-08-21 15:08:24 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test "public_compute_asset_path does not use the asset pipeline" do
|
|
|
|
cases = {
|
|
|
|
compute_asset_path: /\/assets\/application-.*.\.js/,
|
|
|
|
public_compute_asset_path: /application.js/,
|
2016-06-15 15:16:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
cases.each do |(view_method, tag_match)|
|
|
|
|
app_file "app/views/posts/index.html.erb", "<%= #{ view_method } 'application.js' %>"
|
|
|
|
|
|
|
|
app "development"
|
|
|
|
|
|
|
|
class ::PostsController < ActionController::Base ; end
|
|
|
|
|
2016-08-18 16:24:20 -04:00
|
|
|
get "/posts?debug_assets=true"
|
2016-06-15 15:16:21 -04:00
|
|
|
|
|
|
|
body = last_response.body.strip
|
|
|
|
assert_match(tag_match, body, "Expected `#{view_method}` to produce a match to #{ tag_match }, but did not: #{ body }")
|
|
|
|
end
|
|
|
|
end
|
2011-08-25 05:40:10 -04:00
|
|
|
end
|
|
|
|
end
|