Codebase list ruby-sprockets-rails / ecbbd35
New upstream version 3.4.1 Cédric Boutillier 2 years ago
30 changed file(s) with 573 addition(s) and 318 deletion(s). Raw diff Collapse all Expand all
0 name: CI
1 on: [push, pull_request]
2 jobs:
3 tests:
4 runs-on: ubuntu-latest
5 strategy:
6 fail-fast: false
7 matrix:
8 include:
9 - ruby: 2.5
10 os: ubuntu-latest
11 gemfile: "gemfiles/Gemfile.rails-5.2.x"
12 - ruby: 2.5
13 os: ubuntu-latest
14 gemfile: "gemfiles/Gemfile.rails-5.2.x.sprockets-4.x"
15 - ruby: 2.5
16 os: ubuntu-latest
17 gemfile: "gemfiles/Gemfile.rails-6.0.x"
18 - ruby: 2.5
19 os: ubuntu-latest
20 gemfile: "gemfiles/Gemfile.rails-6.0.x.sprockets-4.x"
21 - ruby: 2.6
22 os: ubuntu-latest
23 gemfile: "gemfiles/Gemfile.rails-5.2.x"
24 - ruby: 2.6
25 os: ubuntu-latest
26 gemfile: "gemfiles/Gemfile.rails-5.2.x.sprockets-4.x"
27 - ruby: 2.6
28 os: ubuntu-latest
29 gemfile: "gemfiles/Gemfile.rails-6.0.x"
30 - ruby: 2.6
31 os: ubuntu-latest
32 gemfile: "gemfiles/Gemfile.rails-6.0.x.sprockets-4.x"
33 - ruby: 2.7
34 os: ubuntu-latest
35 gemfile: "gemfiles/Gemfile.rails-5.2.x"
36 - ruby: 2.7
37 os: ubuntu-latest
38 gemfile: "gemfiles/Gemfile.rails-5.2.x.sprockets-4.x"
39 - ruby: 2.7
40 os: ubuntu-latest
41 gemfile: "gemfiles/Gemfile.rails-6.0.x"
42 - ruby: 2.7
43 os: ubuntu-latest
44 gemfile: "gemfiles/Gemfile.rails-6.0.x.sprockets-4.x"
45 - ruby: 2.7
46 os: ubuntu-latest
47 gemfile: "gemfiles/Gemfile.rails-6.1.x.sprockets-4.x"
48 - ruby: 2.7
49 os: ubuntu-latest
50 gemfile: Gemfile
51 allow-failure: true
52 - ruby: 3.0
53 os: ubuntu-latest
54 gemfile: Gemfile
55 allow-failure: true
56 - ruby: head
57 os: ubuntu-latest
58 gemfile: Gemfile
59 allow-failure: true
60 steps:
61 - uses: actions/checkout@v2
62 - uses: actions/cache@v2
63 with:
64 path: /home/runner/bundle
65 key: bundle-use-ruby-${{ matrix.ruby }}-${{ matrix.gemfile }}-gems-${{ hashFiles(matrix.gemfile) }}-${{ hashFiles('**/*.gemspec') }}
66 restore-keys: |
67 bundle-use-ruby-${{ matrix.ruby }}-${{ matrix.gemfile }}-gems-
68 - name: Set up Ruby
69 uses: ruby/setup-ruby@v1
70 with:
71 ruby-version: ${{ matrix.ruby }}
72 - name: Bundle install
73 run: |
74 gem install bundler -v 2.1.4
75 bundle config path /home/runner/bundle
76 bundle config --global gemfile ${{ matrix.gemfile }}
77 bundle install --jobs 4 --retry 3
78 - name: Run tests
79 run: bundle exec rake
+0
-66
.travis.yml less more
0 language: ruby
1 cache: bundler
2 sudo: false
3
4 rvm:
5 - 1.9.3
6 - 2.0.0
7 - 2.1
8 - 2.2.7
9 - 2.3.4
10 - 2.4.1
11
12 gemfile:
13 - Gemfile
14 - gemfiles/Gemfile.rails-4.0.x
15 - gemfiles/Gemfile.rails-4.1.x
16 - gemfiles/Gemfile.rails-4.2.x
17 - gemfiles/Gemfile.rails-5.0.x
18 - gemfiles/Gemfile.rails-5.0.x.sprockets-4.x
19 - gemfiles/Gemfile.rails-5.1.x
20 - gemfiles/Gemfile.rails-5.1.x.sprockets-4.x
21
22 matrix:
23 exclude:
24 - gemfile: gemfiles/Gemfile.rails-4.0.x
25 rvm: 2.4.1
26 - gemfile: gemfiles/Gemfile.rails-4.1.x
27 rvm: 2.4.1
28 - gemfile: Gemfile
29 rvm: 1.9.3
30 - gemfile: Gemfile
31 rvm: 2.0.0
32 - gemfile: Gemfile
33 rvm: 2.1
34 - gemfile: gemfiles/Gemfile.rails-5.0.x
35 rvm: 1.9.3
36 - gemfile: gemfiles/Gemfile.rails-5.0.x
37 rvm: 2.0.0
38 - gemfile: gemfiles/Gemfile.rails-5.0.x
39 rvm: 2.1
40 - gemfile: gemfiles/Gemfile.rails-5.0.x.sprockets-4.x
41 rvm: 1.9.3
42 - gemfile: gemfiles/Gemfile.rails-5.0.x.sprockets-4.x
43 rvm: 2.0.0
44 - gemfile: gemfiles/Gemfile.rails-5.0.x.sprockets-4.x
45 rvm: 2.1
46 - gemfile: gemfiles/Gemfile.rails-5.1.x
47 rvm: 1.9.3
48 - gemfile: gemfiles/Gemfile.rails-5.1.x
49 rvm: 2.0.0
50 - gemfile: gemfiles/Gemfile.rails-5.1.x
51 rvm: 2.1
52 - gemfile: gemfiles/Gemfile.rails-5.1.x.sprockets-4.x
53 rvm: 1.9.3
54 - gemfile: gemfiles/Gemfile.rails-5.1.x.sprockets-4.x
55 rvm: 2.0.0
56 - gemfile: gemfiles/Gemfile.rails-5.1.x.sprockets-4.x
57 rvm: 2.1
58
59 notifications:
60 email: false
61 campfire:
62 on_success: change
63 on_failure: always
64 rooms:
65 - secure: "RkdRnCTQkmW7izb9+p9XB8pDrj7XiwHaOXQtEw29U7gXQDPqz77QzoL4cKlvMwalW77079Abp4gQpYeLFIZVm2VcVwFW3/6s0kgf3LGssbrF5fPSQnXk6GQGJBRGUyLt0TCi63Y/5DskREtEINbRfKLiDGm8ar/ExFoJzcZIsig="
+0
-9
CHANGELOG.md less more
0 # Master
1
2 ## 3.2.1
3
4 - Fix load error with `ActionView::Helpers`. [#383]
5
6 ## 3.2.0
7
8 - Add new config option `config.assets.unknown_asset_fallback` when set to a falsy value will error when a requested asset is not in the pipeline. [#375]
00 Contributing to Sprockets Rails
11 =====================
2
3 [![Build Status](https://travis-ci.org/rails/sprockets-rails.svg?branch=master)](https://travis-ci.org/rails/sprockets-rails)
42
53 Sprockets Rails is work of [many contributors](https://github.com/rails/sprockets-rails/graphs/contributors). You're encouraged to submit [pull requests](https://github.com/rails/sprockets-rails/pulls), [propose features and discuss issues](https://github.com/rails/sprockets-rails/issues).
64
00 source 'https://rubygems.org'
1 git_source(:github) { |repo| "https://github.com/#{repo}.git" }
12 gemspec
23
3 gem 'actionpack', github: 'rails/rails', branch: 'master'
4 gem 'railties', github: 'rails/rails', branch: 'master'
5 gem 'rack', github: 'rack/rack', branch: 'master'
4 gem 'actionpack', github: 'rails/rails', branch: 'main'
5 gem 'railties', github: 'rails/rails', branch: 'main'
6 gem 'rack', github: 'rack/rack', branch: 'master', ref: "e84bb296d1a16b32159608596f1f5a23b2016633"
67 gem 'sprockets', github: 'rails/sprockets', branch: '3.x'
137137 * [coffee-rails](https://github.com/rails/coffee-rails)
138138 * [sass-rails](https://github.com/rails/sass-rails)
139139
140 **NOTE** That these plugins are optional. The core coffee-script, sass, less, uglify, (any many more) features are built into Sprockets itself. Many of these plugins only provide generators and extra helpers. You can probably get by without them.
140 **NOTE** That these plugins are optional. The core coffee-script, sass, less, uglify, (and many more) features are built into Sprockets itself. Many of these plugins only provide generators and extra helpers. You can probably get by without them.
141141
142142
143143 ## Changes from Rails 3.x
148148 * The manifest file is now in a JSON format. Since it lives in public/ by default, the initial filename is also randomized to obfuscate public access to the resource.
149149 * `config.assets.manifest` (if used) must now include the manifest filename, e.g. `Rails.root.join('config/manifest.json')`. It cannot be a directory.
150150 * Two cleanup tasks: `rake assets:clean` is now a safe cleanup that only removes older assets that are no longer used, while `rake assets:clobber` nukes the entire `public/assets` directory. The clean task allows for rolling deploys that may still be linking to an old asset while the new assets are being built.
151
152 ### But what if I want sprockets to generate non-digest assets?
153
154 You have several options:
155
156 * Use the [non-digest-assets gem](https://github.com/mvz/non-digest-assets).
157 * Use the [sprockets-redirect gem](https://github.com/sikachu/sprockets-redirect).
158 * Use the [smart_assets gem](https://github.com/zarqman/smart_assets).
159 * Create [a rake task](https://github.com/rails/sprockets-rails/issues/49#issuecomment-20535134) to pre-generate a non-digest version in `public/`.
151160
152161 ## Experimental
153162
186195
187196 ## Code Status
188197
189 * [![Travis CI](https://travis-ci.org/rails/sprockets-rails.svg?branch=master)](http://travis-ci.org/rails/sprockets-rails)
190198 * [![Gem Version](https://badge.fury.io/rb/sprockets-rails.svg)](http://badge.fury.io/rb/sprockets-rails)
191 * [![Dependencies](https://gemnasium.com/rails/sprockets-rails.svg)](https://gemnasium.com/rails/sprockets-rails)
+0
-7
gemfiles/Gemfile.rails-4.0.x less more
0 source 'https://rubygems.org'
1 gemspec :path => ".."
2
3 gem "actionpack", "~> 4.0.0"
4 gem "railties", "~> 4.0.0"
5 gem "nokogiri", "< 1.7.0" if RUBY_VERSION < "2.1"
6 gem "sass", "< 3.5.0" if RUBY_VERSION < "2.0"
+0
-7
gemfiles/Gemfile.rails-4.1.x less more
0 source 'https://rubygems.org'
1 gemspec :path => ".."
2
3 gem "actionpack", "~> 4.1.0"
4 gem "railties", "~> 4.1.0"
5 gem "nokogiri", "< 1.7.0" if RUBY_VERSION < "2.1"
6 gem "sass", "< 3.5.0" if RUBY_VERSION < "2.0"
+0
-7
gemfiles/Gemfile.rails-4.2.x less more
0 source 'https://rubygems.org'
1 gemspec :path => ".."
2
3 gem 'actionpack', '~> 4.2.0'
4 gem 'railties', '~> 4.2.0'
5 gem "nokogiri", "< 1.7.0" if RUBY_VERSION < "2.1"
6 gem "sass", "< 3.5.0" if RUBY_VERSION < "2.0"
+0
-6
gemfiles/Gemfile.rails-5.0.x less more
0 source 'https://rubygems.org'
1 gemspec path: '..'
2
3 gem 'actionpack', '~> 5.0.0'
4 gem 'railties', '~> 5.0.0'
5 gem 'sprockets', github: 'rails/sprockets', branch: '3.x'
+0
-6
gemfiles/Gemfile.rails-5.0.x.sprockets-4.x less more
0 source 'https://rubygems.org'
1 gemspec path: '..'
2
3 gem 'actionpack', '~> 5.0.0'
4 gem 'railties', '~> 5.0.0'
5 gem 'sprockets', github: 'rails/sprockets', branch: 'master'
+0
-7
gemfiles/Gemfile.rails-5.1.x less more
0 source 'https://rubygems.org'
1 gemspec path: '..'
2
3 gem 'actionpack', github: 'rails/rails', branch: 'master'
4 gem 'railties', github: 'rails/rails', branch: 'master'
5 gem 'rack', github: 'rack/rack', branch: 'master'
6 gem 'sprockets', github: 'rails/sprockets', branch: '3.x'
+0
-6
gemfiles/Gemfile.rails-5.1.x.sprockets-4.x less more
0 source 'https://rubygems.org'
1 gemspec path: '..'
2
3 gem 'actionpack', '~> 5.1.0'
4 gem 'railties', '~> 5.1.0'
5 gem 'sprockets', github: 'rails/sprockets', branch: 'master'
0 source 'https://rubygems.org'
1 git_source(:github) { |repo| "https://github.com/#{repo}.git" }
2 gemspec path: '..'
3
4 gem 'actionpack', '~> 5.2.0'
5 gem 'railties', '~> 5.2.0'
6 gem 'sprockets', github: 'rails/sprockets', branch: '3.x'
0 source 'https://rubygems.org'
1 git_source(:github) { |repo| "https://github.com/#{repo}.git" }
2 gemspec path: '..'
3
4 gem 'actionpack', '~> 5.2.0'
5 gem 'railties', '~> 5.2.0'
6 gem 'sprockets', github: 'rails/sprockets', branch: 'master'
0 source 'https://rubygems.org'
1 git_source(:github) { |repo| "https://github.com/#{repo}.git" }
2 gemspec path: '..'
3
4 gem 'actionpack', '~> 6.0.0'
5 gem 'railties', '~> 6.0.0'
6 gem 'sprockets', github: 'rails/sprockets', branch: '3.x'
0 source 'https://rubygems.org'
1 git_source(:github) { |repo| "https://github.com/#{repo}.git" }
2 gemspec path: '..'
3
4 gem 'actionpack', '~> 6.0.0'
5 gem 'railties', '~> 6.0.0'
6 gem 'sprockets', github: 'rails/sprockets', branch: 'master'
0 source 'https://rubygems.org'
1 git_source(:github) { |repo| "https://github.com/#{repo}.git" }
2 gemspec path: '..'
3
4 gem 'actionpack', '~> 6.1.0'
5 gem 'railties', '~> 6.1.0'
6 gem 'sprockets', '~> 4.0.0'
0 module Sprockets
1 module Rails
2 # Rewrites urls in CSS files with the digested paths
3 class AssetUrlProcessor
4 REGEX = /url\(\s*["']?(?!(?:\#|data|http))(?<relativeToCurrentDir>.\/)?(?<path>[^"'\s)]+)\s*["']?\)/
5 def self.call(input)
6 context = input[:environment].context_class.new(input)
7 data = input[:data].gsub(REGEX) do |_match|
8 path = Regexp.last_match[:path]
9 "url(#{context.asset_path(path)})"
10 end
11
12 context.metadata.merge(data: data)
13 end
14 end
15 end
16 end
66 module Rails
77 module Helper
88 class AssetNotFound < StandardError; end
9
10 class AssetNotPrecompiled < StandardError
9 class AssetNotPrecompiled < StandardError; end
10
11 class AssetNotPrecompiledError < AssetNotPrecompiled
1112 include Sprockets::Rails::Utils
1213 def initialize(source)
1314 msg =
1415 if using_sprockets4?
1516 "Asset `#{ source }` was not declared to be precompiled in production.\n" +
1617 "Declare links to your assets in `app/assets/config/manifest.js`.\n\n" +
17 " //= link #{ source }\n" +
18 " //= link #{ source }\n\n" +
1819 "and restart your server"
1920 else
2021 "Asset was not declared to be precompiled in production.\n" +
5657 end
5758
5859 def self.extended(obj)
59 obj.class_eval do
60 obj.singleton_class.class_eval do
6061 attr_accessor(*VIEW_ACCESSORS)
6162
6263 remove_method :assets_environment
7879 if asset_path = resolve_asset_path(path, debug)
7980 File.join(assets_prefix || "/", legacy_debug_path(asset_path, debug))
8081 else
81 message = "The asset #{ path.inspect } is not present in the asset pipeline."
82 message = "The asset #{ path.inspect } is not present in the asset pipeline.\n"
8283 raise AssetNotFound, message unless unknown_asset_fallback
8384
8485 if respond_to?(:public_compute_asset_path)
350351 end
351352
352353 private
353 def find_asset(path, options = {})
354 @env[path, options]
354 if RUBY_VERSION >= "2.7"
355 class_eval <<-RUBY, __FILE__, __LINE__ + 1
356 def find_asset(path, options = {})
357 @env[path, **options]
358 end
359 RUBY
360 else
361 def find_asset(path, options = {})
362 @env[path, options]
363 end
355364 end
356365
357366 def precompiled?(path)
359368 end
360369
361370 def raise_unless_precompiled_asset(path)
362 raise Helper::AssetNotPrecompiled.new(path) if @check_precompiled_asset && !precompiled?(path)
371 raise Helper::AssetNotPrecompiledError.new(path) if @check_precompiled_asset && !precompiled?(path)
363372 end
364373 end
365374 end
0 module Sprockets
1 module Rails
2 # Rewrites source mapping urls with the digested paths and protect against semicolon appending with a dummy comment line
3 class SourcemappingUrlProcessor
4 REGEX = /\/\/# sourceMappingURL=(.*\.map)/
5
6 class << self
7 def call(input)
8 env = input[:environment]
9 context = env.context_class.new(input)
10 data = input[:data].gsub(REGEX) do |_match|
11 sourcemap_logical_path = combine_sourcemap_logical_path(sourcefile: input[:name], sourcemap: $1)
12
13 begin
14 resolved_sourcemap_comment(sourcemap_logical_path, context: context)
15 rescue Sprockets::FileNotFound
16 removed_sourcemap_comment(sourcemap_logical_path, filename: input[:filename], env: env)
17 end
18 end
19
20 { data: data }
21 end
22
23 private
24 def combine_sourcemap_logical_path(sourcefile:, sourcemap:)
25 if (parts = sourcefile.split("/")).many?
26 parts[0..-2].append(sourcemap).join("/")
27 else
28 sourcemap
29 end
30 end
31
32 def resolved_sourcemap_comment(sourcemap_logical_path, context:)
33 "//# sourceMappingURL=#{sourcemap_asset_path(sourcemap_logical_path, context: context)}\n//!\n"
34 end
35
36 def sourcemap_asset_path(sourcemap_logical_path, context:)
37 # FIXME: Work-around for bug where if the sourcemap is nested two levels deep, it'll resolve as the source file
38 # that's being mapped, rather than the map itself. So context.resolve("a/b/c.js.map") will return "c.js?"
39 if context.resolve(sourcemap_logical_path) =~ /\.map/
40 context.asset_path(sourcemap_logical_path)
41 else
42 raise Sprockets::FileNotFound, "Failed to resolve source map asset due to nesting depth"
43 end
44 end
45
46 def removed_sourcemap_comment(sourcemap_logical_path, filename:, env:)
47 env.logger.warn "Removed sourceMappingURL comment for missing asset '#{sourcemap_logical_path}' from #{filename}"
48 nil
49 end
50 end
51 end
52 end
53 end
11 require 'rake/sprocketstask'
22 require 'sprockets'
33 require 'action_view'
4 require 'action_view/base'
54
65 module Sprockets
76 module Rails
00 module Sprockets
11 module Rails
2 VERSION = "3.2.1"
2 VERSION = "3.4.1"
33 end
44 end
33 require 'active_support/core_ext/module/remove_method'
44 require 'active_support/core_ext/numeric/bytes'
55 require 'sprockets'
6
7 require 'sprockets/rails/asset_url_processor'
8 require 'sprockets/rails/sourcemapping_url_processor'
69 require 'sprockets/rails/context'
710 require 'sprockets/rails/helper'
811 require 'sprockets/rails/quiet_assets'
7578 " //= link_tree ../images\n" +
7679 " //= link_directory ../javascripts .js\n" +
7780 " //= link_directory ../stylesheets .css\n" +
78 "and restart your server"
81 "and restart your server\n\n" +
82 "For more information see: https://github.com/rails/sprockets/blob/070fc01947c111d35bb4c836e9bb71962a8e0595/UPGRADING.md#manifestjs"
7983 super msg
8084 end
8185 end
112116 if app.config.assets.quiet
113117 app.middleware.insert_before ::Rails::Rack::Logger, ::Sprockets::Rails::QuietAssets
114118 end
119 end
120
121 initializer :asset_url_processor do |app|
122 Sprockets.register_postprocessor "text/css", ::Sprockets::Rails::AssetUrlProcessor
123 end
124
125 initializer :asset_sourcemap_url_processor do |app|
126 Sprockets.register_postprocessor "application/javascript", ::Sprockets::Rails::SourcemappingUrlProcessor
115127 end
116128
117129 config.assets.version = ""
1010
1111 s.files = Dir["README.md", "lib/**/*.rb", "MIT-LICENSE"]
1212
13 s.required_ruby_version = '>= 1.9.3'
13 s.required_ruby_version = '>= 2.5'
1414
1515 s.add_dependency "sprockets", ">= 3.0.0"
16 s.add_dependency "actionpack", ">= 4.0"
17 s.add_dependency "activesupport", ">= 4.0"
18 s.add_development_dependency "railties", ">= 4.0"
16 s.add_dependency "actionpack", ">= 5.2"
17 s.add_dependency "activesupport", ">= 5.2"
18 s.add_development_dependency "railties", ">= 5.2"
1919 s.add_development_dependency "rake"
2020 s.add_development_dependency "sass"
2121 s.add_development_dependency "uglifier"
0 require 'minitest/autorun'
1 require 'sprockets/railtie'
2
3
4 Minitest::Test = MiniTest::Unit::TestCase unless defined?(Minitest::Test)
5 class TestAssetUrlProcessor < Minitest::Test
6 FIXTURES_PATH = File.expand_path("../fixtures", __FILE__)
7
8 def setup
9 @env = Sprockets::Environment.new
10 @env.append_path FIXTURES_PATH
11 @env.context_class.class_eval do
12 include ::Sprockets::Rails::Context
13 end
14 @env.context_class.digest_assets = true
15
16 @logo_digest = @env["logo.png"].etag
17 @logo_uri = @env["logo.png"].uri
18 end
19
20 def test_basic
21 input = { environment: @env, data: 'background: url(logo.png);', filename: 'url2.css', metadata: {} }
22 output = Sprockets::Rails::AssetUrlProcessor.call(input)
23 assert_equal("background: url(/logo-#{@logo_digest}.png);", output[:data])
24 end
25
26 def test_spaces
27 input = { environment: @env, data: 'background: url( logo.png );', filename: 'url2.css', metadata: {} }
28 output = Sprockets::Rails::AssetUrlProcessor.call(input)
29 assert_equal("background: url(/logo-#{@logo_digest}.png);", output[:data])
30 end
31
32 def test_single_quote
33 input = { environment: @env, data: "background: url('logo.png');", filename: 'url2.css', metadata: {} }
34 output = Sprockets::Rails::AssetUrlProcessor.call(input)
35 assert_equal("background: url(/logo-#{@logo_digest}.png);", output[:data])
36 end
37
38 def test_double_quote
39 input = { environment: @env, data: 'background: url("logo.png");', filename: 'url2.css', metadata: {} }
40 output = Sprockets::Rails::AssetUrlProcessor.call(input)
41 assert_equal("background: url(/logo-#{@logo_digest}.png);", output[:data])
42 end
43
44 def test_dependencies_are_tracked
45 input = { environment: @env, data: 'background: url(logo.png);', filename: 'url2.css', metadata: {} }
46 output = Sprockets::Rails::AssetUrlProcessor.call(input)
47 assert_equal(1, output[:links].size)
48 assert_equal(@logo_uri, output[:links].first)
49 end
50
51 def test_relative
52 input = { environment: @env, data: 'background: url(./logo.png);', filename: 'url2.css', metadata: {} }
53 output = Sprockets::Rails::AssetUrlProcessor.call(input)
54 assert_equal("background: url(/logo-#{@logo_digest}.png);", output[:data])
55 end
56
57 def test_subdirectory
58 input = { environment: @env, data: "background: url('jquery/jquery.js');", filename: 'url2.css', metadata: {} }
59 output = Sprockets::Rails::AssetUrlProcessor.call(input)
60 jquery_digest = 'c6910e1db4a5ed4905be728ab786471e81565f4a9d544734b199f3790de9f9a3'
61 assert_equal("background: url(/jquery/jquery-#{jquery_digest}.js);", output[:data])
62 end
63 end
33 require 'sprockets'
44 require 'sprockets/rails/context'
55 require 'sprockets/rails/helper'
6 require 'rails/version'
67
78 ActiveSupport::TestCase.test_order = :random if ActiveSupport::TestCase.respond_to?(:test_order=)
9
10 def append_media_attribute
11 if ::Rails::VERSION::MAJOR < 7
12 "media=\"screen\""
13 end
14 end
815
916 class HelperTest < ActionView::TestCase
1017 FIXTURES_PATH = File.expand_path("../fixtures", __FILE__)
1825 tmp = File.expand_path("../../tmp", __FILE__)
1926 @manifest = Sprockets::Manifest.new(@assets, tmp)
2027
21 @view = ActionView::Base.new
28 @view = ActionView::Base.new(ActionView::LookupContext.new([]), {}, nil)
2229 @view.extend ::Sprockets::Rails::Helper
2330 @view.assets_environment = @assets
2431 @view.assets_manifest = @manifest
105112
106113 class NoHostHelperTest < HelperTest
107114 def test_javascript_include_tag
108 assert_dom_equal %(<script src="/javascripts/static.js"></script>),
109 @view.javascript_include_tag("static")
110 assert_dom_equal %(<script src="/javascripts/static.js"></script>),
111 @view.javascript_include_tag("static.js")
112 assert_dom_equal %(<script src="/javascripts/static.js"></script>),
113 @view.javascript_include_tag(:static)
114
115 assert_dom_equal %(<script src="/elsewhere.js"></script>),
116 @view.javascript_include_tag("/elsewhere.js")
117 assert_dom_equal %(<script src="/script1.js"></script>\n<script src="/javascripts/script2.js"></script>),
118 @view.javascript_include_tag("/script1.js", "script2.js")
119
120 assert_dom_equal %(<script src="http://example.com/script"></script>),
121 @view.javascript_include_tag("http://example.com/script")
122 assert_dom_equal %(<script src="http://example.com/script.js"></script>),
123 @view.javascript_include_tag("http://example.com/script.js")
124 assert_dom_equal %(<script src="//example.com/script.js"></script>),
125 @view.javascript_include_tag("//example.com/script.js")
126
127 assert_dom_equal %(<script defer="defer" src="/javascripts/static.js"></script>),
128 @view.javascript_include_tag("static", :defer => "defer")
129 assert_dom_equal %(<script async="async" src="/javascripts/static.js"></script>),
130 @view.javascript_include_tag("static", :async => "async")
115 ActiveSupport::Deprecation.silence do
116 assert_dom_equal %(<script src="/javascripts/static.js"></script>),
117 @view.javascript_include_tag("static")
118 assert_dom_equal %(<script src="/javascripts/static.js"></script>),
119 @view.javascript_include_tag("static.js")
120 assert_dom_equal %(<script src="/javascripts/static.js"></script>),
121 @view.javascript_include_tag(:static)
122
123 assert_dom_equal %(<script src="/elsewhere.js"></script>),
124 @view.javascript_include_tag("/elsewhere.js")
125 assert_dom_equal %(<script src="/script1.js"></script>\n<script src="/javascripts/script2.js"></script>),
126 @view.javascript_include_tag("/script1.js", "script2.js")
127
128 assert_dom_equal %(<script src="http://example.com/script"></script>),
129 @view.javascript_include_tag("http://example.com/script")
130 assert_dom_equal %(<script src="http://example.com/script.js"></script>),
131 @view.javascript_include_tag("http://example.com/script.js")
132 assert_dom_equal %(<script src="//example.com/script.js"></script>),
133 @view.javascript_include_tag("//example.com/script.js")
134
135 assert_dom_equal %(<script defer="defer" src="/javascripts/static.js"></script>),
136 @view.javascript_include_tag("static", :defer => "defer")
137 assert_dom_equal %(<script async="async" src="/javascripts/static.js"></script>),
138 @view.javascript_include_tag("static", :async => "async")
139 end
131140 end
132141
133142 def test_stylesheet_link_tag
134 assert_dom_equal %(<link href="/stylesheets/static.css" media="screen" rel="stylesheet" />),
135 @view.stylesheet_link_tag("static")
136 assert_dom_equal %(<link href="/stylesheets/static.css" media="screen" rel="stylesheet" />),
137 @view.stylesheet_link_tag("static.css")
138 assert_dom_equal %(<link href="/stylesheets/static.css" media="screen" rel="stylesheet" />),
139 @view.stylesheet_link_tag(:static)
140
141 assert_dom_equal %(<link href="/elsewhere.css" media="screen" rel="stylesheet" />),
142 @view.stylesheet_link_tag("/elsewhere.css")
143 assert_dom_equal %(<link href="/style1.css" media="screen" rel="stylesheet" />\n<link href="/stylesheets/style2.css" media="screen" rel="stylesheet" />),
144 @view.stylesheet_link_tag("/style1.css", "style2.css")
145
146 assert_dom_equal %(<link href="http://www.example.com/styles/style" media="screen" rel="stylesheet" />),
147 @view.stylesheet_link_tag("http://www.example.com/styles/style")
148 assert_dom_equal %(<link href="http://www.example.com/styles/style.css" media="screen" rel="stylesheet" />),
149 @view.stylesheet_link_tag("http://www.example.com/styles/style.css")
150 assert_dom_equal %(<link href="//www.example.com/styles/style.css" media="screen" rel="stylesheet" />),
151 @view.stylesheet_link_tag("//www.example.com/styles/style.css")
152
153 assert_dom_equal %(<link href="/stylesheets/print.css" media="print" rel="stylesheet" />),
154 @view.stylesheet_link_tag("print", :media => "print")
155 assert_dom_equal %(<link href="/stylesheets/print.css" media="&lt;hax&gt;" rel="stylesheet" />),
156 @view.stylesheet_link_tag("print", :media => "<hax>")
143 ActiveSupport::Deprecation.silence do
144 assert_dom_equal %(<link href="/stylesheets/static.css" #{append_media_attribute} rel="stylesheet" />),
145 @view.stylesheet_link_tag("static")
146 assert_dom_equal %(<link href="/stylesheets/static.css" #{append_media_attribute} rel="stylesheet" />),
147 @view.stylesheet_link_tag("static.css")
148 assert_dom_equal %(<link href="/stylesheets/static.css" #{append_media_attribute} rel="stylesheet" />),
149 @view.stylesheet_link_tag(:static)
150
151 assert_dom_equal %(<link href="/elsewhere.css" #{append_media_attribute} rel="stylesheet" />),
152 @view.stylesheet_link_tag("/elsewhere.css")
153 assert_dom_equal %(<link href="/style1.css" #{append_media_attribute} rel="stylesheet" />\n<link href="/stylesheets/style2.css" #{append_media_attribute} rel="stylesheet" />),
154 @view.stylesheet_link_tag("/style1.css", "style2.css")
155
156 assert_dom_equal %(<link href="http://www.example.com/styles/style" #{append_media_attribute} rel="stylesheet" />),
157 @view.stylesheet_link_tag("http://www.example.com/styles/style")
158 assert_dom_equal %(<link href="http://www.example.com/styles/style.css" #{append_media_attribute} rel="stylesheet" />),
159 @view.stylesheet_link_tag("http://www.example.com/styles/style.css")
160 assert_dom_equal %(<link href="//www.example.com/styles/style.css" #{append_media_attribute} rel="stylesheet" />),
161 @view.stylesheet_link_tag("//www.example.com/styles/style.css")
162
163 assert_dom_equal %(<link href="/stylesheets/print.css" media="print" rel="stylesheet" />),
164 @view.stylesheet_link_tag("print", :media => "print")
165 assert_dom_equal %(<link href="/stylesheets/print.css" media="&lt;hax&gt;" rel="stylesheet" />),
166 @view.stylesheet_link_tag("print", :media => "<hax>")
167 end
157168 end
158169
159170 def test_javascript_include_tag_integrity
160 assert_dom_equal %(<script src="/javascripts/static.js" integrity="sha-256-TvVUHzSfftWg1rcfL6TIJ0XKEGrgLyEq6lEpcmrG9qs="></script>),
161 @view.javascript_include_tag("static", integrity: "sha-256-TvVUHzSfftWg1rcfL6TIJ0XKEGrgLyEq6lEpcmrG9qs=")
162
163 assert_dom_equal %(<script src="/javascripts/static.js"></script>),
164 @view.javascript_include_tag("static", integrity: true)
165 assert_dom_equal %(<script src="/javascripts/static.js"></script>),
166 @view.javascript_include_tag("static", integrity: false)
167 assert_dom_equal %(<script src="/javascripts/static.js"></script>),
168 @view.javascript_include_tag("static", integrity: nil)
171 ActiveSupport::Deprecation.silence do
172 assert_dom_equal %(<script src="/javascripts/static.js" integrity="sha-256-TvVUHzSfftWg1rcfL6TIJ0XKEGrgLyEq6lEpcmrG9qs="></script>),
173 @view.javascript_include_tag("static", integrity: "sha-256-TvVUHzSfftWg1rcfL6TIJ0XKEGrgLyEq6lEpcmrG9qs=")
174
175 assert_dom_equal %(<script src="/javascripts/static.js"></script>),
176 @view.javascript_include_tag("static", integrity: true)
177 assert_dom_equal %(<script src="/javascripts/static.js"></script>),
178 @view.javascript_include_tag("static", integrity: false)
179 assert_dom_equal %(<script src="/javascripts/static.js"></script>),
180 @view.javascript_include_tag("static", integrity: nil)
181 end
169182 end
170183
171184 def test_stylesheet_link_tag_integrity
172 assert_dom_equal %(<link href="/stylesheets/static.css" media="screen" rel="stylesheet" integrity="sha-256-5YzTQPuOJz/EpeXfN/+v1sxsjAj/dw8q26abiHZM3A4=" />),
173 @view.stylesheet_link_tag("static", integrity: "sha-256-5YzTQPuOJz/EpeXfN/+v1sxsjAj/dw8q26abiHZM3A4=")
174
175 assert_dom_equal %(<link href="/stylesheets/static.css" media="screen" rel="stylesheet" />),
176 @view.stylesheet_link_tag("static", integrity: true)
177 assert_dom_equal %(<link href="/stylesheets/static.css" media="screen" rel="stylesheet" />),
178 @view.stylesheet_link_tag("static", integrity: false)
185 ActiveSupport::Deprecation.silence do
186 assert_dom_equal %(<link href="/stylesheets/static.css" #{append_media_attribute} rel="stylesheet" integrity="sha-256-5YzTQPuOJz/EpeXfN/+v1sxsjAj/dw8q26abiHZM3A4=" />),
187 @view.stylesheet_link_tag("static", integrity: "sha-256-5YzTQPuOJz/EpeXfN/+v1sxsjAj/dw8q26abiHZM3A4=")
188
189 assert_dom_equal %(<link href="/stylesheets/static.css" #{append_media_attribute} rel="stylesheet" />),
190 @view.stylesheet_link_tag("static", integrity: true)
191 assert_dom_equal %(<link href="/stylesheets/static.css" #{append_media_attribute} rel="stylesheet" />),
192 @view.stylesheet_link_tag("static", integrity: false)
193 end
179194 end
180195
181196 def test_javascript_path
182 assert_equal "/javascripts/xmlhr.js", @view.javascript_path("xmlhr")
183 assert_equal "/javascripts/xmlhr.js", @view.javascript_path("xmlhr.js")
184 assert_equal "/javascripts/super/xmlhr.js", @view.javascript_path("super/xmlhr")
185 assert_equal "/super/xmlhr.js", @view.javascript_path("/super/xmlhr")
186
187 assert_equal "/javascripts/xmlhr.js?foo=1", @view.javascript_path("xmlhr.js?foo=1")
188 assert_equal "/javascripts/xmlhr.js?foo=1", @view.javascript_path("xmlhr?foo=1")
189 assert_equal "/javascripts/xmlhr.js#hash", @view.javascript_path("xmlhr.js#hash")
190 assert_equal "/javascripts/xmlhr.js#hash", @view.javascript_path("xmlhr#hash")
191 assert_equal "/javascripts/xmlhr.js?foo=1#hash", @view.javascript_path("xmlhr.js?foo=1#hash")
197 ActiveSupport::Deprecation.silence do
198 assert_equal "/javascripts/xmlhr.js", @view.javascript_path("xmlhr")
199 assert_equal "/javascripts/xmlhr.js", @view.javascript_path("xmlhr.js")
200 assert_equal "/javascripts/super/xmlhr.js", @view.javascript_path("super/xmlhr")
201 assert_equal "/super/xmlhr.js", @view.javascript_path("/super/xmlhr")
202
203 assert_equal "/javascripts/xmlhr.js?foo=1", @view.javascript_path("xmlhr.js?foo=1")
204 assert_equal "/javascripts/xmlhr.js?foo=1", @view.javascript_path("xmlhr?foo=1")
205 assert_equal "/javascripts/xmlhr.js#hash", @view.javascript_path("xmlhr.js#hash")
206 assert_equal "/javascripts/xmlhr.js#hash", @view.javascript_path("xmlhr#hash")
207 assert_equal "/javascripts/xmlhr.js?foo=1#hash", @view.javascript_path("xmlhr.js?foo=1#hash")
208 end
192209 end
193210
194211 def test_stylesheet_path
195 assert_equal "/stylesheets/bank.css", @view.stylesheet_path("bank")
196 assert_equal "/stylesheets/bank.css", @view.stylesheet_path("bank.css")
197 assert_equal "/stylesheets/subdir/subdir.css", @view.stylesheet_path("subdir/subdir")
198 assert_equal "/subdir/subdir.css", @view.stylesheet_path("/subdir/subdir.css")
199
200 assert_equal "/stylesheets/bank.css?foo=1", @view.stylesheet_path("bank.css?foo=1")
201 assert_equal "/stylesheets/bank.css?foo=1", @view.stylesheet_path("bank?foo=1")
202 assert_equal "/stylesheets/bank.css#hash", @view.stylesheet_path("bank.css#hash")
203 assert_equal "/stylesheets/bank.css#hash", @view.stylesheet_path("bank#hash")
204 assert_equal "/stylesheets/bank.css?foo=1#hash", @view.stylesheet_path("bank.css?foo=1#hash")
212 ActiveSupport::Deprecation.silence do
213 assert_equal "/stylesheets/bank.css", @view.stylesheet_path("bank")
214 assert_equal "/stylesheets/bank.css", @view.stylesheet_path("bank.css")
215 assert_equal "/stylesheets/subdir/subdir.css", @view.stylesheet_path("subdir/subdir")
216 assert_equal "/subdir/subdir.css", @view.stylesheet_path("/subdir/subdir.css")
217
218 assert_equal "/stylesheets/bank.css?foo=1", @view.stylesheet_path("bank.css?foo=1")
219 assert_equal "/stylesheets/bank.css?foo=1", @view.stylesheet_path("bank?foo=1")
220 assert_equal "/stylesheets/bank.css#hash", @view.stylesheet_path("bank.css#hash")
221 assert_equal "/stylesheets/bank.css#hash", @view.stylesheet_path("bank#hash")
222 assert_equal "/stylesheets/bank.css?foo=1#hash", @view.stylesheet_path("bank.css?foo=1#hash")
223 end
205224 end
206225 end
207226
213232 end
214233
215234 def test_javascript_include_tag_integrity
216 assert_dom_equal %(<script src="/javascripts/static.js"></script>),
217 @view.javascript_include_tag("static", integrity: true)
218 assert_dom_equal %(<script src="/javascripts/static.js"></script>),
219 @view.javascript_include_tag("static", integrity: false)
220 assert_dom_equal %(<script src="/javascripts/static.js"></script>),
221 @view.javascript_include_tag("static", integrity: nil)
222
223 assert_dom_equal %(<script src="/javascripts/static.js"></script>),
224 @view.javascript_include_tag("static", integrity: "sha-256-TvVUHzSfftWg1rcfL6TIJ0XKEGrgLyEq6lEpcmrG9qs=")
235 ActiveSupport::Deprecation.silence do
236 assert_dom_equal %(<script src="/javascripts/static.js"></script>),
237 @view.javascript_include_tag("static", integrity: true)
238 assert_dom_equal %(<script src="/javascripts/static.js"></script>),
239 @view.javascript_include_tag("static", integrity: false)
240 assert_dom_equal %(<script src="/javascripts/static.js"></script>),
241 @view.javascript_include_tag("static", integrity: nil)
242
243 assert_dom_equal %(<script src="/javascripts/static.js"></script>),
244 @view.javascript_include_tag("static", integrity: "sha-256-TvVUHzSfftWg1rcfL6TIJ0XKEGrgLyEq6lEpcmrG9qs=")
245 end
225246
226247 assert_dom_equal %(<script src="/assets/foo.js"></script>),
227248 @view.javascript_include_tag("foo", integrity: true)
228249 end
229250
230251 def test_stylesheet_link_tag_integrity
231 assert_dom_equal %(<link href="/stylesheets/static.css" media="screen" rel="stylesheet" />),
232 @view.stylesheet_link_tag("static", integrity: true)
233 assert_dom_equal %(<link href="/stylesheets/static.css" media="screen" rel="stylesheet" />),
234 @view.stylesheet_link_tag("static", integrity: false)
235 assert_dom_equal %(<link href="/stylesheets/static.css" media="screen" rel="stylesheet" />),
236 @view.stylesheet_link_tag("static", integrity: nil)
237
238 assert_dom_equal %(<link href="/stylesheets/static.css" media="screen" rel="stylesheet" />),
239 @view.stylesheet_link_tag("static", integrity: "sha-256-5YzTQPuOJz/EpeXfN/+v1sxsjAj/dw8q26abiHZM3A4=")
240
241 assert_dom_equal %(<link href="/assets/foo.css" media="screen" rel="stylesheet" />),
252 ActiveSupport::Deprecation.silence do
253 assert_dom_equal %(<link href="/stylesheets/static.css" #{append_media_attribute} rel="stylesheet" />),
254 @view.stylesheet_link_tag("static", integrity: true)
255 assert_dom_equal %(<link href="/stylesheets/static.css" #{append_media_attribute} rel="stylesheet" />),
256 @view.stylesheet_link_tag("static", integrity: false)
257 assert_dom_equal %(<link href="/stylesheets/static.css" #{append_media_attribute} rel="stylesheet" />),
258 @view.stylesheet_link_tag("static", integrity: nil)
259
260 assert_dom_equal %(<link href="/stylesheets/static.css" #{append_media_attribute} rel="stylesheet" />),
261 @view.stylesheet_link_tag("static", integrity: "sha-256-5YzTQPuOJz/EpeXfN/+v1sxsjAj/dw8q26abiHZM3A4=")
262 end
263
264 assert_dom_equal %(<link href="/assets/foo.css" #{append_media_attribute} rel="stylesheet" />),
242265 @view.stylesheet_link_tag("foo", integrity: true)
243266 end
244267 end
275298 def test_stylesheet_link_tag_integrity
276299 super
277300
278 assert_dom_equal %(<link href="/assets/foo.css" media="screen" rel="stylesheet" />),
301 assert_dom_equal %(<link href="/assets/foo.css" #{append_media_attribute} rel="stylesheet" />),
279302 @view.stylesheet_link_tag("foo", integrity: false)
280 assert_dom_equal %(<link href="/assets/foo.css" media="screen" rel="stylesheet" />),
303 assert_dom_equal %(<link href="/assets/foo.css" #{append_media_attribute} rel="stylesheet" />),
281304 @view.stylesheet_link_tag("foo", integrity: nil)
282305
283 assert_dom_equal %(<link href="/assets/foo.css" media="screen" rel="stylesheet" integrity="#{@foo_css_integrity}" />),
306 assert_dom_equal %(<link href="/assets/foo.css" #{append_media_attribute} rel="stylesheet" integrity="#{@foo_css_integrity}" />),
284307 @view.stylesheet_link_tag("foo", integrity: true)
285 assert_dom_equal %(<link href="/assets/foo.css" media="screen" rel="stylesheet" integrity="#{@foo_css_integrity}" />),
308 assert_dom_equal %(<link href="/assets/foo.css" #{append_media_attribute} rel="stylesheet" integrity="#{@foo_css_integrity}" />),
286309 @view.stylesheet_link_tag("foo.css", integrity: true)
287 assert_dom_equal %(<link href="/assets/foo.css" media="screen" rel="stylesheet" integrity="#{@foo_css_integrity}" />),
310 assert_dom_equal %(<link href="/assets/foo.css" #{append_media_attribute} rel="stylesheet" integrity="#{@foo_css_integrity}" />),
288311 @view.stylesheet_link_tag(:foo, integrity: true)
289312
290 assert_dom_equal %(<link href="/assets/foo.css" media="screen" rel="stylesheet" integrity="#{@foo_css_integrity}" />\n<link href="/assets/bar.css" media="screen" rel="stylesheet" integrity="sha256-Vd370+VAW4D96CVpZcjFLXyeHoagI0VHwofmzRXetuE=" />),
313 assert_dom_equal %(<link href="/assets/foo.css" #{append_media_attribute} rel="stylesheet" integrity="#{@foo_css_integrity}" />\n<link href="/assets/bar.css" #{append_media_attribute} rel="stylesheet" integrity="sha256-Vd370+VAW4D96CVpZcjFLXyeHoagI0VHwofmzRXetuE=" />),
291314 @view.stylesheet_link_tag(:foo, :bar, integrity: true)
292315 end
293316 end
300323 end
301324
302325 def test_javascript_path
303 assert_equal "https://assets.example.com/javascripts/xmlhr.js", @view.javascript_path("xmlhr")
304 assert_equal "https://assets.example.com/javascripts/xmlhr.js", @view.javascript_path("xmlhr.js")
305 assert_equal "https://assets.example.com/javascripts/super/xmlhr.js", @view.javascript_path("super/xmlhr")
306 assert_equal "https://assets.example.com/super/xmlhr.js", @view.javascript_path("/super/xmlhr")
307
308 assert_equal "https://assets.example.com/javascripts/xmlhr.js?foo=1", @view.javascript_path("xmlhr.js?foo=1")
309 assert_equal "https://assets.example.com/javascripts/xmlhr.js?foo=1", @view.javascript_path("xmlhr?foo=1")
310 assert_equal "https://assets.example.com/javascripts/xmlhr.js#hash", @view.javascript_path("xmlhr.js#hash")
311 assert_equal "https://assets.example.com/javascripts/xmlhr.js#hash", @view.javascript_path("xmlhr#hash")
312 assert_equal "https://assets.example.com/javascripts/xmlhr.js?foo=1#hash", @view.javascript_path("xmlhr.js?foo=1#hash")
326 ActiveSupport::Deprecation.silence do
327 assert_equal "https://assets.example.com/javascripts/xmlhr.js", @view.javascript_path("xmlhr")
328 assert_equal "https://assets.example.com/javascripts/xmlhr.js", @view.javascript_path("xmlhr.js")
329 assert_equal "https://assets.example.com/javascripts/super/xmlhr.js", @view.javascript_path("super/xmlhr")
330 assert_equal "https://assets.example.com/super/xmlhr.js", @view.javascript_path("/super/xmlhr")
331
332 assert_equal "https://assets.example.com/javascripts/xmlhr.js?foo=1", @view.javascript_path("xmlhr.js?foo=1")
333 assert_equal "https://assets.example.com/javascripts/xmlhr.js?foo=1", @view.javascript_path("xmlhr?foo=1")
334 assert_equal "https://assets.example.com/javascripts/xmlhr.js#hash", @view.javascript_path("xmlhr.js#hash")
335 assert_equal "https://assets.example.com/javascripts/xmlhr.js#hash", @view.javascript_path("xmlhr#hash")
336 assert_equal "https://assets.example.com/javascripts/xmlhr.js?foo=1#hash", @view.javascript_path("xmlhr.js?foo=1#hash")
337 end
313338
314339 assert_dom_equal %(<script src="https://assets.example.com/assets/foo.js"></script>),
315340 @view.javascript_include_tag("foo")
320345 end
321346
322347 def test_stylesheet_path
323 assert_equal "https://assets.example.com/stylesheets/bank.css", @view.stylesheet_path("bank")
324 assert_equal "https://assets.example.com/stylesheets/bank.css", @view.stylesheet_path("bank.css")
325 assert_equal "https://assets.example.com/stylesheets/subdir/subdir.css", @view.stylesheet_path("subdir/subdir")
326 assert_equal "https://assets.example.com/subdir/subdir.css", @view.stylesheet_path("/subdir/subdir.css")
327
328 assert_equal "https://assets.example.com/stylesheets/bank.css?foo=1", @view.stylesheet_path("bank.css?foo=1")
329 assert_equal "https://assets.example.com/stylesheets/bank.css?foo=1", @view.stylesheet_path("bank?foo=1")
330 assert_equal "https://assets.example.com/stylesheets/bank.css#hash", @view.stylesheet_path("bank.css#hash")
331 assert_equal "https://assets.example.com/stylesheets/bank.css#hash", @view.stylesheet_path("bank#hash")
332 assert_equal "https://assets.example.com/stylesheets/bank.css?foo=1#hash", @view.stylesheet_path("bank.css?foo=1#hash")
333
334 assert_dom_equal %(<link href="https://assets.example.com/assets/foo.css" media="screen" rel="stylesheet" />),
348 ActiveSupport::Deprecation.silence do
349 assert_equal "https://assets.example.com/stylesheets/bank.css", @view.stylesheet_path("bank")
350 assert_equal "https://assets.example.com/stylesheets/bank.css", @view.stylesheet_path("bank.css")
351 assert_equal "https://assets.example.com/stylesheets/subdir/subdir.css", @view.stylesheet_path("subdir/subdir")
352 assert_equal "https://assets.example.com/subdir/subdir.css", @view.stylesheet_path("/subdir/subdir.css")
353
354 assert_equal "https://assets.example.com/stylesheets/bank.css?foo=1", @view.stylesheet_path("bank.css?foo=1")
355 assert_equal "https://assets.example.com/stylesheets/bank.css?foo=1", @view.stylesheet_path("bank?foo=1")
356 assert_equal "https://assets.example.com/stylesheets/bank.css#hash", @view.stylesheet_path("bank.css#hash")
357 assert_equal "https://assets.example.com/stylesheets/bank.css#hash", @view.stylesheet_path("bank#hash")
358 assert_equal "https://assets.example.com/stylesheets/bank.css?foo=1#hash", @view.stylesheet_path("bank.css?foo=1#hash")
359 end
360
361 assert_dom_equal %(<link href="https://assets.example.com/assets/foo.css" #{append_media_attribute} rel="stylesheet" />),
335362 @view.stylesheet_link_tag("foo")
336 assert_dom_equal %(<link href="https://assets.example.com/assets/foo.css" media="screen" rel="stylesheet" />),
363 assert_dom_equal %(<link href="https://assets.example.com/assets/foo.css" #{append_media_attribute} rel="stylesheet" />),
337364 @view.stylesheet_link_tag("foo.css")
338 assert_dom_equal %(<link href="https://assets.example.com/assets/foo.css" media="screen" rel="stylesheet" />),
365 assert_dom_equal %(<link href="https://assets.example.com/assets/foo.css" #{append_media_attribute} rel="stylesheet" />),
339366 @view.stylesheet_link_tag(:foo)
340367 end
341368
344371 assert_equal "p { background: url(//assets.example.com/assets/logo.png); }\n", @assets["url.css"].to_s
345372 end
346373 end
347
348374
349375 class NoDigestHelperTest < NoHostHelperTest
350376 def setup
369395 def test_stylesheet_link_tag
370396 super
371397
372 assert_dom_equal %(<link href="/assets/foo.css" media="screen" rel="stylesheet" />),
398 assert_dom_equal %(<link href="/assets/foo.css" #{append_media_attribute} rel="stylesheet" />),
373399 @view.stylesheet_link_tag("foo")
374 assert_dom_equal %(<link href="/assets/foo.css" media="screen" rel="stylesheet" />),
400 assert_dom_equal %(<link href="/assets/foo.css" #{append_media_attribute} rel="stylesheet" />),
375401 @view.stylesheet_link_tag("foo.css")
376 assert_dom_equal %(<link href="/assets/foo.css" media="screen" rel="stylesheet" />),
402 assert_dom_equal %(<link href="/assets/foo.css" #{append_media_attribute} rel="stylesheet" />),
377403 @view.stylesheet_link_tag(:foo)
378404
379405 assert_servable_asset_url "/assets/foo.css"
425451 def test_stylesheet_link_tag
426452 super
427453
428 assert_dom_equal %(<link href="/assets/foo-#{@foo_css_digest}.css" media="screen" rel="stylesheet" />),
454 assert_dom_equal %(<link href="/assets/foo-#{@foo_css_digest}.css" #{append_media_attribute} rel="stylesheet" />),
429455 @view.stylesheet_link_tag("foo")
430 assert_dom_equal %(<link href="/assets/foo-#{@foo_css_digest}.css" media="screen" rel="stylesheet" />),
456 assert_dom_equal %(<link href="/assets/foo-#{@foo_css_digest}.css" #{append_media_attribute} rel="stylesheet" />),
431457 @view.stylesheet_link_tag("foo.css")
432 assert_dom_equal %(<link href="/assets/foo-#{@foo_css_digest}.css" media="screen" rel="stylesheet" />),
458 assert_dom_equal %(<link href="/assets/foo-#{@foo_css_digest}.css" #{append_media_attribute} rel="stylesheet" />),
433459 @view.stylesheet_link_tag(:foo)
434460
435 assert_dom_equal %(<link href="/assets/foo-#{@foo_css_digest}.css" media="screen" rel="stylesheet" />\n<link href="/assets/bar-#{@bar_css_digest}.css" media="screen" rel="stylesheet" />),
461 assert_dom_equal %(<link href="/assets/foo-#{@foo_css_digest}.css" #{append_media_attribute} rel="stylesheet" />\n<link href="/assets/bar-#{@bar_css_digest}.css" #{append_media_attribute} rel="stylesheet" />),
436462 @view.stylesheet_link_tag(:foo, :bar)
437463
438464 assert_servable_asset_url "/assets/foo-#{@foo_css_digest}.css"
460486 def test_stylesheet_link_tag_integrity
461487 super
462488
463 assert_dom_equal %(<link href="/assets/foo-#{@foo_css_digest}.css" media="screen" rel="stylesheet" />),
489 assert_dom_equal %(<link href="/assets/foo-#{@foo_css_digest}.css" #{append_media_attribute} rel="stylesheet" />),
464490 @view.stylesheet_link_tag("foo", integrity: false)
465 assert_dom_equal %(<link href="/assets/foo-#{@foo_css_digest}.css" media="screen" rel="stylesheet" />),
491 assert_dom_equal %(<link href="/assets/foo-#{@foo_css_digest}.css" #{append_media_attribute} rel="stylesheet" />),
466492 @view.stylesheet_link_tag("foo", integrity: nil)
467493
468 assert_dom_equal %(<link href="/assets/foo-#{@foo_css_digest}.css" media="screen" rel="stylesheet" integrity="#{@foo_css_integrity}" />),
494 assert_dom_equal %(<link href="/assets/foo-#{@foo_css_digest}.css" #{append_media_attribute} rel="stylesheet" integrity="#{@foo_css_integrity}" />),
469495 @view.stylesheet_link_tag("foo", integrity: true)
470 assert_dom_equal %(<link href="/assets/foo-#{@foo_css_digest}.css" media="screen" rel="stylesheet" integrity="#{@foo_css_integrity}" />),
496 assert_dom_equal %(<link href="/assets/foo-#{@foo_css_digest}.css" #{append_media_attribute} rel="stylesheet" integrity="#{@foo_css_integrity}" />),
471497 @view.stylesheet_link_tag("foo.css", integrity: true)
472 assert_dom_equal %(<link href="/assets/foo-#{@foo_css_digest}.css" media="screen" rel="stylesheet" integrity="#{@foo_css_integrity}" />),
498 assert_dom_equal %(<link href="/assets/foo-#{@foo_css_digest}.css" #{append_media_attribute} rel="stylesheet" integrity="#{@foo_css_integrity}" />),
473499 @view.stylesheet_link_tag(:foo, integrity: true)
474500
475 assert_dom_equal %(<link href="/assets/foo-#{@foo_css_digest}.css" media="screen" rel="stylesheet" integrity="#{@foo_css_integrity}" />\n<link href="/assets/bar-#{@bar_css_digest}.css" media="screen" rel="stylesheet" integrity="sha256-Vd370+VAW4D96CVpZcjFLXyeHoagI0VHwofmzRXetuE=" />),
501 assert_dom_equal %(<link href="/assets/foo-#{@foo_css_digest}.css" #{append_media_attribute} rel="stylesheet" integrity="#{@foo_css_integrity}" />\n<link href="/assets/bar-#{@bar_css_digest}.css" #{append_media_attribute} rel="stylesheet" integrity="sha256-Vd370+VAW4D96CVpZcjFLXyeHoagI0VHwofmzRXetuE=" />),
476502 @view.stylesheet_link_tag(:foo, :bar, integrity: true)
477503 end
478504
543569 super
544570
545571 if using_sprockets4?
546 assert_dom_equal %(<link href="/assets/foo.debug.css" media="screen" rel="stylesheet" />),
572 assert_dom_equal %(<link href="/assets/foo.debug.css" #{append_media_attribute} rel="stylesheet" />),
547573 @view.stylesheet_link_tag(:foo)
548 assert_dom_equal %(<link href="/assets/bar.debug.css" media="screen" rel="stylesheet" />),
574 assert_dom_equal %(<link href="/assets/bar.debug.css" #{append_media_attribute} rel="stylesheet" />),
549575 @view.stylesheet_link_tag(:bar)
550 assert_dom_equal %(<link href="/assets/file1.debug.css" media="screen" rel="stylesheet" />\n<link href="/assets/file2.debug.css" media="screen" rel="stylesheet" />),
576 assert_dom_equal %(<link href="/assets/file1.debug.css" #{append_media_attribute} rel="stylesheet" />\n<link href="/assets/file2.debug.css" #{append_media_attribute} rel="stylesheet" />),
551577 @view.stylesheet_link_tag(:file1, :file2)
552578
553579 assert_servable_asset_url "/assets/foo.self.css"
556582 assert_servable_asset_url "/assets/file1.self.css"
557583 assert_servable_asset_url "/assets/file2.self.css"
558584 else
559 assert_dom_equal %(<link href="/assets/foo.self.css?body=1" media="screen" rel="stylesheet" />),
585 assert_dom_equal %(<link href="/assets/foo.self.css?body=1" #{append_media_attribute} rel="stylesheet" />),
560586 @view.stylesheet_link_tag(:foo)
561 assert_dom_equal %(<link href="/assets/foo.self.css?body=1" media="screen" rel="stylesheet" />\n<link href="/assets/bar.self.css?body=1" media="screen" rel="stylesheet" />),
587 assert_dom_equal %(<link href="/assets/foo.self.css?body=1" #{append_media_attribute} rel="stylesheet" />\n<link href="/assets/bar.self.css?body=1" #{append_media_attribute} rel="stylesheet" />),
562588 @view.stylesheet_link_tag(:bar)
563 assert_dom_equal %(<link href="/assets/dependency.self.css?body=1" media="screen" rel="stylesheet" />\n<link href="/assets/file1.self.css?body=1" media="screen" rel="stylesheet" />\n<link href="/assets/file2.self.css?body=1" media="screen" rel="stylesheet" />),
589 assert_dom_equal %(<link href="/assets/dependency.self.css?body=1" #{append_media_attribute} rel="stylesheet" />\n<link href="/assets/file1.self.css?body=1" #{append_media_attribute} rel="stylesheet" />\n<link href="/assets/file2.self.css?body=1" #{append_media_attribute} rel="stylesheet" />),
564590 @view.stylesheet_link_tag(:file1, :file2)
565591
566592 assert_servable_asset_url "/assets/foo.self.css?body=1"
630656 super
631657
632658 if using_sprockets4?
633 assert_dom_equal %(<link href="/assets/foo.debug-#{@foo_debug_css_digest}.css" media="screen" rel="stylesheet" />),
659 assert_dom_equal %(<link href="/assets/foo.debug-#{@foo_debug_css_digest}.css" #{append_media_attribute} rel="stylesheet" />),
634660 @view.stylesheet_link_tag(:foo)
635 assert_dom_equal %(<link href="/assets/bar.debug-#{@bar_debug_css_digest}.css" media="screen" rel="stylesheet" />),
661 assert_dom_equal %(<link href="/assets/bar.debug-#{@bar_debug_css_digest}.css" #{append_media_attribute} rel="stylesheet" />),
636662 @view.stylesheet_link_tag(:bar)
637 assert_dom_equal %(<link href="/assets/file1.debug-#{@file1_debug_css_digest}.css" media="screen" rel="stylesheet" />\n<link href="/assets/file2.debug-#{@file2_debug_css_digest}.css" media="screen" rel="stylesheet" />),
663 assert_dom_equal %(<link href="/assets/file1.debug-#{@file1_debug_css_digest}.css" #{append_media_attribute} rel="stylesheet" />\n<link href="/assets/file2.debug-#{@file2_debug_css_digest}.css" #{append_media_attribute} rel="stylesheet" />),
638664 @view.stylesheet_link_tag(:file1, :file2)
639665
640666 assert_servable_asset_url "/assets/foo.self-#{@foo_self_css_digest}.css"
643669 assert_servable_asset_url "/assets/file1.self-#{@file1_self_css_digest}.css"
644670 assert_servable_asset_url "/assets/file2.self-#{@file2_self_css_digest}.css"
645671 else
646 assert_dom_equal %(<link href="/assets/foo.self-#{@foo_self_css_digest}.css?body=1" media="screen" rel="stylesheet" />),
672 assert_dom_equal %(<link href="/assets/foo.self-#{@foo_self_css_digest}.css?body=1" #{append_media_attribute} rel="stylesheet" />),
647673 @view.stylesheet_link_tag(:foo)
648 assert_dom_equal %(<link href="/assets/foo.self-#{@foo_self_css_digest}.css?body=1" media="screen" rel="stylesheet" />\n<link href="/assets/bar.self-#{@bar_self_css_digest}.css?body=1" media="screen" rel="stylesheet" />),
674 assert_dom_equal %(<link href="/assets/foo.self-#{@foo_self_css_digest}.css?body=1" #{append_media_attribute} rel="stylesheet" />\n<link href="/assets/bar.self-#{@bar_self_css_digest}.css?body=1" #{append_media_attribute} rel="stylesheet" />),
649675 @view.stylesheet_link_tag(:bar)
650 assert_dom_equal %(<link href="/assets/dependency.self-#{@dependency_self_css_digest}.css?body=1" media="screen" rel="stylesheet" />\n<link href="/assets/file1.self-#{@file1_self_css_digest}.css?body=1" media="screen" rel="stylesheet" />\n<link href="/assets/file2.self-#{@file2_self_css_digest}.css?body=1" media="screen" rel="stylesheet" />),
676 assert_dom_equal %(<link href="/assets/dependency.self-#{@dependency_self_css_digest}.css?body=1" #{append_media_attribute} rel="stylesheet" />\n<link href="/assets/file1.self-#{@file1_self_css_digest}.css?body=1" #{append_media_attribute} rel="stylesheet" />\n<link href="/assets/file2.self-#{@file2_self_css_digest}.css?body=1" #{append_media_attribute} rel="stylesheet" />),
651677 @view.stylesheet_link_tag(:file1, :file2)
652678
653679 assert_servable_asset_url "/assets/foo.self-#{@foo_self_css_digest}.css?body=1"
714740 def test_stylesheet_link_tag
715741 super
716742
717 assert_dom_equal %(<link href="/assets/foo-#{@foo_css_digest}.css" media="screen" rel="stylesheet" />),
743 assert_dom_equal %(<link href="/assets/foo-#{@foo_css_digest}.css" #{append_media_attribute} rel="stylesheet" />),
718744 @view.stylesheet_link_tag("foo")
719 assert_dom_equal %(<link href="/assets/foo-#{@foo_css_digest}.css" media="screen" rel="stylesheet" />),
745 assert_dom_equal %(<link href="/assets/foo-#{@foo_css_digest}.css" #{append_media_attribute} rel="stylesheet" />),
720746 @view.stylesheet_link_tag("foo.css")
721 assert_dom_equal %(<link href="/assets/foo-#{@foo_css_digest}.css" media="screen" rel="stylesheet" />),
747 assert_dom_equal %(<link href="/assets/foo-#{@foo_css_digest}.css" #{append_media_attribute} rel="stylesheet" />),
722748 @view.stylesheet_link_tag(:foo)
723749 end
724750
736762 def test_stylesheet_link_tag_integrity
737763 super
738764
739 assert_dom_equal %(<link href="/assets/foo-#{@foo_css_digest}.css" media="screen" rel="stylesheet" integrity="#{@foo_css_integrity}" />),
765 assert_dom_equal %(<link href="/assets/foo-#{@foo_css_digest}.css" #{append_media_attribute} rel="stylesheet" integrity="#{@foo_css_integrity}" />),
740766 @view.stylesheet_link_tag("foo", integrity: true)
741 assert_dom_equal %(<link href="/assets/foo-#{@foo_css_digest}.css" media="screen" rel="stylesheet" integrity="#{@foo_css_integrity}" />),
767 assert_dom_equal %(<link href="/assets/foo-#{@foo_css_digest}.css" #{append_media_attribute} rel="stylesheet" integrity="#{@foo_css_integrity}" />),
742768 @view.stylesheet_link_tag("foo.css", integrity: true)
743 assert_dom_equal %(<link href="/assets/foo-#{@foo_css_digest}.css" media="screen" rel="stylesheet" integrity="#{@foo_css_integrity}" />),
769 assert_dom_equal %(<link href="/assets/foo-#{@foo_css_digest}.css" #{append_media_attribute} rel="stylesheet" integrity="#{@foo_css_integrity}" />),
744770 @view.stylesheet_link_tag(:foo, integrity: true)
745771 end
746772
858884 @bundle_js_name = '/assets/bundle.js'
859885 end
860886
887 # both subclass and more specific error are supported due to
888 # https://github.com/rails/sprockets-rails/pull/414/commits/760a805a9f56d3df0d4b83bd4a5a6476eb3aeb29
861889 def test_javascript_precompile
862890 assert_raises(Sprockets::Rails::Helper::AssetNotPrecompiled) do
891 @view.javascript_include_tag("not_precompiled")
892 end
893 end
894
895 def test_javascript_precompile_thows_the_descriptive_error
896 assert_raises(Sprockets::Rails::Helper::AssetNotPrecompiledError) do
863897 @view.javascript_include_tag("not_precompiled")
864898 end
865899 end
3333 assert_equal Logger::ERROR, middleware.call("PATH_INFO" => "/assets/stylesheets/application.css")
3434 end
3535
36 def test_silencess_with_custom_prefix
36 def test_silences_with_custom_prefix
3737 Rails.application.config.assets.prefix = "path/to"
3838 assert_equal Logger::ERROR, middleware.call("PATH_INFO" => "/path/to/thing")
3939 end
3636 @app.config.time_zone = 'UTC'
3737 @app.config.middleware ||= Rails::Configuration::MiddlewareStackProxy.new
3838 @app.config.active_support.deprecation = :notify
39 ActionView::Base # load ActionView
4039
4140 Dir.chdir(app.root) do
4241 dir = "app/assets/config"
5655 def setup
5756 require 'sprockets/railtie'
5857 super
58
59 # sprockets-4.0.0.beta8 does not like 'rake assets:clobber' when this directory does not exist
60 Dir.chdir(app.root) do
61 dir = "tmp/cache/assets/sprockets"
62 FileUtils.mkdir_p(dir)
63 end
5964 end
6065
6166 def test_defaults_to_compile_assets_with_env_and_manifest_available
150155 end
151156
152157 def test_custom_compressors
153 compressor = Class.new
158 compressor = Class.new do
159 def self.call(input)
160 { data: input[:data] }
161 end
162 end
163
154164 app.configure do
155165 config.assets.configure do |env|
156166 env.register_compressor "application/javascript", :test_js, compressor
186196 def test_default_check_precompiled_assets
187197 assert app.config.assets.check_precompiled_asset
188198 app.initialize!
189 @view = ActionView::Base.new
199 @view = action_view
190200 assert @view.check_precompiled_asset
191201 end
192202
195205 config.assets.check_precompiled_asset = false
196206 end
197207 app.initialize!
198 @view = ActionView::Base.new
208 @view = action_view
199209 refute @view.check_precompiled_asset
200210 end
201211
253263 assert_equal app.assets_manifest, ActionView::Base.assets_manifest
254264 assert_kind_of Sprockets::Environment, ActionView::Base.assets_environment
255265
256 @view = ActionView::Base.new
266 @view = action_view
257267 assert_equal "/javascripts/xmlhr.js", @view.javascript_path("xmlhr")
258268 assert_equal "/assets/foo-4ef5541f349f7ed5a0d6b71f2fa4c82745ca106ae02f212aea5129726ac6f6ab.js", @view.javascript_path("foo")
259269
274284 refute ActionView::Base.assets_environment
275285 assert_equal app.assets_manifest, ActionView::Base.assets_manifest
276286
277 @view = ActionView::Base.new
287 @view = action_view
278288 refute @view.assets_environment
279289 assert_equal app.assets_manifest, @view.assets_manifest
280290 end
323333 def test_task_precompile
324334 app.configure do
325335 config.assets.paths << FIXTURES_PATH
326 config.assets.precompile += ["foo.js"]
336 config.assets.precompile += ["foo.js", "url.css"]
327337 end
328338 app.initialize!
329339 app.load_tasks
330340
331 path = "#{app.assets_manifest.dir}/foo-4ef5541f349f7ed5a0d6b71f2fa4c82745ca106ae02f212aea5129726ac6f6ab.js"
341 path = "#{app.assets_manifest.dir}/foo-#{Rails.application.assets['foo.js'].etag}.js"
332342
333343 silence_stderr do
334344 Rake.application['assets:clobber'].execute
339349 Rake.application['assets:precompile'].execute
340350 end
341351 assert File.exist?(path)
352 url_css_path = File.join(app.assets_manifest.dir, Rails.application.assets['url.css'].digest_path)
353 assert_match(%r{/assets/logo-#{Rails.application.assets['logo.png'].etag}.png}, File.read(url_css_path))
342354
343355 silence_stderr do
344356 Rake.application['assets:clobber'].execute
409421 assert middleware.include?(Sprockets::Rails::QuietAssets)
410422 assert middleware.each_cons(2).include?([Sprockets::Rails::QuietAssets, Rails::Rack::Logger])
411423 end
424
425 private
426 def action_view
427 ActionView::Base.new(ActionView::LookupContext.new([]), {}, nil)
428 end
412429 end
0 require 'minitest/autorun'
1 require 'sprockets/railtie'
2
3 Minitest::Test = MiniTest::Unit::TestCase unless defined?(Minitest::Test)
4 class TestSourceMappingUrlProcessor < Minitest::Test
5 def setup
6 @env = Sprockets::Environment.new
7 end
8
9 def test_successful
10 @env.context_class.class_eval do
11 def resolve(path, **kargs)
12 "/assets/mapped.js.map"
13 end
14
15 def asset_path(path, options = {})
16 "/assets/mapped-HEXGOESHERE.js.map"
17 end
18 end
19
20 input = { environment: @env, data: "var mapped;\n//# sourceMappingURL=mapped.js.map", name: 'mapped', filename: 'mapped.js', metadata: {} }
21 output = Sprockets::Rails::SourcemappingUrlProcessor.call(input)
22 assert_equal({ data: "var mapped;\n//# sourceMappingURL=/assets/mapped-HEXGOESHERE.js.map\n//!\n" }, output)
23 end
24
25 def test_resolving_erroneously_without_map_extension
26 @env.context_class.class_eval do
27 def resolve(path, **kargs)
28 "/assets/mapped.js"
29 end
30 end
31
32 input = { environment: @env, data: "var mapped;\n//# sourceMappingURL=mapped.js.map", name: 'mapped', filename: 'mapped.js', metadata: {} }
33 output = Sprockets::Rails::SourcemappingUrlProcessor.call(input)
34 assert_equal({ data: "var mapped;\n" }, output)
35 end
36
37 def test_missing
38 @env.context_class.class_eval do
39 def resolve(path, **kargs)
40 raise Sprockets::FileNotFound
41 end
42 end
43
44 input = { environment: @env, data: "var mapped;\n//# sourceMappingURL=mappedNOT.js.map", name: 'mapped', filename: 'mapped.js', metadata: {} }
45 output = Sprockets::Rails::SourcemappingUrlProcessor.call(input)
46 assert_equal({ data: "var mapped;\n" }, output)
47 end
48 end