From 1ae3e6f3ce9f16c49d0584e0ad3dc3e709cc4a81 Mon Sep 17 00:00:00 2001 From: k0kubun Date: Sat, 29 Dec 2018 07:26:22 +0000 Subject: [PATCH] erb.rb: increase warn level only when non-zero safe_level is given. This is merging Eric's patch in [Bug #15479] to Ruby 2.6's behavior in r66631. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@66632 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- lib/erb.rb | 4 +++- test/erb/test_erb.rb | 8 +++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/erb.rb b/lib/erb.rb index 43498cc106..2adc5e0e58 100644 --- a/lib/erb.rb +++ b/lib/erb.rb @@ -814,7 +814,7 @@ class ERB def initialize(str, safe_level=NOT_GIVEN, legacy_trim_mode=NOT_GIVEN, legacy_eoutvar=NOT_GIVEN, trim_mode: nil, eoutvar: '_erbout') # Complex initializer for $SAFE deprecation at [Feature #14256]. Use keyword arguments to pass trim_mode or eoutvar. if safe_level != NOT_GIVEN - warn 'Passing safe_level with the 2nd argument of ERB.new is deprecated. Do not use it, and specify other arguments as keyword arguments.', uplevel: 1 if $VERBOSE + warn 'Passing safe_level with the 2nd argument of ERB.new is deprecated. Do not use it, and specify other arguments as keyword arguments.', uplevel: 1 if $VERBOSE || !ZERO_SAFE_LEVELS.include?(safe_level) else safe_level = nil end @@ -836,6 +836,8 @@ class ERB end NOT_GIVEN = Object.new private_constant :NOT_GIVEN + ZERO_SAFE_LEVELS = [0, nil] + private_constant :ZERO_SAFE_LEVELS ## # Creates a new compiler for ERB. See ERB::Compiler.new for details diff --git a/test/erb/test_erb.rb b/test/erb/test_erb.rb index 5342df4ae8..9af7e11ef6 100644 --- a/test/erb/test_erb.rb +++ b/test/erb/test_erb.rb @@ -663,12 +663,18 @@ EOS # [deprecated] These interfaces will be removed later def test_deprecated_interface_warnings - [nil, 0, 1, 2].each do |safe| + [nil, 0].each do |safe| assert_warning(/2nd argument of ERB.new is deprecated/) do ERB.new('', safe) end end + [1, 2].each do |safe| + assert_warn(/2nd argument of ERB.new is deprecated/) do + ERB.new('', safe) + end + end + [nil, '', '%', '%<>'].each do |trim| assert_warning(/3rd argument of ERB.new is deprecated/) do ERB.new('', nil, trim)