1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

[ruby/erb] Remove safe_level and further positional arguments (https://github.com/ruby/erb/pull/7)

[Feature #14256]

https://github.com/ruby/erb/commit/5133efa06f
This commit is contained in:
Takashi Kokubun 2022-01-15 13:46:43 -08:00 committed by git
parent 3cfb6fc479
commit d12a08abb5
3 changed files with 13 additions and 91 deletions

View file

@ -808,20 +808,7 @@ class ERB
# Chicken Fried Steak -- 9.95
# A well messages pattie, breaded and fried.
#
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
end
if legacy_trim_mode != NOT_GIVEN
warn 'Passing trim_mode with the 3rd argument of ERB.new is deprecated. Use keyword argument like ERB.new(str, trim_mode: ...) instead.', uplevel: 1
trim_mode = legacy_trim_mode
end
if legacy_eoutvar != NOT_GIVEN
warn 'Passing eoutvar with the 4th argument of ERB.new is deprecated. Use keyword argument like ERB.new(str, eoutvar: ...) instead.', uplevel: 1
eoutvar = legacy_eoutvar
end
def initialize(str, trim_mode: nil, eoutvar: '_erbout')
compiler = make_compiler(trim_mode)
set_eoutvar(compiler, eoutvar)
@src, @encoding, @frozen_string = *compiler.compile(str)

View file

@ -24,29 +24,6 @@ class TestERB < Test::Unit::TestCase
assert_match(/\Atest filename:1\b/, e.backtrace[0])
end
# [deprecated] This will be removed later
def test_without_filename_with_safe_level
erb = EnvUtil.suppress_warning do
ERB.new("<% raise ::TestERB::MyError %>", 1)
end
e = assert_raise(MyError) {
erb.result
}
assert_match(/\A\(erb\):1\b/, e.backtrace[0])
end
# [deprecated] This will be removed later
def test_with_filename_and_safe_level
erb = EnvUtil.suppress_warning do
ERB.new("<% raise ::TestERB::MyError %>", 1)
end
erb.filename = "test filename"
e = assert_raise(MyError) {
erb.result
}
assert_match(/\Atest filename:1\b/, e.backtrace[0])
end
def test_with_filename_lineno
erb = ERB.new("<% raise ::TestERB::MyError %>")
erb.filename = "test filename"
@ -98,25 +75,16 @@ class TestERBCore < Test::Unit::TestCase
end
def test_core
# [deprecated] Fix initializer later
EnvUtil.suppress_warning do
_test_core(nil)
_test_core(0)
_test_core(1)
end
end
def _test_core(safe)
erb = @erb.new("hello")
assert_equal("hello", erb.result)
erb = @erb.new("hello", safe, 0)
erb = @erb.new("hello", trim_mode: 0)
assert_equal("hello", erb.result)
erb = @erb.new("hello", safe, 1)
erb = @erb.new("hello", trim_mode: 1)
assert_equal("hello", erb.result)
erb = @erb.new("hello", safe, 2)
erb = @erb.new("hello", trim_mode: 2)
assert_equal("hello", erb.result)
src = <<EOS
@ -144,9 +112,9 @@ EOS
EOS
erb = @erb.new(src)
assert_equal(ans, erb.result)
erb = @erb.new(src, safe, 0)
erb = @erb.new(src, trim_mode: 0)
assert_equal(ans, erb.result)
erb = @erb.new(src, safe, '')
erb = EnvUtil.suppress_warning { @erb.new(src, trim_mode: '') }
assert_equal(ans, erb.result)
ans = <<EOS
@ -157,9 +125,9 @@ EOS
* 1% n=0
* 2
EOS
erb = @erb.new(src, safe, 1)
erb = @erb.new(src, trim_mode: 1)
assert_equal(ans.chomp, erb.result)
erb = @erb.new(src, safe, '>')
erb = @erb.new(src, trim_mode: '>')
assert_equal(ans.chomp, erb.result)
ans = <<EOS
@ -173,9 +141,9 @@ EOS
* 2
EOS
erb = @erb.new(src, safe, 2)
erb = @erb.new(src, trim_mode: 2)
assert_equal(ans, erb.result)
erb = @erb.new(src, safe, '<>')
erb = @erb.new(src, trim_mode: '<>')
assert_equal(ans, erb.result)
ans = <<EOS
@ -189,7 +157,7 @@ EOS
* 0
EOS
erb = @erb.new(src, safe, '%')
erb = @erb.new(src, trim_mode: '%')
assert_equal(ans, erb.result)
ans = <<EOS
@ -197,7 +165,7 @@ EOS
= hello
* 0* 0* 0
EOS
erb = @erb.new(src, safe, '%>')
erb = @erb.new(src, trim_mode: '%>')
assert_equal(ans.chomp, erb.result)
ans = <<EOS
@ -207,7 +175,7 @@ EOS
* 0
* 0
EOS
erb = @erb.new(src, safe, '%<>')
erb = @erb.new(src, trim_mode: '%<>')
assert_equal(ans, erb.result)
end
@ -661,27 +629,6 @@ EOS
end
end
# [deprecated] These interfaces will be removed later
def test_deprecated_interface_warnings
[nil, 0, 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_warn(/3rd argument of ERB.new is deprecated/) do
ERB.new('', nil, trim)
end
end
[nil, '_erbout', '_hamlout'].each do |eoutvar|
assert_warn(/4th argument of ERB.new is deprecated/) do
ERB.new('', nil, nil, eoutvar)
end
end
end
def test_prohibited_marshal_dump
erb = ERB.new("")
assert_raise(TypeError) {Marshal.dump(erb)}

View file

@ -15,16 +15,4 @@ class TestErbCommand < Test::Unit::TestCase
File.expand_path("../../libexec/erb", __dir__)],
"<%=''.encoding.to_s%>", ["UTF-8"])
end
# These interfaces will be removed at Ruby 2.7.
def test_deprecated_option
warnings = [
"warning: -S option of erb command is deprecated. Please do not use this.",
/\n.+\/libexec\/erb:\d+: warning: Passing safe_level with the 2nd argument of ERB\.new is deprecated\. Do not use it, and specify other arguments as keyword arguments\.\n/,
]
assert_in_out_err(["-I#{File.expand_path('../../lib', __dir__)}", "-w",
File.expand_path("../../libexec/erb", __dir__),
"-S", "0"],
"hoge", ["hoge"], warnings)
end
end