File: helpers.rb

package info (click to toggle)
ruby-maxitest 7.1.1-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 444 kB
  • sloc: ruby: 1,339; makefile: 7
file content (51 lines) | stat: -rw-r--r-- 1,183 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
# frozen_string_literal: true
module Maxitest
  module Helpers
    module InstanceMethods
      def with_env(env)
        _synchronize do
          old = ENV.to_h
          env.each { |k, v| ENV[k.to_s] = v }
          yield
        ensure
          ENV.replace old
        end
      end

      # stripped down version of capture_io
      def capture_stdout
        _synchronize do
          captured_stdout = StringIO.new
          orig_stdout = $stdout
          $stdout = captured_stdout
          yield
          return captured_stdout.string
        ensure
          $stdout = orig_stdout
        end
      end

      # stripped down version of capture_io
      def capture_stderr
        _synchronize do
          captured_stderr = StringIO.new
          orig_stderr = $stderr
          $stderr = captured_stderr
          yield
          return captured_stderr.string
        ensure
          $stderr = orig_stderr
        end
      end
    end

    module ClassMethods
      def with_env(env)
        around { |t| with_env(env, &t) }
      end
    end
  end
end

Minitest::Test.include Maxitest::Helpers::InstanceMethods
Minitest::Test.extend Maxitest::Helpers::ClassMethods