assets_test.rb 10.6 KB
Newer Older
1
require 'isolation/abstract_unit'
2
require 'active_support/core_ext/kernel/reporting'
3 4 5
require 'rack/test'

module ApplicationTests
6
  class AssetsTest < Test::Unit::TestCase
7 8 9 10
    include ActiveSupport::Testing::Isolation
    include Rack::Test::Methods

    def setup
11
      build_app(:initializers => true)
12 13 14
      boot_rails
    end

15 16 17 18
    def teardown
      teardown_app
    end

J
José Valim 已提交
19 20 21 22
    def app
      @app ||= Rails.application
    end

23 24 25 26 27 28 29 30 31
    test "assets routes have higher priority" do
      app_file "app/assets/javascripts/demo.js.erb", "<%= :alert %>();"

      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

32 33
      require "#{app_path}/config/environment"

34 35 36
      get "/assets/demo.js"
      assert_match "alert()", last_response.body
    end
J
José Valim 已提交
37

J
José Valim 已提交
38 39
    test "assets do not require compressors until it is used" do
      app_file "app/assets/javascripts/demo.js.erb", "<%= :alert %>();"
40
      add_to_config "config.assets.compile = true"
41

J
José Valim 已提交
42 43 44 45 46 47 48 49 50
      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

S
Santiago Pastorino 已提交
51
    test "precompile creates the file, gives it the original asset's content and run in production as default" do
52
      app_file "app/assets/javascripts/application.js", "alert();"
53
      app_file "app/assets/javascripts/foo/application.js", "alert();"
54

55
      ENV["RAILS_ENV"] = nil
56 57 58
      capture(:stdout) do
        Dir.chdir(app_path){ `bundle exec rake assets:precompile` }
      end
J
Jon Leighton 已提交
59 60
      files = Dir["#{app_path}/public/assets/application-*.js"]
      files << Dir["#{app_path}/public/assets/foo/application-*.js"].first
61 62
      files.each do |file|
        assert_not_nil file, "Expected application.js asset to be generated, but none found"
63
        assert_equal "alert()", File.read(file)
64
      end
65 66
    end

67
    test "asset pipeline should use a Sprockets::Index when config.assets.digest is true" do
68 69 70
      add_to_config "config.assets.digest = true"
      add_to_config "config.action_controller.perform_caching = false"

71 72 73 74 75 76
      ENV["RAILS_ENV"] = "production"
      require "#{app_path}/config/environment"

      assert_equal Sprockets::Index, Rails.application.assets.class
    end

77
    test "precompile creates a manifest file with all the assets listed" do
78 79
      app_file "app/assets/stylesheets/application.css.erb", "<%= asset_path('rails.png') %>"
      app_file "app/assets/javascripts/application.js", "alert();"
80
      # digest is default in false, we must enable it for test environment
81
      add_to_config "config.assets.digest = true"
82 83 84 85 86 87 88 89

      capture(:stdout) do
        Dir.chdir(app_path){ `bundle exec rake assets:precompile` }
      end

      manifest = "#{app_path}/public/assets/manifest.yml"

      assets = YAML.load_file(manifest)
90 91
      assert_match(/application-([0-z]+)\.js/, assets["application.js"])
      assert_match(/application-([0-z]+)\.css/, assets["application.css"])
92 93
    end

94 95 96
    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();"
97
      # digest is default in false, we must enable it for test environment
98 99 100
      add_to_config "config.assets.digest = true"
      add_to_config "config.assets.manifest = '#{app_path}/shared'"
      FileUtils.mkdir "#{app_path}/shared"
101 102 103 104 105 106 107 108

      capture(:stdout) do
        Dir.chdir(app_path){ `bundle exec rake assets:precompile` }
      end

      manifest = "#{app_path}/shared/manifest.yml"

      assets = YAML.load_file(manifest)
109 110
      assert_match(/application-([0-z]+)\.js/, assets["application.js"])
      assert_match(/application-([0-z]+)\.css/, assets["application.css"])
111 112
    end

113

114 115
    test "the manifest file should be saved by default in the same assets folder" do
      app_file "app/assets/javascripts/application.js", "alert();"
116
      # digest is default in false, we must enable it for test environment
117 118
      add_to_config "config.assets.digest = true"
      add_to_config "config.assets.prefix = '/x'"
119 120 121 122 123 124 125

      capture(:stdout) do
        Dir.chdir(app_path){ `bundle exec rake assets:precompile` }
      end

      manifest = "#{app_path}/public/x/manifest.yml"
      assets = YAML.load_file(manifest)
126
      assert_match(/application-([0-z]+)\.js/, assets["application.js"])
127 128
    end

129 130 131
    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();"
132
      add_to_config "config.assets.digest = false"
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147

      capture(:stdout) do
        Dir.chdir(app_path){ `bundle exec rake assets:precompile` }
      end

      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

148
    test "assets do not require any assets group gem when manifest file is present" do
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
      app_file "app/assets/javascripts/application.js", "alert();"

      ENV["RAILS_ENV"] = "production"
      capture(:stdout) do
        Dir.chdir(app_path){ `bundle exec rake assets:precompile` }
      end
      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

168
    test "assets raise AssetNotPrecompiledError when manifest file is present and requested file isn't precompiled" do
169 170 171 172 173 174 175 176 177
      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"
178 179 180 181 182 183 184
      capture(:stdout) do
        Dir.chdir(app_path){ `bundle exec rake assets:precompile` }
      end

      # Create file after of precompile
      app_file "app/assets/javascripts/app.js", "alert();"

185 186 187 188
      require "#{app_path}/config/environment"
      class ::PostsController < ActionController::Base ; end

      get '/posts'
189
      assert_match(/AssetNotPrecompiledError/, last_response.body)
190 191 192 193 194
      assert_match(/app.js isn'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' %>"
195
      add_to_config "config.assets.compile = false"
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215

      app_file "config/routes.rb", <<-RUBY
        AppTemplate::Application.routes.draw do
          match '/posts', :to => "posts#index"
        end
      RUBY

      ENV["RAILS_ENV"] = "development"
      capture(:stdout) do
        Dir.chdir(app_path){ `bundle exec rake assets:precompile` }
      end

      # Create file after of precompile
      app_file "app/assets/javascripts/app.js", "alert();"

      require "#{app_path}/config/environment"
      class ::PostsController < ActionController::Base ; end

      get '/posts'
      assert_match(/AssetNotPrecompiledError/, last_response.body)
216
      assert_match(/app.js isn't precompiled/, last_response.body)
217 218
    end

219
    test "precompile appends the md5 hash to files referenced with asset_path and run in the provided RAILS_ENV" do
220
      app_file "app/assets/stylesheets/application.css.erb", "<%= asset_path('rails.png') %>"
221
      # digest is default in false, we must enable it for test environment
222
      add_to_config "config.assets.digest = true"
223

224 225 226
      # capture(:stdout) do
        Dir.chdir(app_path){ `bundle exec rake assets:precompile RAILS_ENV=test` }
      # end
J
Jon Leighton 已提交
227
      file = Dir["#{app_path}/public/assets/application-*.css"].first
228
      assert_match(/\/assets\/rails-([0-z]+)\.png/, File.read(file))
229 230 231 232
    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') %>"
233
      add_to_config "config.assets.compile = true"
234 235

      ENV["RAILS_ENV"] = nil
236
      capture(:stdout) do
237
        Dir.chdir(app_path){ `bundle exec rake assets:precompile RAILS_GROUPS=assets` }
238
      end
J
Jon Leighton 已提交
239
      file = Dir["#{app_path}/public/assets/application-*.css"].first
240
      assert_match(/\/assets\/rails-([0-z]+)\.png/, File.read(file))
241 242
    end

243 244 245 246 247 248 249 250 251
    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"

      capture(:stdout) do
        Dir.chdir(app_path){ `bundle exec rake assets:clean` }
      end

252
      files = Dir["#{app_path}/public/assets/**/*", "#{app_path}/tmp/cache/*"]
253 254 255
      assert_equal 0, files.length, "Expected no assets, but found #{files.join(', ')}"
    end

J
José Valim 已提交
256
    test "does not stream session cookies back" do
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
      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"]
J
José Valim 已提交
280
    end
281 282 283 284 285 286 287 288 289 290 291 292 293 294

    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
295 296
  end
end