|
1 | 1 | # frozen_string_literal: true |
2 | 2 |
|
3 | 3 | require 'factory_bot' |
4 | | - |
5 | | -Spree::Deprecation.silence do |
6 | | - require 'spree/testing_support/factories' |
7 | | - puts <<~MSG |
8 | | - We are transitioning to a new way of loading factories for extensions. |
9 | | - Be sure this extension does not load factories using require but uses |
10 | | - the load_for() method in its spec_helper.rb, eg: |
11 | | -
|
12 | | - SolidusDevSupport::TestingSupport::Factories.load_for(ExtensionName1::Engine, ExtensionName2::Engine) |
13 | | -
|
14 | | - This will load Solidus Core factories right before the ones defined in |
15 | | - lib/extension_name/testing_support/factories/*_factory.rb or |
16 | | - lib/extension_name/testing_support/factories.rb |
17 | | -
|
18 | | - This message will be removed when all extensions are updated. |
19 | | - MSG |
20 | | -end |
21 | | - |
22 | | -begin |
23 | | - require 'spree/testing_support/factory_bot' |
24 | | -rescue LoadError |
25 | | - # Do nothing, we are testing the extension against an old version of Solidus |
26 | | -end |
| 4 | +require 'spree/testing_support/factory_bot' |
27 | 5 |
|
28 | 6 | module SolidusDevSupport |
29 | 7 | module TestingSupport |
30 | 8 | module Factories |
31 | 9 | def self.load_for(*engines) |
32 | 10 | paths = engines.flat_map do |engine| |
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? |
37 | | - ActiveSupport::Deprecation.warn <<-WARN.squish, caller(4) |
38 | | - SolidusDevSupport::TestingSupport::Factories.load_for() is automatically loading |
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. |
41 | | - WARN |
42 | | - |
43 | | - [obsolete_factories_file] |
44 | | - else |
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 | | - elsif factories_file_or_folder.first.directory? |
60 | | - engine.root.glob('lib/*/testing_support/factories/**/*_factory.rb') |
61 | | - else |
62 | | - factories_file_or_folder |
63 | | - end |
64 | | - end |
| 11 | + engine.root.glob('lib/*/testing_support/factories{,.rb}') |
65 | 12 | end.map { |path| path.sub(/.rb\z/, '').to_s } |
66 | 13 |
|
67 | | - if using_factory_bot_definition_file_paths? |
68 | | - FactoryBot.definition_file_paths = [ |
69 | | - Spree::TestingSupport::FactoryBot.definition_file_paths, |
70 | | - paths, |
71 | | - ].flatten |
72 | | - |
73 | | - FactoryBot.reload |
74 | | - else |
75 | | - FactoryBot.find_definitions |
76 | | - |
77 | | - paths.each { |path| require path } |
78 | | - end |
79 | | - end |
| 14 | + FactoryBot.definition_file_paths = [ |
| 15 | + Spree::TestingSupport::FactoryBot.definition_file_paths, |
| 16 | + paths, |
| 17 | + ].flatten |
80 | 18 |
|
81 | | - def self.using_factory_bot_definition_file_paths? |
82 | | - defined?(Spree::TestingSupport::FactoryBot) && |
83 | | - Spree::TestingSupport::FactoryBot.respond_to?(:definition_file_paths) |
| 19 | + FactoryBot.reload |
84 | 20 | end |
85 | 21 | end |
86 | 22 | end |
|
0 commit comments