Skip to content

Commit e473ec4

Browse files
committed
io capture thread safety
1 parent 200ce83 commit e473ec4

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

lib/puppet-check/utils.rb

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,26 @@
1+
require 'stringio'
2+
13
# utility methods totally not edited from StackOverflow
24
class Utils
35
# captures stdout from a block: out = capture_stdout { code }
46
def self.capture_stdout
5-
old_stdout = $stdout
7+
Thread.current[:old_stdout] = $stdout
68
$stdout = StringIO.new
79
yield
810
$stdout.string
911
ensure
10-
$stdout = old_stdout
12+
$stdout = Thread.current[:old_stdout] if Thread.current[:old_stdout]
13+
Thread.current[:old_stdout] = nil
1114
end
1215

1316
# captures stderr from a block: err = capture_stderr { code }
1417
def self.capture_stderr
15-
old_stderr = $stderr
18+
Thread.current[:old_stderr] = $stderr
1619
$stderr = StringIO.new
1720
yield
1821
$stderr.string
1922
ensure
20-
$stderr = old_stderr
23+
$stderr = Thread.current[:old_stderr] if Thread.current[:old_stderr]
24+
Thread.current[:old_stderr] = nil
2125
end
2226
end

0 commit comments

Comments
 (0)