DEV: Use caller for plugin_file_from_fixtures (#26387)

Followup 0bbca318f2,
rather than making developers provide the plugin path
name (which may not always be the same depending on
dir names and git cloning etc) we can infer the plugin
dir from the caller in plugin_file_from_fixtures
This commit is contained in:
Martin Brennan 2024-03-27 13:12:51 +10:00 committed by GitHub
parent 476d91d233
commit 8e08a3b31f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 31 additions and 2 deletions

View File

@ -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({})
end

View File

@ -810,8 +810,32 @@ def file_from_fixtures(filename, directory = "images", root_path = "#{Rails.root
File.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")
end
def file_from_contents(contents, filename, directory = "images")