Skip to content

Commit 5cce9c6

Browse files
committed
Load factories for extension using the old path in lib
Most of the extension still have factories into /lib/extension_name/factories.rb If that's the case, this commit will make them work and print a deprecation message to ask users to move factories into the right folder, which is: /lib/extension_name/testing_support/factories.rb or even better, using file with a semantic name for each, like: /lib/extension_name/testing_support/factories/something_factory.rb
1 parent 5fbafab commit 5cce9c6

1 file changed

Lines changed: 26 additions & 9 deletions

File tree

lib/solidus_dev_support/testing_support/factories.rb

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,37 @@ module TestingSupport
3030
module Factories
3131
def self.load_for(*engines)
3232
paths = engines.flat_map do |engine|
33-
factories_file_or_folder = engine.root.glob('lib/*/testing_support/factories{,.rb}')
34-
if factories_file_or_folder.size == 2
35-
folder, file = factories_file_or_folder.partition(&:directory?).map(&:first).map { |path| path.to_s.gsub(engine.root.to_s, '') }
33+
# Check if the extension has a lib/*/factories.rb. If it does, we emit a
34+
# deprecation warning and just use it.
35+
obsolete_factories_file = engine.root.glob('lib/*/factories.rb').first # 'lib/*/factories/*_factory.rb'
36+
if obsolete_factories_file.present?
3637
ActiveSupport::Deprecation.warn <<-WARN.squish, caller(4)
3738
SolidusDevSupport::TestingSupport::Factories.load_for() is automatically loading
38-
all factories present into #{folder}. You should now safely remove #{file} if it
39-
is only used to load ./factories content.
39+
all factories present in #{obsolete_factories_file.dirname.to_s.gsub(engine.root.to_s, '')}/testing_support/factories/.
40+
Please move the content of #{obsolete_factories_file.to_s.gsub(engine.root.to_s, '')} to that directory.
4041
WARN
4142

42-
engine.root.glob('lib/*/testing_support/factories/**/*_factory.rb')
43+
[obsolete_factories_file]
4344
else
44-
factories_file_or_folder
45-
end.map { |path| path.sub(/.rb\z/, '').to_s }
46-
end
45+
# If there are both a lib/*/testing_support/factories.rb and a lib/*/testing_support/factories/,
46+
# we assume that the factories.rb file is only used to load all factories prensent in the directory.
47+
# That file can be removed from the extension, in fact, we ignore it and emit a message asking to
48+
# remove it.
49+
factories_file_or_folder = engine.root.glob('lib/*/testing_support/factories{,.rb}')
50+
if factories_file_or_folder.size == 2
51+
folder, file = factories_file_or_folder.partition(&:directory?).map(&:first).map { |path| path.to_s.gsub(engine.root.to_s, '') }
52+
ActiveSupport::Deprecation.warn <<-WARN.squish, caller(4)
53+
SolidusDevSupport::TestingSupport::Factories.load_for() is automatically loading
54+
all factories present into #{folder}. You should now safely remove #{file} if it
55+
is only used to load ./factories content.
56+
WARN
57+
58+
engine.root.glob('lib/*/testing_support/factories/**/*_factory.rb')
59+
else
60+
factories_file_or_folder
61+
end
62+
end
63+
end.map { |path| path.sub(/.rb\z/, '').to_s }
4764

4865
if using_factory_bot_definition_file_paths?
4966
FactoryBot.definition_file_paths = [

0 commit comments

Comments
 (0)