Skip to content

Commit

Permalink
Merge pull request solidusio#5813 from SuperGoodSoft/raise-error-with…
Browse files Browse the repository at this point in the history
…-spree-deprecator

Raise on deprecation when `SOLIDUS_RAISE_DEPRECATIONS` set
  • Loading branch information
kennyadsl authored Aug 19, 2024
2 parents ea43f41 + 586c519 commit 5d82826
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 5 deletions.
11 changes: 6 additions & 5 deletions core/lib/spree/testing_support/dummy_app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ def self.setup(gem_root:, lib_name:, auto_migrate: true)
DummyApp::Application.config.root = root
DummyApp::Application.initialize! unless DummyApp::Application.initialized?

# Raise on deprecation warnings.
# NOTE: This needs to happen after the application is initialized.
if ENV['SOLIDUS_RAISE_DEPRECATIONS'].present?
Spree.deprecator.behavior = :raise
end

if auto_migrate
DummyApp::Migrations.auto_migrate
end
Expand Down Expand Up @@ -151,8 +157,3 @@ class Application < ::Rails::Application
config.taxon_attachment_module = 'Spree::Taxon::PaperclipAttachment'
end
end

# Raise on deprecation warnings
if ENV['SOLIDUS_RAISE_DEPRECATIONS'].present?
Spree.deprecator.behavior = :raise
end
48 changes: 48 additions & 0 deletions core/spec/lib/spree/deprecator_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe "Spree.deprecator" do
Dummy = Class.new do
def deprecated_method
"foo"
end
deprecate :deprecated_method, deprecator: Spree.deprecator
end

context "by default" do
it "returns a valid deprecator" do
expect(Spree.deprecator).to have_attributes(
deprecation_horizon: "5.0",
gem_name: "Solidus"
)
end

it "does not raise an error unless overridden by environment" do
if ENV["SOLIDUS_RAISE_DEPRECATIONS"]
expect { Dummy.new.deprecated_method }.to raise_error(ActiveSupport::DeprecationException)
else
expect { Dummy.new.deprecated_method }.not_to raise_error
end
end
end

context "when the behavior has been changed to :raise" do
around do |example|
behavior_name = ActiveSupport::Deprecation::DEFAULT_BEHAVIORS.detect { |_behavior_name, behavior_proc|
behavior_proc == Spree.deprecator.behavior.first
}.first

Spree.deprecator.behavior = :raise

example.run

Spree.deprecator.behavior = behavior_name
end

it "raises an error when a deprecated method is called" do
expect { Dummy.new.deprecated_method }
.to raise_error(ActiveSupport::DeprecationException)
end
end
end

0 comments on commit 5d82826

Please sign in to comment.