Skip to content

Commit

Permalink
Merge pull request #7482 from route/syncing_tests_for_sprockets
Browse files Browse the repository at this point in the history
Use last version of sprockets-rails and sync tests for assets.
  • Loading branch information
rafaelfranca committed Sep 19, 2012
2 parents f4ad0eb + bbcb625 commit bf4c8fe
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 33 deletions.
3 changes: 2 additions & 1 deletion Gemfile
Expand Up @@ -27,7 +27,8 @@ end

# This needs to be with require false to avoid
# it being automatically loaded by sprockets
gem 'uglifier', '>= 1.0.3', require: false
gem 'uglifier', require: false
gem 'sprockets-rails', github: 'rails/sprockets-rails'

group :doc do
# The current sdoc cannot generate GitHub links due
Expand Down
67 changes: 35 additions & 32 deletions railties/test/application/assets_test.rb
@@ -1,6 +1,5 @@
# -*- coding: utf-8 -*-
require 'isolation/abstract_unit'
require 'active_support/core_ext/kernel/reporting'
require 'rack/test'

module ApplicationTests
Expand Down Expand Up @@ -28,10 +27,18 @@ def precompile!(env = nil)

def clean_assets!
quietly do
assert Dir.chdir(app_path){ system('bundle exec rake assets:clean') }
assert Dir.chdir(app_path) { system('bundle exec rake assets:clean') }
end
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 "assets routes have higher priority" do
app_file "app/assets/javascripts/demo.js.erb", "a = <%= image_path('rails.png').inspect %>;"

Expand Down Expand Up @@ -92,9 +99,8 @@ def clean_assets!

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", "happy.face", "-happyface",
"-happy.png", "-happy.face.png", "_happyface", "_happy.face.png",
"_happy.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"
Expand All @@ -103,7 +109,7 @@ def clean_assets!
precompile!

images_should_compile.each do |filename|
assert_file_exists "#{app_path}/public/assets/#{filename}"
assert_file_exists("#{app_path}/public/assets/#{filename}")
end

assert_file_exists("#{app_path}/public/assets/application.js")
Expand All @@ -119,12 +125,13 @@ def clean_assets!
assert_no_file_exists("#{app_path}/public/assets/something.else.css")
end

def assert_file_exists(filename)
assert File.exists?(filename), "missing #{filename}"
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();"

def assert_no_file_exists(filename)
assert !File.exists?(filename), "#{filename} does exist"
precompile!

assert_file_exists("#{app_path}/public/assets/something.js")
end

test "asset pipeline should use a Sprockets::Index when config.assets.digest is true" do
Expand All @@ -151,21 +158,6 @@ def assert_no_file_exists(filename)
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
Expand All @@ -186,8 +178,8 @@ def assert_no_file_exists(filename)

precompile!

assert File.exists?("#{app_path}/public/assets/application.js")
assert File.exists?("#{app_path}/public/assets/application.css")
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"

Expand Down Expand Up @@ -238,7 +230,7 @@ def show_detailed_exceptions?() true end

get '/posts'
assert_match(/AssetNotPrecompiledError/, last_response.body)
assert_match(/app.js isn&#39;t precompiled/, last_response.body)
assert_match(/app\.js isn&#39;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
Expand All @@ -262,7 +254,7 @@ class ::PostsController < ActionController::Base ; end

get '/posts'
assert_match(/AssetNotPrecompiledError/, last_response.body)
assert_match(/app.js isn&#39;t precompiled/, last_response.body)
assert_match(/app\.js isn&#39;t precompiled/, last_response.body)
end

test "precompile properly refers files referenced with asset_path and and run in the provided RAILS_ENV" do
Expand Down Expand Up @@ -318,7 +310,7 @@ class ::PostsController < ActionController::Base ; end

get "/assets/#{URI.parser.escape(filename)}"
assert_match "not a image really", last_response.body
assert File.exists?("#{app_path}/public/assets/#{filename}")
assert_file_exists("#{app_path}/public/assets/#{filename}")
end

test "assets are cleaned up properly" do
Expand Down Expand Up @@ -454,7 +446,6 @@ class ::PostsController < ActionController::Base ; end
add_to_config "config.assets.compile = true"
add_to_config "config.assets.digest = true"

clean_assets!
precompile!

files = Dir["#{app_path}/public/assets/application-*.js"]
Expand Down Expand Up @@ -504,6 +495,18 @@ class ::PostsController < ActionController::Base; end
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

test "assets:cache:clean should clean cache" do
ENV["RAILS_ENV"] = "production"
precompile!
Expand Down

0 comments on commit bf4c8fe

Please sign in to comment.