A platform for community discussion. Free, open, simple.
DEV: Use caller for plugin_file_from_fixtures (#26387)
Martin Brennan8e08a3b31fa3
diff --git a/lib/plugin/instance.rb b/lib/plugin/instance.rbindex aaa4bb27e61..00978bca2b8 100644--- a/lib/plugin/instance.rb+++ b/lib/plugin/instance.rb@@ -66,6 +66,11 @@ class Plugin::Instance}end+ def root_dir+ return if Rails.env.production?+ File.dirname(path)+ end+def seed_data@seed_data ||= HashWithIndifferentAccess.new({})enddiff --git a/spec/rails_helper.rb b/spec/rails_helper.rbindex 5be28c50b80..cb42b4fb0d0 100644--- a/spec/rails_helper.rb+++ b/spec/rails_helper.rb@@ -810,8 +810,32 @@ def file_from_fixtures(filename, directory = "images", root_path = "#{Rails.rootFile.new(tmp_file_path)end-def plugin_file_from_fixtures(plugin_directory, filename, directory = "images")- file_from_fixtures(filename, directory, "#{Rails.root}/plugins/#{plugin_directory}/spec/fixtures")+def plugin_file_from_fixtures(filename, directory = "images")+ # We [1] here instead of [0] because the first caller is the current method.+ #+ # /home/mb/repos/discourse-ai/spec/lib/modules/ai_bot/tools/discourse_meta_search_spec.rb:17:in `block (2 levels) in <main>'+ first_non_gem_caller = caller_locations.select { |loc| !loc.to_s.match?(/gems/) }[1]&.path+ raise StandardError.new("Could not find caller for fixture #{filename}") if !first_non_gem_caller++ # This is the full path of the plugin spec file that needs a fixture.+ # realpath makes sure we follow symlinks.+ #+ # #<Pathname:/home/mb/repos/discourse-ai/spec/lib/modules/ai_bot/tools/discourse_meta_search_spec.rb>+ plugin_caller_path = Pathname.new(first_non_gem_caller).realpath++ plugin_match =+ Discourse.plugins.find do |plugin|+ # realpath makes sure we follow symlinks+ plugin_caller_path.to_s.starts_with?(Pathname.new(plugin.root_dir).realpath.to_s)+ end++ if !plugin_match+ raise StandardError.new(+ "Could not find matching plugin for #{plugin_caller_path} and fixture #{filename}",+ )+ end++ file_from_fixtures(filename, directory, "#{plugin_match.root_dir}/spec/fixtures")enddef file_from_contents(contents, filename, directory = "images")