Skip to content

Commit

Permalink
rake assets:precompile executes in production environment as default …
Browse files Browse the repository at this point in the history
…if RAILS_ENV was not provided
  • Loading branch information
spastorino committed Aug 15, 2011
1 parent b01cc22 commit 4ca605b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
8 changes: 4 additions & 4 deletions actionpack/lib/sprockets/assets.rake
Expand Up @@ -3,9 +3,9 @@ namespace :assets do
task :precompile do task :precompile do
# We need to do this dance because RAILS_GROUPS is used # We need to do this dance because RAILS_GROUPS is used
# too early in the boot process and changing here is already too late. # too early in the boot process and changing here is already too late.
if ENV["RAILS_GROUPS"].to_s.empty? if ENV["RAILS_GROUPS"].to_s.empty? || ENV["RAILS_ENV"].to_s.empty?
ENV["RAILS_GROUPS"] = "assets" ENV["RAILS_GROUPS"] ||= "assets"
ENV["RAILS_ENV"] ||= "production" ENV["RAILS_ENV"] ||= "production"
Kernel.exec $0, *ARGV Kernel.exec $0, *ARGV
else else
Rake::Task["environment"].invoke Rake::Task["environment"].invoke
Expand All @@ -24,4 +24,4 @@ namespace :assets do
public_asset_path = Rails.public_path + assets.prefix public_asset_path = Rails.public_path + assets.prefix
rm_rf public_asset_path, :secure => true rm_rf public_asset_path, :secure => true
end end
end end
26 changes: 19 additions & 7 deletions railties/test/application/assets_test.rb
Expand Up @@ -46,28 +46,40 @@ def app
assert defined?(Uglifier) assert defined?(Uglifier)
end end


test "precompile creates the file and gives it the original asset's content" do test "precompile creates the filem, 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/application.js", "alert();"
app_file "app/assets/javascripts/foo/application.js", "alert();" app_file "app/assets/javascripts/foo/application.js", "alert();"


ENV["RAILS_ENV"] = nil
capture(:stdout) do capture(:stdout) do
Dir.chdir(app_path){ `bundle exec rake assets:precompile` } Dir.chdir(app_path){ `bundle exec rake assets:precompile` }
end end
files = Dir["#{app_path}/public/assets/application-*.js"] files = Dir["#{app_path}/public/assets/application-b29a188b3d9c74ef7cbb7ddf9e99f953.js"]
files << Dir["#{app_path}/public/assets/foo/application-*.js"].first files << Dir["#{app_path}/public/assets/foo/application-b29a188b3d9c74ef7cbb7ddf9e99f953.js"].first
files.each do |file| files.each do |file|
assert_not_nil file, "Expected application.js asset to be generated, but none found" assert_not_nil file, "Expected application.js asset to be generated, but none found"
assert_equal "alert();\n", File.read(file) assert_equal "alert()", File.read(file)
end end
end end


test "precompile appends the md5 hash to files referenced with asset_path" do test "precompile appends the md5 hash to files referenced with asset_path and run in the provided RAILS_ENV" do
app_file "app/assets/stylesheets/application.css.erb", "<%= asset_path('rails.png') %>" app_file "app/assets/stylesheets/application.css.erb", "<%= asset_path('rails.png') %>"


# capture(:stdout) do
Dir.chdir(app_path){ `bundle exec rake assets:precompile RAILS_ENV=test` }
# end
file = Dir["#{app_path}/public/assets/application-4bd8b7059c5336ec7ad515c9dbd59974.css"].first
assert_match /\/assets\/rails-([0-z]+)\.png/, File.read(file)
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') %>"

ENV["RAILS_ENV"] = nil
capture(:stdout) do capture(:stdout) do
Dir.chdir(app_path){ `bundle exec rake assets:precompile` } Dir.chdir(app_path){ `bundle exec rake assets:precompile RAILS_GROUPS=assets` }
end end
file = Dir["#{app_path}/public/assets/application-*.css"].first file = Dir["#{app_path}/public/assets/application-8d301a938f1abfd789bbec87ed1ef770.css"].first
assert_match /\/assets\/rails-([0-z]+)\.png/, File.read(file) assert_match /\/assets\/rails-([0-z]+)\.png/, File.read(file)
end end


Expand Down

0 comments on commit 4ca605b

Please sign in to comment.