@@ -82,5 +82,128 @@ class WarningTest < ActiveSupport::TestCase
8282 assert_match ( /Using the last argument as keyword parameters/ , error . message )
8383 assert_match ( /The called method/ , error . message )
8484 end
85+
86+ test "'Ruby 2.7 last argument as keyword parameters' real deprecation warning is handled with normalized paths" do
87+ Configuration . warnings_treated_as_deprecation = [ /Using the last argument as keyword parameters/ ]
88+
89+ error = assert_raises ( Behaviors ::DeprecationIntroduced ) do
90+ warn ( "#{ Dir . pwd } /path/to/caller.rb:1: warning: Using the last argument as keyword parameters is deprecated; " \
91+ "maybe ** should be added to the call" )
92+ warn ( "#{ Dir . pwd } /path/to/callee.rb:1: warning: The called method `method_name' is defined here" )
93+
94+ trigger_deprecation_toolkit_behavior
95+ end
96+
97+ assert_match ( %r{^DEPRECATION WARNING: path/to/caller\. rb:1: warning: Using the last} ,
98+ error . message )
99+ assert_match ( %r{^path/to/callee\. rb:1: warning: The called method} , error . message )
100+ end
101+
102+ test "`assert_nil` real deprecation warning is handled with normalized paths" do
103+ Configuration . warnings_treated_as_deprecation = [ /Use assert_nil if expecting nil/ ]
104+
105+ error = assert_raises ( Behaviors ::DeprecationIntroduced ) do
106+ warn ( "Use assert_nil if expecting nil from #{ Dir . pwd } /path/to/file.rb:1. This will fail in Minitest 6." )
107+
108+ trigger_deprecation_toolkit_behavior
109+ end
110+
111+ assert_match (
112+ %r{^DEPRECATION WARNING: Use assert_nil if expecting nil from path/to/file\. rb:1} , error . message
113+ )
114+ end
115+
116+ test "the path to warn itself is handled too" do
117+ Configuration . warnings_treated_as_deprecation = [ /boom/ ]
118+
119+ error = assert_raises ( Behaviors ::DeprecationIntroduced ) do
120+ warn ( "boom" )
121+
122+ trigger_deprecation_toolkit_behavior
123+ end
124+
125+ assert_includes ( error . message , <<~MESSAGE . chomp )
126+ DEPRECATION WARNING: boom
127+ (called from call at <RUBY_INTERNALS>/rubygems/core_ext/kernel_warn.rb:22)
128+ MESSAGE
129+ end
130+
131+ test "Rails.root is normalized in deprecation messages" do
132+ rails_stub = Object . new
133+ rails_stub . define_singleton_method ( :inspect ) { "Rails (stub)" }
134+ rails_stub . define_singleton_method ( :root ) { "/path/to/rails/root" }
135+
136+ original_rails = defined? ( ::Rails ) && ::Rails
137+ Object . const_set ( :Rails , rails_stub )
138+
139+ assert_normalizes (
140+ from : "#{ Rails . root } /app/models/whatever.rb" ,
141+ to : "app/models/whatever.rb" ,
142+ )
143+ ensure
144+ if original_rails . nil?
145+ Object . send ( :remove_const , :Rails )
146+ else
147+ Object . const_set ( :Rails , original_rails )
148+ end
149+ end
150+
151+ test "Bundler.root is normalized in deprecation messages" do
152+ assert_normalizes (
153+ from : "#{ Bundler . root } /lib/whatever.rb" ,
154+ to : "lib/whatever.rb" ,
155+ )
156+ end
157+
158+ test "Gem spec gem_dirs are normalized in deprecation messages" do
159+ spec = Gem . loaded_specs . each_value . first
160+ assert_normalizes (
161+ from : "#{ spec . gem_dir } /lib/whatever.rb" ,
162+ to : "<GEM_DIR:#{ spec . name } >/lib/whatever.rb" ,
163+ )
164+ end
165+
166+ test "Gem spec extension_dirs are normalized in deprecation messages" do
167+ spec = Gem . loaded_specs . each_value . first
168+ assert_normalizes (
169+ from : "#{ spec . extension_dir } /lib/whatever.rb" ,
170+ to : "<GEM_EXTENSION_DIR:#{ spec . name } >/lib/whatever.rb" ,
171+ )
172+ end
173+
174+ test "Gem spec bin_dirs are normalized in deprecation messages" do
175+ spec = Gem . loaded_specs . each_value . first
176+ assert_normalizes (
177+ from : "#{ spec . bin_dir } /lib/whatever.rb" ,
178+ to : "<GEM_BIN_DIR:#{ spec . name } >/lib/whatever.rb" ,
179+ )
180+ end
181+
182+ test "Gem paths are normalized in deprecation messages" do
183+ paths = Gem . path
184+ assert_normalizes (
185+ from : paths . map . with_index { |path , index | "#{ path } /file-#{ index } " } . join ( "\n " ) ,
186+ to : Array . new ( paths . length ) { |index | "<GEM_PATH>/file-#{ index } " } . join ( "\n " ) ,
187+ )
188+ end
189+
190+ test "RbConfig paths are normalized in deprecation messages" do
191+ paths = RbConfig ::CONFIG . values_at ( "prefix" , "sitelibdir" , "rubylibdir" ) . compact
192+ assert_normalizes (
193+ from : paths . map . with_index { |path , index | "#{ path } /file-#{ index } " } . join ( "\n " ) ,
194+ to : Array . new ( paths . length ) { |index | "<RUBY_INTERNALS>/file-#{ index } " } . join ( "\n " ) ,
195+ )
196+ end
197+
198+ private
199+
200+ def assert_normalizes ( from :, to :)
201+ Configuration . warnings_treated_as_deprecation = [ /test deprecation/ ]
202+ error = assert_raises ( Behaviors ::DeprecationIntroduced ) do
203+ warn ( "test deprecation: #{ from } ." )
204+ trigger_deprecation_toolkit_behavior
205+ end
206+ assert_includes ( error . message , to )
207+ end
85208 end
86209end
0 commit comments