mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
[ruby/erb] Revert "Remove safe_level and further positional arguments (https://github.com/ruby/erb/pull/7)"
This reverts commit5133efa06f
. While we already handled this deprecation in many libraries, we noticed that some (e.g. sprockets) relied on the format of `ERB.version` and2b4182eb10
broke such handling. Given that the `ERB.version` change was released at 3.1 and it's obviously new, I'll skip this removal in 3.2 and postpone this to a future version.
This commit is contained in:
parent
f3c77bd480
commit
c0d18a1aa2
4 changed files with 91 additions and 18 deletions
5
NEWS.md
5
NEWS.md
|
@ -106,10 +106,6 @@ The following deprecated methods are removed.
|
|||
|
||||
## Stdlib compatibility issues
|
||||
|
||||
* ERB
|
||||
* `ERB.new` no longer takes more than one positional arguments
|
||||
[[Feature #14256]]
|
||||
|
||||
## C API updates
|
||||
|
||||
### Removed C APIs
|
||||
|
@ -142,7 +138,6 @@ The following deprecated APIs are removed.
|
|||
## Miscellaneous changes
|
||||
|
||||
[Feature #12737]: https://bugs.ruby-lang.org/issues/12737
|
||||
[Feature #14256]: https://bugs.ruby-lang.org/issues/14256
|
||||
[Feature #14332]: https://bugs.ruby-lang.org/issues/14332
|
||||
[Feature #15231]: https://bugs.ruby-lang.org/issues/15231
|
||||
[Bug #15928]: https://bugs.ruby-lang.org/issues/15928
|
||||
|
|
15
lib/erb.rb
15
lib/erb.rb
|
@ -808,7 +808,20 @@ class ERB
|
|||
# Chicken Fried Steak -- 9.95
|
||||
# A well messages pattie, breaded and fried.
|
||||
#
|
||||
def initialize(str, trim_mode: nil, eoutvar: '_erbout')
|
||||
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
|
||||
|
||||
compiler = make_compiler(trim_mode)
|
||||
set_eoutvar(compiler, eoutvar)
|
||||
@src, @encoding, @frozen_string = *compiler.compile(str)
|
||||
|
|
|
@ -24,6 +24,29 @@ 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"
|
||||
|
@ -75,16 +98,25 @@ 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", trim_mode: 0)
|
||||
erb = @erb.new("hello", safe, 0)
|
||||
assert_equal("hello", erb.result)
|
||||
|
||||
erb = @erb.new("hello", trim_mode: 1)
|
||||
erb = @erb.new("hello", safe, 1)
|
||||
assert_equal("hello", erb.result)
|
||||
|
||||
erb = @erb.new("hello", trim_mode: 2)
|
||||
erb = @erb.new("hello", safe, 2)
|
||||
assert_equal("hello", erb.result)
|
||||
|
||||
src = <<EOS
|
||||
|
@ -112,9 +144,9 @@ EOS
|
|||
EOS
|
||||
erb = @erb.new(src)
|
||||
assert_equal(ans, erb.result)
|
||||
erb = @erb.new(src, trim_mode: 0)
|
||||
erb = @erb.new(src, safe, 0)
|
||||
assert_equal(ans, erb.result)
|
||||
erb = EnvUtil.suppress_warning { @erb.new(src, trim_mode: '') }
|
||||
erb = @erb.new(src, safe, '')
|
||||
assert_equal(ans, erb.result)
|
||||
|
||||
ans = <<EOS
|
||||
|
@ -125,9 +157,9 @@ EOS
|
|||
* 1% n=0
|
||||
* 2
|
||||
EOS
|
||||
erb = @erb.new(src, trim_mode: 1)
|
||||
erb = @erb.new(src, safe, 1)
|
||||
assert_equal(ans.chomp, erb.result)
|
||||
erb = @erb.new(src, trim_mode: '>')
|
||||
erb = @erb.new(src, safe, '>')
|
||||
assert_equal(ans.chomp, erb.result)
|
||||
|
||||
ans = <<EOS
|
||||
|
@ -141,9 +173,9 @@ EOS
|
|||
* 2
|
||||
EOS
|
||||
|
||||
erb = @erb.new(src, trim_mode: 2)
|
||||
erb = @erb.new(src, safe, 2)
|
||||
assert_equal(ans, erb.result)
|
||||
erb = @erb.new(src, trim_mode: '<>')
|
||||
erb = @erb.new(src, safe, '<>')
|
||||
assert_equal(ans, erb.result)
|
||||
|
||||
ans = <<EOS
|
||||
|
@ -157,7 +189,7 @@ EOS
|
|||
* 0
|
||||
|
||||
EOS
|
||||
erb = @erb.new(src, trim_mode: '%')
|
||||
erb = @erb.new(src, safe, '%')
|
||||
assert_equal(ans, erb.result)
|
||||
|
||||
ans = <<EOS
|
||||
|
@ -165,7 +197,7 @@ EOS
|
|||
= hello
|
||||
* 0* 0* 0
|
||||
EOS
|
||||
erb = @erb.new(src, trim_mode: '%>')
|
||||
erb = @erb.new(src, safe, '%>')
|
||||
assert_equal(ans.chomp, erb.result)
|
||||
|
||||
ans = <<EOS
|
||||
|
@ -175,7 +207,7 @@ EOS
|
|||
* 0
|
||||
* 0
|
||||
EOS
|
||||
erb = @erb.new(src, trim_mode: '%<>')
|
||||
erb = @erb.new(src, safe, '%<>')
|
||||
assert_equal(ans, erb.result)
|
||||
end
|
||||
|
||||
|
@ -629,6 +661,27 @@ 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)}
|
||||
|
|
|
@ -15,4 +15,16 @@ 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
|
||||
|
|
Loading…
Add table
Reference in a new issue