Skip to content

Commit

Permalink
Migrate FactoryBot helpers out of the TestingSupport module
Browse files Browse the repository at this point in the history
  • Loading branch information
elia committed Feb 5, 2021
1 parent 70aa414 commit 640d52d
Show file tree
Hide file tree
Showing 67 changed files with 197 additions and 160 deletions.
4 changes: 2 additions & 2 deletions api/spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
# in spec/support/ and its subdirectories.
Dir[File.dirname(__FILE__) + "/support/**/*.rb"].each { |f| require f }

require 'spree/testing_support'
require 'spree/testing_support/factory_bot'
require 'spree/testing_support/partial_double_verification'
require 'spree/testing_support/preferences'
require 'spree/testing_support/authorization_helpers'
Expand All @@ -38,7 +38,7 @@

ActiveJob::Base.queue_adapter = :test

Spree::TestingSupport.load_all_factories
Spree::TestingSupport::FactoryBot.add_paths_and_load!

RSpec.configure do |config|
config.backtrace_exclusion_patterns = [/gems\/activesupport/, /gems\/actionpack/, /gems\/rspec/]
Expand Down
4 changes: 2 additions & 2 deletions backend/spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
require 'database_cleaner'
require 'with_model'

require 'spree/testing_support'
require 'spree/testing_support/factory_bot'
require 'spree/testing_support/partial_double_verification'
require 'spree/testing_support/authorization_helpers'
require 'spree/testing_support/preferences'
Expand Down Expand Up @@ -63,7 +63,7 @@

ActiveJob::Base.queue_adapter = :test

Spree::TestingSupport.load_all_factories
Spree::TestingSupport::FactoryBot.add_paths_and_load!

RSpec.configure do |config|
config.color = true
Expand Down
8 changes: 4 additions & 4 deletions core/lib/spree/core/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ class Engine < ::Rails::Engine
end

if defined?(FactoryBotRails)
initializer after: "factory_bot.set_factory_paths" do
require "spree/testing_support"
Spree::TestingSupport.check_factory_bot_version
FactoryBot.definition_file_paths.unshift(*Spree::TestingSupport.factory_bot_paths)
initializer "spree.factory_bot.set_factory_paths", after: "factory_bot.set_factory_paths" do
require "spree/testing_support/factory_bot"
Spree::TestingSupport::FactoryBot.check_factory_bot_version
FactoryBot.definition_file_paths.unshift(*Spree::TestingSupport::FactoryBot.definition_file_paths)
end
end
end
Expand Down
48 changes: 18 additions & 30 deletions core/lib/spree/testing_support.rb
Original file line number Diff line number Diff line change
@@ -1,43 +1,31 @@
# frozen_string_literal: true

require 'spree/testing_support/factory_bot'

module Spree
module TestingSupport
SEQUENCES = ["#{::Spree::Core::Engine.root}/lib/spree/testing_support/sequences.rb"]
FACTORIES = Dir["#{::Spree::Core::Engine.root}/lib/spree/testing_support/factories/**/*_factory.rb"]

def self.factory_bot_paths
@paths ||= (SEQUENCES + FACTORIES).sort.map { |path| path.sub(/.rb\z/, '') }
end
autoload :FactoryBot, "spree/testing_support/factory_bot"

def self.deprecate_cherry_picking_factory_bot_files
# All good if the factory is being loaded by FactoryBot.
return if caller.find { |line| line.include? "/factory_bot/find_definitions.rb" }
autoload :SEQUENCES, "spree/testing_support/factory_bot"
autoload :FACTORIES, "spree/testing_support/factory_bot"

Spree::Deprecation.warn(
"Please do not cherry-pick factories, this is not well supported by FactoryBot. " \
'Use `require "spree/testing_support/factories"` instead.', caller(2)
)
def factory_bot_paths
Spree::TestingSupport::FactoryBot.definition_file_paths
end

def self.check_factory_bot_version
require 'factory_bot/version'

requirement = Gem::Requirement.new("~> 4.8")
version = Gem::Version.new(FactoryBot::VERSION)

unless requirement.satisfied_by? version
Spree::Deprecation.warn(
"Please be aware that the supported version of FactoryBot is #{requirement}, " \
"using version #{version} could lead to factory loading issues.", caller(2)
)
end
def check_factory_bot_version
Spree::TestingSupport::FactoryBot.check_version
end

def self.load_all_factories
require 'factory_bot'

FactoryBot.definition_file_paths.unshift(*factory_bot_paths).uniq!
FactoryBot.reload
def load_all_factories
Spree::TestingSupport::FactoryBot.add_paths_and_load!
end

deprecate(
factory_bot_paths: "Spree::TestingSupport::FactoryBot.definition_file_paths",
check_factory_bot_version: "Spree::TestingSupport::FactoryBot.check_version",
load_all_factories: "Spree::TestingSupport::FactoryBot.add_paths_and_load!",
deprecator: Spree::Deprecator
)
end
end
6 changes: 3 additions & 3 deletions core/lib/spree/testing_support/factories.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# frozen_string_literal: true

require 'spree/testing_support'
require 'spree/testing_support/factory_bot'

Spree::Deprecation.warn(
"Please do not try to load factories directly. " \
'Use factory_bot_rails and rely on the default configuration instead.', caller(1)
)

Spree::TestingSupport.check_factory_bot_version
Spree::TestingSupport.load_all_factories
Spree::TestingSupport::FactoryBot.check_factory_bot_version
Spree::TestingSupport::FactoryBot::PATHS.each { |path| require path }
4 changes: 2 additions & 2 deletions core/lib/spree/testing_support/factories/address_factory.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

require 'spree/testing_support'
Spree::TestingSupport.deprecate_cherry_picking_factory_bot_files
require 'spree/testing_support/factory_bot'
Spree::TestingSupport::FactoryBot.deprecate_cherry_picking

FactoryBot.define do
factory :address, class: 'Spree::Address' do
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

require 'spree/testing_support'
Spree::TestingSupport.deprecate_cherry_picking_factory_bot_files
require 'spree/testing_support/factory_bot'
Spree::TestingSupport::FactoryBot.deprecate_cherry_picking

FactoryBot.define do
factory :adjustment, class: 'Spree::Adjustment' do
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

require 'spree/testing_support'
Spree::TestingSupport.deprecate_cherry_picking_factory_bot_files
require 'spree/testing_support/factory_bot'
Spree::TestingSupport::FactoryBot.deprecate_cherry_picking

FactoryBot.define do
factory :adjustment_reason, class: 'Spree::AdjustmentReason' do
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

require 'spree/testing_support'
Spree::TestingSupport.deprecate_cherry_picking_factory_bot_files
require 'spree/testing_support/factory_bot'
Spree::TestingSupport::FactoryBot.deprecate_cherry_picking

FactoryBot.define do
factory :calculator, aliases: [:flat_rate_calculator], class: 'Spree::Calculator::FlatRate' do
Expand Down
4 changes: 2 additions & 2 deletions core/lib/spree/testing_support/factories/carton_factory.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

require 'spree/testing_support'
Spree::TestingSupport.deprecate_cherry_picking_factory_bot_files
require 'spree/testing_support/factory_bot'
Spree::TestingSupport::FactoryBot.deprecate_cherry_picking

FactoryBot.define do
factory :carton, class: 'Spree::Carton' do
Expand Down
4 changes: 2 additions & 2 deletions core/lib/spree/testing_support/factories/country_factory.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

require 'spree/testing_support'
Spree::TestingSupport.deprecate_cherry_picking_factory_bot_files
require 'spree/testing_support/factory_bot'
Spree::TestingSupport::FactoryBot.deprecate_cherry_picking

require 'carmen'

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

require 'spree/testing_support'
Spree::TestingSupport.deprecate_cherry_picking_factory_bot_files
require 'spree/testing_support/factory_bot'
Spree::TestingSupport::FactoryBot.deprecate_cherry_picking

FactoryBot.define do
factory :credit_card, class: 'Spree::CreditCard' do
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

require 'spree/testing_support'
Spree::TestingSupport.deprecate_cherry_picking_factory_bot_files
require 'spree/testing_support/factory_bot'
Spree::TestingSupport::FactoryBot.deprecate_cherry_picking

FactoryBot.define do
factory :customer_return, class: 'Spree::CustomerReturn' do
Expand Down
4 changes: 2 additions & 2 deletions core/lib/spree/testing_support/factories/image_factory.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

require 'spree/testing_support'
Spree::TestingSupport.deprecate_cherry_picking_factory_bot_files
require 'spree/testing_support/factory_bot'
Spree::TestingSupport::FactoryBot.deprecate_cherry_picking

FactoryBot.define do
factory :image, class: 'Spree::Image' do
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

require 'spree/testing_support'
Spree::TestingSupport.deprecate_cherry_picking_factory_bot_files
require 'spree/testing_support/factory_bot'
Spree::TestingSupport::FactoryBot.deprecate_cherry_picking

FactoryBot.define do
factory :inventory_unit, class: 'Spree::InventoryUnit' do
Expand Down
4 changes: 2 additions & 2 deletions core/lib/spree/testing_support/factories/line_item_factory.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

require 'spree/testing_support'
Spree::TestingSupport.deprecate_cherry_picking_factory_bot_files
require 'spree/testing_support/factory_bot'
Spree::TestingSupport::FactoryBot.deprecate_cherry_picking

FactoryBot.define do
factory :line_item, class: 'Spree::LineItem' do
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

require 'spree/testing_support'
Spree::TestingSupport.deprecate_cherry_picking_factory_bot_files
require 'spree/testing_support/factory_bot'
Spree::TestingSupport::FactoryBot.deprecate_cherry_picking

FactoryBot.define do
factory :option_type, class: 'Spree::OptionType' do
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

require 'spree/testing_support'
Spree::TestingSupport.deprecate_cherry_picking_factory_bot_files
require 'spree/testing_support/factory_bot'
Spree::TestingSupport::FactoryBot.deprecate_cherry_picking

FactoryBot.define do
factory :option_value, class: 'Spree::OptionValue' do
Expand Down
4 changes: 2 additions & 2 deletions core/lib/spree/testing_support/factories/order_factory.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

require 'spree/testing_support'
Spree::TestingSupport.deprecate_cherry_picking_factory_bot_files
require 'spree/testing_support/factory_bot'
Spree::TestingSupport::FactoryBot.deprecate_cherry_picking

FactoryBot.define do
factory :order, class: 'Spree::Order' do
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

require 'spree/testing_support'
Spree::TestingSupport.deprecate_cherry_picking_factory_bot_files
require 'spree/testing_support/factory_bot'
Spree::TestingSupport::FactoryBot.deprecate_cherry_picking

FactoryBot.define do
factory :order_promotion, class: 'Spree::OrderPromotion' do
Expand Down
4 changes: 2 additions & 2 deletions core/lib/spree/testing_support/factories/payment_factory.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

require 'spree/testing_support'
Spree::TestingSupport.deprecate_cherry_picking_factory_bot_files
require 'spree/testing_support/factory_bot'
Spree::TestingSupport::FactoryBot.deprecate_cherry_picking

FactoryBot.define do
factory :payment, aliases: [:credit_card_payment], class: 'Spree::Payment' do
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

require 'spree/testing_support'
Spree::TestingSupport.deprecate_cherry_picking_factory_bot_files
require 'spree/testing_support/factory_bot'
Spree::TestingSupport::FactoryBot.deprecate_cherry_picking

FactoryBot.define do
factory :payment_method, aliases: [:credit_card_payment_method], class: 'Spree::PaymentMethod::BogusCreditCard' do
Expand Down
4 changes: 2 additions & 2 deletions core/lib/spree/testing_support/factories/price_factory.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

require 'spree/testing_support'
Spree::TestingSupport.deprecate_cherry_picking_factory_bot_files
require 'spree/testing_support/factory_bot'
Spree::TestingSupport::FactoryBot.deprecate_cherry_picking

FactoryBot.define do
factory :price, class: 'Spree::Price' do
Expand Down
4 changes: 2 additions & 2 deletions core/lib/spree/testing_support/factories/product_factory.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

require 'spree/testing_support'
Spree::TestingSupport.deprecate_cherry_picking_factory_bot_files
require 'spree/testing_support/factory_bot'
Spree::TestingSupport::FactoryBot.deprecate_cherry_picking

FactoryBot.define do
factory :base_product, class: 'Spree::Product' do
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

require 'spree/testing_support'
Spree::TestingSupport.deprecate_cherry_picking_factory_bot_files
require 'spree/testing_support/factory_bot'
Spree::TestingSupport::FactoryBot.deprecate_cherry_picking

FactoryBot.define do
factory :product_option_type, class: 'Spree::ProductOptionType' do
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

require 'spree/testing_support'
Spree::TestingSupport.deprecate_cherry_picking_factory_bot_files
require 'spree/testing_support/factory_bot'
Spree::TestingSupport::FactoryBot.deprecate_cherry_picking

FactoryBot.define do
factory :product_property, class: 'Spree::ProductProperty' do
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

require 'spree/testing_support'
Spree::TestingSupport.deprecate_cherry_picking_factory_bot_files
require 'spree/testing_support/factory_bot'
Spree::TestingSupport::FactoryBot.deprecate_cherry_picking

FactoryBot.define do
factory :promotion_category, class: 'Spree::PromotionCategory' do
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

require 'spree/testing_support'
Spree::TestingSupport.deprecate_cherry_picking_factory_bot_files
require 'spree/testing_support/factory_bot'
Spree::TestingSupport::FactoryBot.deprecate_cherry_picking

FactoryBot.define do
factory :promotion_code, class: 'Spree::PromotionCode' do
Expand Down
4 changes: 2 additions & 2 deletions core/lib/spree/testing_support/factories/promotion_factory.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

require 'spree/testing_support'
Spree::TestingSupport.deprecate_cherry_picking_factory_bot_files
require 'spree/testing_support/factory_bot'
Spree::TestingSupport::FactoryBot.deprecate_cherry_picking

FactoryBot.define do
factory :promotion, class: 'Spree::Promotion' do
Expand Down
4 changes: 2 additions & 2 deletions core/lib/spree/testing_support/factories/property_factory.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

require 'spree/testing_support'
Spree::TestingSupport.deprecate_cherry_picking_factory_bot_files
require 'spree/testing_support/factory_bot'
Spree::TestingSupport::FactoryBot.deprecate_cherry_picking

FactoryBot.define do
factory :property, class: 'Spree::Property' do
Expand Down
4 changes: 2 additions & 2 deletions core/lib/spree/testing_support/factories/refund_factory.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

require 'spree/testing_support'
Spree::TestingSupport.deprecate_cherry_picking_factory_bot_files
require 'spree/testing_support/factory_bot'
Spree::TestingSupport::FactoryBot.deprecate_cherry_picking

FactoryBot.define do
sequence(:refund_transaction_id) { |n| "fake-refund-transaction-#{n}" }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

require 'spree/testing_support'
Spree::TestingSupport.deprecate_cherry_picking_factory_bot_files
require 'spree/testing_support/factory_bot'
Spree::TestingSupport::FactoryBot.deprecate_cherry_picking

FactoryBot.define do
factory :refund_reason, class: 'Spree::RefundReason' do
Expand Down
Loading

0 comments on commit 640d52d

Please sign in to comment.