Module: DataShifter::Internal::SideEffectGuards

Defined in:
lib/data_shifter/internal/side_effect_guards.rb

Overview

Applies and restores side-effect guards during dry runs so that HTTP, mail, and job enqueues are blocked (or faked) unless explicitly allowed.

Production impact:

  • WebMock: required only when apply_webmock runs (i.e. during a dry run), so commit-only production runs never load WebMock. On restore we revert to the previous state (enable! or disable!) so e.g. specs that had WebMock enabled are not left with it disabled.

  • ActionMailer / ActiveJob / Sidekiq: no extra loading; we only toggle existing config for the duration of the block and restore in ensure (Sidekiq restores prior fake/inline/disable).

Constant Summary collapse

DEFAULT_LOOPBACK_HOSTS =

Loopback hosts allowed by default during dry-run net blocking (unless opted out via DataShifter.config.allow_loopback_requests = false). Localhost is by definition local —the guard’s purpose is to prevent accidental external state mutations, not to fight tracing/metrics sidecars (Datadog agent on 8126, statsd on 8125, OTLP collector, etc.) which commonly listen on these hosts.

%w[127.0.0.1 ::1 localhost].freeze

Class Method Summary collapse

Class Method Details

.with_guards(shift_class:, &block) ⇒ Object

Applies side-effect guards, yields, then restores. Call only when running in dry run.



26
27
28
29
30
31
32
33
34
35
# File 'lib/data_shifter/internal/side_effect_guards.rb', line 26

def with_guards(shift_class:, &block)
  saved = {}
  apply_guards(shift_class, saved)
  block.call
rescue webmock_net_connect_error => e
  host = extract_host_from_webmock_message(e.message)
  raise DataShifter::ExternalRequestNotAllowedError.new(attempted_host: host), cause: e
ensure
  restore_guards(saved) if saved.any?
end