Skip to content

Commit

Permalink
Fix loading factories in extensions after the last changes in Solidus
Browse files Browse the repository at this point in the history
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
kennyadsl committed Feb 17, 2021
1 parent 89d7acc commit 5bae9d8
Showing 1 changed file with 30 additions and 6 deletions.
36 changes: 30 additions & 6 deletions lib/solidus_dev_support/testing_support/factories.rb
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

0 comments on commit 5bae9d8

Please sign in to comment.