Skip to content
Browse files

new reporting method Kernel#quietly

  • Loading branch information...
1 parent b0062ee commit bdbb15e5a5c647823aa6b9fd04020dab16360ae1 @fxn fxn committed May 13, 2011
View
2 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]
View
14 activesupport/lib/active_support/core_ext/kernel/reporting.rb
@@ -62,7 +62,7 @@ def suppress(*exception_classes)
# 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 @@ def capture(stream)
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
View
8 railties/guides/source/active_support_core_extensions.textile
@@ -417,6 +417,14 @@ silence_stream(STDOUT) do
end
</ruby>
+The +quietly+ method addresses the common use case where you want to silence STDOUT and STDERR, even in subprocesses:
+
+<ruby>
+quietly { system 'bundle install' }
+</ruby>
+
+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:
<ruby>
View
2 railties/test/generators/app_generator_test.rb
@@ -66,7 +66,7 @@ def test_invalid_application_name_is_fixed
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
View
4 railties/test/generators/plugin_new_generator_test.rb
@@ -122,14 +122,14 @@ def test_template_from_dir_pwd
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

0 comments on commit bdbb15e

Please sign in to comment.
Something went wrong with that request. Please try again.