From 7b87fe21355a29e606277a8f24b19c4829ee3948 Mon Sep 17 00:00:00 2001 From: Alexander Momchilov Date: Fri, 2 Jan 2026 18:02:40 -0500 Subject: [PATCH] Silence expected warning about `__id__` definition --- spec/spec_helper.rb | 43 +++++++++++++++++++++++++ spec/tapioca/runtime/reflection_spec.rb | 14 ++++++-- 2 files changed, 55 insertions(+), 2 deletions(-) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index d781ce4c0..3e465991e 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -29,3 +29,46 @@ class Test extend Rails::LineFiltering end end + +module WarnMonkeyPatch + # extend T::Sig + + # Detects the file, line number and "warning: " prefix at the + # start of every warning message, for us to chop off. + # Users can get the file/line from the backtrace. + WARNING_PREFIX_PATTERN = /^.+?:\d+: warning: / + + #: (String message, ?category: Symbol?, **top) -> void + def warn(message, category: nil, **kwargs) + if Thread.current[:__raise_warnings_as_exceptions] + message.sub!(WARNING_PREFIX_PATTERN, "") + message.chomp! + Kernel.raise Warning::Exception, message + else + super + end + end + + # sig do + # type_parameters(:R) + # .params(block: T.proc.returns(T.type_parameter(:R))) + # .returns(T.type_parameter(:R)) + # end + #: [R] { -> R } -> R + def raise_warnings_as_exceptions(&block) + previous_state = Thread.current[:__raise_warnings_as_exceptions] + Thread.current[:__raise_warnings_as_exceptions] = true + block.call + ensure + Thread.current[:__raise_warnings_as_exceptions] = previous_state + end +end + +module ::Warning + extend T::Helpers + + Exception = Class.new(StandardError) + + singleton_class.prepend(WarnMonkeyPatch) + mixes_in_class_methods(WarnMonkeyPatch) +end \ No newline at end of file diff --git a/spec/tapioca/runtime/reflection_spec.rb b/spec/tapioca/runtime/reflection_spec.rb index 0b087d72f..8550743ae 100644 --- a/spec/tapioca/runtime/reflection_spec.rb +++ b/spec/tapioca/runtime/reflection_spec.rb @@ -52,8 +52,18 @@ def class ::String end - def __id__ # rubocop:disable Naming/MethodName - 1 + begin + ::Warning.raise_warnings_as_exceptions do + def __id__ # rubocop:disable Naming/MethodName + 1 + end + end + rescue ::Warning::Exception => e + if e.message == "redefining '__id__' may cause serious problems" + # We know, that's why we're testing this :) + else + raise + end end def equal?(other)