How can I mute Rails 3 deprecation warnings selectively?
Asked Answered
V

5

10

I am upgrading a Rails 2 to Rails 3 application (code not written by me). The (well tested code) uses shoulda and Test::Unit, and extensively uses the macros should_create and should_change.

I understand from this discussion that the shoulda maintainers want to get rid of both methods but that people using Test::Unit don't find it necessary (not sure I am grasping the whole discussion though).

Anaway, is there a way to selectively turn of the deprecation warnings for specified macros? I already know from this posting that you can turn off the deprecation warnings in the Rake test output entirely by setting:

ActiveSupport::Deprecation.silenced = true

in your the test environment file and I also know that you can put specific pieces of code in a block to get them silenced:

ActiveSupport::Deprecation.silence do
# no warnings for any use of deprecated methods here
end

The latter is an option but would require me to go over all the tests and enclose the should_create macros in such a block. So I was wondering there was a way to eliminate warnings for specific macros entirely with one configuration setting?

Vermicelli answered 4/2, 2011 at 17:59 Comment(0)
V
3

In fact I stil had lots of other deprecation warnings from code that was in plugins or gems I had installed. In order to avoid most of that, I overwrote the Deprecation::warn method in test_helper.rb. So instead of the previous code, use:

module ActiveSupport
  module Deprecation
    class << self
      def warn(message = nil, callstack = caller)
        # modif pvh the following lines make sure no deprecation warnings are sent 
        # for code that is
        # not by my but in some gem or plugin...
        return if silenced  || callstack.grep(/myrailsappname/).blank?
        # return if silenced 
        deprecation_message(callstack, message).tap do |m|
          behavior.each { |b| b.call(m, callstack) }
        end
      end
    end
  end
end  

BTW you need to replace myrailsappname with your app's name (the name of the folder it resides in). There is probably a more generic way to get that name, but I couldn't find it right now.

Vermicelli answered 7/2, 2011 at 18:18 Comment(0)
D
6

Old question - but if you have new depreciations you'd like to selectively ignore:

ActiveSupport::Deprecation.behavior = lambda do |msg, stack| 
  unless /LIBRARY_NAME/ =~ msg
    ActiveSupport::Deprecation::DEFAULT_BEHAVIORS[:stderr].call(msg,stack) # whichever handlers you want - this is the default
  end
end

This is for ActiveSupport 3.

Dacha answered 29/3, 2012 at 13:28 Comment(0)
V
3

In fact I stil had lots of other deprecation warnings from code that was in plugins or gems I had installed. In order to avoid most of that, I overwrote the Deprecation::warn method in test_helper.rb. So instead of the previous code, use:

module ActiveSupport
  module Deprecation
    class << self
      def warn(message = nil, callstack = caller)
        # modif pvh the following lines make sure no deprecation warnings are sent 
        # for code that is
        # not by my but in some gem or plugin...
        return if silenced  || callstack.grep(/myrailsappname/).blank?
        # return if silenced 
        deprecation_message(callstack, message).tap do |m|
          behavior.each { |b| b.call(m, callstack) }
        end
      end
    end
  end
end  

BTW you need to replace myrailsappname with your app's name (the name of the folder it resides in). There is probably a more generic way to get that name, but I couldn't find it right now.

Vermicelli answered 7/2, 2011 at 18:18 Comment(0)
C
2

Can I recommend an alternative?

module ActiveSupport
  class Deprecation
    module Reporting
      # Mute specific deprecation messages
      def warn(message = nil, callstack = nil)
        return if message.match(/Automatic updating of counter caches/)

        super
      end
    end
  end
end
Constriction answered 21/6, 2015 at 0:23 Comment(0)
V
0

I think I have found a solution: in test/test_helper.rb I reopened the module and overwrote macro definition with an identical definition but the deprecation warning commented out. There are probably much more elegant ways to do this though...

# modif pvh DEPREC
# reopen the module and silence the deprecation statement to avoid 
# having my results overflown by these deprecation warnings...
module Shoulda # :nodoc:
  module Macros
    def should_create(class_name)
      ##::ActiveSupport::Deprecation.warn
      should_change_record_count_of(class_name, 1, 'create')
    end
  end
end   
Vermicelli answered 4/2, 2011 at 18:31 Comment(0)
B
0

Create a file called selective_deprecation_silencer.rb in your config/initializers folder with the following content:

#place in the following array the messages you want to silence
silenced = [/Using a dynamic :action segment in a route is deprecated/,
        /Using a dynamic :controller segment in a route is deprecated/] 

silenced_expr = Regexp.new(silenced.join('|'))

ActiveSupport::Deprecation.behavior = lambda do |msg, stack, deprecation_horizon, gem_name|
  unless msg =~ silenced_expr
    ActiveSupport::Deprecation::DEFAULT_BEHAVIORS[:stderr].call(msg, stack, deprecation_horizon, gem_name)
  end
end
Boson answered 3/6, 2022 at 8:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.