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.
This commit is contained in:
yuuji.yaginuma 2019-04-04 14:24:07 +09:00 committed by Daniel Colson
parent 2b7bca0094
commit 9b83f482b5
4 changed files with 71 additions and 3 deletions

View File

@ -19,5 +19,9 @@ module FactoryBotRails
def files
@files.select { |file| File.exist?(file) }
end
def any?
directories.any? || files.any?
end
end
end

View File

@ -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

View File

@ -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

View File

@ -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