activesupport 7.2.3.1 → 8.1.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (135) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +584 -234
  3. data/lib/active_support/backtrace_cleaner.rb +72 -1
  4. data/lib/active_support/benchmark.rb +21 -0
  5. data/lib/active_support/benchmarkable.rb +3 -2
  6. data/lib/active_support/cache/file_store.rb +13 -2
  7. data/lib/active_support/cache/mem_cache_store.rb +17 -14
  8. data/lib/active_support/cache/memory_store.rb +9 -3
  9. data/lib/active_support/cache/redis_cache_store.rb +51 -34
  10. data/lib/active_support/cache/strategy/local_cache.rb +17 -8
  11. data/lib/active_support/cache/strategy/local_cache_middleware.rb +7 -7
  12. data/lib/active_support/cache.rb +86 -18
  13. data/lib/active_support/callbacks.rb +25 -11
  14. data/lib/active_support/class_attribute.rb +33 -0
  15. data/lib/active_support/code_generator.rb +9 -0
  16. data/lib/active_support/concurrency/load_interlock_aware_monitor.rb +8 -62
  17. data/lib/active_support/concurrency/share_lock.rb +0 -1
  18. data/lib/active_support/concurrency/thread_monitor.rb +55 -0
  19. data/lib/active_support/configurable.rb +34 -0
  20. data/lib/active_support/configuration_file.rb +15 -6
  21. data/lib/active_support/continuous_integration.rb +145 -0
  22. data/lib/active_support/core_ext/array/conversions.rb +3 -3
  23. data/lib/active_support/core_ext/array.rb +7 -7
  24. data/lib/active_support/core_ext/benchmark.rb +0 -16
  25. data/lib/active_support/core_ext/big_decimal.rb +1 -1
  26. data/lib/active_support/core_ext/class/attribute.rb +24 -18
  27. data/lib/active_support/core_ext/class.rb +2 -2
  28. data/lib/active_support/core_ext/date/conversions.rb +2 -0
  29. data/lib/active_support/core_ext/date.rb +5 -5
  30. data/lib/active_support/core_ext/date_and_time/compatibility.rb +0 -35
  31. data/lib/active_support/core_ext/date_time/calculations.rb +1 -0
  32. data/lib/active_support/core_ext/date_time/compatibility.rb +3 -5
  33. data/lib/active_support/core_ext/date_time.rb +5 -5
  34. data/lib/active_support/core_ext/digest.rb +1 -1
  35. data/lib/active_support/core_ext/enumerable.rb +8 -3
  36. data/lib/active_support/core_ext/erb/util.rb +3 -3
  37. data/lib/active_support/core_ext/file.rb +1 -1
  38. data/lib/active_support/core_ext/hash/deep_merge.rb +1 -0
  39. data/lib/active_support/core_ext/hash/except.rb +0 -12
  40. data/lib/active_support/core_ext/hash.rb +8 -8
  41. data/lib/active_support/core_ext/integer.rb +3 -3
  42. data/lib/active_support/core_ext/kernel.rb +3 -3
  43. data/lib/active_support/core_ext/module/attr_internal.rb +3 -4
  44. data/lib/active_support/core_ext/module.rb +11 -11
  45. data/lib/active_support/core_ext/numeric.rb +3 -3
  46. data/lib/active_support/core_ext/object/instance_variables.rb +1 -1
  47. data/lib/active_support/core_ext/object/json.rb +24 -11
  48. data/lib/active_support/core_ext/object/to_query.rb +7 -1
  49. data/lib/active_support/core_ext/object.rb +13 -13
  50. data/lib/active_support/core_ext/pathname.rb +2 -2
  51. data/lib/active_support/core_ext/range/compare_range.rb +2 -14
  52. data/lib/active_support/core_ext/range/overlap.rb +3 -3
  53. data/lib/active_support/core_ext/range/sole.rb +2 -0
  54. data/lib/active_support/core_ext/range.rb +4 -5
  55. data/lib/active_support/core_ext/string/filters.rb +2 -0
  56. data/lib/active_support/core_ext/string/inflections.rb +1 -1
  57. data/lib/active_support/core_ext/string/multibyte.rb +10 -1
  58. data/lib/active_support/core_ext/string/output_safety.rb +27 -13
  59. data/lib/active_support/core_ext/string.rb +13 -13
  60. data/lib/active_support/core_ext/symbol.rb +1 -1
  61. data/lib/active_support/core_ext/thread/backtrace/location.rb +2 -7
  62. data/lib/active_support/core_ext/time/calculations.rb +11 -2
  63. data/lib/active_support/core_ext/time/compatibility.rb +2 -27
  64. data/lib/active_support/core_ext/time/conversions.rb +2 -0
  65. data/lib/active_support/core_ext/time.rb +5 -5
  66. data/lib/active_support/current_attributes/test_helper.rb +2 -2
  67. data/lib/active_support/current_attributes.rb +13 -10
  68. data/lib/active_support/delegation.rb +25 -44
  69. data/lib/active_support/dependencies/interlock.rb +11 -5
  70. data/lib/active_support/dependencies.rb +6 -2
  71. data/lib/active_support/deprecation/reporting.rb +4 -21
  72. data/lib/active_support/deprecation.rb +1 -1
  73. data/lib/active_support/duration.rb +20 -16
  74. data/lib/active_support/editor.rb +70 -0
  75. data/lib/active_support/encrypted_configuration.rb +20 -2
  76. data/lib/active_support/error_reporter.rb +76 -2
  77. data/lib/active_support/event_reporter/test_helper.rb +32 -0
  78. data/lib/active_support/event_reporter.rb +592 -0
  79. data/lib/active_support/evented_file_update_checker.rb +5 -2
  80. data/lib/active_support/execution_context.rb +75 -7
  81. data/lib/active_support/file_update_checker.rb +7 -5
  82. data/lib/active_support/gem_version.rb +3 -3
  83. data/lib/active_support/gzip.rb +1 -0
  84. data/lib/active_support/hash_with_indifferent_access.rb +44 -24
  85. data/lib/active_support/i18n_railtie.rb +19 -11
  86. data/lib/active_support/inflector/inflections.rb +34 -16
  87. data/lib/active_support/inflector/methods.rb +6 -22
  88. data/lib/active_support/inflector/transliterate.rb +18 -10
  89. data/lib/active_support/isolated_execution_state.rb +17 -17
  90. data/lib/active_support/json/decoding.rb +9 -3
  91. data/lib/active_support/json/encoding.rb +149 -19
  92. data/lib/active_support/log_subscriber.rb +2 -6
  93. data/lib/active_support/logger_thread_safe_level.rb +6 -3
  94. data/lib/active_support/message_encryptors.rb +52 -0
  95. data/lib/active_support/message_pack/extensions.rb +23 -1
  96. data/lib/active_support/message_verifiers.rb +52 -0
  97. data/lib/active_support/messages/rotation_coordinator.rb +9 -0
  98. data/lib/active_support/messages/rotator.rb +5 -0
  99. data/lib/active_support/messages/serializer_with_fallback.rb +1 -1
  100. data/lib/active_support/multibyte/chars.rb +8 -1
  101. data/lib/active_support/multibyte.rb +4 -0
  102. data/lib/active_support/notifications/fanout.rb +64 -43
  103. data/lib/active_support/notifications/instrumenter.rb +1 -1
  104. data/lib/active_support/notifications.rb +2 -2
  105. data/lib/active_support/number_helper/number_converter.rb +4 -1
  106. data/lib/active_support/number_helper/number_to_currency_converter.rb +1 -1
  107. data/lib/active_support/number_helper/number_to_delimited_converter.rb +3 -4
  108. data/lib/active_support/number_helper/number_to_phone_converter.rb +3 -7
  109. data/lib/active_support/number_helper/rounding_helper.rb +1 -1
  110. data/lib/active_support/number_helper.rb +22 -0
  111. data/lib/active_support/ordered_options.rb +2 -2
  112. data/lib/active_support/railtie.rb +32 -9
  113. data/lib/active_support/security_utils.rb +4 -5
  114. data/lib/active_support/structured_event_subscriber.rb +99 -0
  115. data/lib/active_support/subscriber.rb +0 -5
  116. data/lib/active_support/syntax_error_proxy.rb +9 -1
  117. data/lib/active_support/tagged_logging.rb +5 -0
  118. data/lib/active_support/test_case.rb +67 -6
  119. data/lib/active_support/testing/assertions.rb +118 -27
  120. data/lib/active_support/testing/autorun.rb +5 -0
  121. data/lib/active_support/testing/error_reporter_assertions.rb +17 -0
  122. data/lib/active_support/testing/event_reporter_assertions.rb +227 -0
  123. data/lib/active_support/testing/isolation.rb +0 -2
  124. data/lib/active_support/testing/notification_assertions.rb +92 -0
  125. data/lib/active_support/testing/parallelization/worker.rb +7 -1
  126. data/lib/active_support/testing/parallelization.rb +17 -0
  127. data/lib/active_support/testing/tests_without_assertions.rb +1 -1
  128. data/lib/active_support/testing/time_helpers.rb +9 -4
  129. data/lib/active_support/time_with_zone.rb +36 -23
  130. data/lib/active_support/values/time_zone.rb +22 -13
  131. data/lib/active_support/xml_mini.rb +1 -2
  132. data/lib/active_support.rb +21 -9
  133. metadata +33 -17
  134. data/lib/active_support/core_ext/range/each.rb +0 -24
  135. data/lib/active_support/proxy_object.rb +0 -20
@@ -6,6 +6,7 @@ require "active_support/testing/setup_and_teardown"
6
6
  require "active_support/testing/tests_without_assertions"
7
7
  require "active_support/testing/assertions"
8
8
  require "active_support/testing/error_reporter_assertions"
9
+ require "active_support/testing/event_reporter_assertions"
9
10
  require "active_support/testing/deprecation"
10
11
  require "active_support/testing/declarative"
11
12
  require "active_support/testing/isolation"
@@ -15,13 +16,29 @@ require "active_support/testing/constant_stubbing"
15
16
  require "active_support/testing/file_fixtures"
16
17
  require "active_support/testing/parallelization"
17
18
  require "active_support/testing/parallelize_executor"
19
+ require "active_support/testing/notification_assertions"
18
20
  require "concurrent/utility/processor_counter"
19
21
 
20
22
  module ActiveSupport
21
23
  class TestCase < ::Minitest::Test
22
24
  Assertion = Minitest::Assertion
23
25
 
26
+ # Class variable to store the parallel worker ID
27
+ @@parallel_worker_id = nil
28
+
24
29
  class << self
30
+ # Returns the current parallel worker ID if tests are running in parallel,
31
+ # nil otherwise.
32
+ #
33
+ # ActiveSupport::TestCase.parallel_worker_id # => 2
34
+ def parallel_worker_id
35
+ @@parallel_worker_id
36
+ end
37
+
38
+ def parallel_worker_id=(value) # :nodoc:
39
+ @@parallel_worker_id = value
40
+ end
41
+
25
42
  # Sets the order in which test cases are run.
26
43
  #
27
44
  # ActiveSupport::TestCase.test_order = :random # => :random
@@ -45,14 +62,20 @@ module ActiveSupport
45
62
  ActiveSupport.test_order ||= :random
46
63
  end
47
64
 
65
+ if Minitest::Runnable.respond_to? :run_order # MT6 API change
66
+ def run_order # :nodoc:
67
+ test_order
68
+ end
69
+ end
70
+
48
71
  # Parallelizes the test suite.
49
72
  #
50
73
  # Takes a +workers+ argument that controls how many times the process
51
74
  # is forked. For each process a new database will be created suffixed
52
75
  # with the worker number.
53
76
  #
54
- # test-database-0
55
- # test-database-1
77
+ # test-database_0
78
+ # test-database_1
56
79
  #
57
80
  # If <tt>ENV["PARALLEL_WORKERS"]</tt> is set the workers argument will be ignored
58
81
  # and the environment variable will be used instead. This is useful for CI
@@ -78,14 +101,45 @@ module ActiveSupport
78
101
  # Because parallelization presents an overhead, it is only enabled when the
79
102
  # number of tests to run is above the +threshold+ param. The default value is
80
103
  # 50, and it's configurable via +config.active_support.test_parallelization_threshold+.
81
- def parallelize(workers: :number_of_processors, with: :processes, threshold: ActiveSupport.test_parallelization_threshold)
82
- workers = Concurrent.processor_count if workers == :number_of_processors
83
- workers = ENV["PARALLEL_WORKERS"].to_i if ENV["PARALLEL_WORKERS"]
104
+ #
105
+ # If you want to skip Rails default creation of one database per process in favor of
106
+ # writing your own implementation, you can set +parallelize_databases+, or configure it
107
+ # via +config.active_support.parallelize_test_databases+.
108
+ #
109
+ # parallelize(workers: :number_of_processors, parallelize_databases: false)
110
+ #
111
+ # Note that your test suite may deadlock if you attempt to use only one database
112
+ # with multiple processes.
113
+ def parallelize(workers: :number_of_processors, with: :processes, threshold: ActiveSupport.test_parallelization_threshold, parallelize_databases: ActiveSupport.parallelize_test_databases)
114
+ case
115
+ when ENV["PARALLEL_WORKERS"]
116
+ workers = ENV["PARALLEL_WORKERS"].to_i
117
+ when workers == :number_of_processors
118
+ workers = (Concurrent.available_processor_count || Concurrent.processor_count).floor
119
+ end
120
+
121
+ if with == :processes
122
+ ActiveSupport.parallelize_test_databases = parallelize_databases
123
+ end
84
124
 
85
125
  Minitest.parallel_executor = ActiveSupport::Testing::ParallelizeExecutor.new(size: workers, with: with, threshold: threshold)
86
126
  end
87
127
 
88
- # Set up hook for parallel testing. This can be used if you have multiple
128
+ # Before fork hook for parallel testing. This can be used to run anything
129
+ # before the processes are forked.
130
+ #
131
+ # In your +test_helper.rb+ add the following:
132
+ #
133
+ # class ActiveSupport::TestCase
134
+ # parallelize_before_fork do
135
+ # # run this before fork
136
+ # end
137
+ # end
138
+ def parallelize_before_fork(&block)
139
+ ActiveSupport::Testing::Parallelization.before_fork_hook(&block)
140
+ end
141
+
142
+ # Setup hook for parallel testing. This can be used if you have multiple
89
143
  # databases or any behavior that needs to be run after the process is forked
90
144
  # but before the tests run.
91
145
  #
@@ -141,11 +195,18 @@ module ActiveSupport
141
195
 
142
196
  alias_method :method_name, :name
143
197
 
198
+ # Returns the current parallel worker ID if tests are running in parallel
199
+ def parallel_worker_id
200
+ self.class.parallel_worker_id
201
+ end
202
+
144
203
  include ActiveSupport::Testing::TaggedLogging
145
204
  prepend ActiveSupport::Testing::SetupAndTeardown
146
205
  prepend ActiveSupport::Testing::TestsWithoutAssertions
147
206
  include ActiveSupport::Testing::Assertions
148
207
  include ActiveSupport::Testing::ErrorReporterAssertions
208
+ include ActiveSupport::Testing::EventReporterAssertions
209
+ include ActiveSupport::Testing::NotificationAssertions
149
210
  include ActiveSupport::Testing::Deprecation
150
211
  include ActiveSupport::Testing::ConstantStubbing
151
212
  include ActiveSupport::Testing::TimeHelpers
@@ -19,7 +19,7 @@ module ActiveSupport
19
19
  #
20
20
  # assert_not foo, 'foo should be false'
21
21
  def assert_not(object, message = nil)
22
- message ||= "Expected #{mu_pp(object)} to be nil or false"
22
+ message ||= -> { "Expected #{mu_pp(object)} to be nil or false" }
23
23
  assert !object, message
24
24
  end
25
25
 
@@ -71,19 +71,19 @@ module ActiveSupport
71
71
  # post :delete, params: { id: ... }
72
72
  # end
73
73
  #
74
- # An array of expressions can also be passed in and evaluated.
74
+ # An array of expressions can be passed in and evaluated.
75
75
  #
76
76
  # assert_difference [ 'Article.count', 'Post.count' ], 2 do
77
77
  # post :create, params: { article: {...} }
78
78
  # end
79
79
  #
80
- # A hash of expressions/numeric differences can also be passed in and evaluated.
80
+ # A hash of expressions/numeric differences can be passed in and evaluated.
81
81
  #
82
- # assert_difference ->{ Article.count } => 1, ->{ Notification.count } => 2 do
82
+ # assert_difference({ 'Article.count' => 1, 'Notification.count' => 2 }) do
83
83
  # post :create, params: { article: {...} }
84
84
  # end
85
85
  #
86
- # A lambda or a list of lambdas can be passed in and evaluated:
86
+ # A lambda, a list of lambdas or a hash of lambdas/numeric differences can be passed in and evaluated:
87
87
  #
88
88
  # assert_difference ->{ Article.count }, 2 do
89
89
  # post :create, params: { article: {...} }
@@ -93,6 +93,10 @@ module ActiveSupport
93
93
  # post :create, params: { article: {...} }
94
94
  # end
95
95
  #
96
+ # assert_difference ->{ Article.count } => 1, ->{ Notification.count } => 2 do
97
+ # post :create, params: { article: {...} }
98
+ # end
99
+ #
96
100
  # An error message can be specified.
97
101
  #
98
102
  # assert_difference 'Article.count', -1, 'An Article should be destroyed' do
@@ -118,9 +122,14 @@ module ActiveSupport
118
122
 
119
123
  expressions.zip(exps, before) do |(code, diff), exp, before_value|
120
124
  actual = exp.call
121
- error = "#{code.inspect} didn't change by #{diff}, but by #{actual - before_value}"
122
- error = "#{message}.\n#{error}" if message
123
- assert_equal(before_value + diff, actual, error)
125
+ rich_message = -> do
126
+ code_string = code.respond_to?(:call) ? _callable_to_source_string(code) : code
127
+ error = "`#{code_string}` didn't change by #{diff}, but by #{actual - before_value}."
128
+ error = "#{error}\n#{diff before_value + diff, actual}" if Minitest::VERSION > "6"
129
+ error = "#{message}.\n#{error}" if message
130
+ error
131
+ end
132
+ assert_equal(before_value + diff, actual, rich_message)
124
133
  end
125
134
 
126
135
  retval
@@ -177,12 +186,24 @@ module ActiveSupport
177
186
  #
178
187
  # The keyword arguments +:from+ and +:to+ can be given to specify the
179
188
  # expected initial value and the expected value after the block was
180
- # executed.
189
+ # executed. The comparison is done using case equality (===), which means
190
+ # you can specify patterns or classes:
181
191
  #
192
+ # # Exact value match
182
193
  # assert_changes :@object, from: nil, to: :foo do
183
194
  # @object = :foo
184
195
  # end
185
196
  #
197
+ # # Case equality
198
+ # assert_changes -> { user.token }, to: /\w{32}/ do
199
+ # user.generate_token
200
+ # end
201
+ #
202
+ # # Type check
203
+ # assert_changes -> { current_error }, from: nil, to: RuntimeError do
204
+ # raise "Oops"
205
+ # end
206
+ #
186
207
  # An error message can be specified.
187
208
  #
188
209
  # assert_changes -> { Status.all_good? }, 'Expected the status to be bad' do
@@ -195,22 +216,32 @@ module ActiveSupport
195
216
  retval = _assert_nothing_raised_or_warn("assert_changes", &block)
196
217
 
197
218
  unless from == UNTRACKED
198
- error = "Expected change from #{from.inspect}, got #{before.inspect}"
199
- error = "#{message}.\n#{error}" if message
200
- assert from === before, error
219
+ rich_message = -> do
220
+ error = "Expected change from #{from.inspect}, got #{before.inspect}"
221
+ error = "#{message}.\n#{error}" if message
222
+ error
223
+ end
224
+ assert from === before, rich_message
201
225
  end
202
226
 
203
227
  after = exp.call
204
228
 
205
- error = "#{expression.inspect} didn't change"
206
- error = "#{error}. It was already #{to.inspect}" if before == to
207
- error = "#{message}.\n#{error}" if message
208
- refute_equal before, after, error
229
+ rich_message = -> do
230
+ code_string = expression.respond_to?(:call) ? _callable_to_source_string(expression) : expression
231
+ error = "`#{code_string}` didn't change"
232
+ error = "#{error}. It was already #{to.inspect}." if before == to
233
+ error = "#{message}.\n#{error}" if message
234
+ error
235
+ end
236
+ refute_equal before, after, rich_message
209
237
 
210
238
  unless to == UNTRACKED
211
- error = "Expected change to #{to.inspect}, got #{after.inspect}\n"
212
- error = "#{message}.\n#{error}" if message
213
- assert to === after, error
239
+ rich_message = -> do
240
+ error = "Expected change to #{to.inspect}, got #{after.inspect}\n"
241
+ error = "#{message}.\n#{error}" if message
242
+ error
243
+ end
244
+ assert to === after, rich_message
214
245
  end
215
246
 
216
247
  retval
@@ -224,12 +255,24 @@ module ActiveSupport
224
255
  # end
225
256
  #
226
257
  # Provide the optional keyword argument +:from+ to specify the expected
227
- # initial value.
258
+ # initial value. The comparison is done using case equality (===), which means
259
+ # you can specify patterns or classes:
228
260
  #
261
+ # # Exact value match
229
262
  # assert_no_changes -> { Status.all_good? }, from: true do
230
263
  # post :create, params: { status: { ok: true } }
231
264
  # end
232
265
  #
266
+ # # Case equality
267
+ # assert_no_changes -> { user.token }, from: /\w{32}/ do
268
+ # user.touch
269
+ # end
270
+ #
271
+ # # Type check
272
+ # assert_no_changes -> { current_error }, from: RuntimeError do
273
+ # retry_operation
274
+ # end
275
+ #
233
276
  # An error message can be specified.
234
277
  #
235
278
  # assert_no_changes -> { Status.all_good? }, 'Expected the status to be good' do
@@ -242,20 +285,28 @@ module ActiveSupport
242
285
  retval = _assert_nothing_raised_or_warn("assert_no_changes", &block)
243
286
 
244
287
  unless from == UNTRACKED
245
- error = "Expected initial value of #{from.inspect}, got #{before.inspect}"
246
- error = "#{message}.\n#{error}" if message
247
- assert from === before, error
288
+ rich_message = -> do
289
+ error = "Expected initial value of #{from.inspect}, got #{before.inspect}"
290
+ error = "#{message}.\n#{error}" if message
291
+ error
292
+ end
293
+ assert from === before, rich_message
248
294
  end
249
295
 
250
296
  after = exp.call
251
297
 
252
- error = "#{expression.inspect} changed"
253
- error = "#{message}.\n#{error}" if message
298
+ rich_message = -> do
299
+ code_string = expression.respond_to?(:call) ? _callable_to_source_string(expression) : expression
300
+ error = "`#{code_string}` changed."
301
+ error = "#{message}.\n#{error}" if message
302
+ error = "#{error}\n#{diff before, after}" if Minitest::VERSION > "6"
303
+ error
304
+ end
254
305
 
255
306
  if before.nil?
256
- assert_nil after, error
307
+ assert_nil after, rich_message
257
308
  else
258
- assert_equal before, after, error
309
+ assert_equal before, after, rich_message
259
310
  end
260
311
 
261
312
  retval
@@ -276,6 +327,46 @@ module ActiveSupport
276
327
 
277
328
  raise
278
329
  end
330
+
331
+ def _callable_to_source_string(callable)
332
+ if defined?(RubyVM::InstructionSequence) && callable.is_a?(Proc)
333
+ iseq = RubyVM::InstructionSequence.of(callable)
334
+ source =
335
+ if iseq.script_lines
336
+ iseq.script_lines.join("\n")
337
+ elsif File.readable?(iseq.absolute_path)
338
+ File.read(iseq.absolute_path)
339
+ end
340
+
341
+ return callable unless source
342
+
343
+ location = iseq.to_a[4][:code_location]
344
+ return callable unless location
345
+
346
+ lines = source.lines[(location[0] - 1)..(location[2] - 1)]
347
+ lines[-1] = lines[-1].byteslice(...location[3])
348
+ lines[0] = lines[0].byteslice(location[1]...)
349
+ source = lines.join.strip
350
+
351
+ # Strip stabby lambda from Ruby 4.1+
352
+ source = source.sub(/^->\s*/, "")
353
+
354
+ # We ignore procs defined with do/end as they are likely multi-line anyway.
355
+ if source.start_with?("{")
356
+ source.delete_suffix!("}")
357
+ source.delete_prefix!("{")
358
+ source.strip!
359
+ # It won't read nice if the callable contains multiple
360
+ # lines, and it should be a rare occurrence anyway.
361
+ # Same if it takes arguments.
362
+ if !source.include?("\n") && !source.start_with?("|")
363
+ return source
364
+ end
365
+ end
366
+ end
367
+
368
+ callable
369
+ end
279
370
  end
280
371
  end
281
372
  end
@@ -2,4 +2,9 @@
2
2
 
3
3
  require "minitest"
4
4
 
5
+ # This respond_to check handles tests running sub-processes in an
6
+ # unbundled environment, which triggers MT5 usage. This conditional may
7
+ # be removable after the version bump, though it currently safeguards
8
+ # against issues in environments with multiple versions installed.
9
+ Minitest.load :rails if Minitest.respond_to? :load
5
10
  Minitest.autorun
@@ -102,6 +102,23 @@ module ActiveSupport
102
102
  assert(false, message)
103
103
  end
104
104
  end
105
+
106
+ # Captures reported errors from within the block that match the given
107
+ # error class.
108
+ #
109
+ # reports = capture_error_reports(IOError) do
110
+ # Rails.error.report(IOError.new("Oops"))
111
+ # Rails.error.report(IOError.new("Oh no"))
112
+ # Rails.error.report(StandardError.new)
113
+ # end
114
+ #
115
+ # assert_equal 2, reports.size
116
+ # assert_equal "Oops", reports.first.error.message
117
+ # assert_equal "Oh no", reports.last.error.message
118
+ def capture_error_reports(error_class = StandardError, &block)
119
+ reports = ErrorCollector.record(&block)
120
+ reports.select { |r| error_class === r.error }
121
+ end
105
122
  end
106
123
  end
107
124
  end
@@ -0,0 +1,227 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveSupport
4
+ module Testing
5
+ # Provides test helpers for asserting on ActiveSupport::EventReporter events.
6
+ module EventReporterAssertions
7
+ module EventCollector # :nodoc:
8
+ @subscribed = false
9
+ @mutex = Mutex.new
10
+
11
+ class Event # :nodoc:
12
+ attr_reader :event_data
13
+
14
+ def initialize(event_data)
15
+ @event_data = event_data
16
+ end
17
+
18
+ def inspect
19
+ "#{event_data[:name]} (payload: #{event_data[:payload].inspect}, tags: #{event_data[:tags].inspect})"
20
+ end
21
+
22
+ def matches?(name, payload, tags)
23
+ return false unless name.to_s == event_data[:name]
24
+
25
+ if payload && payload.is_a?(Hash)
26
+ return false unless matches_hash?(payload, :payload)
27
+ end
28
+
29
+ return false unless matches_hash?(tags, :tags)
30
+ true
31
+ end
32
+
33
+ private
34
+ def matches_hash?(expected_hash, event_key)
35
+ expected_hash.all? do |k, v|
36
+ if v.is_a?(Regexp)
37
+ event_data.dig(event_key, k).to_s.match?(v)
38
+ else
39
+ event_data.dig(event_key, k) == v
40
+ end
41
+ end
42
+ end
43
+ end
44
+
45
+ class << self
46
+ def emit(event)
47
+ event_recorders&.each do |events|
48
+ events << Event.new(event)
49
+ end
50
+ true
51
+ end
52
+
53
+ def record
54
+ subscribe
55
+ events = []
56
+ event_recorders << events
57
+ begin
58
+ yield
59
+ events
60
+ ensure
61
+ event_recorders.delete_if { |r| events.equal?(r) }
62
+ end
63
+ end
64
+
65
+ private
66
+ def subscribe
67
+ return if @subscribed
68
+
69
+ @mutex.synchronize do
70
+ unless @subscribed
71
+ if ActiveSupport.event_reporter
72
+ ActiveSupport.event_reporter.subscribe(self)
73
+ @subscribed = true
74
+ else
75
+ raise Minitest::Assertion, "No event reporter is configured"
76
+ end
77
+ end
78
+ end
79
+ end
80
+
81
+ def event_recorders
82
+ ActiveSupport::IsolatedExecutionState[:active_support_event_reporter_assertions] ||= []
83
+ end
84
+ end
85
+ end
86
+
87
+ # Asserts that the block does not cause an event to be reported to +Rails.event+.
88
+ #
89
+ # If no name is provided, passes if evaluated code in the yielded block reports no events.
90
+ #
91
+ # assert_no_event_reported do
92
+ # service_that_does_not_report_events.perform
93
+ # end
94
+ #
95
+ # If a name is provided, passes if evaluated code in the yielded block reports no events
96
+ # with that name.
97
+ #
98
+ # assert_no_event_reported("user.created") do
99
+ # service_that_does_not_report_events.perform
100
+ # end
101
+ def assert_no_event_reported(name = nil, payload: {}, tags: {}, &block)
102
+ events = EventCollector.record(&block)
103
+
104
+ if name.nil?
105
+ assert_predicate(events, :empty?)
106
+ else
107
+ matching_event = events.find { |event| event.matches?(name, payload, tags) }
108
+ if matching_event
109
+ message = "Expected no '#{name}' event to be reported, but found:\n " \
110
+ "#{matching_event.inspect}"
111
+ flunk(message)
112
+ end
113
+ assert(true)
114
+ end
115
+ end
116
+
117
+ # Asserts that the block causes an event with the given name to be reported
118
+ # to +Rails.event+.
119
+ #
120
+ # Passes if the evaluated code in the yielded block reports a matching event.
121
+ #
122
+ # assert_event_reported("user.created") do
123
+ # Rails.event.notify("user.created", { id: 123 })
124
+ # end
125
+ #
126
+ # To test further details about the reported event, you can specify payload and tag matchers.
127
+ #
128
+ # assert_event_reported("user.created",
129
+ # payload: { id: 123, name: "John Doe" },
130
+ # tags: { request_id: /[0-9]+/ }
131
+ # ) do
132
+ # Rails.event.tagged(request_id: "123") do
133
+ # Rails.event.notify("user.created", { id: 123, name: "John Doe" })
134
+ # end
135
+ # end
136
+ #
137
+ # The matchers support partial matching - only the specified keys need to match.
138
+ #
139
+ # assert_event_reported("user.created", payload: { id: 123 }) do
140
+ # Rails.event.notify("user.created", { id: 123, name: "John Doe" })
141
+ # end
142
+ def assert_event_reported(name, payload: nil, tags: {}, &block)
143
+ events = EventCollector.record(&block)
144
+
145
+ if events.empty?
146
+ flunk("Expected an event to be reported, but there were no events reported.")
147
+ elsif (event = events.find { |event| event.matches?(name, payload, tags) })
148
+ assert(true)
149
+ event.event_data
150
+ else
151
+ message = "Expected an event to be reported matching:\n " \
152
+ "name: #{name.inspect}\n " \
153
+ "payload: #{payload.inspect}\n " \
154
+ "tags: #{tags.inspect}\n" \
155
+ "but none of the #{events.size} reported events matched:\n " \
156
+ "#{events.map(&:inspect).join("\n ")}"
157
+ flunk(message)
158
+ end
159
+ end
160
+
161
+ # Asserts that the provided events were reported, regardless of order.
162
+ #
163
+ # assert_events_reported([
164
+ # { name: "user.created", payload: { id: 123 } },
165
+ # { name: "email.sent", payload: { to: "user@example.com" } }
166
+ # ]) do
167
+ # create_user_and_send_welcome_email
168
+ # end
169
+ #
170
+ # Supports the same payload and tag matching as +assert_event_reported+.
171
+ #
172
+ # assert_events_reported([
173
+ # {
174
+ # name: "process.started",
175
+ # payload: { id: 123 },
176
+ # tags: { request_id: /[0-9]+/ }
177
+ # },
178
+ # { name: "process.completed" }
179
+ # ]) do
180
+ # Rails.event.tagged(request_id: "456") do
181
+ # start_and_complete_process(123)
182
+ # end
183
+ # end
184
+ def assert_events_reported(expected_events, &block)
185
+ events = EventCollector.record(&block)
186
+
187
+ if events.empty? && expected_events.size > 0
188
+ flunk("Expected #{expected_events.size} events to be reported, but there were no events reported.")
189
+ end
190
+
191
+ events_copy = events.dup
192
+
193
+ expected_events.each do |expected_event|
194
+ name = expected_event[:name]
195
+ payload = expected_event[:payload] || {}
196
+ tags = expected_event[:tags] || {}
197
+
198
+ matching_event_index = events_copy.find_index { |event| event.matches?(name, payload, tags) }
199
+
200
+ if matching_event_index
201
+ events_copy.delete_at(matching_event_index)
202
+ else
203
+ message = "Expected an event to be reported matching:\n " \
204
+ "name: #{name.inspect}\n " \
205
+ "payload: #{payload.inspect}\n " \
206
+ "tags: #{tags.inspect}\n" \
207
+ "but none of the #{events.size} reported events matched:\n " \
208
+ "#{events.map(&:inspect).join("\n ")}"
209
+ flunk(message)
210
+ end
211
+ end
212
+
213
+ assert(true)
214
+ end
215
+
216
+ # Allows debug events to be reported to +Rails.event+ for the duration of a given block.
217
+ #
218
+ # with_debug_event_reporting do
219
+ # service_that_reports_debug_events.perform
220
+ # end
221
+ #
222
+ def with_debug_event_reporting(&block)
223
+ ActiveSupport.event_reporter.with_debug(&block)
224
+ end
225
+ end
226
+ end
227
+ end
@@ -5,8 +5,6 @@ require "active_support/testing/parallelize_executor"
5
5
  module ActiveSupport
6
6
  module Testing
7
7
  module Isolation
8
- require "thread"
9
-
10
8
  SubprocessCrashed = Class.new(StandardError)
11
9
 
12
10
  def self.included(klass) # :nodoc: