Skip to content

Commit 3b7b6f6

Browse files
committed
[Fix rubocop#12910] Ensure that RuboCop runs warning-free
This is inspired by how Rails approaches this
1 parent a9bce34 commit 3b7b6f6

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

spec/spec_helper.rb

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
require 'rainbow'
55
Rainbow.enabled = false
66

7+
require_relative 'support/strict_warnings'
8+
StrictWarnings.enable!
9+
710
require 'rubocop'
811
require 'rubocop/cop/internal_affairs'
912
require 'rubocop/server'

spec/support/strict_warnings.rb

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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

Comments
 (0)