summaryrefslogtreecommitdiffhomepage
path: root/test/support/capture_warnings.rb
blob: 632f87a304eba13a68debecea4a6c977c759cc03 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# frozen_string_literal: true

module CaptureWarnings
  def capture_warnings
    # Turn off warnings with setting $VERBOSE to nil
    original_verbose = $VERBOSE
    $VERBOSE = nil

    # Redefine warn to redirect warning into our array
    original_warn = Kernel.instance_method(:warn)
    warnings = []
    Kernel.send(:define_method, :warn) { |string| warnings << string }
    yield

    # Revert to the original warn method and set back $VERBOSE to previous value
    Kernel.send(:define_method, :warn, original_warn)
    $VERBOSE = original_verbose

    # Give back the received warnings
    warnings
  end
end