-
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix loading factories in extensions after the last changes in Solidus
This commit reflects the last changes we made in Solidus to support the new FactoryBot loading mechanism. When testing extensions using Solidus vesions that support the definition_file_paths, it will load the core factory first and then the ones defined in `lib/extension_name/testing_support/factories`.
- Loading branch information
Showing
1 changed file
with
30 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,51 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'spree/testing_support/factories' | ||
begin | ||
require 'spree/testing_support/factory_bot' | ||
rescue LoadError | ||
require 'spree/testing_support/factories' | ||
end | ||
require 'factory_bot' | ||
|
||
module SolidusDevSupport | ||
module TestingSupport | ||
module Factories | ||
def self.load_for(*engines) | ||
paths = engines.flat_map do |engine| | ||
engine.root.glob('lib/**/factories.rb') | ||
factories_file_or_folder = engine.root.glob('lib/*/testing_support/factories{,.rb}') | ||
|
||
if factories_file_or_folder.size == 2 && using_factory_bot_deinition_file_paths? | ||
folder, file = factories_file_or_folder.partition(&:directory?).map(&:first).map { |path| path.to_s.gsub(engine.root.to_s, '') } | ||
ActiveSupport::Deprecation.warn <<-WARN.squish, caller(4) | ||
SolidusDevSupport::TestingSupport::Factories.load_for() is automatically loading | ||
all factories present into #{folder}. You should now safely remove #{file} if it | ||
is only used to load ./factories content. | ||
WARN | ||
|
||
engine.root.glob('lib/*/testing_support/factories/**/*_factory.rb') | ||
else | ||
factories_file_or_folder | ||
end.map { |path| path.sub(/.rb\z/, '').to_s } | ||
end | ||
|
||
if Spree::TestingSupport.respond_to? :load_all_factories | ||
FactoryBot.definition_file_paths.concat( | ||
paths.map { |path| path.sub(/.rb\z/, '').to_s } | ||
) | ||
if using_factory_bot_deinition_file_paths? | ||
FactoryBot.definition_file_paths = [ | ||
Spree::TestingSupport::FactoryBot.definition_file_paths, | ||
paths, | ||
].flatten | ||
|
||
FactoryBot.reload | ||
else | ||
FactoryBot.find_definitions | ||
|
||
paths.each { |path| require path } | ||
end | ||
end | ||
|
||
def self.using_factory_bot_deinition_file_paths? | ||
defined?(Spree::TestingSupport::FactoryBot) && | ||
Spree::TestingSupport::FactoryBot.respond_to? :definition_file_paths | ||
end | ||
end | ||
end | ||
end |