aboutsummaryrefslogtreecommitdiffstats
path: root/railties/test/application/asset_debugging_test.rb
blob: 128040262f1116eee52ca01d085f142c4589575b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# frozen_string_literal: true

require "isolation/abstract_unit"
require "rack/test"

module ApplicationTests
  class AssetDebuggingTest < ActiveSupport::TestCase
    include ActiveSupport::Testing::Isolation
    include Rack::Test::Methods

    def setup
      # FIXME: shush Sass warning spam, not relevant to testing Railties
      Kernel.silence_warnings do
        build_app(initializers: true)
      end

      app_file "app/assets/javascripts/application.js", "//= require_tree ."
      app_file "app/assets/javascripts/xmlhr.js", "function f1() { alert(); }"
      app_file "app/views/posts/index.html.erb", "<%= javascript_include_tag 'application' %>"

      app_file "config/routes.rb", <<-RUBY
        Rails.application.routes.draw do
          get '/posts', to: "posts#index"
        end
      RUBY

      app_file "app/controllers/posts_controller.rb", <<-RUBY
        class PostsController < ActionController::Base
        end
      RUBY

      ENV["RAILS_ENV"] = "production"
    end

    def teardown
      teardown_app
    end

    # FIXME: shush Sass warning spam, not relevant to testing Railties
    def get(*)
      Kernel.silence_warnings { super }
    end

    test "assets are concatenated when debug is off and compile is off either if debug_assets param is provided" do
      # config.assets.debug and config.assets.compile are false for production environment
      ENV["RAILS_ENV"] = "production"
      output = Dir.chdir(app_path) { `bin/rails assets:precompile --trace 2>&1` }
      assert $?.success?, output

      # Load app env
      app "production"

      class ::PostsController < ActionController::Base ; end

      # the debug_assets params isn't used if compile is off
      get "/posts?debug_assets=true"
      assert_match(/<script src="\/assets\/application-([0-z]+)\.js"><\/script>/, last_response.body)
      assert_no_match(/<script src="\/assets\/xmlhr-([0-z]+)\.js"><\/script>/, last_response.body)
    end

    test "assets aren't concatenated when compile is true is on and debug_assets params is true" do
      add_to_env_config "production", "config.assets.compile = true"

      # Load app env
      app "production"

      class ::PostsController < ActionController::Base ; end

      get "/posts?debug_assets=true"
      assert_match(/<script src="\/assets\/application(\.self)?-([0-z]+)\.js\?body=1"><\/script>/, last_response.body)
      assert_match(/<script src="\/assets\/xmlhr(\.self)?-([0-z]+)\.js\?body=1"><\/script>/, last_response.body)
    end

    test "public path and tag methods are not over-written by the asset pipeline" do
      contents = "doesnotexist"
      cases = {
        asset_path:             %r{/#{contents}},
        image_path:             %r{/images/#{contents}},
        video_path:             %r{/videos/#{contents}},
        audio_path:             %r{/audios/#{contents}},
        font_path:              %r{/fonts/#{contents}},
        javascript_path:        %r{/javascripts/#{contents}},
        stylesheet_path:        %r{/stylesheets/#{contents}},
        image_tag:              %r{<img src="/images/#{contents}"},
        favicon_link_tag:       %r{<link rel="shortcut icon" type="image/x-icon" href="/images/#{contents}" />},
        stylesheet_link_tag:    %r{<link rel="stylesheet" media="screen" href="/stylesheets/#{contents}.css" />},
        javascript_include_tag: %r{<script src="/javascripts/#{contents}.js">},
        audio_tag:              %r{<audio src="/audios/#{contents}"></audio>},
        video_tag:              %r{<video src="/videos/#{contents}"></video>}
      }

      cases.each do |(view_method, tag_match)|
        app_file "app/views/posts/index.html.erb", "<%= #{view_method} '#{contents}', skip_pipeline: true %>"

        app "development"

        class ::PostsController < ActionController::Base ; end

        get "/posts?debug_assets=true"

        body = last_response.body
        assert_match(tag_match, body, "Expected `#{view_method}` to produce a match to #{tag_match}, but did not: #{body}")
      end
    end

    test "public url methods are not over-written by the asset pipeline" do
      contents = "doesnotexist"
      cases = {
        asset_url:       %r{http://example.org/#{contents}},
        image_url:       %r{http://example.org/images/#{contents}},
        video_url:       %r{http://example.org/videos/#{contents}},
        audio_url:       %r{http://example.org/audios/#{contents}},
        font_url:        %r{http://example.org/fonts/#{contents}},
        javascript_url:  %r{http://example.org/javascripts/#{contents}},
        stylesheet_url:  %r{http://example.org/stylesheets/#{contents}},
      }

      cases.each do |(view_method, tag_match)|
        app_file "app/views/posts/index.html.erb", "<%= #{view_method} '#{contents}', skip_pipeline: true %>"

        app "development"

        class ::PostsController < ActionController::Base ; end

        get "/posts?debug_assets=true"

        body = last_response.body
        assert_match(tag_match, body, "Expected `#{view_method}` to produce a match to #{tag_match}, but did not: #{body}")
      end
    end

    test "{ skip_pipeline: true } does not use the asset pipeline" do
      cases = {
        /\/assets\/application-.*.\.js/ => {},
        /application.js/                => { skip_pipeline: true },
      }
      cases.each do |(tag_match, options_hash)|
        app_file "app/views/posts/index.html.erb", "<%= asset_path('application.js', #{options_hash}) %>"

        app "development"

        class ::PostsController < ActionController::Base ; end

        get "/posts?debug_assets=true"

        body = last_response.body.strip
        assert_match(tag_match, body, "Expected `asset_path` with `#{options_hash}` to produce a match to #{tag_match}, but did not: #{body}")
      end
    end

    test "public_compute_asset_path does not use the asset pipeline" do
      cases = {
        compute_asset_path:        /\/assets\/application-.*.\.js/,
        public_compute_asset_path: /application.js/,
      }

      cases.each do |(view_method, tag_match)|
        app_file "app/views/posts/index.html.erb", "<%= #{ view_method } 'application.js' %>"

        app "development"

        class ::PostsController < ActionController::Base ; end

        get "/posts?debug_assets=true"

        body = last_response.body.strip
        assert_match(tag_match, body, "Expected `#{view_method}` to produce a match to #{ tag_match }, but did not: #{ body }")
      end
    end
  end
end