assets_test.rb 4.7 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 40 41 42 43 44 45 46 47 48
    test "assets do not require compressors until it is used" do
      app_file "app/assets/javascripts/demo.js.erb", "<%= :alert %>();"
      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 已提交
49
    test "precompile creates the file, gives it the original asset's content and run in production as default" do
50
      app_file "app/assets/javascripts/application.js", "alert();"
51
      app_file "app/assets/javascripts/foo/application.js", "alert();"
52

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

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

68 69 70 71 72 73 74 75 76 77 78
      # 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
79
      capture(:stdout) do
80
        Dir.chdir(app_path){ `bundle exec rake assets:precompile RAILS_GROUPS=assets` }
81
      end
82
      file = Dir["#{app_path}/public/assets/application-8d301a938f1abfd789bbec87ed1ef770.css"].first
83 84 85
      assert_match /\/assets\/rails-([0-z]+)\.png/, File.read(file)
    end

86 87 88 89 90 91 92 93 94
    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

95
      files = Dir["#{app_path}/public/assets/**/*", "#{app_path}/tmp/cache/*"]
96 97 98
      assert_equal 0, files.length, "Expected no assets, but found #{files.join(', ')}"
    end

J
José Valim 已提交
99
    test "does not stream session cookies back" do
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
      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 已提交
123
    end
124 125 126 127 128 129 130 131 132 133 134 135 136 137

    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
138 139
  end
end