Merge pull request #28 from github/3-2-github+rip-out-sprockets

[3.2] Rip out Sprockets
This commit is contained in:
Charlie Somerville
2014-09-16 11:49:05 +10:00
33 changed files with 8 additions and 1790 deletions

View File

@@ -21,7 +21,6 @@ PATH
rack (~> 1.4.5)
rack-cache (~> 1.2)
rack-test (~> 0.6.1)
sprockets (~> 2.2.1)
activemodel (3.2.19)
activesupport (= 3.2.19)
builder (~> 3.2)
@@ -62,7 +61,6 @@ GEM
builder (3.2.2)
erubis (2.7.0)
execjs (2.0.2)
hike (1.2.3)
i18n (0.6.11)
journey (1.0.4)
jquery-rails (3.1.0)
@@ -93,14 +91,8 @@ GEM
rake (10.2.2)
rdoc (3.12.2)
json (~> 1.4)
sprockets (2.2.2)
hike (~> 1.2)
multi_json (~> 1.0)
rack (~> 1.0)
tilt (~> 1.1, != 1.3.0)
sqlite3 (1.3.9)
thor (0.19.1)
tilt (1.4.1)
treetop (1.4.15)
polyglot
polyglot (>= 0.3.1)

View File

@@ -26,7 +26,6 @@ Gem::Specification.new do |s|
s.add_dependency('rack', '~> 1.4.5')
s.add_dependency('rack-test', '~> 0.6.1')
s.add_dependency('journey', '~> 1.0.4')
s.add_dependency('sprockets', '~> 2.2.1')
s.add_dependency('erubis', '~> 2.7.0')
s.add_development_dependency('tzinfo', '~> 0.3.29')

View File

@@ -1,99 +0,0 @@
require "fileutils"
namespace :assets do
def ruby_rake_task(task, fork = true)
env = ENV['RAILS_ENV'] || 'production'
groups = ENV['RAILS_GROUPS'] || 'assets'
args = [$0, task,"RAILS_ENV=#{env}","RAILS_GROUPS=#{groups}"]
args << "--trace" if Rake.application.options.trace
if $0 =~ /rake\.bat\Z/i
Kernel.exec $0, *args
else
fork ? ruby(*args) : Kernel.exec(FileUtils::RUBY, *args)
end
end
# We are currently running with no explicit bundler group
# and/or no explicit environment - we have to reinvoke rake to
# execute this task.
def invoke_or_reboot_rake_task(task)
if ENV['RAILS_GROUPS'].to_s.empty? || ENV['RAILS_ENV'].to_s.empty?
ruby_rake_task task
else
Rake::Task[task].invoke
end
end
desc "Compile all the assets named in config.assets.precompile"
task :precompile do
invoke_or_reboot_rake_task "assets:precompile:all"
end
namespace :precompile do
def internal_precompile(digest=nil)
unless Rails.application.config.assets.enabled
warn "Cannot precompile assets if sprockets is disabled. Please set config.assets.enabled to true"
exit
end
# Ensure that action view is loaded and the appropriate
# sprockets hooks get executed
_ = ActionView::Base
config = Rails.application.config
config.assets.compile = true
config.assets.digest = digest unless digest.nil?
config.assets.digests = {}
env = Rails.application.assets
target = File.join(Rails.public_path, config.assets.prefix)
compiler = Sprockets::StaticCompiler.new(env,
target,
config.assets.precompile,
:manifest_path => config.assets.manifest,
:digest => config.assets.digest,
:manifest => digest.nil?)
compiler.compile
end
task :all do
Rake::Task["assets:precompile:primary"].invoke
# We need to reinvoke in order to run the secondary digestless
# asset compilation run - a fresh Sprockets environment is
# required in order to compile digestless assets as the
# environment has already cached the assets on the primary
# run.
ruby_rake_task("assets:precompile:nondigest", false) if Rails.application.config.assets.digest
end
task :primary => ["assets:environment", "tmp:cache:clear"] do
internal_precompile
end
task :nondigest => ["assets:environment", "tmp:cache:clear"] do
internal_precompile(false)
end
end
desc "Remove compiled assets"
task :clean do
invoke_or_reboot_rake_task "assets:clean:all"
end
namespace :clean do
task :all => ["assets:environment", "tmp:cache:clear"] do
config = Rails.application.config
public_asset_path = File.join(Rails.public_path, config.assets.prefix)
rm_rf public_asset_path, :secure => true
end
end
task :environment do
if Rails.application.config.assets.initialize_on_precompile
Rake::Task["environment"].invoke
else
Rails.application.initialize!(:assets)
Sprockets::Bootstrap.new(Rails.application).run
end
end
end

View File

@@ -1,37 +0,0 @@
module Sprockets
class Bootstrap
def initialize(app)
@app = app
end
# TODO: Get rid of config.assets.enabled
def run
app, config = @app, @app.config
return unless app.assets
config.assets.paths.each { |path| app.assets.append_path(path) }
if config.assets.compress
# temporarily hardcode default JS compressor to uglify. Soon, it will work
# the same as SCSS, where a default plugin sets the default.
unless config.assets.js_compressor == false
app.assets.js_compressor = LazyCompressor.new { Sprockets::Compressors.registered_js_compressor(config.assets.js_compressor || :uglifier) }
end
unless config.assets.css_compressor == false
app.assets.css_compressor = LazyCompressor.new { Sprockets::Compressors.registered_css_compressor(config.assets.css_compressor) }
end
end
if config.assets.compile
app.routes.prepend do
mount app.assets => config.assets.prefix
end
end
if config.assets.digest
app.assets = app.assets.index
end
end
end
end

View File

@@ -1,83 +0,0 @@
module Sprockets
module Compressors
@@css_compressors = {}
@@js_compressors = {}
@@default_css_compressor = nil
@@default_js_compressor = nil
def self.register_css_compressor(name, klass, options = {})
@@default_css_compressor = name.to_sym if options[:default] || @@default_css_compressor.nil?
@@css_compressors[name.to_sym] = {:klass => klass.to_s, :require => options[:require]}
end
def self.register_js_compressor(name, klass, options = {})
@@default_js_compressor = name.to_sym if options[:default] || @@default_js_compressor.nil?
@@js_compressors[name.to_sym] = {:klass => klass.to_s, :require => options[:require]}
end
def self.registered_css_compressor(name)
if name.respond_to?(:to_sym)
compressor = @@css_compressors[name.to_sym] || @@css_compressors[@@default_css_compressor]
require compressor[:require] if compressor[:require]
compressor[:klass].constantize.new
else
name
end
end
def self.registered_js_compressor(name)
if name.respond_to?(:to_sym)
compressor = @@js_compressors[name.to_sym] || @@js_compressors[@@default_js_compressor]
require compressor[:require] if compressor[:require]
compressor[:klass].constantize.new
else
name
end
end
# The default compressors must be registered in default plugins (ex. Sass-Rails)
register_css_compressor(:scss, 'Sass::Rails::Compressor', :require => 'sass/rails/compressor', :default => true)
register_js_compressor(:uglifier, 'Uglifier', :require => 'uglifier', :default => true)
# Automaticaly register some compressors
register_css_compressor(:yui, 'YUI::CssCompressor', :require => 'yui/compressor')
register_js_compressor(:closure, 'Closure::Compiler', :require => 'closure-compiler')
register_js_compressor(:yui, 'YUI::JavaScriptCompressor', :require => 'yui/compressor')
end
# An asset compressor which does nothing.
#
# This compressor simply returns the asset as-is, without any compression
# whatsoever. It is useful in development mode, when compression isn't
# needed but using the same asset pipeline as production is desired.
class NullCompressor #:nodoc:
def compress(content)
content
end
end
# An asset compressor which only initializes the underlying compression
# engine when needed.
#
# This postpones the initialization of the compressor until
# <code>#compress</code> is called the first time.
class LazyCompressor #:nodoc:
# Initializes a new LazyCompressor.
#
# The block should return a compressor when called, i.e. an object
# which responds to <code>#compress</code>.
def initialize(&block)
@block = block
end
def compress(content)
compressor.compress(content)
end
private
def compressor
@compressor ||= (@block.call || NullCompressor.new)
end
end
end

View File

@@ -1,6 +0,0 @@
module Sprockets
module Helpers
autoload :RailsHelper, "sprockets/helpers/rails_helper"
autoload :IsolatedHelper, "sprockets/helpers/isolated_helper"
end
end

View File

@@ -1,13 +0,0 @@
module Sprockets
module Helpers
module IsolatedHelper
def controller
nil
end
def config
Rails.application.config.action_controller
end
end
end
end

View File

@@ -1,182 +0,0 @@
require "action_view"
module Sprockets
module Helpers
module RailsHelper
extend ActiveSupport::Concern
include ActionView::Helpers::AssetTagHelper
def asset_paths
@asset_paths ||= begin
paths = RailsHelper::AssetPaths.new(config, controller)
paths.asset_environment = asset_environment
paths.asset_digests = asset_digests
paths.compile_assets = compile_assets?
paths.digest_assets = digest_assets?
paths
end
end
def javascript_include_tag(*sources)
options = sources.extract_options!
debug = options.key?(:debug) ? options.delete(:debug) : debug_assets?
body = options.key?(:body) ? options.delete(:body) : false
digest = options.key?(:digest) ? options.delete(:digest) : digest_assets?
sources.collect do |source|
if debug && asset = asset_paths.asset_for(source, 'js')
asset.to_a.map { |dep|
super(dep.pathname.to_s, { :src => path_to_asset(dep, :ext => 'js', :body => true, :digest => digest) }.merge!(options))
}
else
super(source.to_s, { :src => path_to_asset(source, :ext => 'js', :body => body, :digest => digest) }.merge!(options))
end
end.flatten.uniq.join("\n").html_safe
end
def stylesheet_link_tag(*sources)
options = sources.extract_options!
debug = options.key?(:debug) ? options.delete(:debug) : debug_assets?
body = options.key?(:body) ? options.delete(:body) : false
digest = options.key?(:digest) ? options.delete(:digest) : digest_assets?
sources.collect do |source|
if debug && asset = asset_paths.asset_for(source, 'css')
asset.to_a.map { |dep|
super(dep.pathname.to_s, { :href => path_to_asset(dep, :ext => 'css', :body => true, :protocol => :request, :digest => digest) }.merge!(options))
}
else
super(source.to_s, { :href => path_to_asset(source, :ext => 'css', :body => body, :protocol => :request, :digest => digest) }.merge!(options))
end
end.flatten.uniq.join("\n").html_safe
end
def asset_path(source, options = {})
source = source.logical_path if source.respond_to?(:logical_path)
path = asset_paths.compute_public_path(source, asset_prefix, options.merge(:body => true))
options[:body] ? "#{path}?body=1" : path
end
alias_method :path_to_asset, :asset_path # aliased to avoid conflicts with an asset_path named route
def image_path(source)
path_to_asset(source)
end
alias_method :path_to_image, :image_path # aliased to avoid conflicts with an image_path named route
def font_path(source)
path_to_asset(source)
end
alias_method :path_to_font, :font_path # aliased to avoid conflicts with an font_path named route
def javascript_path(source)
path_to_asset(source, :ext => 'js')
end
alias_method :path_to_javascript, :javascript_path # aliased to avoid conflicts with an javascript_path named route
def stylesheet_path(source)
path_to_asset(source, :ext => 'css')
end
alias_method :path_to_stylesheet, :stylesheet_path # aliased to avoid conflicts with an stylesheet_path named route
private
def debug_assets?
compile_assets? && (Rails.application.config.assets.debug || params[:debug_assets])
rescue NameError
false
end
# Override to specify an alternative prefix for asset path generation.
# When combined with a custom +asset_environment+, this can be used to
# implement themes that can take advantage of the asset pipeline.
#
# If you only want to change where the assets are mounted, refer to
# +config.assets.prefix+ instead.
def asset_prefix
Rails.application.config.assets.prefix
end
def asset_digests
Rails.application.config.assets.digests
end
def compile_assets?
Rails.application.config.assets.compile
end
def digest_assets?
Rails.application.config.assets.digest
end
# Override to specify an alternative asset environment for asset
# path generation. The environment should already have been mounted
# at the prefix returned by +asset_prefix+.
def asset_environment
Rails.application.assets
end
class AssetPaths < ::ActionView::AssetPaths #:nodoc:
attr_accessor :asset_environment, :asset_prefix, :asset_digests, :compile_assets, :digest_assets
class AssetNotPrecompiledError < StandardError; end
def asset_for(source, ext)
source = source.to_s
return nil if is_uri?(source)
source = rewrite_extension(source, nil, ext)
asset_environment[source]
rescue Sprockets::FileOutsidePaths
nil
end
def digest_for(logical_path)
if digest_assets && asset_digests && (digest = asset_digests[logical_path])
return digest
end
if compile_assets
if digest_assets && asset = asset_environment[logical_path]
return asset.digest_path
end
return logical_path
else
raise AssetNotPrecompiledError.new("#{logical_path} isn't precompiled")
end
end
def rewrite_asset_path(source, dir, options = {})
if source[0] == ?/
source
else
if digest_assets && options[:digest] != false
source = digest_for(source)
end
source = File.join(dir, source)
source = "/#{source}" unless source =~ /^\//
source
end
end
def rewrite_extension(source, dir, ext)
source_ext = File.extname(source)[1..-1]
if !ext || ext == source_ext
source
elsif source_ext.blank?
"#{source}.#{ext}"
elsif File.exists?(source) || exact_match_present?(source)
source
else
"#{source}.#{ext}"
end
end
def exact_match_present?(source)
pathname = asset_environment.resolve(source)
pathname.to_s =~ /#{Regexp.escape(source)}\Z/
rescue Sprockets::FileNotFound
false
end
end
end
end
end

View File

@@ -1,62 +0,0 @@
require "action_controller/railtie"
module Sprockets
autoload :Bootstrap, "sprockets/bootstrap"
autoload :Helpers, "sprockets/helpers"
autoload :Compressors, "sprockets/compressors"
autoload :LazyCompressor, "sprockets/compressors"
autoload :NullCompressor, "sprockets/compressors"
autoload :StaticCompiler, "sprockets/static_compiler"
# TODO: Get rid of config.assets.enabled
class Railtie < ::Rails::Railtie
rake_tasks do
load "sprockets/assets.rake"
end
initializer "sprockets.environment", :group => :all do |app|
config = app.config
next unless config.assets.enabled
require 'sprockets'
app.assets = Sprockets::Environment.new(app.root.to_s) do |env|
env.version = ::Rails.env + "-#{config.assets.version}"
if config.assets.logger != false
env.logger = config.assets.logger || ::Rails.logger
end
if config.assets.cache_store != false
env.cache = ActiveSupport::Cache.lookup_store(config.assets.cache_store) || ::Rails.cache
end
end
if config.assets.manifest
path = File.join(config.assets.manifest, "manifest.yml")
else
path = File.join(Rails.public_path, config.assets.prefix, "manifest.yml")
end
if File.exist?(path)
config.assets.digests = YAML.load_file(path)
end
ActiveSupport.on_load(:action_view) do
include ::Sprockets::Helpers::RailsHelper
app.assets.context_class.instance_eval do
include ::Sprockets::Helpers::IsolatedHelper
include ::Sprockets::Helpers::RailsHelper
end
end
end
# We need to configure this after initialization to ensure we collect
# paths from all engines. This hook is invoked exactly before routes
# are compiled, and so that other Railties have an opportunity to
# register compressors.
config.after_initialize do |app|
Sprockets::Bootstrap.new(app).run
end
end
end

View File

@@ -1,56 +0,0 @@
require 'fileutils'
module Sprockets
class StaticCompiler
attr_accessor :env, :target, :paths
def initialize(env, target, paths, options = {})
@env = env
@target = target
@paths = paths
@digest = options.key?(:digest) ? options.delete(:digest) : true
@manifest = options.key?(:manifest) ? options.delete(:manifest) : true
@manifest_path = options.delete(:manifest_path) || target
end
def compile
manifest = {}
env.each_logical_path(paths) do |logical_path|
if asset = env.find_asset(logical_path)
digest_path = write_asset(asset)
manifest[asset.logical_path] = digest_path
manifest[aliased_path_for(asset.logical_path)] = digest_path
end
end
write_manifest(manifest) if @manifest
end
def write_manifest(manifest)
FileUtils.mkdir_p(@manifest_path)
File.open("#{@manifest_path}/manifest.yml", 'wb') do |f|
YAML.dump(manifest, f)
end
end
def write_asset(asset)
path_for(asset).tap do |path|
filename = File.join(target, path)
FileUtils.mkdir_p File.dirname(filename)
asset.write_to(filename)
asset.write_to("#{filename}.gz") if filename.to_s =~ /\.(css|js)$/
end
end
def path_for(asset)
@digest ? asset.digest_path : asset.logical_path
end
def aliased_path_for(logical_path)
if File.basename(logical_path).start_with?('index')
logical_path.sub(/\/index([^\/]+)$/, '\1')
else
logical_path.sub(/\.([^\/]+)$/, '/index.\1')
end
end
end
end

View File

@@ -14,17 +14,17 @@ class TestRoutingMount < ActionDispatch::IntegrationTest
end
Router.draw do
SprocketsApp = lambda { |env|
RackApp = lambda { |env|
[200, {"Content-Type" => "text/html"}, ["#{env["SCRIPT_NAME"]} -- #{env["PATH_INFO"]}"]]
}
mount SprocketsApp, :at => "/sprockets"
mount SprocketsApp => "/shorthand"
mount RackApp, :at => "/sprockets"
mount RackApp => "/shorthand"
mount FakeEngine, :at => "/fakeengine"
scope "/its_a" do
mount SprocketsApp, :at => "/sprocket"
mount RackApp, :at => "/sprocket"
end
end

View File

@@ -5,7 +5,7 @@ require 'controller/fake_controllers'
require 'active_support/core_ext/object/inclusion'
class TestRoutingMapper < ActionDispatch::IntegrationTest
SprocketsApp = lambda { |env|
RackApp = lambda { |env|
[200, {"Content-Type" => "text/html"}, ["javascripts"]]
}
@@ -284,7 +284,7 @@ class TestRoutingMapper < ActionDispatch::IntegrationTest
get "products/list"
end
match 'sprockets.js' => ::TestRoutingMapper::SprocketsApp
match 'sprockets.js' => ::TestRoutingMapper::RackApp
match 'people/:id/update', :to => 'people#update', :as => :update_person
match '/projects/:project_id/people/:id/update', :to => 'people#update', :as => :update_project_person

View File

@@ -1,28 +0,0 @@
require 'abstract_unit'
require 'sprockets/compressors'
class CompressorsTest < ActiveSupport::TestCase
def test_register_css_compressor
Sprockets::Compressors.register_css_compressor(:null, Sprockets::NullCompressor)
compressor = Sprockets::Compressors.registered_css_compressor(:null)
assert_kind_of Sprockets::NullCompressor, compressor
end
def test_register_js_compressor
Sprockets::Compressors.register_js_compressor(:uglifier, 'Uglifier', :require => 'uglifier')
compressor = Sprockets::Compressors.registered_js_compressor(:uglifier)
assert_kind_of Uglifier, compressor
end
def test_register_default_css_compressor
Sprockets::Compressors.register_css_compressor(:null, Sprockets::NullCompressor, :default => true)
compressor = Sprockets::Compressors.registered_css_compressor(:default)
assert_kind_of Sprockets::NullCompressor, compressor
end
def test_register_default_js_compressor
Sprockets::Compressors.register_js_compressor(:null, Sprockets::NullCompressor, :default => true)
compressor = Sprockets::Compressors.registered_js_compressor(:default)
assert_kind_of Sprockets::NullCompressor, compressor
end
end

View File

@@ -1,392 +0,0 @@
require 'abstract_unit'
require 'sprockets'
require 'sprockets/helpers/rails_helper'
require 'mocha/setup'
class SprocketsHelperTest < ActionView::TestCase
include Sprockets::Helpers::RailsHelper
attr_accessor :assets
class MockRequest
def protocol() 'http://' end
def ssl?() false end
def host_with_port() 'localhost' end
end
def setup
super
@controller = BasicController.new
@controller.request = MockRequest.new
@assets = Sprockets::Environment.new
@assets.append_path(FIXTURES.join("sprockets/app/javascripts"))
@assets.append_path(FIXTURES.join("sprockets/app/stylesheets"))
@assets.append_path(FIXTURES.join("sprockets/app/images"))
@assets.append_path(FIXTURES.join("sprockets/app/fonts"))
application = Struct.new(:config, :assets).new(config, @assets)
Rails.stubs(:application).returns(application)
@config = config
@config.perform_caching = true
@config.assets.digest = true
@config.assets.compile = true
end
def url_for(*args)
"http://www.example.com"
end
def config
@controller ? @controller.config : @config
end
def compute_host(source, request, options = {})
raise "Should never get here"
end
test "asset_path" do
assert_match %r{/assets/logo-[0-9a-f]+.png},
asset_path("logo.png")
assert_match %r{/assets/logo-[0-9a-f]+.png},
asset_path("logo.png", :digest => true)
assert_match %r{/assets/logo.png},
asset_path("logo.png", :digest => false)
end
test "custom_asset_path" do
@config.assets.prefix = '/s'
assert_match %r{/s/logo-[0-9a-f]+.png},
asset_path("logo.png")
assert_match %r{/s/logo-[0-9a-f]+.png},
asset_path("logo.png", :digest => true)
assert_match %r{/s/logo.png},
asset_path("logo.png", :digest => false)
end
test "asset_path with root relative assets" do
assert_equal "/images/logo",
asset_path("/images/logo")
assert_equal "/images/logo.gif",
asset_path("/images/logo.gif")
assert_equal "/dir/audio",
asset_path("/dir/audio")
end
test "asset_path with absolute urls" do
assert_equal "http://www.example.com/video/play",
asset_path("http://www.example.com/video/play")
assert_equal "http://www.example.com/video/play.mp4",
asset_path("http://www.example.com/video/play.mp4")
end
test "with a simple asset host the url should default to protocol relative" do
@controller.config.default_asset_host_protocol = :relative
@controller.config.asset_host = "assets-%d.example.com"
assert_match %r{^//assets-\d.example.com/assets/logo-[0-9a-f]+.png},
asset_path("logo.png")
end
test "with a simple asset host the url can be changed to use the request protocol" do
@controller.config.asset_host = "assets-%d.example.com"
@controller.config.default_asset_host_protocol = :request
assert_match %r{http://assets-\d.example.com/assets/logo-[0-9a-f]+.png},
asset_path("logo.png")
end
test "With a proc asset host that returns no protocol the url should be protocol relative" do
@controller.config.default_asset_host_protocol = :relative
@controller.config.asset_host = Proc.new do |asset|
"assets-999.example.com"
end
assert_match %r{^//assets-999.example.com/assets/logo-[0-9a-f]+.png},
asset_path("logo.png")
end
test "with a proc asset host that returns a protocol the url use it" do
@controller.config.asset_host = Proc.new do |asset|
"http://assets-999.example.com"
end
assert_match %r{http://assets-999.example.com/assets/logo-[0-9a-f]+.png},
asset_path("logo.png")
end
test "stylesheets served with a controller in scope can access the request" do
config.asset_host = Proc.new do |asset, request|
assert_not_nil request
"http://assets-666.example.com"
end
assert_match %r{http://assets-666.example.com/assets/logo-[0-9a-f]+.png},
asset_path("logo.png")
end
test "stylesheets served without a controller in scope cannot access the request" do
@controller = nil
@config.asset_host = Proc.new do |asset, request|
fail "This should not have been called."
end
assert_raises ActionController::RoutingError do
asset_path("logo.png")
end
@config.asset_host = method :compute_host
assert_raises ActionController::RoutingError do
asset_path("logo.png")
end
end
test "image_tag" do
assert_dom_equal '<img alt="Xml" src="/assets/xml.png" />', image_tag("xml.png")
end
test "image_path" do
assert_match %r{/assets/logo-[0-9a-f]+.png},
image_path("logo.png")
assert_match %r{/assets/logo-[0-9a-f]+.png},
path_to_image("logo.png")
end
test "font_path" do
assert_match %r{/assets/font-[0-9a-f]+.ttf},
font_path("font.ttf")
assert_match %r{/assets/font-[0-9a-f]+.ttf},
path_to_font("font.ttf")
end
test "javascript_path" do
assert_match %r{/assets/application-[0-9a-f]+.js},
javascript_path("application")
assert_match %r{/assets/application-[0-9a-f]+.js},
javascript_path("application.js")
assert_match %r{/assets/application-[0-9a-f]+.js},
path_to_javascript("application.js")
end
test "stylesheet_path" do
assert_match %r{/assets/application-[0-9a-f]+.css},
stylesheet_path("application")
assert_match %r{/assets/application-[0-9a-f]+.css},
stylesheet_path("application.css")
assert_match %r{/assets/application-[0-9a-f]+.css},
path_to_stylesheet("application.css")
end
test "stylesheets served without a controller in do not use asset hosts when the default protocol is :request" do
@controller = nil
@config.asset_host = "assets-%d.example.com"
@config.default_asset_host_protocol = :request
@config.perform_caching = true
assert_match %r{/assets/logo-[0-9a-f]+.png},
asset_path("logo.png")
end
test "asset path with relative url root" do
@controller.config.relative_url_root = "/collaboration/hieraki"
assert_equal "/collaboration/hieraki/images/logo.gif",
asset_path("/images/logo.gif")
end
test "asset path with relative url root when controller isn't present but relative_url_root is" do
@controller = nil
@config.relative_url_root = "/collaboration/hieraki"
assert_equal "/collaboration/hieraki/images/logo.gif",
asset_path("/images/logo.gif")
end
test "font path through asset_path" do
assert_match %r{/assets/font-[0-9a-f]+.ttf},
asset_path('font.ttf')
assert_match %r{/assets/dir/font-[0-9a-f]+.ttf},
asset_path("dir/font.ttf")
assert_equal "http://www.example.com/fonts/font.ttf",
asset_path("http://www.example.com/fonts/font.ttf")
end
test "javascript path through asset_path" do
assert_match %r{/assets/application-[0-9a-f]+.js},
asset_path(:application, :ext => "js")
assert_match %r{/assets/xmlhr-[0-9a-f]+.js},
asset_path("xmlhr", :ext => "js")
assert_match %r{/assets/dir/xmlhr-[0-9a-f]+.js},
asset_path("dir/xmlhr.js", :ext => "js")
assert_equal "/dir/xmlhr.js",
asset_path("/dir/xmlhr", :ext => "js")
assert_equal "http://www.example.com/js/xmlhr",
asset_path("http://www.example.com/js/xmlhr", :ext => "js")
assert_equal "http://www.example.com/js/xmlhr.js",
asset_path("http://www.example.com/js/xmlhr.js", :ext => "js")
end
test "javascript include tag" do
assert_match %r{<script src="/assets/application-[0-9a-f]+.js" type="text/javascript"></script>},
javascript_include_tag(:application)
assert_match %r{<script src="/assets/application-[0-9a-f]+.js" type="text/javascript"></script>},
javascript_include_tag(:application, :digest => true)
assert_match %r{<script src="/assets/application.js" type="text/javascript"></script>},
javascript_include_tag(:application, :digest => false)
assert_match %r{<script src="/assets/xmlhr-[0-9a-f]+.js" type="text/javascript"></script>},
javascript_include_tag("xmlhr")
assert_match %r{<script src="/assets/xmlhr-[0-9a-f]+.js" type="text/javascript"></script>},
javascript_include_tag("xmlhr.js")
assert_equal '<script src="http://www.example.com/xmlhr" type="text/javascript"></script>',
javascript_include_tag("http://www.example.com/xmlhr")
assert_match %r{<script src=\"/assets/xmlhr-[0-9a-f]+.js" type=\"text/javascript\"></script>\n<script src=\"/assets/extra-[0-9a-f]+.js" type=\"text/javascript\"></script>},
javascript_include_tag("xmlhr", "extra")
assert_match %r{<script src="/assets/xmlhr-[0-9a-f]+.js\?body=1" type="text/javascript"></script>\n<script src="/assets/application-[0-9a-f]+.js\?body=1" type="text/javascript"></script>},
javascript_include_tag(:application, :debug => true)
assert_match %r{<script src="/assets/jquery.plugin.js" type="text/javascript"></script>},
javascript_include_tag('jquery.plugin', :digest => false)
assert_match %r{\A<script src="/assets/xmlhr-[0-9a-f]+.js" type="text/javascript"></script>\Z},
javascript_include_tag("xmlhr", "xmlhr")
assert_match %r{\A<script src="/assets/foo.min-[0-9a-f]+.js" type="text/javascript"></script>\Z},
javascript_include_tag("foo.min")
@config.assets.compile = true
@config.assets.debug = true
assert_match %r{<script src="/javascripts/application.js" type="text/javascript"></script>},
javascript_include_tag('/javascripts/application')
assert_match %r{<script src="/assets/xmlhr-[0-9a-f]+.js\?body=1" type="text/javascript"></script>\n<script src="/assets/application-[0-9a-f]+.js\?body=1" type="text/javascript"></script>},
javascript_include_tag(:application)
assert_match %r{<script src="/assets/xmlhr-[0-9a-f]+.js\?body=1" type="text/javascript"></script>\n<script src="/assets/application-[0-9a-f]+.js\?body=1" type="text/javascript"></script>\n<script src="/assets/extra-[0-9a-f]+.js\?body=1" type="text/javascript"></script>},
javascript_include_tag(:application, :extra)
end
test "precompiled assets with an extension when no JS runtime is available" do
@config.assets.compile = false
@config.assets.digests = {'foo.min.js' => 'foo.min-f00.js'}
Sprockets::Index.any_instance.stubs(:build_asset).raises
assert_nothing_raised { javascript_include_tag('foo.min') }
end
test "assets that exist on filesystem don't need to go through Sprockets" do
@config.assets.digest = false
@config.assets.debug = true
Rails.application.assets.expects(:resolve).never
asset_paths.asset_for(FIXTURES.join("sprockets/app/javascripts/foo.min.js"), 'min')
end
test "stylesheet path through asset_path" do
assert_match %r{/assets/application-[0-9a-f]+.css}, asset_path(:application, :ext => "css")
assert_match %r{/assets/style-[0-9a-f]+.css}, asset_path("style", :ext => "css")
assert_match %r{/assets/dir/style-[0-9a-f]+.css}, asset_path("dir/style.css", :ext => "css")
assert_equal "/dir/style.css", asset_path("/dir/style.css", :ext => "css")
assert_equal "http://www.example.com/css/style",
asset_path("http://www.example.com/css/style", :ext => "css")
assert_equal "http://www.example.com/css/style.css",
asset_path("http://www.example.com/css/style.css", :ext => "css")
end
test "stylesheet link tag" do
assert_match %r{<link href="/assets/application-[0-9a-f]+.css" media="screen" rel="stylesheet" type="text/css" />},
stylesheet_link_tag(:application)
assert_match %r{<link href="/assets/application-[0-9a-f]+.css" media="screen" rel="stylesheet" type="text/css" />},
stylesheet_link_tag(:application, :digest => true)
assert_match %r{<link href="/assets/application.css" media="screen" rel="stylesheet" type="text/css" />},
stylesheet_link_tag(:application, :digest => false)
assert_match %r{<link href="/assets/style-[0-9a-f]+.css" media="screen" rel="stylesheet" type="text/css" />},
stylesheet_link_tag("style")
assert_match %r{<link href="/assets/style-[0-9a-f]+.css" media="screen" rel="stylesheet" type="text/css" />},
stylesheet_link_tag("style.css")
assert_equal '<link href="http://www.example.com/style.css" media="screen" rel="stylesheet" type="text/css" />',
stylesheet_link_tag("http://www.example.com/style.css")
assert_match %r{<link href="/assets/style-[0-9a-f]+.css" media="all" rel="stylesheet" type="text/css" />},
stylesheet_link_tag("style", :media => "all")
assert_match %r{<link href="/assets/style-[0-9a-f]+.css" media="print" rel="stylesheet" type="text/css" />},
stylesheet_link_tag("style", :media => "print")
assert_match %r{<link href="/assets/style-[0-9a-f]+.css" media="screen" rel="stylesheet" type="text/css" />\n<link href="/assets/extra-[0-9a-f]+.css" media="screen" rel="stylesheet" type="text/css" />},
stylesheet_link_tag("style", "extra")
assert_match %r{<link href="/assets/style-[0-9a-f]+.css\?body=1" media="screen" rel="stylesheet" type="text/css" />\n<link href="/assets/application-[0-9a-f]+.css\?body=1" media="screen" rel="stylesheet" type="text/css" />},
stylesheet_link_tag(:application, :debug => true)
assert_match %r{\A<link href="/assets/style-[0-9a-f]+.css" media="screen" rel="stylesheet" type="text/css" />\Z},
stylesheet_link_tag("style", "style")
assert_match %r{\A<link href="/assets/style-[0-9a-f]+.ext" media="screen" rel="stylesheet" type="text/css" />\Z},
stylesheet_link_tag("style.ext")
assert_match %r{\A<link href="/assets/style.min-[0-9a-f]+.css" media="screen" rel="stylesheet" type="text/css" />\Z},
stylesheet_link_tag("style.min")
@config.assets.compile = true
@config.assets.debug = true
assert_match %r{<link href="/stylesheets/application.css" media="screen" rel="stylesheet" type="text/css" />},
stylesheet_link_tag('/stylesheets/application')
assert_match %r{<link href="/assets/style-[0-9a-f]+.css\?body=1" media="screen" rel="stylesheet" type="text/css" />\n<link href="/assets/application-[0-9a-f]+.css\?body=1" media="screen" rel="stylesheet" type="text/css" />},
stylesheet_link_tag(:application)
assert_match %r{<link href="/assets/style-[0-9a-f]+.css\?body=1" media="screen" rel="stylesheet" type="text/css" />\n<link href="/assets/application-[0-9a-f]+.css\?body=1" media="screen" rel="stylesheet" type="text/css" />\n<link href="/assets/extra-[0-9a-f]+.css\?body=1" media="screen" rel="stylesheet" type="text/css" />},
stylesheet_link_tag(:application, :extra)
assert_match %r{<link href="/assets/style-[0-9a-f]+.css\?body=1" media="print" rel="stylesheet" type="text/css" />\n<link href="/assets/application-[0-9a-f]+.css\?body=1" media="print" rel="stylesheet" type="text/css" />},
stylesheet_link_tag(:application, :media => "print")
end
test "alternate asset prefix" do
stubs(:asset_prefix).returns("/themes/test")
assert_match %r{/themes/test/style-[0-9a-f]+.css}, asset_path("style", :ext => "css")
end
test "alternate asset environment" do
assets = Sprockets::Environment.new
assets.append_path(FIXTURES.join("sprockets/alternate/stylesheets"))
stubs(:asset_environment).returns(assets)
assert_match %r{/assets/style-[0-9a-f]+.css}, asset_path("style", :ext => "css")
end
test "alternate hash based on environment" do
assets = Sprockets::Environment.new
assets.version = 'development'
assets.append_path(FIXTURES.join("sprockets/alternate/stylesheets"))
stubs(:asset_environment).returns(assets)
dev_path = asset_path("style", :ext => "css")
assets.version = 'production'
prod_path = asset_path("style", :ext => "css")
assert_not_equal prod_path, dev_path
end
test "precedence of `config.digest = false` over manifest.yml asset digests" do
Rails.application.config.assets.digests = {'logo.png' => 'logo-d1g3st.png'}
@config.assets.digest = false
assert_equal '/assets/logo.png',
asset_path("logo.png")
end
test "`config.digest = false` works with `config.compile = false`" do
@config.assets.digest = false
@config.assets.compile = false
assert_equal '/assets/logo.png',
asset_path("logo.png")
end
end

View File

@@ -1,57 +0,0 @@
require 'abstract_unit'
require 'sprockets'
require 'sprockets/helpers/rails_helper'
require 'mocha/setup'
class SprocketsHelperWithRoutesTest < ActionView::TestCase
include Sprockets::Helpers::RailsHelper
# Let's bring in some named routes to test namespace conflicts with potential *_paths.
# We have to do this after we bring in the Sprockets RailsHelper so if there are conflicts,
# they'll fail in the way we expect in a real live Rails app.
routes = ActionDispatch::Routing::RouteSet.new
routes.draw do
resources :assets
end
include routes.url_helpers
def setup
super
@controller = BasicController.new
@assets = Sprockets::Environment.new
@assets.append_path(FIXTURES.join("sprockets/app/javascripts"))
@assets.append_path(FIXTURES.join("sprockets/app/stylesheets"))
@assets.append_path(FIXTURES.join("sprockets/app/images"))
application = Struct.new(:config, :assets).new(config, @assets)
Rails.stubs(:application).returns(application)
@config = config
@config.perform_caching = true
@config.assets.digest = true
@config.assets.compile = true
end
test "namespace conflicts on a named route called asset_path" do
# Testing this for sanity - asset_path is now a named route!
assert_match asset_path('test_asset'), '/assets/test_asset'
assert_match %r{/assets/logo-[0-9a-f]+.png},
path_to_asset("logo.png")
assert_match %r{/assets/logo-[0-9a-f]+.png},
path_to_asset("logo.png", :digest => true)
assert_match %r{/assets/logo.png},
path_to_asset("logo.png", :digest => false)
end
test "javascript_include_tag with a named_route named asset_path" do
assert_match %r{<script src="/assets/application-[0-9a-f]+.js" type="text/javascript"></script>},
javascript_include_tag(:application)
end
test "stylesheet_link_tag with a named_route named asset_path" do
assert_match %r{<link href="/assets/application-[0-9a-f]+.css" media="screen" rel="stylesheet" type="text/css" />},
stylesheet_link_tag(:application)
end
end

View File

@@ -6,7 +6,6 @@ require "rails"
action_mailer
active_resource
rails/test_unit
sprockets
).each do |framework|
begin
require "#{framework}/railtie"

View File

@@ -41,23 +41,6 @@ module Rails
@reload_classes_only_on_change = true
@file_watcher = ActiveSupport::FileUpdateChecker
@exceptions_app = nil
@assets = ActiveSupport::OrderedOptions.new
@assets.enabled = false
@assets.paths = []
@assets.precompile = [ Proc.new{ |path| !File.extname(path).in?(['.js', '.css']) },
/(?:\/|\\|\A)application\.(css|js)$/ ]
@assets.prefix = "/assets"
@assets.version = ''
@assets.debug = false
@assets.compile = true
@assets.digest = false
@assets.manifest = nil
@assets.cache_store = [ :file_store, "#{root}/tmp/cache/assets/" ]
@assets.js_compressor = nil
@assets.css_compressor = nil
@assets.initialize_on_precompile = true
@assets.logger = nil
end
def compiled_asset_path

View File

@@ -41,8 +41,7 @@ module Rails
{:name => route.name.to_s, :verb => verb, :path => route.path.spec.to_s, :reqs => reqs }
end
# Skip the route if it's internal info route
routes.reject { |r| r[:path] =~ %r{/rails/info/properties|^#{Rails.application.config.assets.prefix}} }
routes
end
def collect_engine_routes(name, rack_app)

View File

@@ -576,12 +576,6 @@ module Rails
require environment if environment
end
initializer :append_assets_path, :group => :all do |app|
app.config.assets.paths.unshift(*paths["vendor/assets"].existent_directories)
app.config.assets.paths.unshift(*paths["lib/assets"].existent_directories)
app.config.assets.paths.unshift(*paths["app/assets"].existent_directories)
end
initializer :prepend_helpers_path do |app|
if !isolated? || (app == self)
app.config.helpers_paths.unshift(*paths["app/helpers"].existent)

View File

@@ -37,9 +37,6 @@ module Rails
class_option :skip_active_record, :type => :boolean, :aliases => "-O", :default => false,
:desc => "Skip Active Record files"
class_option :skip_sprockets, :type => :boolean, :aliases => "-S", :default => false,
:desc => "Skip Sprockets files"
class_option :database, :type => :string, :aliases => "-d", :default => "sqlite3",
:desc => "Preconfigure for selected database (options: #{DATABASES.join('/')})"
@@ -127,7 +124,7 @@ module Rails
end
def include_all_railties?
!options[:skip_active_record] && !options[:skip_test_unit] && !options[:skip_sprockets]
!options[:skip_active_record] && !options[:skip_test_unit]
end
def comment_if(value)
@@ -192,40 +189,6 @@ module Rails
end
end
def assets_gemfile_entry
return if options[:skip_sprockets]
gemfile = if options.dev? || options.edge?
<<-GEMFILE
# Gems used only for assets and not required
# in production environments by default.
group :assets do
gem 'sass-rails', :git => 'git://github.com/rails/sass-rails.git', :branch => '3-2-stable'
gem 'coffee-rails', :git => 'git://github.com/rails/coffee-rails.git', :branch => '3-2-stable'
# See https://github.com/sstephenson/execjs#readme for more supported runtimes
#{javascript_runtime_gemfile_entry}
gem 'uglifier', '>= 1.0.3'
end
GEMFILE
else
<<-GEMFILE
# Gems used only for assets and not required
# in production environments by default.
group :assets do
gem 'sass-rails', '~> 3.2.3'
gem 'coffee-rails', '~> 3.2.1'
# See https://github.com/sstephenson/execjs#readme for more supported runtimes
#{javascript_runtime_gemfile_entry}
gem 'uglifier', '>= 1.0.3'
end
GEMFILE
end
gemfile.strip_heredoc.gsub(/^[ \t]*$/, '')
end
def javascript_gemfile_entry
"gem '#{options[:javascript]}-rails'" unless options[:skip_javascript]
end

View File

@@ -7,7 +7,6 @@ source 'https://rubygems.org'
<%= "gem 'jruby-openssl'\n" if defined?(JRUBY_VERSION) -%>
<%= "gem 'json'\n" if RUBY_VERSION < "1.9.2" -%>
<%= assets_gemfile_entry %>
<%= javascript_gemfile_entry %>
# To use ActiveModel has_secure_password

View File

@@ -8,7 +8,6 @@ require 'rails/all'
require "action_controller/railtie"
require "action_mailer/railtie"
require "active_resource/railtie"
<%= comment_if :skip_sprockets %>require "sprockets/railtie"
<%= comment_if :skip_test_unit %>require "rails/test_unit/railtie"
<% end -%>
@@ -62,13 +61,5 @@ module <%= app_const_base %>
# in your app. As such, your models will need to explicitly whitelist or blacklist accessible
# parameters by using an attr_accessible or attr_protected declaration.
<%= comment_if :skip_active_record %>config.active_record.whitelist_attributes = true
<% unless options.skip_sprockets? -%>
# Enable the asset pipeline
config.assets.enabled = true
# Version of your assets, change this if you want to expire all your assets
config.assets.version = '1.0'
<% end -%>
end
end

View File

@@ -30,12 +30,4 @@
# with SQLite, MySQL, and PostgreSQL)
config.active_record.auto_explain_threshold_in_seconds = 0.5
<%- end -%>
<%- unless options.skip_sprockets? -%>
# Do not compress assets
config.assets.compress = false
# Expands the lines which load the assets
config.assets.debug = true
<%- end -%>
end

View File

@@ -11,20 +11,6 @@
# Disable Rails's static asset server (Apache or nginx will already do this)
config.serve_static_assets = false
<%- unless options.skip_sprockets? -%>
# Compress JavaScripts and CSS
config.assets.compress = true
# Don't fallback to assets pipeline if a precompiled asset is missed
config.assets.compile = false
# Generate digests for assets URLs
config.assets.digest = true
# Defaults to nil and saved in location specified by config.assets.prefix
# config.assets.manifest = YOUR_PATH
<%- end -%>
# Specifies the header that your server uses for sending files
# config.action_dispatch.x_sendfile_header = "X-Sendfile" # for apache
# config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for nginx

View File

@@ -8,7 +8,6 @@ require 'rails/all'
require "action_controller/railtie"
require "action_mailer/railtie"
require "active_resource/railtie"
<%= comment_if :skip_sprockets %>require "sprockets/railtie"
<%= comment_if :skip_test_unit %>require "rails/test_unit/railtie"
<% end -%>

View File

@@ -1,65 +0,0 @@
require 'isolation/abstract_unit'
require 'rack/test'
module ApplicationTests
class AssetDebuggingTest < Test::Unit::TestCase
include ActiveSupport::Testing::Isolation
include Rack::Test::Methods
def setup
build_app(:initializers => true)
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
app_file "app/controllers/posts_controller.rb", <<-RUBY
class PostsController < ActionController::Base
end
RUBY
ENV["RAILS_ENV"] = "production"
boot_rails
end
def teardown
teardown_app
end
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"
capture(:stdout) do
Dir.chdir(app_path){ `bundle exec rake assets:precompile` }
end
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_file "config/initializers/compile.rb", "Rails.application.config.assets.compile = 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
end
end

View File

@@ -1,556 +0,0 @@
# -*- coding: utf-8 -*-
require 'isolation/abstract_unit'
require 'active_support/core_ext/kernel/reporting'
require 'rack/test'
require 'yaml'
module ApplicationTests
class AssetsTest < Test::Unit::TestCase
include ActiveSupport::Testing::Isolation
include Rack::Test::Methods
def setup
build_app(:initializers => true)
boot_rails
end
def teardown
teardown_app
end
def precompile!
quietly 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", "a = <%= image_path('rails.png').inspect %>;"
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
require "#{app_path}/config/environment"
get "/assets/demo.js"
assert_equal 'a = "/assets/rails.png";', last_response.body.strip
end
test "assets do not require compressors until it is used" do
app_file "app/assets/javascripts/demo.js.erb", "<%= :alert %>();"
add_to_env_config "production", "config.assets.compile = true"
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
test "precompile creates the file, gives it the original asset's content and run in production as default" do
app_file "app/assets/javascripts/application.js", "alert();"
app_file "app/assets/javascripts/foo/application.js", "alert();"
ENV["RAILS_ENV"] = nil
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)
end
end
test "precompile application.js and application.css and all other non JS/CSS files" 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{}"
app_file "app/assets/javascripts/something.else.js.erb", "alert();"
app_file "app/assets/stylesheets/something.else.css.erb", "body{}"
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"]
images_should_compile.each do |filename|
app_file "app/assets/images/#{filename}", "happy"
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_no_file_exists("#{app_path}/public/assets/someapplication.js")
assert_no_file_exists("#{app_path}/public/assets/someapplication.css")
assert_no_file_exists("#{app_path}/public/assets/something.min.js")
assert_no_file_exists("#{app_path}/public/assets/something.min.css")
assert_no_file_exists("#{app_path}/public/assets/something.else.js")
assert_no_file_exists("#{app_path}/public/assets/something.else.css")
end
def assert_file_exists(filename)
assert File.exists?(filename), "missing #{filename}"
end
def assert_no_file_exists(filename)
assert !File.exists?(filename), "#{filename} does exist"
end
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();"
precompile!
assert File.exists?("#{app_path}/public/assets/something.js")
assets = YAML.load_file("#{app_path}/public/assets/manifest.yml")
assert_not_nil assets['something/index.js'], "Expected something/index.js among #{assets.keys.inspect}"
assert_not_nil assets['something.js'], "Expected something.js among #{assets.keys.inspect}"
end
test "precompile something/index.js for directory containing index file" do
add_to_config "config.assets.precompile = [ 'something/index.js' ]"
app_file "app/assets/javascripts/something/index.js.erb", "alert();"
precompile!
assert File.exists?("#{app_path}/public/assets/something/index.js")
assets = YAML.load_file("#{app_path}/public/assets/manifest.yml")
assert_not_nil assets['something/index.js'], "Expected something/index.js among #{assets.keys.inspect}"
assert_not_nil assets['something.js'], "Expected something.js among #{assets.keys.inspect}"
end
test "asset pipeline should use a Sprockets::Index when config.assets.digest is true" do
add_to_config "config.assets.digest = true"
add_to_config "config.action_controller.perform_caching = false"
ENV["RAILS_ENV"] = "production"
require "#{app_path}/config/environment"
assert_equal Sprockets::Index, Rails.application.assets.class
end
test "precompile creates a manifest file 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();"
# digest is default in false, we must enable it for test environment
add_to_config "config.assets.digest = true"
precompile!
manifest = "#{app_path}/public/assets/manifest.yml"
assets = YAML.load_file(manifest)
assert_match(/application-([0-z]+)\.js/, assets["application.js"])
assert_match(/application-([0-z]+)\.css/, assets["application.css"])
end
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();"
# 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'"
precompile!
manifest = "#{app_path}/shared/manifest.yml"
assets = YAML.load_file(manifest)
assert_match(/application-([0-z]+)\.js/, assets["application.js"])
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'"
precompile!
manifest = "#{app_path}/public/x/manifest.yml"
assets = YAML.load_file(manifest)
assert_match(/application-([0-z]+)\.js/, assets["application.js"])
end
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();"
add_to_config "config.assets.digest = false"
precompile!
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
test "assets do not require any assets group gem when manifest file is present" do
app_file "app/assets/javascripts/application.js", "alert();"
add_to_env_config "production", "config.serve_static_assets = true"
ENV["RAILS_ENV"] = "production"
precompile!
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
test "assets raise AssetNotPrecompiledError when manifest file is present and requested file isn't precompiled" do
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"
precompile!
# Create file after of precompile
app_file "app/assets/javascripts/app.js", "alert();"
require "#{app_path}/config/environment"
class ::PostsController < ActionController::Base
def show_detailed_exceptions?() true end
end
get '/posts'
assert_match(/AssetNotPrecompiledError/, last_response.body)
assert_match(/app.js isn&#x27;t precompiled/, last_response.body)
end
test "assets raise AssetNotPrecompiledError when manifest file is present and requested file isn't precompiled if digest is disabled" do
app_file "app/views/posts/index.html.erb", "<%= javascript_include_tag 'app' %>"
add_to_config "config.assets.compile = false"
add_to_config "config.assets.digest = false"
app_file "config/routes.rb", <<-RUBY
AppTemplate::Application.routes.draw do
match '/posts', :to => "posts#index"
end
RUBY
ENV["RAILS_ENV"] = "production"
precompile!
# Create file after of precompile
app_file "app/assets/javascripts/app.js", "alert();"
require "#{app_path}/config/environment"
class ::PostsController < ActionController::Base
def show_detailed_exceptions?() true end
end
get '/posts'
assert_match(/AssetNotPrecompiledError/, last_response.body)
assert_match(/app.js isn&#x27;t precompiled/, last_response.body)
end
test "precompile properly refers files referenced with asset_path and and run in the provided RAILS_ENV" do
app_file "app/assets/stylesheets/application.css.erb", "<%= asset_path('rails.png') %>"
# digest is default in false, we must enable it for test environment
add_to_env_config "test", "config.assets.digest = true"
quietly do
Dir.chdir(app_path){ `bundle exec rake assets:precompile RAILS_ENV=test` }
end
file = Dir["#{app_path}/public/assets/application.css"].first
assert_match(/\/assets\/rails\.png/, File.read(file))
file = Dir["#{app_path}/public/assets/application-*.css"].first
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"
ENV["RAILS_ENV"] = nil
quietly do
Dir.chdir(app_path){ `bundle exec rake assets:precompile RAILS_GROUPS=assets` }
end
file = Dir["#{app_path}/public/assets/application-*.css"].first
assert_match(/\/assets\/rails-([0-z]+)\.png/, File.read(file))
end
test "precompile should handle utf8 filenames" do
filename = "レイルズ.png"
app_file "app/assets/images/#{filename}", "not a image really"
add_to_config "config.assets.precompile = [ /\.png$/, /application.(css|js)$/ ]"
precompile!
require "#{app_path}/config/environment"
get "/assets/#{URI.parser.escape(filename)}"
assert_match "not a image really", last_response.body
assert File.exists?("#{app_path}/public/assets/#{filename}")
end
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"
quietly do
Dir.chdir(app_path){ `bundle exec rake assets:clean` }
end
files = Dir["#{app_path}/public/assets/**/*", "#{app_path}/tmp/cache/*"]
assert_equal 0, files.length, "Expected no assets, but found #{files.join(', ')}"
end
test "assets routes are not drawn when compilation is disabled" do
app_file "app/assets/javascripts/demo.js.erb", "<%= :alert %>();"
add_to_config "config.assets.compile = false"
ENV["RAILS_ENV"] = "production"
require "#{app_path}/config/environment"
get "/assets/demo.js"
assert_equal 404, last_response.status
end
test "does not stream session cookies back" do
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"]
end
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
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
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 %>"
add_to_config "config.assets.digest = false"
precompile!
assert_equal "Post;\n", File.read("#{app_path}/public/assets/application.js")
end
test "assets can't access model information when precompiling if not initializing the app" 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", "<%= defined?(Post) || :NoPost %>"
add_to_config "config.assets.digest = false"
add_to_config "config.assets.initialize_on_precompile = false"
precompile!
assert_equal "NoPost;\n", File.read("#{app_path}/public/assets/application.js")
end
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
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
test "digested assets are not mistakenly removed" do
app_file "app/assets/application.js", "alert();"
add_to_config "config.assets.compile = true"
add_to_config "config.assets.digest = true"
quietly do
Dir.chdir(app_path){ `bundle exec rake assets:clean assets:precompile` }
end
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
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
quietly do
Dir.chdir(app_path){ `bundle exec rake assets:clean` }
end
files = Dir["#{app_path}/public/production_assets/application.js"]
assert_equal 0, files.length, "Expected application.js asset to be removed, but still exists"
end
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'"
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
app_file "app/assets/javascripts/image_loader.js.erb", 'var src="<%= image_path("rails.png") %>";'
add_to_config "config.assets.precompile = %w{image_loader.js}"
add_to_config "config.asset_host = 'example.com'"
precompile!
assert_match 'src="//example.com/assets/rails.png"', File.read("#{app_path}/public/assets/image_loader.js")
end
test "asset paths should use RAILS_RELATIVE_URL_ROOT by default" do
ENV["RAILS_RELATIVE_URL_ROOT"] = "/sub/uri"
app_file "app/assets/javascripts/app.js.erb", 'var src="<%= image_path("rails.png") %>";'
add_to_config "config.assets.precompile = %w{app.js}"
precompile!
assert_match 'src="/sub/uri/assets/rails.png"', File.read("#{app_path}/public/assets/app.js")
end
test "html assets are compiled when executing precompile" do
app_file "app/assets/pages/page.html.erb", "<%= javascript_include_tag :application %>"
ENV["RAILS_ENV"] = "production"
ENV["RAILS_GROUP"] = "assets"
quietly do
Dir.chdir(app_path){ `bundle exec rake assets:precompile` }
end
assert File.exists?("#{app_path}/public/assets/page.html")
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

View File

@@ -150,14 +150,5 @@ module ApplicationTests
output = @inspector.format @set.routes
assert_equal [" /foo #{RackApp.name} {:constraint=>( my custom constraint )}"], output
end
def test_rake_routes_dont_show_app_mounted_in_assets_prefix
@set.draw do
match '/sprockets' => RackApp
end
output = @inspector.format @set.routes
assert_no_match(/RackApp/, output.first)
assert_no_match(/\/sprockets/, output.first)
end
end
end

View File

@@ -720,22 +720,6 @@ module RailtiesTest
App's bar partial
RUBY
@plugin.write "app/assets/javascripts/foo.js", <<-RUBY
// Bukkit's foo js
RUBY
app_file "app/assets/javascripts/foo.js", <<-RUBY
// App's foo js
RUBY
@blog.write "app/assets/javascripts/bar.js", <<-RUBY
// Blog's bar js
RUBY
app_file "app/assets/javascripts/bar.js", <<-RUBY
// App's bar js
RUBY
add_to_config("config.railties_order = [:all, :main_app, Blog::Engine]")
boot_rails
@@ -747,12 +731,6 @@ module RailtiesTest
get("/bar")
assert_equal "App's bar partial", last_response.body.strip
get("/assets/foo.js")
assert_equal "// Bukkit's foo js\n;", last_response.body.strip
get("/assets/bar.js")
assert_equal "// App's bar js\n;", last_response.body.strip
# ensure that railties are not added twice
railties = Rails.application.ordered_railties.map(&:class)
assert_equal railties, railties.uniq

View File

@@ -10,17 +10,6 @@ module RailtiesTest
@app ||= Rails.application
end
def test_serving_sprockets_assets
@plugin.write "app/assets/javascripts/engine.js.erb", "<%= :alert %>();"
boot_rails
require 'rack/test'
extend Rack::Test::Methods
get "/assets/engine.js"
assert_match "alert()", last_response.body
end
def test_rake_environment_can_be_called_in_the_engine_or_plugin
boot_rails

Binary file not shown.

Binary file not shown.

Binary file not shown.