From bdbb15e5a5c647823aa6b9fd04020dab16360ae1 Mon Sep 17 00:00:00 2001 From: Xavier Noria Date: Fri, 13 May 2011 01:41:52 +0200 Subject: [PATCH] new reporting method Kernel#quietly --- activesupport/CHANGELOG | 2 ++ .../active_support/core_ext/kernel/reporting.rb | 14 +++++++++++++- .../source/active_support_core_extensions.textile | 8 ++++++++ railties/test/generators/app_generator_test.rb | 2 +- .../test/generators/plugin_new_generator_test.rb | 4 ++-- 5 files changed, 26 insertions(+), 4 deletions(-) diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG index b5b0af8e85..23b0df1d5c 100644 --- a/activesupport/CHANGELOG +++ b/activesupport/CHANGELOG @@ -1,5 +1,7 @@ *Rails 3.1.0 (unreleased)* +* New reporting method Kernel#quietly. [fxn] + * Add String#inquiry as a convenience method for turning a string into a StringInquirer object [DHH] * Add Object#in? to test if an object is included in another object [Prem Sichanugrist, Brian Morearty, John Reitano] diff --git a/activesupport/lib/active_support/core_ext/kernel/reporting.rb b/activesupport/lib/active_support/core_ext/kernel/reporting.rb index 37a827123a..c6920098a8 100644 --- a/activesupport/lib/active_support/core_ext/kernel/reporting.rb +++ b/activesupport/lib/active_support/core_ext/kernel/reporting.rb @@ -62,7 +62,7 @@ module Kernel # Captures the given stream and returns it: # - # stream = capture(:stdout){ puts "Cool" } + # stream = capture(:stdout) { puts "Cool" } # stream # => "Cool\n" # def capture(stream) @@ -78,4 +78,16 @@ module Kernel result end alias :silence :capture + + # Silences both STDOUT and STDERR, even for subprocesses. + # + # quietly { system 'bundle install' } + # + def quietly + silence_stream(STDOUT) do + silence_stream(STDERR) do + yield + end + end + end end diff --git a/railties/guides/source/active_support_core_extensions.textile b/railties/guides/source/active_support_core_extensions.textile index f2170e120b..d82bdfee43 100644 --- a/railties/guides/source/active_support_core_extensions.textile +++ b/railties/guides/source/active_support_core_extensions.textile @@ -417,6 +417,14 @@ silence_stream(STDOUT) do end +The +quietly+ method addresses the common use case where you want to silence STDOUT and STDERR, even in subprocesses: + + +quietly { system 'bundle install' } + + +For example, the railties test suite uses that one in a few places to prevent command messages from being echoed intermixed with the progress status. + Silencing exceptions is also possible with +suppress+. This method receives an arbitrary number of exception classes. If an exception is raised during the execution of the block and is +kind_of?+ any of the arguments, +suppress+ captures it and returns silently. Otherwise the exception is reraised: diff --git a/railties/test/generators/app_generator_test.rb b/railties/test/generators/app_generator_test.rb index 5175a6e2e6..8de829b6dd 100644 --- a/railties/test/generators/app_generator_test.rb +++ b/railties/test/generators/app_generator_test.rb @@ -66,7 +66,7 @@ class AppGeneratorTest < Rails::Generators::TestCase end def test_application_new_exits_with_non_zero_code_on_invalid_application_name - silence_stderr { `rails new test` } + quietly { system 'rails new test' } assert_equal false, $?.success? end diff --git a/railties/test/generators/plugin_new_generator_test.rb b/railties/test/generators/plugin_new_generator_test.rb index 673856c34d..2af728e766 100644 --- a/railties/test/generators/plugin_new_generator_test.rb +++ b/railties/test/generators/plugin_new_generator_test.rb @@ -122,14 +122,14 @@ class PluginNewGeneratorTest < Rails::Generators::TestCase def test_ensure_that_tests_work run_generator FileUtils.cd destination_root - `bundle install` # use backticks to silence stdout + quietly { system 'bundle install' } assert_match(/1 tests, 1 assertions, 0 failures, 0 errors/, `bundle exec rake test`) end def test_ensure_that_tests_works_in_full_mode run_generator [destination_root, "--full", "--skip_active_record"] FileUtils.cd destination_root - `bundle install` # use backticks to silence stdout + quietly { system 'bundle install' } assert_match(/1 tests, 1 assertions, 0 failures, 0 errors/, `bundle exec rake test`) end