mirror of
https://github.com/discourse/discourse.git
synced 2025-01-18 10:52:45 +08:00
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:
parent
476d91d233
commit
8e08a3b31f
|
@ -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
|
||||
|
|
|
@ -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")
|
||||
|
|
Loading…
Reference in New Issue
Block a user