diff --git a/lib/factory_bot_rails/definition_file_paths.rb b/lib/factory_bot_rails/definition_file_paths.rb index dd0178b..5b3ab60 100644 --- a/lib/factory_bot_rails/definition_file_paths.rb +++ b/lib/factory_bot_rails/definition_file_paths.rb @@ -2,8 +2,6 @@ module FactoryBotRails class DefinitionFilePaths - attr_reader :files, :directories - def initialize(definition_file_paths) @files = [] @directories = {} @@ -13,5 +11,13 @@ def initialize(definition_file_paths) @directories[path.to_s] = [:rb] end end + + def directories + @directories.select { |path| Dir.exist?(path) } + end + + def files + @files.select { |file| File.exist?(file) } + end end end diff --git a/spec/factory_bot_rails/definition_file_paths_spec.rb b/spec/factory_bot_rails/definition_file_paths_spec.rb index 68ccad6..8268baf 100644 --- a/spec/factory_bot_rails/definition_file_paths_spec.rb +++ b/spec/factory_bot_rails/definition_file_paths_spec.rb @@ -2,33 +2,32 @@ describe FactoryBotRails::DefinitionFilePaths do describe "#files" do - it "returns a list of definition files" do - definition_file_paths = ["definition_path", "another_definition_path"] + it "returns a list of definition files that only exist" do + definition_file_paths = ["spec/fixtures/factories", "not_exist_directory"] files = described_class.new(definition_file_paths).files - expect(files).to eq ["definition_path.rb", "another_definition_path.rb"] + expect(files).to eq ["spec/fixtures/factories.rb"] end end describe "#directories" do - it "returns a hash of definition directories" do - definition_file_paths = ["definition_path", "another_definition_path"] + it "returns a hash of definition directories that only exist" do + definition_file_paths = ["spec/fixtures/factories", "not_exist_directory"] directories = described_class.new(definition_file_paths).directories expect(directories).to eq( - "definition_path" => [:rb], - "another_definition_path" => [:rb], + "spec/fixtures/factories" => [:rb], ) end it "converts Pathname objects to strings" do - definition_file_paths = [Pathname.new("definition_path")] + definition_file_paths = [Pathname.new("spec/fixtures/factories")] directories = described_class.new(definition_file_paths).directories - expect(directories).to eq("definition_path" => [:rb]) + expect(directories).to eq("spec/fixtures/factories" => [:rb]) end end end