File: helper_methods.rb

package info (click to toggle)
ruby-rspec 3.13.0c0e0m0s1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,856 kB
  • sloc: ruby: 70,868; sh: 1,423; makefile: 99
file content (53 lines) | stat: -rw-r--r-- 1,557 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
module RSpecHelpers
  SAFE_LEVEL_THAT_TRIGGERS_SECURITY_ERRORS = RUBY_VERSION >= '2.3' ? 1 : 3
  SAFE_IS_GLOBAL_VARIABLE = RUBY_VERSION >= '2.6'

  def relative_path(path)
    RSpec::Core::Metadata.relative_path(path)
  end

  def ignoring_warnings
    original = $VERBOSE
    $VERBOSE = nil
    result = yield
    $VERBOSE = original
    result
  end

  # Intended for use with indented heredocs.
  # taken from Ruby Tapas:
  # https://rubytapas.dpdcart.com/subscriber/post?id=616#files
  def unindent(s)
    s.gsub(/^#{s.scan(/^[ \t]+(?=\S)/).min}/, "")
  end

  # In Ruby 2.7 taint was removed and has no effect, whilst SAFE warns that it
  # has no effect and will become a normal variable in 3.0. Other engines do not
  # implement SAFE.
  if RUBY_VERSION >= '2.7' || (defined?(RUBY_ENGINE) && RUBY_ENGINE != "ruby")
    def with_safe_set_to_level_that_triggers_security_errors
      yield
    end
  else
    def with_safe_set_to_level_that_triggers_security_errors
      result = nil

      orig_safe = $SAFE
      Thread.new do
        ignoring_warnings { $SAFE = SAFE_LEVEL_THAT_TRIGGERS_SECURITY_ERRORS }
        result = yield
      end.join

      # $SAFE is not supported on Rubinius
      # In Ruby 2.6, $SAFE became a global variable; previously it was local to a thread.
      unless defined?(Rubinius) || SAFE_IS_GLOBAL_VARIABLE
        # $SAFE should not have changed in this thread.
        expect($SAFE).to eql orig_safe
      end

      result
    ensure
      $SAFE = orig_safe if orig_safe && SAFE_IS_GLOBAL_VARIABLE
    end
  end
end