Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
Disable update check in test environments and on CI (#14891)
Damian Legawiec684919c4e9e8
diff --git a/docs/developer/deployment/usage-data.mdx b/docs/developer/deployment/usage-data.mdxindex ff953ab7cc..4188edf099 100644--- a/docs/developer/deployment/usage-data.mdx+++ b/docs/developer/deployment/usage-data.mdx@@ -58,3 +58,5 @@ end```Either way, turning a switch off wins over the other method: an environment variable cannot turn back on what the initializer turned off, and the reverse.++The check never runs in the `test` environment or on a CI server (any machine with the `CI` environment variable set, which GitHub Actions, GitLab CI, CircleCI and most other services do), so test runs send nothing.diff --git a/spree/core/app/services/spree/update_check.rb b/spree/core/app/services/spree/update_check.rbindex 6f94ce4036..93b4a64679 100644--- a/spree/core/app/services/spree/update_check.rb+++ b/spree/core/app/services/spree/update_check.rb@@ -34,9 +34,16 @@ module SpreeLARGEST_BUCKET = '100k+'.freezeclass << self+ # Always false in the test environment and on CI servers (which set+ # +CI+), so no test run ever reaches spreecloud.io; specs stub the+ # request instead.+ ## @return [Boolean] false when the operator turned the check offdef enabled?- Spree::Config.update_check_enabled && !env_flag?('SPREE_UPDATE_CHECK_DISABLED')+ !Rails.env.test? &&+ !env_flag?('CI') &&+ Spree::Config.update_check_enabled &&+ !env_flag?('SPREE_UPDATE_CHECK_DISABLED')end# When false, the check still runs but sends the running version only.diff --git a/spree/core/lib/spree/core/configuration.rb b/spree/core/lib/spree/core/configuration.rbindex e350277e4a..0ecf17c583 100644--- a/spree/core/lib/spree/core/configuration.rb+++ b/spree/core/lib/spree/core/configuration.rb@@ -157,7 +157,7 @@ module Spreepreference :track_inventory_levels, :boolean, default: true, deprecated: 'Set it on the store instead' # Determines whether to track on_hand values for variants / products.preference :track_price_history, :boolean, default: true, deprecated: 'Set it on the store instead' # Records price changes for Omnibus Directive compliance. Disable for non-EU stores.preference :price_history_retention_days, :integer, default: 30, deprecated: 'Set it on the store instead' # Days to retain price history records. Used by spree:price_history:prune rake task.- # SPREE_UPDATE_CHECK_DISABLED turns it off as well.+ # SPREE_UPDATE_CHECK_DISABLED turns it off as well; it never runs in the test environment or on CI.preference :update_check_enabled, :boolean, default: truepreference :use_user_locale, :boolean, default: true, deprecated: truediff --git a/spree/core/spec/services/spree/update_check_spec.rb b/spree/core/spec/services/spree/update_check_spec.rbindex 283e748c8d..98406db15d 100644--- a/spree/core/spec/services/spree/update_check_spec.rb+++ b/spree/core/spec/services/spree/update_check_spec.rb@@ -25,13 +25,15 @@ RSpec.describe Spree::UpdateCheck doendbefore do+ allow(Rails.env).to receive(:test?).and_return(false)+ with_env({})allow(Net::HTTP).to receive(:start).with('spreecloud.io', 443, hash_including(use_ssl: true, max_retries: 0, open_timeout: 1, read_timeout: 1)) { |*, **, &block| block.call(http) }allow(http).to receive(:get) { |path| @requested_path = path; response }enddef with_env(values)allow(ENV).to receive(:[]).and_call_original- values.each { |name, value| allow(ENV).to receive(:[]).with(name).and_return(value) }+ { 'CI' => nil }.merge(values).each { |name, value| allow(ENV).to receive(:[]).with(name).and_return(value) }enddescribe '.status' do@@ -171,5 +173,21 @@ RSpec.describe Spree::UpdateCheck doexpect(Net::HTTP).not_to have_received(:start)endend++ context 'when running tests' do+ it 'sends nothing so test suites never reach spreecloud.io' do+ allow(Rails.env).to receive(:test?).and_return(true)++ expect(described_class.status.enabled).to be(false)+ expect(Net::HTTP).not_to have_received(:start)+ end++ it 'sends nothing on a CI server, whatever the Rails environment' do+ with_env('CI' => 'true')++ expect(described_class.status.enabled).to be(false)+ expect(Net::HTTP).not_to have_received(:start)+ end+ endendend