-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature Add file_fixture helper to syntax methods
Why: This is to add simple support to files on factories. This is the original [issue](#1282). What: It is a common behavior to interact with files when creating factories, and it is used heavily, as demonstrated with the many examples in the issue, this is a first draft and I would love to see more use-cases to add to the acceptance test. For the time being we have at least the base case added.
- Loading branch information
Showing
7 changed files
with
98 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
require "tempfile" | ||
|
||
module FactoryBot | ||
class FileLoader | ||
attr_reader :tempfile, :original_filename | ||
|
||
def initialize(file_path) | ||
from_file_path(file_path) | ||
end | ||
|
||
def path | ||
tempfile.path | ||
end | ||
|
||
def method_missing(method_name, *args, &block) # rubocop:disable Style/MethodMissing | ||
tempfile.public_send(method_name, *args, &block) | ||
end | ||
|
||
def respond_to_missing?(method_name, include_private = false) | ||
tempfile.respond_to?(method_name, include_private) || super | ||
end | ||
|
||
private | ||
|
||
def from_file_path(path) | ||
raise FileDoesNotExistError unless ::File.exist?(path) | ||
|
||
@original_filename = ::File.basename(path) | ||
extension = ::File.extname(@original_filename) | ||
|
||
@tempfile = Tempfile.new( | ||
[::File.basename(@original_filename, extension), extension], | ||
) | ||
@tempfile.set_encoding(Encoding::BINARY) if @tempfile.respond_to?(:set_encoding) | ||
|
||
FileUtils.copy_file(path, @tempfile.path) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
describe "file attributes" do | ||
context "when an attribute uses a file" do | ||
it "assigns an file to the attribute" do | ||
define_class("Post") do |_class| | ||
attr_accessor :attachment | ||
end | ||
filename = File.expand_path("spec/support/text_file.txt") | ||
file_contents = File.read(filename) | ||
|
||
FactoryBot.define do | ||
factory :post do | ||
attachment { file_fixture(filename) } | ||
end | ||
end | ||
post = FactoryBot.build(:post) | ||
expect(post.attachment.original_filename).to eq ::File.basename filename | ||
expect(File.read(post.attachment)).to eq file_contents | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
describe FactoryBot::FileLoader do | ||
it "creates a tempfile copy from the original" do | ||
original_file_path = File.expand_path("spec/support/text_file.txt") | ||
original_file_contents = File.read(original_file_path) | ||
file_loader = FactoryBot::FileLoader.new(original_file_path) | ||
|
||
expect(original_file_contents).to eq File.read(file_loader.tempfile) | ||
end | ||
|
||
it "delegates all the methods to the tempfile" do | ||
original_file_path = File.expand_path("spec/support/text_file.txt") | ||
file_loader = FactoryBot::FileLoader.new(original_file_path) | ||
|
||
expect(file_loader.path).to eq file_loader.tempfile.path | ||
end | ||
|
||
it "raises an exception is the file path does not exists" do | ||
expect { FactoryBot::FileLoader.new("nonexistent.path") }. | ||
to raise_error FactoryBot::FileDoesNotExistError | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Test Content |