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"))
```
This commit is contained in:
Martin Brennan 2024-03-26 16:17:51 +10:00 committed by GitHub
parent ef99b97ea7
commit 0bbca318f2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 6 additions and 2 deletions

View File

@ -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)