From 0bbca318f27089567d43f103cbfb18fa01eff15e Mon Sep 17 00:00:00 2001 From: Martin Brennan Date: Tue, 26 Mar 2024 16:17:51 +1000 Subject: [PATCH] DEV: Add plugin_file_from_fixtures helper (#26359) This allows plugins to also easily read fixture files for tests, rather than having to do stuff like this: ``` File.open(File.join(__dir__, "../../../fixtures/100x100.jpg")) ``` --- spec/rails_helper.rb | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index f99d5fd898e..5be28c50b80 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -804,12 +804,16 @@ def unfreeze_time TrackTimeStub.unstub(:stubbed) end -def file_from_fixtures(filename, directory = "images") +def file_from_fixtures(filename, directory = "images", root_path = "#{Rails.root}/spec/fixtures") tmp_file_path = File.join(concurrency_safe_tmp_dir, SecureRandom.hex << filename) - FileUtils.cp("#{Rails.root}/spec/fixtures/#{directory}/#{filename}", tmp_file_path) + FileUtils.cp("#{root_path}/#{directory}/#{filename}", tmp_file_path) 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") +end + def file_from_contents(contents, filename, directory = "images") tmp_file_path = File.join(concurrency_safe_tmp_dir, SecureRandom.hex << filename) File.write(tmp_file_path, contents)