Skip to content

Commit

Permalink
[Fix rubocop#12910] Ensure that RuboCop runs warning-free
Browse files Browse the repository at this point in the history
This is inspired by how Rails approaches this
  • Loading branch information
Earlopain authored and bbatsov committed May 29, 2024
1 parent 1cfe0b3 commit 54421d6
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
3 changes: 3 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
require 'rainbow'
Rainbow.enabled = false

require_relative 'support/strict_warnings'
StrictWarnings.enable!

require 'rubocop'
require 'rubocop/cop/internal_affairs'
require 'rubocop/server'
Expand Down
37 changes: 37 additions & 0 deletions spec/support/strict_warnings.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# frozen_string_literal: true

# Ensure that RuboCop runs warning-free. This hooks into Ruby's `warn`
# method and raises when an unexpected warning is encountered.
module StrictWarnings
class WarningError < StandardError; end

# Warnings from 3rd-party gems, or other things that are unactionable
SUPPRESSED_WARNINGS = Regexp.union(
%r{lib/parser/builders/default.*Unknown escape},
%r{lib/parser/builders/default.*character class has duplicated range},
/Float.*out of range/, # also from the parser gem
/`Process` does not respond to `fork` method/, # JRuby
/File#readline accesses caller method's state and should not be aliased/, # JRuby, test stub
/instance variable @.* not initialized/ # Ruby 2.7
)

def warn(message, ...)
return if SUPPRESSED_WARNINGS.match?(message)

super
# RuboCop uses `warn` to display some of its output and tests assert against
# that. Assume that warnings are intentional when stderr is redirected.
return if $stderr.is_a?(StringIO)
# Ignore warnings from dev/rc ruby versions. Things are subject to change and
# contributors should not be bothered by them with red CI.
return if RUBY_PATCHLEVEL == -1

raise WarningError, message
end

def self.enable!
$VERBOSE = true
Warning[:deprecated] = true
Warning.singleton_class.prepend(self)
end
end

0 comments on commit 54421d6

Please sign in to comment.