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 54 55
|
# frozen_string_literal: true
module MagicTestHelpers
def capture_stderr(children: false)
require 'thread'
mutex = Mutex.new
if children
require 'tempfile'
captured_stderr = Tempfile.new('captured_stderr')
mutex.synchronize do
backup_stderr = $stderr.dup
$stderr.reopen(captured_stderr)
begin
yield
$stderr.rewind
captured_stderr.read
ensure
captured_stderr.unlink
$stderr.reopen(backup_stderr)
end
end
else
require 'stringio'
captured_stderr = StringIO.new
mutex.synchronize do
backup_stderr = $stderr
$stderr = captured_stderr
begin
yield
captured_stderr.string
ensure
$stderr = backup_stderr
end
end
end
end
def with_fixtures(fixtures: 'fixtures', &block)
cwd = File.join('test', fixtures)
Dir.chdir(cwd) do
yield(Dir.pwd)
end
end
def with_env(env, &blk)
before = ENV.to_h.dup
env.each { |k, v| ENV[k] = v }
yield
ensure
ENV.replace(before)
end
end
|