2018-08-27 10:25:00 -04:00
|
|
|
require_relative '../spec_helper'
|
|
|
|
|
|
|
|
describe "The $SAFE variable" do
|
2019-09-20 22:06:22 -04:00
|
|
|
ruby_version_is ""..."2.7" do
|
2021-07-29 16:11:21 -04:00
|
|
|
after :each do
|
|
|
|
$SAFE = 0
|
2018-08-27 10:25:00 -04:00
|
|
|
end
|
|
|
|
|
2019-09-20 22:06:22 -04:00
|
|
|
it "is 0 by default" do
|
2018-08-27 10:25:00 -04:00
|
|
|
$SAFE.should == 0
|
2019-09-20 22:06:22 -04:00
|
|
|
proc {
|
|
|
|
$SAFE.should == 0
|
|
|
|
}.call
|
|
|
|
end
|
2018-08-27 10:25:00 -04:00
|
|
|
|
2019-09-20 22:06:22 -04:00
|
|
|
it "can be set to 0" do
|
|
|
|
proc {
|
|
|
|
$SAFE = 0
|
|
|
|
$SAFE.should == 0
|
|
|
|
}.call
|
|
|
|
end
|
2018-08-27 10:25:00 -04:00
|
|
|
|
2019-09-20 22:06:22 -04:00
|
|
|
it "can be set to 1" do
|
|
|
|
proc {
|
|
|
|
$SAFE = 1
|
|
|
|
$SAFE.should == 1
|
|
|
|
}.call
|
|
|
|
end
|
2018-08-27 10:25:00 -04:00
|
|
|
|
2019-09-20 22:06:22 -04:00
|
|
|
[2, 3, 4].each do |n|
|
|
|
|
it "cannot be set to #{n}" do
|
|
|
|
-> {
|
|
|
|
proc {
|
|
|
|
$SAFE = n
|
|
|
|
}.call
|
|
|
|
}.should raise_error(ArgumentError, /\$SAFE=2 to 4 are obsolete/)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-07-29 16:11:21 -04:00
|
|
|
it "raises ArgumentError when set to values below 0" do
|
|
|
|
-> {
|
|
|
|
proc {
|
|
|
|
$SAFE = -100
|
|
|
|
}.call
|
|
|
|
}.should raise_error(ArgumentError, "$SAFE should be >= 0")
|
2019-09-20 22:06:22 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "cannot be set to values above 4" do
|
2019-07-27 06:40:09 -04:00
|
|
|
-> {
|
2018-08-27 10:25:00 -04:00
|
|
|
proc {
|
2019-09-20 22:06:22 -04:00
|
|
|
$SAFE = 100
|
2018-08-27 10:25:00 -04:00
|
|
|
}.call
|
|
|
|
}.should raise_error(ArgumentError, /\$SAFE=2 to 4 are obsolete/)
|
|
|
|
end
|
|
|
|
|
2021-07-29 16:11:21 -04:00
|
|
|
it "can be manually lowered" do
|
|
|
|
$SAFE = 1
|
|
|
|
$SAFE = 0
|
|
|
|
$SAFE.should == 0
|
2018-08-27 10:25:00 -04:00
|
|
|
end
|
|
|
|
|
2021-07-29 16:11:21 -04:00
|
|
|
it "is not Proc local" do
|
|
|
|
$SAFE.should == 0
|
|
|
|
proc {
|
2018-08-27 10:25:00 -04:00
|
|
|
$SAFE = 1
|
2021-07-29 16:11:21 -04:00
|
|
|
}.call
|
|
|
|
$SAFE.should == 1
|
|
|
|
end
|
2019-09-20 22:06:22 -04:00
|
|
|
|
2021-07-29 16:11:21 -04:00
|
|
|
it "is not lambda local" do
|
|
|
|
$SAFE.should == 0
|
|
|
|
-> {
|
2018-08-27 10:25:00 -04:00
|
|
|
$SAFE = 1
|
2021-07-29 16:11:21 -04:00
|
|
|
}.call
|
|
|
|
$SAFE.should == 1
|
|
|
|
end
|
|
|
|
|
|
|
|
it "is global like regular global variables" do
|
|
|
|
Thread.new { $SAFE }.value.should == 0
|
|
|
|
$SAFE = 1
|
|
|
|
Thread.new { $SAFE }.value.should == 1
|
2018-08-27 10:25:00 -04:00
|
|
|
end
|
|
|
|
|
2019-09-20 22:06:22 -04:00
|
|
|
it "can be read when default from Thread#safe_level" do
|
|
|
|
Thread.current.safe_level.should == 0
|
2019-01-20 15:38:57 -05:00
|
|
|
end
|
|
|
|
|
2019-09-20 22:06:22 -04:00
|
|
|
it "can be read when modified from Thread#safe_level" do
|
2019-01-20 15:38:57 -05:00
|
|
|
proc {
|
|
|
|
$SAFE = 1
|
2019-09-20 22:06:22 -04:00
|
|
|
Thread.current.safe_level.should == 1
|
2019-01-20 15:38:57 -05:00
|
|
|
}.call
|
|
|
|
end
|
2018-08-27 10:25:00 -04:00
|
|
|
end
|
2020-10-24 09:52:37 -04:00
|
|
|
|
|
|
|
ruby_version_is "2.7"..."3.0" do
|
|
|
|
it "warn when access" do
|
|
|
|
-> {
|
|
|
|
$SAFE
|
|
|
|
}.should complain(/\$SAFE will become a normal global variable in Ruby 3.0/)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "warn when set" do
|
|
|
|
-> {
|
|
|
|
$SAFE = 1
|
|
|
|
}.should complain(/\$SAFE will become a normal global variable in Ruby 3.0/)
|
|
|
|
end
|
|
|
|
end
|
2018-08-27 10:25:00 -04:00
|
|
|
end
|