mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
r3240@asus: jeremy | 2005-11-20 23:22:34 -0800
Introduce enable_warnings counterpart to silence_warnings. git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3134 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
parent
d792427301
commit
9a77e2f2be
4 changed files with 48 additions and 34 deletions
|
@ -1,6 +1,6 @@
|
|||
*SVN*
|
||||
|
||||
* Turn warnings on when loading a file if Dependencies.mechanism == :load. Common mistakes such as redefined methods will print warnings to stderr. [Jeremy Kemper]
|
||||
* Introduce enable_warnings counterpart to silence_warnings. Turn warnings on when loading a file if Dependencies.mechanism == :load. Common mistakes such as redefined methods will print warnings to stderr. [Jeremy Kemper]
|
||||
|
||||
* Add Symbol#to_proc, which allows for, e.g. [:foo, :bar].map(&:to_s). [Marcel Molina Jr.]
|
||||
|
||||
|
|
|
@ -29,6 +29,14 @@ class Object
|
|||
$VERBOSE = old_verbose
|
||||
end
|
||||
|
||||
# Sets $VERBOSE to true for the duration of the block and back to its original value afterwards.
|
||||
def enable_warnings
|
||||
old_verbose, $VERBOSE = $VERBOSE, true
|
||||
yield
|
||||
ensure
|
||||
$VERBOSE = old_verbose
|
||||
end
|
||||
|
||||
# Silences stderr for the duration of the block.
|
||||
#
|
||||
# silence_stderr do
|
||||
|
@ -54,26 +62,27 @@ class Object
|
|||
rescue Errno::ENOENT => e
|
||||
STDERR.puts "#$0: #{e}"
|
||||
end
|
||||
|
||||
# Method that requires a library, ensuring that rubygems is loaded
|
||||
|
||||
# Require a library with fallback to RubyGems. Warnings during library
|
||||
# loading are silenced to increase signal/noise for application warnings.
|
||||
def require_library_or_gem(library_name)
|
||||
begin
|
||||
require library_name
|
||||
rescue LoadError => cannot_require
|
||||
# 1. Requiring the module is unsuccessful, maybe it's a gem and nobody required rubygems yet. Try.
|
||||
begin
|
||||
require 'rubygems'
|
||||
rescue LoadError => rubygems_not_installed
|
||||
raise cannot_require
|
||||
end
|
||||
# 2. Rubygems is installed and loaded. Try to load the library again
|
||||
silence_warnings do
|
||||
begin
|
||||
require library_name
|
||||
rescue LoadError => gem_not_installed
|
||||
raise cannot_require
|
||||
rescue LoadError => cannot_require
|
||||
# 1. Requiring the module is unsuccessful, maybe it's a gem and nobody required rubygems yet. Try.
|
||||
begin
|
||||
require 'rubygems'
|
||||
rescue LoadError => rubygems_not_installed
|
||||
raise cannot_require
|
||||
end
|
||||
# 2. Rubygems is installed and loaded. Try to load the library again
|
||||
begin
|
||||
require library_name
|
||||
rescue LoadError => gem_not_installed
|
||||
raise cannot_require
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
end
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
require File.dirname(__FILE__) + '/module_attribute_accessors'
|
||||
require File.dirname(__FILE__) + '/core_ext/load_error'
|
||||
require File.dirname(__FILE__) + '/core_ext/kernel'
|
||||
|
||||
module Dependencies #:nodoc:
|
||||
extend self
|
||||
|
@ -37,12 +38,7 @@ module Dependencies #:nodoc:
|
|||
def require_or_load(file_name)
|
||||
file_name = "#{file_name}.rb" unless ! load? || file_name[-3..-1] == '.rb'
|
||||
if load?
|
||||
begin
|
||||
original_verbosity, $VERBOSE = $VERBOSE, true
|
||||
load file_name
|
||||
ensure
|
||||
$VERBOSE = original_verbosity
|
||||
end
|
||||
enable_warnings { load file_name }
|
||||
else
|
||||
require file_name
|
||||
end
|
||||
|
|
|
@ -9,24 +9,33 @@ class KernelTest < Test::Unit::TestCase
|
|||
|
||||
def test_silence_warnings_verbose_invariant
|
||||
old_verbose = $VERBOSE
|
||||
begin
|
||||
silence_warnings { raise }
|
||||
flunk
|
||||
rescue
|
||||
assert_equal old_verbose, $VERBOSE
|
||||
end
|
||||
silence_warnings { raise }
|
||||
flunk
|
||||
rescue
|
||||
assert_equal old_verbose, $VERBOSE
|
||||
end
|
||||
|
||||
def test_silence_warnings_with_return_value
|
||||
assert_equal 1, silence_warnings { 1 }
|
||||
|
||||
|
||||
def test_enable_warnings
|
||||
enable_warnings { assert_equal true, $VERBOSE }
|
||||
assert_equal 1234, enable_warnings { 1234 }
|
||||
end
|
||||
|
||||
|
||||
def test_enable_warnings_verbose_invariant
|
||||
old_verbose = $VERBOSE
|
||||
enable_warnings { raise }
|
||||
flunk
|
||||
rescue
|
||||
assert_equal old_verbose, $VERBOSE
|
||||
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
|
||||
|
|
Loading…
Reference in a new issue