File: test_quiet_assets.rb

package info (click to toggle)
ruby-sprockets-rails 3.4.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 436 kB
  • sloc: ruby: 2,071; makefile: 4; javascript: 4
file content (51 lines) | stat: -rw-r--r-- 1,448 bytes parent folder | download
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
require 'active_support'
require 'active_support/testing/isolation'
require 'active_support/log_subscriber/test_helper'
require 'minitest/autorun'

require 'sprockets/railtie'
require 'rails'

Minitest::Test = MiniTest::Unit::TestCase unless defined?(Minitest::Test)

class TestQuietAssets < Minitest::Test
  include ActiveSupport::Testing::Isolation

  ROOT_PATH = Pathname.new(File.expand_path("../../tmp/app", __FILE__))
  ASSET_PATH = ROOT_PATH.join("app","assets", "config")

  def setup
    FileUtils.mkdir_p(ROOT_PATH)
    Dir.chdir(ROOT_PATH)

    @app = Class.new(Rails::Application)
    @app.config.eager_load = false
    @app.config.logger = ActiveSupport::Logger.new("/dev/null")

    FileUtils.mkdir_p(ASSET_PATH)
    File.open(ASSET_PATH.join("manifest.js"), "w") { |f| f << "" }

    @app.initialize!

    Rails.logger.level = Logger::DEBUG
  end

  def test_silences_with_default_prefix
    assert_equal Logger::ERROR, middleware.call("PATH_INFO" => "/assets/stylesheets/application.css")
  end

  def test_silences_with_custom_prefix
    Rails.application.config.assets.prefix = "path/to"
    assert_equal Logger::ERROR, middleware.call("PATH_INFO" => "/path/to/thing")
  end

  def test_does_not_silence_without_match
    assert_equal Logger::DEBUG, middleware.call("PATH_INFO" => "/path/to/thing")
  end

  private

  def middleware
    @middleware ||= Sprockets::Rails::QuietAssets.new(->(env) { Rails.logger.level })
  end
end