From 3cf7a0a46037f32b95b5ffb358cb898233d5c5db Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Sun, 11 Sep 2005 04:58:27 +0000 Subject: Added Kernel#silence_warnings and puts it into use throughout the framework git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@2179 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- actionmailer/lib/action_mailer.rb | 4 +--- actionpack/test/controller/helper_test.rb | 4 +--- actionpack/test/template/form_helper_test.rb | 14 +++++++------- actionpack/test/template/form_options_helper_test.rb | 12 ++++++------ activesupport/CHANGELOG | 2 ++ activesupport/lib/active_support.rb | 1 - activesupport/lib/active_support/core_ext/kernel.rb | 14 ++++++++++++++ activesupport/lib/active_support/misc.rb | 8 -------- activesupport/test/core_ext/kernel_test.rb | 19 +++++++++++++++++++ activesupport/test/core_ext/string_ext_test.rb | 3 ++- activesupport/test/dependencies_test.rb | 1 - activesupport/test/misc_test.rb | 19 ------------------- railties/lib/initializer.rb | 4 +--- 13 files changed, 53 insertions(+), 52 deletions(-) delete mode 100644 activesupport/lib/active_support/misc.rb create mode 100644 activesupport/test/core_ext/kernel_test.rb delete mode 100644 activesupport/test/misc_test.rb diff --git a/actionmailer/lib/action_mailer.rb b/actionmailer/lib/action_mailer.rb index 40ad6f15e8..a489e32bac 100755 --- a/actionmailer/lib/action_mailer.rb +++ b/actionmailer/lib/action_mailer.rb @@ -48,6 +48,4 @@ ActionMailer::Base.class_eval do helper MailHelper end -old_verbose, $VERBOSE = $VERBOSE, nil -TMail::Encoder.const_set("MAX_LINE_LEN", 200) -$VERBOSE = old_verbose +silence_warnings { TMail::Encoder.const_set("MAX_LINE_LEN", 200) } \ No newline at end of file diff --git a/actionpack/test/controller/helper_test.rb b/actionpack/test/controller/helper_test.rb index 77a3fc6115..cd48704e3a 100644 --- a/actionpack/test/controller/helper_test.rb +++ b/actionpack/test/controller/helper_test.rb @@ -121,9 +121,7 @@ class HelperTest < Test::Unit::TestCase end def test_helper=(helper_module) - old_verbose, $VERBOSE = $VERBOSE, nil - self.class.const_set('TestHelper', helper_module) - $VERBOSE = old_verbose + silence_warnings { self.class.const_set('TestHelper', helper_module) } end end diff --git a/actionpack/test/template/form_helper_test.rb b/actionpack/test/template/form_helper_test.rb index 1b9df865f3..9cc89a5860 100644 --- a/actionpack/test/template/form_helper_test.rb +++ b/actionpack/test/template/form_helper_test.rb @@ -6,14 +6,14 @@ require File.dirname(__FILE__) + '/../../../activesupport/lib/active_support/cor class FormHelperTest < Test::Unit::TestCase include ActionView::Helpers::FormHelper - old_verbose, $VERBOSE = $VERBOSE, nil - Post = Struct.new("Post", :title, :author_name, :body, :secret, :written_on, :cost) - Post.class_eval do - alias_method :title_before_type_cast, :title unless respond_to?(:title_before_type_cast) - alias_method :body_before_type_cast, :body unless respond_to?(:body_before_type_cast) - alias_method :author_name_before_type_cast, :author_name unless respond_to?(:author_name_before_type_cast) + silence_warnings do + Post = Struct.new("Post", :title, :author_name, :body, :secret, :written_on, :cost) + Post.class_eval do + alias_method :title_before_type_cast, :title unless respond_to?(:title_before_type_cast) + alias_method :body_before_type_cast, :body unless respond_to?(:body_before_type_cast) + alias_method :author_name_before_type_cast, :author_name unless respond_to?(:author_name_before_type_cast) + end end - $VERBOSE = old_verbose def setup @post = Post.new diff --git a/actionpack/test/template/form_options_helper_test.rb b/actionpack/test/template/form_options_helper_test.rb index 7a83f57341..d92a01a4e3 100644 --- a/actionpack/test/template/form_options_helper_test.rb +++ b/actionpack/test/template/form_options_helper_test.rb @@ -25,12 +25,12 @@ ActionView::Helpers::FormOptionsHelper::TimeZone = MockTimeZone class FormOptionsHelperTest < Test::Unit::TestCase include ActionView::Helpers::FormOptionsHelper - old_verbose, $VERBOSE = $VERBOSE, nil - Post = Struct.new('Post', :title, :author_name, :body, :secret, :written_on, :category, :origin) - Continent = Struct.new('Continent', :continent_name, :countries) - Country = Struct.new('Country', :country_id, :country_name) - Firm = Struct.new('Firm', :time_zone) - $VERBOSE = old_verbose + silence_warnings do + Post = Struct.new('Post', :title, :author_name, :body, :secret, :written_on, :category, :origin) + Continent = Struct.new('Continent', :continent_name, :countries) + Country = Struct.new('Country', :country_id, :country_name) + Firm = Struct.new('Firm', :time_zone) + end def test_collection_options @posts = [ diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG index c4eed21534..acf9788285 100644 --- a/activesupport/CHANGELOG +++ b/activesupport/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Added Kernel#silence_warnings to turn off warnings temporarily for the passed block + * Added String#starts_with? and String#ends_with? #2118 [thijs@vandervossen.net] * Added easy extendability to the inflector through Inflector.inflections (using the Inflector::Inflections singleton class). Examples: diff --git a/activesupport/lib/active_support.rb b/activesupport/lib/active_support.rb index ba6945f3e5..7c7ae56e61 100644 --- a/activesupport/lib/active_support.rb +++ b/activesupport/lib/active_support.rb @@ -29,7 +29,6 @@ require 'active_support/inflector' require 'active_support/core_ext' require 'active_support/clean_logger' -require 'active_support/misc' require 'active_support/dependencies' require 'active_support/values/time_zone' \ No newline at end of file diff --git a/activesupport/lib/active_support/core_ext/kernel.rb b/activesupport/lib/active_support/core_ext/kernel.rb index a2cb827d77..d4c3114488 100644 --- a/activesupport/lib/active_support/core_ext/kernel.rb +++ b/activesupport/lib/active_support/core_ext/kernel.rb @@ -14,4 +14,18 @@ module Kernel yield value end + + # Sets $VERBOSE to nil for the duration of the block and back to its original value afterwards. + # + # silence_warnings do + # noisy_call # no warning voiced + # end + # + # noisy_call # warning voiced + def silence_warnings + old_verbose, $VERBOSE = $VERBOSE, nil + yield + ensure + $VERBOSE = old_verbose + end end diff --git a/activesupport/lib/active_support/misc.rb b/activesupport/lib/active_support/misc.rb deleted file mode 100644 index 1ca00db1e7..0000000000 --- a/activesupport/lib/active_support/misc.rb +++ /dev/null @@ -1,8 +0,0 @@ -def silence_warnings - old_verbose, $VERBOSE = $VERBOSE, nil - begin - yield - ensure - $VERBOSE = old_verbose - end -end diff --git a/activesupport/test/core_ext/kernel_test.rb b/activesupport/test/core_ext/kernel_test.rb new file mode 100644 index 0000000000..d757362243 --- /dev/null +++ b/activesupport/test/core_ext/kernel_test.rb @@ -0,0 +1,19 @@ +require 'test/unit' +require File.dirname(__FILE__) + '/../../lib/active_support/core_ext/kernel' + +class KernelTest < Test::Unit::TestCase + def test_silence_warnings + silence_warnings { assert_nil $VERBOSE } + assert_equal 1234, silence_warnings { 1234 } + end + + def test_silence_warnings_verbose_invariant + old_verbose = $VERBOSE + begin + silence_warnings { raise } + flunk + rescue + assert_equal old_verbose, $VERBOSE + end + end +end diff --git a/activesupport/test/core_ext/string_ext_test.rb b/activesupport/test/core_ext/string_ext_test.rb index c1c1918817..15189a75b1 100644 --- a/activesupport/test/core_ext/string_ext_test.rb +++ b/activesupport/test/core_ext/string_ext_test.rb @@ -1,6 +1,7 @@ require 'test/unit' +require 'date' require File.dirname(__FILE__) + '/../../lib/active_support/core_ext/string' -require File.dirname(__FILE__) + '/../../lib/active_support/misc' +require File.dirname(__FILE__) + '/../../lib/active_support/core_ext/kernel' silence_warnings do require File.dirname(__FILE__) + '/../inflector_test' diff --git a/activesupport/test/dependencies_test.rb b/activesupport/test/dependencies_test.rb index 1af960ee36..a34fc0d323 100644 --- a/activesupport/test/dependencies_test.rb +++ b/activesupport/test/dependencies_test.rb @@ -1,6 +1,5 @@ require 'test/unit' $LOAD_PATH.unshift File.dirname(__FILE__) + '/../lib/active_support/' -require 'misc' require 'dependencies' class DependenciesTest < Test::Unit::TestCase diff --git a/activesupport/test/misc_test.rb b/activesupport/test/misc_test.rb deleted file mode 100644 index cfd54c0ad0..0000000000 --- a/activesupport/test/misc_test.rb +++ /dev/null @@ -1,19 +0,0 @@ -require 'test/unit' -require File.dirname(__FILE__) + '/../lib/active_support/misc' - -class MiscTest < Test::Unit::TestCase - def test_silence_warnings - silence_warnings { assert_nil $VERBOSE } - assert_equal 1234, silence_warnings { 1234 } - end - - def test_silence_warnings_verbose_invariant - old_verbose = $VERBOSE - begin - silence_warnings { raise } - flunk - rescue - assert_equal old_verbose, $VERBOSE - end - end -end diff --git a/railties/lib/initializer.rb b/railties/lib/initializer.rb index 13a1e1308a..fd9f2fb61a 100644 --- a/railties/lib/initializer.rb +++ b/railties/lib/initializer.rb @@ -76,9 +76,7 @@ module Rails ) end - old_verbose, $VERBOSE = $VERBOSE, nil - Object.const_set "RAILS_DEFAULT_LOGGER", logger - $VERBOSE = old_verbose + silence_warnings { Object.const_set "RAILS_DEFAULT_LOGGER", logger } end def initialize_framework_logging -- cgit v1.2.3