Skip to content

Commit

Permalink
IndifferentHash monkeypatch warning improvements
Browse files Browse the repository at this point in the history
* Print warning on Rubies that require monkeypatching only
* Print warning to stderr instead of stdout
* Load ActiveSupport during internal testing only
* Add SINATRA_ACTIVESUPPORT_WARNING opt-out environment variable
  • Loading branch information
mwpastore committed Oct 17, 2018
1 parent b7c1064 commit 91ba82e
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 14 deletions.
17 changes: 3 additions & 14 deletions lib/sinatra/indifferent_hash.rb
@@ -1,22 +1,11 @@
# frozen_string_literal: true
if !$LOAD_PATH.grep(%r{gems/activesupport}).empty? && $LOADED_FEATURES.grep(%r{active_support/core_ext/hash}).empty?
puts <<-EOF
$stderr.puts <<EOF if !Hash.method_defined?(:slice) && !$LOAD_PATH.grep(%r{gems/activesupport}).empty? && ENV['SINATRA_ACTIVESUPPORT_WARNING'] != 'false'
WARNING: If you plan to load any of ActiveSupport's core extensions to Hash, be
sure to do so *before* loading Sinatra::Application or Sinatra::Base. If not,
you may disregard this warning.
EOF
end
if ENV['APP_ENV'] == 'test' && !Hash.method_defined?(:slice)
# Some extensions get loaded during testing (e.g. by RABL and our RABL test)
# that we have no control over, but we need it to load *before*
# IndifferentHash, so we'll do it preemptively here.
#
# Newer Rubies have these methods built-in, so the extensions are no-ops.
require 'active_support/core_ext/hash/conversions'
require 'active_support/core_ext/hash/slice'
require 'active_support/core_ext/hash/keys'
end
Set SINATRA_ACTIVESUPPORT_WARNING=false in the environment to hide this warning.
EOF

module Sinatra
# A poor man's ActiveSupport::HashWithIndifferentAccess, with all the Rails-y
Expand Down
12 changes: 12 additions & 0 deletions test/helper.rb
Expand Up @@ -23,6 +23,18 @@
require 'minitest'
require 'contest'
require 'rack/test'

# Some of ActiveSupport's core extensions to Hash get loaded during internal
# testing (e.g. by RABL and our RABL test) that we have no control over, but we
# need them to load *before* Sinatra::IndifferentHash (which is itself loaded
# by Sinatra::Base) whenever the full test suite is executed, so we'll do it
# preemptively here.
#
# Newer Rubies have these methods built-in, so the extensions are no-ops.
require 'active_support/core_ext/hash/conversions'
require 'active_support/core_ext/hash/slice'
require 'active_support/core_ext/hash/keys'

require 'sinatra/base'

class Sinatra::Base
Expand Down
4 changes: 4 additions & 0 deletions test/indifferent_hash_test.rb
Expand Up @@ -4,6 +4,10 @@
#
require 'minitest/autorun' unless defined?(Minitest)

# Suppress the ActiveSupport warning when this test is executed independently,
# outside of the full suite, on older Rubies.
ENV['SINATRA_ACTIVESUPPORT_WARNING'] = 'false'

require_relative '../lib/sinatra/indifferent_hash'

class TestIndifferentHashBasics < Minitest::Test
Expand Down

0 comments on commit 91ba82e

Please sign in to comment.