Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Kernel#warn for deprecations #333

Closed
wants to merge 1 commit into from
Closed

Use Kernel#warn for deprecations #333

wants to merge 1 commit into from

Conversation

etiennebarrie
Copy link

That makes it possible to override Warning.warn starting with Ruby 2.5.0

See more at https://bugs.ruby-lang.org/issues/12299

That makes it possible to override Warning.warn starting with Ruby 2.5.0

See more at https://bugs.ruby-lang.org/issues/12299
@etiennebarrie
Copy link
Author

This only applies to deprecations, if you're interested I can look at implementing this for the mocking warnings as well.
There were no tests for Mocha::Deprecation so I didn't add one specifically but I think it could be done even if it changes a global, because the tests don't run in parallel.

@floehopper
Copy link
Member

I've only had a chance to read this very quickly and I'm not sure I understand. Can you give some example code demonstrating the problem you are trying to solve?

@floehopper floehopper self-assigned this Jun 5, 2018
@etiennebarrie
Copy link
Author

Using $stderr.puts does not respect the Ruby conventions which allow to set -W0 to silence warnings:

$ ruby -W0 -e 'warn "warning: not shown"'
# no output
$ ruby -e 'warn "warning: shown"'
warning: shown

https://idiosyncratic-ruby.com/3-ruby-can-you-speak-louder.html#command-line-options-for-debug-modes
https://mislav.net/2011/06/ruby-verbose-mode/

Using Kernel#warn gets us this feature for free, so we could remove Mocha::Deprecation.mode and change $VERBOSE directly in DeprecationDisabler.disable_deprecations.


Additionally, since Ruby 2.5.0, Kernel#warn calls Warning.warn in verbose mode, which lets you override it as described in feature 12299: https://bugs.ruby-lang.org/issues/12299

$ ruby -W0 -e 'def Warning.warn(warning) super("warning: #{warning}") end' -e 'warn "not shown"'
# no output
$ ruby -e 'def Warning.warn(warning) super("warning: #{warning}") end' -e 'warn "shown"'
warning: shown

I don't know if it's a good idea really, but my use case is to raise for warnings in CI/dev in a Rails app:

unless Rails.env.production?
  def Warning.warn(warning)
    raise warning
  end
end

@etiennebarrie
Copy link
Author

Fixed in 196970a. Thanks!

@etiennebarrie etiennebarrie deleted the use-kernel-warn branch August 2, 2018 18:18
@floehopper
Copy link
Member

@etiennebarrie Oh, cool! Sorry for not responding sooner and thanks for noticing the (accidental!) fix. I'll try to get a release including the fix shipped in the not too distant future.

@etiennebarrie
Copy link
Author

No problem. Thank you for your work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants