File: helpers.rb

package info (click to toggle)
ruby-maxitest 5.8.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 536 kB
  • sloc: ruby: 1,578; makefile: 7
file content (54 lines) | stat: -rw-r--r-- 1,257 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
54
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
          begin
            captured_stdout = StringIO.new
            orig_stdout = $stdout
            $stdout = captured_stdout
            yield
            return captured_stdout.string
          ensure
            $stdout = orig_stdout
          end
        end
      end

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

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

Minitest::Test.send(:include, Maxitest::Helpers::InstanceMethods)
Minitest::Test.send(:extend, Maxitest::Helpers::ClassMethods)