From 9b83f482b566d724e6b0489c76624d0b94bac586 Mon Sep 17 00:00:00 2001 From: "yuuji.yaginuma" Date: Thu, 4 Apr 2019 14:24:07 +0900 Subject: [PATCH] Do not register a reloader when file paths not exist If all the paths specified in `FactoryBot.definition_file_paths` do not exist, an empty array is passed to the reloader. If an empty array is specified, reloader passes it to `Listen.to` as it is. This causes the application root to be watched and `node_modules` to become a target of listening. The behavior when an empty array is passed to reloader depends on the Rails side. Fixes to not register a reloader when file paths do not exist consistent behavior regardless of Rails side. Fixes #328. --- .../definition_file_paths.rb | 4 ++ lib/factory_bot_rails/reloader.rb | 7 +-- .../definition_file_paths_spec.rb | 10 ++++ spec/factory_bot_rails/reloader_spec.rb | 53 +++++++++++++++++++ 4 files changed, 71 insertions(+), 3 deletions(-) create mode 100644 spec/factory_bot_rails/reloader_spec.rb diff --git a/lib/factory_bot_rails/definition_file_paths.rb b/lib/factory_bot_rails/definition_file_paths.rb index 5b3ab60..35e07f8 100644 --- a/lib/factory_bot_rails/definition_file_paths.rb +++ b/lib/factory_bot_rails/definition_file_paths.rb @@ -19,5 +19,9 @@ module FactoryBotRails def files @files.select { |file| File.exist?(file) } end + + def any? + directories.any? || files.any? + end end end diff --git a/lib/factory_bot_rails/reloader.rb b/lib/factory_bot_rails/reloader.rb index e3e3d95..f3d66fd 100644 --- a/lib/factory_bot_rails/reloader.rb +++ b/lib/factory_bot_rails/reloader.rb @@ -7,9 +7,12 @@ module FactoryBotRails def initialize(app, config) @app = app @config = config + @paths = DefinitionFilePaths.new(FactoryBot.definition_file_paths) end def run + return unless @paths.any? + register_reloader(build_reloader) end @@ -18,9 +21,7 @@ module FactoryBotRails attr_reader :app, :config def build_reloader - paths = DefinitionFilePaths.new(FactoryBot.definition_file_paths) - - reloader_class.new(paths.files, paths.directories) do + reloader_class.new(@paths.files, @paths.directories) do FactoryBot.reload 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 8268baf..006ed18 100644 --- a/spec/factory_bot_rails/definition_file_paths_spec.rb +++ b/spec/factory_bot_rails/definition_file_paths_spec.rb @@ -30,4 +30,14 @@ describe FactoryBotRails::DefinitionFilePaths do expect(directories).to eq("spec/fixtures/factories" => [:rb]) end end + + describe "#any?" do + it "returns true only if definition file paths exist" do + definition_file_paths = ["spec/fixtures/factories", "not_exist_directory"] + expect(described_class.new(definition_file_paths).any?).to eq true + + definition_file_paths = ["not_exist_directory"] + expect(described_class.new(definition_file_paths).any?).to eq false + end + end end diff --git a/spec/factory_bot_rails/reloader_spec.rb b/spec/factory_bot_rails/reloader_spec.rb new file mode 100644 index 0000000..188e791 --- /dev/null +++ b/spec/factory_bot_rails/reloader_spec.rb @@ -0,0 +1,53 @@ +# frozen_string_literal: true + +describe FactoryBotRails::Reloader do + describe "#run" do + before do + @original_definition_file_paths = FactoryBot.definition_file_paths + end + + after do + FactoryBot.definition_file_paths = @original_definition_file_paths + end + + context "when a definition file paths exist" do + it "registers a reloader" do + allow(reloader_class).to receive(:new) + + run_reloader(["spec/fixtures/factories", "not_exist_directory"]) + + expect(reloader_class).to have_received(:new) + end + end + + context "when a file exists but not a directory" do + it "registers a reloader" do + allow(reloader_class).to receive(:new) + + run_reloader(["spec/fake_app", "not_exist_directory"]) + + expect(reloader_class).to have_received(:new) + end + end + + context "when a definition file paths NOT exist" do + it "does NOT register a reloader" do + allow(reloader_class).to receive(:new) + + run_reloader(["not_exist_directory"]) + + expect(reloader_class).not_to have_received(:new) + end + end + + def run_reloader(definition_file_paths) + FactoryBot.definition_file_paths = definition_file_paths + FactoryBotRails::Reloader. + new(Rails.application, Rails.application.config).run + end + + def reloader_class + Rails.application.config.file_watcher + end + end +end