aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/core_ext/kernel/reporting.rb
diff options
context:
space:
mode:
authorDmitry Vorotilin <d.vorotilin@gmail.com>2012-09-26 13:04:41 +0400
committerDmitry Vorotilin <d.vorotilin@gmail.com>2012-11-01 13:36:40 +0400
commit346ccf376c13bef58e575474a2f717b78dfa583d (patch)
tree95b3c12162d37203d1201e9fecdecf3da83cbd4b /activesupport/lib/active_support/core_ext/kernel/reporting.rb
parentc7e233ce47c050e086aa9eae7b0217c3d9e405dd (diff)
downloadrails-346ccf376c13bef58e575474a2f717b78dfa583d.tar.gz
rails-346ccf376c13bef58e575474a2f717b78dfa583d.tar.bz2
rails-346ccf376c13bef58e575474a2f717b78dfa583d.zip
Kernel#capture replaced by version which can catch output from subprocesses
Diffstat (limited to 'activesupport/lib/active_support/core_ext/kernel/reporting.rb')
-rw-r--r--activesupport/lib/active_support/core_ext/kernel/reporting.rb37
1 files changed, 26 insertions, 11 deletions
diff --git a/activesupport/lib/active_support/core_ext/kernel/reporting.rb b/activesupport/lib/active_support/core_ext/kernel/reporting.rb
index bc97da6ef2..7b518821c8 100644
--- a/activesupport/lib/active_support/core_ext/kernel/reporting.rb
+++ b/activesupport/lib/active_support/core_ext/kernel/reporting.rb
@@ -1,4 +1,5 @@
require 'rbconfig'
+require 'tempfile'
module Kernel
# Sets $VERBOSE to nil for the duration of the block and back to its original
@@ -66,19 +67,33 @@ module Kernel
# Captures the given stream and returns it:
#
- # stream = capture(:stdout) { puts 'Cool' }
- # stream # => "Cool\n"
+ # stream = capture(:stdout) { puts 'notice' }
+ # stream # => "notice\n"
+ #
+ # stream = capture(:stderr) { warn 'error' }
+ # stream # => "error\n"
+ #
+ # even for subprocesses:
+ #
+ # stream = capture(:stdout) { system('echo notice') }
+ # stream # => "notice\n"
+ #
+ # stream = capture(:stderr) { system('echo error 1>&2') }
+ # stream # => "error\n"
def capture(stream)
- begin
- stream = stream.to_s
- eval "$#{stream} = StringIO.new"
- yield
- result = eval("$#{stream}").string
- ensure
- eval("$#{stream} = #{stream.upcase}")
- end
+ stream = stream.to_s
+ captured_stream = Tempfile.new(stream)
+ stream_io = eval("$#{stream}")
+ origin_stream = stream_io.dup
+ stream_io.reopen(captured_stream)
+
+ yield
- result
+ stream_io.rewind
+ return captured_stream.read
+ ensure
+ captured_stream.unlink
+ stream_io.reopen(origin_stream)
end
alias :silence :capture