diff options
Diffstat (limited to 'activesupport')
-rw-r--r-- | activesupport/CHANGELOG.md | 1 | ||||
-rw-r--r-- | activesupport/activesupport.gemspec | 19 | ||||
-rw-r--r-- | activesupport/lib/active_support/core_ext/kernel/reporting.rb | 37 | ||||
-rw-r--r-- | activesupport/test/core_ext/kernel_test.rb | 2 |
4 files changed, 39 insertions, 20 deletions
diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md index 0781abb6ed..61d85543d1 100644 --- a/activesupport/CHANGELOG.md +++ b/activesupport/CHANGELOG.md @@ -1,4 +1,5 @@ ## 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`. diff --git a/activesupport/activesupport.gemspec b/activesupport/activesupport.gemspec index 30f4ded005..a4216d2cb4 100644 --- a/activesupport/activesupport.gemspec +++ b/activesupport/activesupport.gemspec @@ -1,4 +1,4 @@ -version = File.read(File.expand_path("../../RAILS_VERSION", __FILE__)).strip +version = File.read(File.expand_path('../../RAILS_VERSION', __FILE__)).strip Gem::Specification.new do |s| s.platform = Gem::Platform::RUBY @@ -8,19 +8,20 @@ Gem::Specification.new do |s| s.description = 'A toolkit of support libraries and Ruby core extensions extracted from the Rails framework. Rich support for multibyte strings, internationalization, time zones, and testing.' s.required_ruby_version = '>= 1.9.3' - s.license = 'MIT' - s.author = 'David Heinemeier Hansson' - s.email = 'david@loudthinking.com' - s.homepage = 'http://www.rubyonrails.org' + s.license = 'MIT' + + s.author = 'David Heinemeier Hansson' + s.email = 'david@loudthinking.com' + s.homepage = 'http://www.rubyonrails.org' s.files = Dir['CHANGELOG.md', 'MIT-LICENSE', 'README.rdoc', 'lib/**/*'] s.require_path = 'lib' s.rdoc_options.concat ['--encoding', 'UTF-8'] - s.add_dependency('i18n', '~> 0.6') - s.add_dependency('multi_json', '~> 1.3') - s.add_dependency('tzinfo', '~> 0.3.33') - s.add_dependency('minitest', '~> 4.1') + s.add_dependency 'i18n', '~> 0.6' + s.add_dependency 'multi_json', '~> 1.3' + s.add_dependency 'tzinfo', '~> 0.3.33' + s.add_dependency 'minitest', '~> 4.1' end 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 diff --git a/activesupport/test/core_ext/kernel_test.rb b/activesupport/test/core_ext/kernel_test.rb index 439bc87323..1583c1fa32 100644 --- a/activesupport/test/core_ext/kernel_test.rb +++ b/activesupport/test/core_ext/kernel_test.rb @@ -51,6 +51,8 @@ class KernelTest < ActiveSupport::TestCase def test_capture assert_equal 'STDERR', capture(:stderr) { $stderr.print 'STDERR' } 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 |