1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

new reporting method Kernel#quietly

This commit is contained in:
Xavier Noria 2011-05-13 01:41:52 +02:00
parent b0062eef34
commit bdbb15e5a5
5 changed files with 26 additions and 4 deletions

View file

@ -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 file

@ -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

View file

@ -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 file

@ -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

View file

@ -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