sprockets-rails 1.0.0 → 2.0.0.backport1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (41) hide show
  1. checksums.yaml +7 -0
  2. data/README.md +155 -0
  3. data/lib/sprockets/rails.rb +3 -0
  4. data/lib/sprockets/rails/helper.rb +145 -0
  5. data/lib/sprockets/rails/legacy_asset_tag_helper.rb +32 -0
  6. data/lib/sprockets/rails/legacy_asset_url_helper.rb +130 -0
  7. data/lib/sprockets/rails/task.rb +82 -0
  8. data/lib/sprockets/railtie.rb +125 -0
  9. metadata +52 -81
  10. data/MIT-LICENSE +0 -20
  11. data/README.rdoc +0 -3
  12. data/Rakefile +0 -24
  13. data/lib/sprockets-rails.rb +0 -7
  14. data/lib/sprockets/rails/bootstrap.rb +0 -41
  15. data/lib/sprockets/rails/compressors.rb +0 -87
  16. data/lib/sprockets/rails/helpers.rb +0 -8
  17. data/lib/sprockets/rails/helpers/isolated_helper.rb +0 -15
  18. data/lib/sprockets/rails/helpers/rails_helper.rb +0 -169
  19. data/lib/sprockets/rails/railtie.rb +0 -64
  20. data/lib/sprockets/rails/static_compiler.rb +0 -64
  21. data/lib/sprockets/rails/version.rb +0 -5
  22. data/lib/tasks/assets.rake +0 -105
  23. data/test/abstract_unit.rb +0 -145
  24. data/test/assets_debugging_test.rb +0 -65
  25. data/test/assets_test.rb +0 -532
  26. data/test/fixtures/alternate/stylesheets/style.css +0 -1
  27. data/test/fixtures/app/fonts/dir/font.ttf +0 -0
  28. data/test/fixtures/app/fonts/font.ttf +0 -0
  29. data/test/fixtures/app/images/logo.png +0 -0
  30. data/test/fixtures/app/javascripts/application.js +0 -1
  31. data/test/fixtures/app/javascripts/dir/xmlhr.js +0 -0
  32. data/test/fixtures/app/javascripts/extra.js +0 -0
  33. data/test/fixtures/app/javascripts/xmlhr.js +0 -0
  34. data/test/fixtures/app/stylesheets/application.css +0 -1
  35. data/test/fixtures/app/stylesheets/dir/style.css +0 -0
  36. data/test/fixtures/app/stylesheets/extra.css +0 -0
  37. data/test/fixtures/app/stylesheets/style.css +0 -0
  38. data/test/sprockets_compressors_test.rb +0 -27
  39. data/test/sprockets_helper_test.rb +0 -345
  40. data/test/sprockets_helper_with_routes_test.rb +0 -60
  41. data/test/test_helper.rb +0 -84
@@ -1,5 +0,0 @@
1
- module Sprockets
2
- module Rails
3
- VERSION = "1.0.0"
4
- end
5
- end
@@ -1,105 +0,0 @@
1
- require "fileutils"
2
-
3
- namespace :assets do
4
- def ruby_rake_task(task, fork = true)
5
- env = ENV['RAILS_ENV'] || 'production'
6
- groups = ENV['RAILS_GROUPS'] || 'assets'
7
- args = [$0, task,"RAILS_ENV=#{env}","RAILS_GROUPS=#{groups}"]
8
- args << "--trace" if Rake.application.options.trace
9
- if $0 =~ /rake\.bat\Z/i
10
- Kernel.exec $0, *args
11
- else
12
- fork ? ruby(*args) : Kernel.exec(FileUtils::RUBY, *args)
13
- end
14
- end
15
-
16
- # We are currently running with no explicit bundler group
17
- # and/or no explicit environment - we have to reinvoke rake to
18
- # execute this task.
19
- def invoke_or_reboot_rake_task(task)
20
- if ENV['RAILS_GROUPS'].to_s.empty? || ENV['RAILS_ENV'].to_s.empty?
21
- ruby_rake_task task
22
- else
23
- Rake::Task[task].invoke
24
- end
25
- end
26
-
27
- desc "Compile all the assets named in config.assets.precompile"
28
- task :precompile do
29
- invoke_or_reboot_rake_task "assets:precompile:all"
30
- end
31
-
32
- namespace :precompile do
33
- def internal_precompile(digest=nil)
34
- unless ::Rails.application.config.assets.enabled
35
- warn "Cannot precompile assets if sprockets is disabled. Please set config.assets.enabled to true"
36
- exit
37
- end
38
-
39
- # Ensure that action view is loaded and the appropriate
40
- # sprockets hooks get executed
41
- _ = ActionView::Base
42
-
43
- config = ::Rails.application.config
44
- config.assets.compile = true
45
- config.assets.digest = digest unless digest.nil?
46
- config.assets.digests = {}
47
-
48
- env = ::Rails.application.assets
49
- target = File.join(::Rails.public_path, config.assets.prefix)
50
- compiler = Sprockets::Rails::StaticCompiler.new(env,
51
- target,
52
- config.assets.precompile,
53
- :manifest_path => config.assets.manifest,
54
- :digest => config.assets.digest,
55
- :manifest => digest.nil?)
56
- compiler.compile
57
- end
58
-
59
- task :all do
60
- Rake::Task["assets:precompile:primary"].invoke
61
- # We need to reinvoke in order to run the secondary digestless
62
- # asset compilation run - a fresh Sprockets environment is
63
- # required in order to compile digestless assets as the
64
- # environment has already cached the assets on the primary
65
- # run.
66
- ruby_rake_task("assets:precompile:nondigest", false) if ::Rails.application.config.assets.digest
67
- end
68
-
69
- task :primary => ["assets:cache:clean"] do
70
- internal_precompile
71
- end
72
-
73
- task :nondigest => ["assets:cache:clean"] do
74
- internal_precompile(false)
75
- end
76
- end
77
-
78
- desc "Remove compiled assets"
79
- task :clean do
80
- invoke_or_reboot_rake_task "assets:clean:all"
81
- end
82
-
83
- namespace :clean do
84
- task :all => ["assets:cache:clean"] do
85
- config = ::Rails.application.config
86
- public_asset_path = File.join(::Rails.public_path, config.assets.prefix)
87
- rm_rf public_asset_path, :secure => true
88
- end
89
- end
90
-
91
- namespace :cache do
92
- task :clean => ["assets:environment"] do
93
- ::Rails.application.assets.cache.clear
94
- end
95
- end
96
-
97
- task :environment do
98
- if ::Rails.application.config.assets.initialize_on_precompile
99
- Rake::Task["environment"].invoke
100
- else
101
- ::Rails.application.initialize!(:assets)
102
- Sprockets::Rails::Bootstrap.new(Rails.application).run
103
- end
104
- end
105
- end
@@ -1,145 +0,0 @@
1
- lib = File.expand_path("#{File.dirname(__FILE__)}/../lib")
2
- $:.unshift(lib) unless $:.include?('lib') || $:.include?(lib)
3
-
4
- require 'sprockets-rails'
5
- require 'fileutils'
6
- require 'minitest/autorun'
7
- require 'active_support/test_case'
8
- require 'rails/generators'
9
- require "active_support/testing/isolation"
10
- require "active_support/testing/declarative"
11
- require "active_support/core_ext/kernel/reporting"
12
-
13
- module TestHelpers
14
- module Paths
15
- module_function
16
-
17
- TMP_PATH = File.expand_path(File.join(File.dirname(__FILE__), *%w[.. tmp]))
18
-
19
- def tmp_path(*args)
20
- File.join(TMP_PATH, *args)
21
- end
22
-
23
- def app_path(*args)
24
- tmp_path(*%w[app] + args)
25
- end
26
-
27
- def rails_root
28
- app_path
29
- end
30
- end
31
-
32
- module Rack
33
- def app(env = "production")
34
- old_env = ENV["RAILS_ENV"]
35
- @app ||= begin
36
- ENV["RAILS_ENV"] = env
37
- require "#{app_path}/config/environment"
38
- Rails.application
39
- end
40
- ensure
41
- ENV["RAILS_ENV"] = old_env
42
- end
43
-
44
- def get(path)
45
- @app.call(::Rack::MockRequest.env_for(path))
46
- end
47
- end
48
-
49
- module Generation
50
- # Build an application by invoking the generator and going through the whole stack.
51
- def build_app(options = {})
52
- @prev_rails_env = ENV['RAILS_ENV']
53
- ENV['RAILS_ENV'] = 'development'
54
-
55
- FileUtils.rm_rf(app_path)
56
- FileUtils.cp_r(tmp_path('app_template'), app_path)
57
-
58
- # Delete the initializers unless requested
59
- unless options[:initializers]
60
- Dir["#{app_path}/config/initializers/*.rb"].each do |initializer|
61
- File.delete(initializer)
62
- end
63
- end
64
-
65
- unless options[:gemfile]
66
- File.delete"#{app_path}/Gemfile"
67
- end
68
-
69
- routes = File.read("#{app_path}/config/routes.rb")
70
- if routes =~ /(\n\s*end\s*)\Z/
71
- File.open("#{app_path}/config/routes.rb", 'w') do |f|
72
- f.puts $` + "\nmatch ':controller(/:action(/:id))(.:format)'\n" + $1
73
- end
74
- end
75
-
76
- add_to_config 'config.secret_token = "3b7cd727ee24e8444053437c36cc66c4"; config.session_store :cookie_store, :key => "_myapp_session"; config.active_support.deprecation = :log'
77
- end
78
-
79
- def teardown_app
80
- ENV['RAILS_ENV'] = @prev_rails_env if @prev_rails_env
81
- end
82
-
83
- def add_to_config(str)
84
- environment = File.read("#{app_path}/config/application.rb")
85
- if environment =~ /(\n\s*end\s*end\s*)\Z/
86
- File.open("#{app_path}/config/application.rb", 'w') do |f|
87
- f.puts $` + "\n#{str}\n" + $1
88
- end
89
- end
90
- end
91
-
92
- def add_to_env_config(env, str)
93
- environment = File.read("#{app_path}/config/environments/#{env}.rb")
94
- if environment =~ /(\n\s*end\s*)\Z/
95
- File.open("#{app_path}/config/environments/#{env}.rb", 'w') do |f|
96
- f.puts $` + "\n#{str}\n" + $1
97
- end
98
- end
99
- end
100
-
101
- def app_file(path, contents)
102
- FileUtils.mkdir_p File.dirname("#{app_path}/#{path}")
103
- File.open("#{app_path}/#{path}", 'w') do |f|
104
- f.puts contents
105
- end
106
- end
107
-
108
- def boot_rails
109
- require 'rubygems' unless defined? Gem
110
- require 'bundler'
111
- Bundler.setup
112
- end
113
- end
114
- end
115
-
116
- class ActiveSupport::TestCase
117
- include TestHelpers::Paths
118
- include TestHelpers::Rack
119
- include TestHelpers::Generation
120
- extend ActiveSupport::Testing::Declarative
121
- end
122
-
123
- # Create a scope and build a fixture rails app
124
- Module.new do
125
- extend TestHelpers::Paths
126
- # Build a rails app
127
- if File.exist?(tmp_path)
128
- FileUtils.rm_rf(tmp_path)
129
- end
130
- FileUtils.mkdir(tmp_path)
131
-
132
- quietly do
133
- Rails::Generators.invoke('app', ["#{tmp_path('app_template')}", "--skip-active-record", "--skip-test-unit"])
134
- end
135
-
136
- File.open("#{tmp_path}/app_template/config/boot.rb", 'w') do |f|
137
- f.puts 'require "action_controller/railtie"'
138
- end
139
-
140
- # This is temporary, disable sprockets-rails railtie done in rails
141
- file = "#{tmp_path}/app_template/config/application.rb"
142
- contents = File.read(file)
143
- contents.sub!(/require \"sprockets\/railtie\"/, "")
144
- File.open(file, "w+") { |f| f.puts contents }
145
- end
@@ -1,65 +0,0 @@
1
- require File.expand_path(File.dirname(__FILE__) + "/abstract_unit")
2
- require 'rack/test'
3
-
4
- module ApplicationTests
5
- class AssetsDebuggingTest < ActiveSupport::TestCase
6
- include ActiveSupport::Testing::Isolation
7
- include Rack::Test::Methods
8
-
9
- def setup
10
- build_app(:initializers => true)
11
-
12
- app_file "app/assets/javascripts/application.js", "//= require_tree ."
13
- app_file "app/assets/javascripts/xmlhr.js", "function f1() { alert(); }"
14
- app_file "app/views/posts/index.html.erb", "<%= javascript_include_tag 'application' %>"
15
-
16
- app_file "config/routes.rb", <<-RUBY
17
- AppTemplate::Application.routes.draw do
18
- match '/posts', :to => "posts#index"
19
- end
20
- RUBY
21
-
22
- app_file "app/controllers/posts_controller.rb", <<-RUBY
23
- class PostsController < ActionController::Base
24
- end
25
- RUBY
26
-
27
- ENV["RAILS_ENV"] = "production"
28
-
29
- boot_rails
30
- end
31
-
32
- def teardown
33
- teardown_app
34
- end
35
-
36
- test "assets are concatenated when debug is off and compile is off either if debug_assets param is provided" do
37
- # config.assets.debug and config.assets.compile are false for production environment
38
- ENV["RAILS_ENV"] = "production"
39
- capture(:stdout) do
40
- Dir.chdir(app_path){ `bundle exec rake assets:precompile` }
41
- end
42
- require "#{app_path}/config/environment"
43
-
44
- class ::PostsController < ActionController::Base ; end
45
-
46
- # the debug_assets params isn't used if compile is off
47
- get '/posts?debug_assets=true'
48
- assert_match(/<script src="\/assets\/application-([0-z]+)\.js" type="text\/javascript"><\/script>/, last_response.body)
49
- assert_no_match(/<script src="\/assets\/xmlhr-([0-z]+)\.js" type="text\/javascript"><\/script>/, last_response.body)
50
- end
51
-
52
- test "assets aren't concatened when compile is true is on and debug_assets params is true" do
53
- app_file "config/initializers/compile.rb", "Rails.application.config.assets.compile = true"
54
-
55
- ENV["RAILS_ENV"] = "production"
56
- require "#{app_path}/config/environment"
57
-
58
- class ::PostsController < ActionController::Base ; end
59
-
60
- get '/posts?debug_assets=true'
61
- assert_match(/<script src="\/assets\/application-([0-z]+)\.js\?body=1" type="text\/javascript"><\/script>/, last_response.body)
62
- assert_match(/<script src="\/assets\/xmlhr-([0-z]+)\.js\?body=1" type="text\/javascript"><\/script>/, last_response.body)
63
- end
64
- end
65
- end
data/test/assets_test.rb DELETED
@@ -1,532 +0,0 @@
1
- # -*- coding: utf-8 -*-
2
- require File.expand_path(File.dirname(__FILE__) + "/abstract_unit")
3
- require 'rack/test'
4
-
5
- module ApplicationTests
6
- class AssetsTest < ActiveSupport::TestCase
7
- include ActiveSupport::Testing::Isolation
8
- include Rack::Test::Methods
9
-
10
- def setup
11
- build_app(:initializers => true)
12
- boot_rails
13
- end
14
-
15
- def teardown
16
- teardown_app
17
- end
18
-
19
- def precompile!
20
- quietly do
21
- Dir.chdir(app_path){ `bundle exec rake assets:precompile` }
22
- end
23
- end
24
-
25
- test "assets routes have higher priority" do
26
- app_file "app/assets/javascripts/demo.js.erb", "a = <%= image_path('rails.png').inspect %>;"
27
-
28
- app_file 'config/routes.rb', <<-RUBY
29
- AppTemplate::Application.routes.draw do
30
- match '*path', :to => lambda { |env| [200, { "Content-Type" => "text/html" }, "Not an asset"] }
31
- end
32
- RUBY
33
-
34
- require "#{app_path}/config/environment"
35
-
36
- get "/assets/demo.js"
37
- assert_equal 'a = "/assets/rails.png";', last_response.body.strip
38
- end
39
-
40
- test "assets do not require compressors until it is used" do
41
- app_file "app/assets/javascripts/demo.js.erb", "<%= :alert %>();"
42
- add_to_env_config "production", "config.assets.compile = true"
43
-
44
- ENV["RAILS_ENV"] = "production"
45
- require "#{app_path}/config/environment"
46
-
47
- assert !defined?(Uglifier)
48
- get "/assets/demo.js"
49
- assert_match "alert()", last_response.body
50
- assert defined?(Uglifier)
51
- end
52
-
53
- test "precompile creates the file, gives it the original asset's content and run in production as default" do
54
- app_file "app/assets/javascripts/application.js", "alert();"
55
- app_file "app/assets/javascripts/foo/application.js", "alert();"
56
-
57
- ENV["RAILS_ENV"] = nil
58
- precompile!
59
-
60
- files = Dir["#{app_path}/public/assets/application-*.js"]
61
- files << Dir["#{app_path}/public/assets/application.js"].first
62
- files << Dir["#{app_path}/public/assets/foo/application-*.js"].first
63
- files << Dir["#{app_path}/public/assets/foo/application.js"].first
64
- files.each do |file|
65
- assert_not_nil file, "Expected application.js asset to be generated, but none found"
66
- assert_equal "alert();", File.read(file)
67
- end
68
- end
69
-
70
- test "precompile application.js and application.css and all other non JS/CSS files" do
71
- app_file "app/assets/javascripts/application.js", "alert();"
72
- app_file "app/assets/stylesheets/application.css", "body{}"
73
-
74
- app_file "app/assets/javascripts/someapplication.js", "alert();"
75
- app_file "app/assets/stylesheets/someapplication.css", "body{}"
76
-
77
- app_file "app/assets/javascripts/something.min.js", "alert();"
78
- app_file "app/assets/stylesheets/something.min.css", "body{}"
79
-
80
- app_file "app/assets/javascripts/something.else.js.erb", "alert();"
81
- app_file "app/assets/stylesheets/something.else.css.erb", "body{}"
82
-
83
- images_should_compile = ["a.png", "happyface.png", "happy_face.png", "happy.face.png",
84
- "happy-face.png", "happy.happy_face.png", "happy_happy.face.png",
85
- "happy.happy.face.png", "happy", "happy.face", "-happyface",
86
- "-happy.png", "-happy.face.png", "_happyface", "_happy.face.png",
87
- "_happy.png"]
88
-
89
- images_should_compile.each do |filename|
90
- app_file "app/assets/images/#{filename}", "happy"
91
- end
92
-
93
- precompile!
94
-
95
- images_should_compile.each do |filename|
96
- assert File.exists?("#{app_path}/public/assets/#{filename}")
97
- end
98
-
99
- assert File.exists?("#{app_path}/public/assets/application.js")
100
- assert File.exists?("#{app_path}/public/assets/application.css")
101
-
102
- assert !File.exists?("#{app_path}/public/assets/someapplication.js")
103
- assert !File.exists?("#{app_path}/public/assets/someapplication.css")
104
-
105
- assert !File.exists?("#{app_path}/public/assets/something.min.js")
106
- assert !File.exists?("#{app_path}/public/assets/something.min.css")
107
-
108
- assert !File.exists?("#{app_path}/public/assets/something.else.js")
109
- assert !File.exists?("#{app_path}/public/assets/something.else.css")
110
- end
111
-
112
- test "asset pipeline should use a Sprockets::Index when config.assets.digest is true" do
113
- add_to_config "config.assets.digest = true"
114
- add_to_config "config.action_controller.perform_caching = false"
115
-
116
- ENV["RAILS_ENV"] = "production"
117
- require "#{app_path}/config/environment"
118
-
119
- assert_equal Sprockets::Index, Rails.application.assets.class
120
- end
121
-
122
- test "precompile creates a manifest file with all the assets listed" do
123
- app_file "app/assets/stylesheets/application.css.erb", "<%= asset_path('rails.png') %>"
124
- app_file "app/assets/javascripts/application.js", "alert();"
125
- # digest is default in false, we must enable it for test environment
126
- add_to_config "config.assets.digest = true"
127
-
128
- precompile!
129
- manifest = "#{app_path}/public/assets/manifest.yml"
130
-
131
- assets = YAML.load_file(manifest)
132
- assert_match(/application-([0-z]+)\.js/, assets["application.js"])
133
- assert_match(/application-([0-z]+)\.css/, assets["application.css"])
134
- end
135
-
136
- test "precompile creates a manifest file in a custom path with all the assets listed" do
137
- app_file "app/assets/stylesheets/application.css.erb", "<%= asset_path('rails.png') %>"
138
- app_file "app/assets/javascripts/application.js", "alert();"
139
- # digest is default in false, we must enable it for test environment
140
- add_to_config "config.assets.digest = true"
141
- add_to_config "config.assets.manifest = '#{app_path}/shared'"
142
-
143
- precompile!
144
- manifest = "#{app_path}/shared/manifest.yml"
145
-
146
- assets = YAML.load_file(manifest)
147
- assert_match(/application-([0-z]+)\.js/, assets["application.js"])
148
- assert_match(/application-([0-z]+)\.css/, assets["application.css"])
149
- end
150
-
151
- test "the manifest file should be saved by default in the same assets folder" do
152
- app_file "app/assets/javascripts/application.js", "alert();"
153
- # digest is default in false, we must enable it for test environment
154
- add_to_config "config.assets.digest = true"
155
- add_to_config "config.assets.prefix = '/x'"
156
-
157
- precompile!
158
-
159
- manifest = "#{app_path}/public/x/manifest.yml"
160
- assets = YAML.load_file(manifest)
161
- assert_match(/application-([0-z]+)\.js/, assets["application.js"])
162
- end
163
-
164
- test "precompile does not append asset digests when config.assets.digest is false" do
165
- app_file "app/assets/stylesheets/application.css.erb", "<%= asset_path('rails.png') %>"
166
- app_file "app/assets/javascripts/application.js", "alert();"
167
- add_to_config "config.assets.digest = false"
168
-
169
- precompile!
170
-
171
- assert File.exists?("#{app_path}/public/assets/application.js")
172
- assert File.exists?("#{app_path}/public/assets/application.css")
173
-
174
- manifest = "#{app_path}/public/assets/manifest.yml"
175
-
176
- assets = YAML.load_file(manifest)
177
- assert_equal "application.js", assets["application.js"]
178
- assert_equal "application.css", assets["application.css"]
179
- end
180
-
181
- test "assets do not require any assets group gem when manifest file is present" do
182
- app_file "app/assets/javascripts/application.js", "alert();"
183
- add_to_env_config "production", "config.serve_static_assets = true"
184
-
185
- ENV["RAILS_ENV"] = "production"
186
- precompile!
187
-
188
- manifest = "#{app_path}/public/assets/manifest.yml"
189
- assets = YAML.load_file(manifest)
190
- asset_path = assets["application.js"]
191
-
192
- require "#{app_path}/config/environment"
193
-
194
- # Checking if Uglifier is defined we can know if Sprockets was reached or not
195
- assert !defined?(Uglifier)
196
- get "/assets/#{asset_path}"
197
- assert_match "alert()", last_response.body
198
- assert !defined?(Uglifier)
199
- end
200
-
201
- test "assets raise AssetNotPrecompiledError when manifest file is present and requested file isn't precompiled" do
202
- app_file "app/views/posts/index.html.erb", "<%= javascript_include_tag 'app' %>"
203
-
204
- app_file "config/routes.rb", <<-RUBY
205
- AppTemplate::Application.routes.draw do
206
- match '/posts', :to => "posts#index"
207
- end
208
- RUBY
209
-
210
- ENV["RAILS_ENV"] = "production"
211
- precompile!
212
-
213
- # Create file after of precompile
214
- app_file "app/assets/javascripts/app.js", "alert();"
215
-
216
- require "#{app_path}/config/environment"
217
- class ::PostsController < ActionController::Base
218
- def show_detailed_exceptions?() true end
219
- end
220
-
221
- get '/posts'
222
- assert_match(/AssetNotPrecompiledError/, last_response.body)
223
- assert_match(/app.js isn't precompiled/, last_response.body)
224
- end
225
-
226
- test "assets raise AssetNotPrecompiledError when manifest file is present and requested file isn't precompiled if digest is disabled" do
227
- app_file "app/views/posts/index.html.erb", "<%= javascript_include_tag 'app' %>"
228
- add_to_config "config.assets.compile = false"
229
-
230
- app_file "config/routes.rb", <<-RUBY
231
- AppTemplate::Application.routes.draw do
232
- match '/posts', :to => "posts#index"
233
- end
234
- RUBY
235
-
236
- ENV["RAILS_ENV"] = "development"
237
- precompile!
238
-
239
- # Create file after of precompile
240
- app_file "app/assets/javascripts/app.js", "alert();"
241
-
242
- require "#{app_path}/config/environment"
243
- class ::PostsController < ActionController::Base ; end
244
-
245
- get '/posts'
246
- assert_match(/AssetNotPrecompiledError/, last_response.body)
247
- assert_match(/app.js isn't precompiled/, last_response.body)
248
- end
249
-
250
- test "precompile properly refers files referenced with asset_path and and run in the provided RAILS_ENV" do
251
- app_file "app/assets/stylesheets/application.css.erb", "<%= asset_path('rails.png') %>"
252
- # digest is default in false, we must enable it for test environment
253
- add_to_env_config "test", "config.assets.digest = true"
254
-
255
- quietly do
256
- Dir.chdir(app_path){ `bundle exec rake assets:precompile RAILS_ENV=test` }
257
- end
258
- file = Dir["#{app_path}/public/assets/application.css"].first
259
- assert_match(/\/assets\/rails\.png/, File.read(file))
260
- file = Dir["#{app_path}/public/assets/application-*.css"].first
261
- assert_match(/\/assets\/rails-([0-z]+)\.png/, File.read(file))
262
- end
263
-
264
- test "precompile shouldn't use the digests present in manifest.yml" do
265
- app_file "app/assets/stylesheets/application.css.erb", "<%= asset_path('rails.png') %>"
266
-
267
- ENV["RAILS_ENV"] = "production"
268
- precompile!
269
-
270
- manifest = "#{app_path}/public/assets/manifest.yml"
271
- assets = YAML.load_file(manifest)
272
- asset_path = assets["application.css"]
273
-
274
- app_file "app/assets/images/rails.png", "image changed"
275
-
276
- precompile!
277
- assets = YAML.load_file(manifest)
278
-
279
- assert_not_equal asset_path, assets["application.css"]
280
- end
281
-
282
- test "precompile appends the md5 hash to files referenced with asset_path and run in production as default even using RAILS_GROUPS=assets" do
283
- app_file "app/assets/stylesheets/application.css.erb", "<%= asset_path('rails.png') %>"
284
- add_to_config "config.assets.compile = true"
285
-
286
- ENV["RAILS_ENV"] = nil
287
- quietly do
288
- Dir.chdir(app_path){ `bundle exec rake assets:precompile RAILS_GROUPS=assets` }
289
- end
290
- file = Dir["#{app_path}/public/assets/application-*.css"].first
291
- assert_match(/\/assets\/rails-([0-z]+)\.png/, File.read(file))
292
- end
293
-
294
- test "precompile should handle utf8 filenames" do
295
- filename = "レイルズ.png"
296
- app_file "app/assets/images/#{filename}", "not a image really"
297
- add_to_config "config.assets.precompile = [ /\.png$/, /application.(css|js)$/ ]"
298
-
299
- precompile!
300
- require "#{app_path}/config/environment"
301
-
302
- get "/assets/#{URI.parser.escape(filename)}"
303
- assert_match "not a image really", last_response.body
304
- assert File.exists?("#{app_path}/public/assets/#{filename}")
305
- end
306
-
307
- test "assets are cleaned up properly" do
308
- app_file "public/assets/application.js", "alert();"
309
- app_file "public/assets/application.css", "a { color: green; }"
310
- app_file "public/assets/subdir/broken.png", "not really an image file"
311
-
312
- quietly do
313
- Dir.chdir(app_path){ `bundle exec rake assets:clean` }
314
- end
315
-
316
- files = Dir["#{app_path}/public/assets/**/*", "#{app_path}/tmp/cache/assets/*"]
317
- assert_equal 0, files.length, "Expected no assets, but found #{files.join(', ')}"
318
- end
319
-
320
- test "assets routes are not drawn when compilation is disabled" do
321
- app_file "app/assets/javascripts/demo.js.erb", "<%= :alert %>();"
322
- add_to_config "config.assets.compile = false"
323
-
324
- ENV["RAILS_ENV"] = "production"
325
- require "#{app_path}/config/environment"
326
-
327
- get "/assets/demo.js"
328
- assert_equal 404, last_response.status
329
- end
330
-
331
- test "does not stream session cookies back" do
332
- app_file "app/assets/javascripts/demo.js.erb", "<%= :alert %>();"
333
-
334
- app_file "config/routes.rb", <<-RUBY
335
- AppTemplate::Application.routes.draw do
336
- match '/omg', :to => "omg#index"
337
- end
338
- RUBY
339
-
340
- require "#{app_path}/config/environment"
341
-
342
- class ::OmgController < ActionController::Base
343
- def index
344
- flash[:cool_story] = true
345
- render :text => "ok"
346
- end
347
- end
348
-
349
- get "/omg"
350
- assert_equal 'ok', last_response.body
351
-
352
- get "/assets/demo.js"
353
- assert_match "alert()", last_response.body
354
- assert_equal nil, last_response.headers["Set-Cookie"]
355
- end
356
-
357
- test "files in any assets/ directories are not added to Sprockets" do
358
- %w[app lib vendor].each do |dir|
359
- app_file "#{dir}/assets/#{dir}_test.erb", "testing"
360
- end
361
-
362
- app_file "app/assets/javascripts/demo.js", "alert();"
363
-
364
- require "#{app_path}/config/environment"
365
-
366
- get "/assets/demo.js"
367
- assert_match "alert();", last_response.body
368
- assert_equal 200, last_response.status
369
- end
370
-
371
- test "assets are concatenated when debug is off and compile is off either if debug_assets param is provided" do
372
- app_with_assets_in_view
373
-
374
- # config.assets.debug and config.assets.compile are false for production environment
375
- ENV["RAILS_ENV"] = "production"
376
- precompile!
377
-
378
- require "#{app_path}/config/environment"
379
-
380
- class ::PostsController < ActionController::Base ; end
381
-
382
- # the debug_assets params isn't used if compile is off
383
- get '/posts?debug_assets=true'
384
- assert_match(/<script src="\/assets\/application-([0-z]+)\.js" type="text\/javascript"><\/script>/, last_response.body)
385
- assert_no_match(/<script src="\/assets\/xmlhr-([0-z]+)\.js" type="text\/javascript"><\/script>/, last_response.body)
386
- end
387
-
388
- test "assets aren't concatened when compile is true is on and debug_assets params is true" do
389
- app_with_assets_in_view
390
- add_to_env_config "production", "config.assets.compile = true"
391
- add_to_env_config "production", "config.assets.allow_debugging = true"
392
-
393
- ENV["RAILS_ENV"] = "production"
394
- require "#{app_path}/config/environment"
395
-
396
- class ::PostsController < ActionController::Base ; end
397
-
398
- get '/posts?debug_assets=true'
399
- assert_match(/<script src="\/assets\/application-([0-z]+)\.js\?body=1" type="text\/javascript"><\/script>/, last_response.body)
400
- assert_match(/<script src="\/assets\/xmlhr-([0-z]+)\.js\?body=1" type="text\/javascript"><\/script>/, last_response.body)
401
- end
402
-
403
- test "assets can access model information when precompiling" do
404
- app_file "app/models/post.rb", "class Post; end"
405
- app_file "app/assets/javascripts/application.js", "//= require_tree ."
406
- app_file "app/assets/javascripts/xmlhr.js.erb", "<%= Post.name %>"
407
-
408
- add_to_config "config.assets.digest = false"
409
- precompile!
410
- assert_equal "Post;\n", File.read("#{app_path}/public/assets/application.js")
411
- end
412
-
413
- test "assets can't access model information when precompiling if not initializing the app" do
414
- app_file "app/models/post.rb", "class Post; end"
415
- app_file "app/assets/javascripts/application.js", "//= require_tree ."
416
- app_file "app/assets/javascripts/xmlhr.js.erb", "<%= defined?(Post) || :NoPost %>"
417
-
418
- add_to_config "config.assets.digest = false"
419
- add_to_config "config.assets.initialize_on_precompile = false"
420
-
421
- precompile!
422
- assert_equal "NoPost;\n", File.read("#{app_path}/public/assets/application.js")
423
- end
424
-
425
- test "initialization on the assets group should set assets_dir" do
426
- require "#{app_path}/config/application"
427
- Rails.application.initialize!(:assets)
428
- assert_not_nil Rails.application.config.action_controller.assets_dir
429
- end
430
-
431
- test "enhancements to assets:precompile should only run once" do
432
- app_file "lib/tasks/enhance.rake", "Rake::Task['assets:precompile'].enhance { puts 'enhancement' }"
433
- output = precompile!
434
- assert_equal 1, output.scan("enhancement").size
435
- end
436
-
437
- test "digested assets are not mistakenly removed" do
438
- app_file "app/assets/application.js", "alert();"
439
- add_to_config "config.assets.compile = true"
440
- add_to_config "config.assets.digest = true"
441
-
442
- quietly do
443
- Dir.chdir(app_path){ `bundle exec rake assets:clean assets:precompile` }
444
- end
445
-
446
- files = Dir["#{app_path}/public/assets/application-*.js"]
447
- assert_equal 1, files.length, "Expected digested application.js asset to be generated, but none found"
448
- end
449
-
450
- test "digested assets are removed from configured path" do
451
- app_file "public/production_assets/application.js", "alert();"
452
- add_to_env_config "production", "config.assets.prefix = 'production_assets'"
453
-
454
- ENV["RAILS_ENV"] = nil
455
- quietly do
456
- Dir.chdir(app_path){ `bundle exec rake assets:clean` }
457
- end
458
-
459
- files = Dir["#{app_path}/public/production_assets/application.js"]
460
- assert_equal 0, files.length, "Expected application.js asset to be removed, but still exists"
461
- end
462
-
463
- test "asset urls should use the request's protocol by default" do
464
- app_with_assets_in_view
465
- add_to_config "config.asset_host = 'example.com'"
466
- require "#{app_path}/config/environment"
467
- class ::PostsController < ActionController::Base; end
468
-
469
- get '/posts', {}, {'HTTPS'=>'off'}
470
- assert_match('src="http://example.com/assets/application.js', last_response.body)
471
- get '/posts', {}, {'HTTPS'=>'on'}
472
- assert_match('src="https://example.com/assets/application.js', last_response.body)
473
- end
474
-
475
- test "asset urls should be protocol-relative if no request is in scope" do
476
- app_file "app/assets/javascripts/image_loader.js.erb", 'var src="<%= image_path("rails.png") %>";'
477
- add_to_config "config.assets.precompile = %w{image_loader.js}"
478
- add_to_config "config.asset_host = 'example.com'"
479
- precompile!
480
-
481
- assert_match 'src="//example.com/assets/rails.png"', File.read("#{app_path}/public/assets/image_loader.js")
482
- end
483
-
484
- test "asset paths should use RAILS_RELATIVE_URL_ROOT by default" do
485
- ENV["RAILS_RELATIVE_URL_ROOT"] = "/sub/uri"
486
-
487
- app_file "app/assets/javascripts/app.js.erb", 'var src="<%= image_path("rails.png") %>";'
488
- add_to_config "config.assets.precompile = %w{app.js}"
489
- precompile!
490
-
491
- assert_match 'src="/sub/uri/assets/rails.png"', File.read("#{app_path}/public/assets/app.js")
492
- end
493
-
494
- test "html assets are compiled when executing precompile" do
495
- app_file "app/assets/pages/page.html.erb", "<%= javascript_include_tag :application %>"
496
- ENV["RAILS_ENV"] = "production"
497
- ENV["RAILS_GROUP"] = "assets"
498
-
499
- quietly do
500
- Dir.chdir(app_path){ `bundle exec rake assets:precompile` }
501
- end
502
-
503
- assert File.exists?("#{app_path}/public/assets/page.html")
504
- end
505
-
506
- test "assets:cache:clean should clean cache" do
507
- ENV["RAILS_ENV"] = "production"
508
- precompile!
509
-
510
- quietly do
511
- Dir.chdir(app_path){ `bundle exec rake assets:cache:clean` }
512
- end
513
-
514
- require "#{app_path}/config/environment"
515
- assert_equal 0, Dir.entries(Rails.application.assets.cache.cache_path).size - 2 # reject [".", ".."]
516
- end
517
-
518
- private
519
-
520
- def app_with_assets_in_view
521
- app_file "app/assets/javascripts/application.js", "//= require_tree ."
522
- app_file "app/assets/javascripts/xmlhr.js", "function f1() { alert(); }"
523
- app_file "app/views/posts/index.html.erb", "<%= javascript_include_tag 'application' %>"
524
-
525
- app_file "config/routes.rb", <<-RUBY
526
- AppTemplate::Application.routes.draw do
527
- match '/posts', :to => "posts#index"
528
- end
529
- RUBY
530
- end
531
- end
532
- end