mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge pull request #7763 from route/full-capture
Kernel#capture replaced by version which can catch output from subprocesses
This commit is contained in:
commit
b67a03cf0b
3 changed files with 29 additions and 11 deletions
|
@ -1,4 +1,5 @@
|
||||||
## Rails 4.0.0 (unreleased) ##
|
## Rails 4.0.0 (unreleased) ##
|
||||||
|
* Kernel#capture can catch output from subprocesses *Dmitry Vorotilin*
|
||||||
|
|
||||||
* `to_xml` conversions now use builder's `tag!` method instead of explicit invocation of `method_missing`.
|
* `to_xml` conversions now use builder's `tag!` method instead of explicit invocation of `method_missing`.
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
require 'rbconfig'
|
require 'rbconfig'
|
||||||
|
require 'tempfile'
|
||||||
|
|
||||||
module Kernel
|
module Kernel
|
||||||
# Sets $VERBOSE to nil for the duration of the block and back to its original
|
# 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:
|
# Captures the given stream and returns it:
|
||||||
#
|
#
|
||||||
# stream = capture(:stdout) { puts 'Cool' }
|
# stream = capture(:stdout) { puts 'notice' }
|
||||||
# stream # => "Cool\n"
|
# 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)
|
def capture(stream)
|
||||||
begin
|
stream = stream.to_s
|
||||||
stream = stream.to_s
|
captured_stream = Tempfile.new(stream)
|
||||||
eval "$#{stream} = StringIO.new"
|
stream_io = eval("$#{stream}")
|
||||||
yield
|
origin_stream = stream_io.dup
|
||||||
result = eval("$#{stream}").string
|
stream_io.reopen(captured_stream)
|
||||||
ensure
|
|
||||||
eval("$#{stream} = #{stream.upcase}")
|
|
||||||
end
|
|
||||||
|
|
||||||
result
|
yield
|
||||||
|
|
||||||
|
stream_io.rewind
|
||||||
|
return captured_stream.read
|
||||||
|
ensure
|
||||||
|
captured_stream.unlink
|
||||||
|
stream_io.reopen(origin_stream)
|
||||||
end
|
end
|
||||||
alias :silence :capture
|
alias :silence :capture
|
||||||
|
|
||||||
|
|
|
@ -51,6 +51,8 @@ class KernelTest < ActiveSupport::TestCase
|
||||||
def test_capture
|
def test_capture
|
||||||
assert_equal 'STDERR', capture(:stderr) { $stderr.print 'STDERR' }
|
assert_equal 'STDERR', capture(:stderr) { $stderr.print 'STDERR' }
|
||||||
assert_equal 'STDOUT', capture(:stdout) { print 'STDOUT' }
|
assert_equal 'STDOUT', capture(:stdout) { print 'STDOUT' }
|
||||||
|
assert_equal "STDERR\n", capture(:stderr) { system('echo STDERR 1>&2') }
|
||||||
|
assert_equal "STDOUT\n", capture(:stdout) { system('echo STDOUT') }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue