Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IndifferentHash monkeypatch warning improvements #1477

Merged
merged 1 commit into from
Nov 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
IndifferentHash monkeypatch warning improvements
* 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
commit 91ba82ec512351db9438e5001fcbbe48f30f5bfd
17 changes: 3 additions & 14 deletions lib/sinatra/indifferent_hash.rb
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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