1
0
Fork 0
mirror of https://github.com/thoughtbot/factory_bot.git synced 2022-11-09 11:43:51 -05:00

Support definition file paths with extensions (#1464)

In the process of adding reload support to @solidusio which has the need to control the loading sequence of its factories in relation to those provided by applications and extension, I wished I had the ability to add paths coming from a glob (e.g. coming from Dir["…"]).

* Add specs for different usages of definition_file_paths
* Add support for paths with extensions in definition_file_paths
* Reduce complexity of find_definitions
This commit is contained in:
Elia Schito 2021-10-12 03:49:56 +02:00 committed by GitHub
parent 5c01f05269
commit 37fde91df1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 52 additions and 5 deletions

View file

@ -13,12 +13,18 @@ module FactoryBot
absolute_definition_file_paths = definition_file_paths.map { |path| File.expand_path(path) }
absolute_definition_file_paths.uniq.each do |path|
load("#{path}.rb") if File.exist?("#{path}.rb")
load_file_or_directory(path)
end
end
if File.directory? path
Dir[File.join(path, "**", "*.rb")].sort.each do |file|
load file
end
def self.load_file_or_directory(path)
load("#{path}.rb") if File.exist?("#{path}.rb")
load path if File.file? path
if File.directory? path
Dir[File.join(path, "**", "*.rb")].sort.each do |file|
load file
end
end
end

View file

@ -109,4 +109,45 @@ describe "definition loading" do
end
end
end
describe "definition_file_paths" do
in_directory_with_files "spec/my_factories/factory_one.rb", "spec/my_factories/factory_two.rb"
before { allow(FactoryBot).to receive(:load) }
it "supports files without extension" do
old_paths = FactoryBot.definition_file_paths
FactoryBot.definition_file_paths = ["spec/my_factories/factory_one"]
FactoryBot.find_definitions
expect(FactoryBot).to have_received(:load).once.with(File.expand_path("spec/my_factories/factory_one.rb"))
ensure
FactoryBot.definition_file_paths = old_paths
end
it "supports directories" do
old_paths = FactoryBot.definition_file_paths
FactoryBot.definition_file_paths = ["spec/my_factories"]
FactoryBot.find_definitions
expect(FactoryBot).to have_received(:load).twice
expect(FactoryBot).to have_received(:load).with(File.expand_path("spec/my_factories/factory_one.rb"))
expect(FactoryBot).to have_received(:load).with(File.expand_path("spec/my_factories/factory_two.rb"))
ensure
FactoryBot.definition_file_paths = old_paths
end
it "supports files with extension" do
old_paths = FactoryBot.definition_file_paths
FactoryBot.definition_file_paths = ["spec/my_factories/factory_one.rb"]
FactoryBot.find_definitions
expect(FactoryBot).to have_received(:load).once.with(File.expand_path("spec/my_factories/factory_one.rb"))
ensure
FactoryBot.definition_file_paths = old_paths
end
end
end