Skip to content

Commit

Permalink
Restore $SAFE after running a safe mode test (if possible).
Browse files Browse the repository at this point in the history
Ruby 2.6 will make $SAFE a global option (instead of per-thread) and
will allow the $SAFE level to be downgraded.

https://bugs.ruby-lang.org/issues/14250

(cherry picked from commit d84b8ae)
  • Loading branch information
philr committed Jan 27, 2018
1 parent 90711f9 commit 1c12363
Showing 1 changed file with 26 additions and 10 deletions.
36 changes: 26 additions & 10 deletions test/test_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,34 @@ def safe_test(options = {})
thread = Thread.new do
orig_diff = Minitest::Assertions.diff

$SAFE = options[:level] || 1 if available

# Disable the use of external diff tools during safe mode tests (since
# safe mode will prevent their use). The initial value is retrieved
# before activating safe mode because the first time
# Minitest::Assertions.diff is called, it will attempt to find a diff
# tool. Finding the diff tool will also fail in safe mode.
Minitest::Assertions.diff = nil
if available
orig_safe = $SAFE
$SAFE = options[:level] || 1
end
begin
yield
# Disable the use of external diff tools during safe mode tests (since
# safe mode will prevent their use). The initial value is retrieved
# before activating safe mode because the first time
# Minitest::Assertions.diff is called, it will attempt to find a diff
# tool. Finding the diff tool will also fail in safe mode.
Minitest::Assertions.diff = nil
begin
yield
ensure
Minitest::Assertions.diff = orig_diff
end
ensure
Minitest::Assertions.diff = orig_diff
if available
# On Ruby < 2.6, setting $SAFE affects only the current thread
# and the $SAFE level cannot be downgraded. Catch and ignore the
# SecurityError.
# On Ruby >= 2.6, setting $SAFE is global, and the $SAFE level
# can be downgraded. Restore $SAFE back to the original level.
begin
$SAFE = orig_safe
rescue SecurityError
end
end
end
end

Expand Down

0 comments on commit 1c12363

Please sign in to comment.