From f505cee07afe05eafadb95baadc74bece7cb5089 Mon Sep 17 00:00:00 2001 From: Earlopain <14981592+Earlopain@users.noreply.github.com> Date: Mon, 27 May 2024 22:55:04 +0200 Subject: [PATCH] [Fix #12910] Ensure that RuboCop runs warning-free This is inspired by how Rails approaches this --- spec/spec_helper.rb | 3 +++ spec/support/strict_warnings.rb | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 spec/support/strict_warnings.rb diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index d65c777089a2..f643ca4fe93b 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -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' diff --git a/spec/support/strict_warnings.rb b/spec/support/strict_warnings.rb new file mode 100644 index 000000000000..86dc87913d9b --- /dev/null +++ b/spec/support/strict_warnings.rb @@ -0,0 +1,32 @@ +# frozen_string_literal: true + +module StrictWarnings + class WarningError < StandardError; end + + # Warnings from 3rd-party gems + SUPPRESSED_WARNINGS = Regexp.union( + %r{lib/parser/builders/default.*Unknown escape}, + %r{lib/parser/builders/default.*character class has duplicated range}, + /internal.*Float.*out of range/ # also from the parser gem + ) + + 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 + end + + def self.enable! + $VERBOSE = true + Warning[:deprecated] = true + Warning.singleton_class.prepend(self) + end +end