diff --git a/lib/factory_bot/find_definitions.rb b/lib/factory_bot/find_definitions.rb index bd20ee4..44b970c 100644 --- a/lib/factory_bot/find_definitions.rb +++ b/lib/factory_bot/find_definitions.rb @@ -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 diff --git a/spec/factory_bot/find_definitions_spec.rb b/spec/factory_bot/find_definitions_spec.rb index cb7208d..6c00e5b 100644 --- a/spec/factory_bot/find_definitions_spec.rb +++ b/spec/factory_bot/find_definitions_spec.rb @@ -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