From 645de3391276a654ab4a653f5bbe47efc5759b77 Mon Sep 17 00:00:00 2001 From: Sam Stephenson <sam@37signals.com> Date: Mon, 7 Nov 2005 00:37:32 +0000 Subject: Added Kernel#silence_stderr to silence stderr for the duration of the given block. Changed Kernel#` to print a message to stderr (like Unix) instead of raising Errno::ENOENT on Win32. git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@2899 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activesupport/CHANGELOG | 4 ++++ .../lib/active_support/core_ext/kernel.rb | 26 ++++++++++++++++++++++ activesupport/test/core_ext/kernel_test.rb | 10 +++++++++ 3 files changed, 40 insertions(+) (limited to 'activesupport') diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG index be4ed83c54..faf281f0a5 100644 --- a/activesupport/CHANGELOG +++ b/activesupport/CHANGELOG @@ -1,5 +1,9 @@ *SVN* +* Added Kernel#silence_stderr to silence stderr for the duration of the given block [Sam Stephenson] + +* Changed Kernel#` to print a message to stderr (like Unix) instead of raising Errno::ENOENT on Win32 [Sam Stephenson] + * Changed 0.blank? to false rather than true since it violates everyone's expectation of blankness. #2518, #2705 [rails@jeffcole.net] * When loading classes using const_missing, raise a NameError if and only if the file we tried to load was not present. [Nicholas Seckar] diff --git a/activesupport/lib/active_support/core_ext/kernel.rb b/activesupport/lib/active_support/core_ext/kernel.rb index 168c9cddd3..310bba0904 100644 --- a/activesupport/lib/active_support/core_ext/kernel.rb +++ b/activesupport/lib/active_support/core_ext/kernel.rb @@ -29,6 +29,32 @@ module Kernel $VERBOSE = old_verbose end + # Silences stderr for the duration of the block. + # + # silence_stderr do + # $stderr.puts 'This will never be seen' + # end + # + # $stderr.puts 'But this will' + def silence_stderr + old_stderr = STDERR.dup + STDERR.reopen(RUBY_PLATFORM =~ /mswin/ ? 'NUL:' : '/dev/null') + STDERR.sync = true + yield + ensure + STDERR.reopen(old_stderr) + end + + # Makes backticks behave (somewhat more) similarly on all platforms. + # On win32 `nonexistent_command` raises Errno::ENOENT; on Unix, the + # spawned shell prints a message to stderr and sets $?. We emulate + # Unix on the former but not the latter. + def `(command) #:nodoc: + super + rescue Errno::ENOENT => e + STDERR.puts "#$0: #{e}" + end + # Method that requires a library, ensuring that rubygems is loaded def require_library_or_gem(library_name) begin diff --git a/activesupport/test/core_ext/kernel_test.rb b/activesupport/test/core_ext/kernel_test.rb index 7d1565157a..e2d89176b9 100644 --- a/activesupport/test/core_ext/kernel_test.rb +++ b/activesupport/test/core_ext/kernel_test.rb @@ -20,4 +20,14 @@ class KernelTest < Test::Unit::TestCase def test_silence_warnings_with_return_value assert_equal 1, silence_warnings { 1 } end + + def test_silence_stderr + old_stderr_position = STDERR.tell + silence_stderr { STDERR.puts 'hello world' } + assert_equal old_stderr_position, STDERR.tell + end + + def test_silence_stderr_with_return_value + assert_equal 1, silence_stderr { 1 } + end end -- cgit v1.2.3