|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +# Ensure that RuboCop runs warning-free. This hooks into Ruby's `warn` |
| 4 | +# method and raises when an unexpected warning is encountered. |
| 5 | +module StrictWarnings |
| 6 | + class WarningError < StandardError; end |
| 7 | + |
| 8 | + # Warnings from 3rd-party gems, or other things that are unactionable |
| 9 | + SUPPRESSED_WARNINGS = Regexp.union( |
| 10 | + %r{lib/parser/builders/default.*Unknown escape}, |
| 11 | + %r{lib/parser/builders/default.*character class has duplicated range}, |
| 12 | + /Float.*out of range/, # also from the parser gem |
| 13 | + /`Process` does not respond to `fork` method/, # JRuby |
| 14 | + /File#readline accesses caller method's state and should not be aliased/, # JRuby, test stub |
| 15 | + /instance variable @.* not initialized/ # Ruby 2.7 |
| 16 | + ) |
| 17 | + |
| 18 | + def warn(message, ...) |
| 19 | + return if SUPPRESSED_WARNINGS.match?(message) |
| 20 | + |
| 21 | + super |
| 22 | + # RuboCop uses `warn` to display some of its output and tests assert against |
| 23 | + # that. Assume that warnings are intentional when stderr is redirected. |
| 24 | + return if $stderr.is_a?(StringIO) |
| 25 | + # Ignore warnings from dev/rc ruby versions. Things are subject to change and |
| 26 | + # contributors should not be bothered by them with red CI. |
| 27 | + return if RUBY_PATCHLEVEL == -1 |
| 28 | + |
| 29 | + raise WarningError, message |
| 30 | + end |
| 31 | + |
| 32 | + def self.enable! |
| 33 | + $VERBOSE = true |
| 34 | + Warning[:deprecated] = true |
| 35 | + Warning.singleton_class.prepend(self) |
| 36 | + end |
| 37 | +end |
0 commit comments