DEV: Prevent clearing plugin modifiers during plugin spec runs (#21359)

Clearing modifiers during a plugin spec run will affect all future specs. Instead, this commit introduces a more surgical `.unregister_modifier` API which plugins can use if they need to add/remove a modifier during a specific spec.
This commit is contained in:
David Taylor 2023-05-03 10:18:08 +01:00 committed by GitHub
parent ed077cf3f1
commit c1c50cb90b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 15 additions and 0 deletions

View File

@ -247,6 +247,9 @@ class DiscoursePluginRegistry
end
def self.clear_modifiers!
if Rails.env.test? && GlobalSetting.load_plugins?
raise "Clearing modifiers during a plugin spec run will affect all future specs. Use unregister_modifier instead."
end
@modifiers = nil
end
@ -256,6 +259,18 @@ class DiscoursePluginRegistry
modifiers << [plugin_instance, blk]
end
def self.unregister_modifier(plugin_instance, name, &blk)
raise "unregister_modifier can only be used in tests" if !Rails.env.test?
modifiers_for_name = @modifiers&.[](name)
raise "no #{name} modifiers found" if !modifiers_for_name
i = modifiers_for_name.find_index { |info| info == [plugin_instance, blk] }
raise "no modifier found for that plugin/block combination" if !i
modifiers_for_name.delete_at(i)
end
def self.apply_modifier(name, arg, *more_args)
return arg if !@modifiers