Skip Sentry in the test environment
Config gains environment/test? (APP_ENV, then RACK_ENV, default
development) so web.rb can skip Sentry.init under test; the DSN stays
required everywhere else.

Assisted-by: GLM 5.2 via pi
change xvmtzzzpzsxkpmyynqxykqsyrsnynozp
commit a3f3f9b22f9a560311e40b5a7300bde7d1736757
author Alpha Chen <alpha@kejadlen.dev>
date
parent zuwnwxum
diff --git a/Rakefile b/Rakefile
index 0946e9c..09b9c1a 100644
--- a/Rakefile
+++ b/Rakefile
@@ -11,6 +11,7 @@ end
 
 desc "Regenerate macOS exchange response fixtures from current responses"
 task :fixtures do
+  ENV["RACK_ENV"] = "test"
   $LOAD_PATH.unshift(File.expand_path("lib", __dir__))
   require "pro_tacts/web"
   require_relative "test/pro_tacts/exchange_fixtures"
diff --git a/lib/pro_tacts/config.rb b/lib/pro_tacts/config.rb
index fb123dd..d8233ab 100644
--- a/lib/pro_tacts/config.rb
+++ b/lib/pro_tacts/config.rb
@@ -11,6 +11,16 @@ module ProTacts
       @env = env
     end
 
+    # The deployment environment: APP_ENV wins, then RACK_ENV, then
+    # "development".
+    def environment
+      @env.fetch("APP_ENV") { @env.fetch("RACK_ENV", "development") }
+    end
+
+    def test?
+      environment == "test"
+    end
+
     # Sentry DSN. Required at boot; raises if unset so the failure is loud.
     def sentry_dsn
       @env.fetch("SENTRY_DSN")
diff --git a/lib/pro_tacts/web.rb b/lib/pro_tacts/web.rb
index 17f09a9..7709388 100644
--- a/lib/pro_tacts/web.rb
+++ b/lib/pro_tacts/web.rb
@@ -3,18 +3,22 @@
 require "pro_tacts"
 require "sentry-ruby"
 
-Sentry.init do |sentry|
-  sentry.dsn = ProTacts.config.sentry_dsn
+# Tests skip Sentry entirely: capture_message and the rack middleware are
+# no-ops while Sentry is uninitialized, so no DSN is needed there.
+unless ProTacts.config.test?
+  Sentry.init do |sentry|
+    sentry.dsn = ProTacts.config.sentry_dsn
 
-  # Get breadcrumbs from logs
-  sentry.breadcrumbs_logger = [:sentry_logger, :http_logger]
+    # Get breadcrumbs from logs
+    sentry.breadcrumbs_logger = [:sentry_logger, :http_logger]
 
-  # Add data like request headers and IP for users, if applicable;
-  # see https://docs.sentry.io/platforms/ruby/data-management/data-collected/ for more info
-  sentry.send_default_pii = true
+    # Add data like request headers and IP for users, if applicable;
+    # see https://docs.sentry.io/platforms/ruby/data-management/data-collected/ for more info
+    sentry.send_default_pii = true
 
-  # Trace all the things!
-  sentry.traces_sample_rate = 1.0
+    # Trace all the things!
+    sentry.traces_sample_rate = 1.0
+  end
 end
 
 require "rack/rewindable_input"
diff --git a/test/pro_tacts/test_config.rb b/test/pro_tacts/test_config.rb
index 1524d8f..9268eea 100644
--- a/test/pro_tacts/test_config.rb
+++ b/test/pro_tacts/test_config.rb
@@ -5,6 +5,19 @@ require "minitest/autorun"
 require "pro_tacts/config"
 
 class ConfigTest < Minitest::Test
+  def test_environment_defaults_to_development
+    assert_equal "development", ProTacts::Config.new({}).environment
+    assert_equal "production", ProTacts::Config.new("RACK_ENV" => "production").environment
+    assert_equal "production", ProTacts::Config.new("RACK_ENV" => "development", "APP_ENV" => "production").environment
+  end
+
+  def test_testenv
+    assert ProTacts::Config.new("RACK_ENV" => "test").test?
+    assert ProTacts::Config.new("APP_ENV" => "test").test?
+    refute ProTacts::Config.new({}).test?
+    refute ProTacts::Config.new("RACK_ENV" => "production").test?
+  end
+
   def test_sentry_dsn_is_passed_through
     assert_equal "https://example/1", ProTacts::Config.new("SENTRY_DSN" => "https://example/1").sentry_dsn
   end
diff --git a/test/pro_tacts/test_macos_exchange.rb b/test/pro_tacts/test_macos_exchange.rb
index 6049584..9d0ae3b 100644
--- a/test/pro_tacts/test_macos_exchange.rb
+++ b/test/pro_tacts/test_macos_exchange.rb
@@ -1,6 +1,6 @@
 # frozen_string_literal: true
 
-require "minitest/autorun"
+require_relative "../test_helper"
 require "rack/test"
 
 require_relative "exchange_fixtures"
diff --git a/test/pro_tacts/test_web.rb b/test/pro_tacts/test_web.rb
index fbbecb2..e26eec7 100644
--- a/test/pro_tacts/test_web.rb
+++ b/test/pro_tacts/test_web.rb
@@ -1,6 +1,6 @@
 # frozen_string_literal: true
 
-require "minitest/autorun"
+require_relative "../test_helper"
 require "rack/test"
 
 require "pro_tacts/web"
diff --git a/test/test_helper.rb b/test/test_helper.rb
new file mode 100644
index 0000000..bc8d5e0
--- /dev/null
+++ b/test/test_helper.rb
@@ -0,0 +1,7 @@
+# frozen_string_literal: true
+
+# Marks the process as running tests before anything requires the app, so
+# web.rb skips Sentry.init and no SENTRY_DSN is needed.
+ENV["RACK_ENV"] = "test"
+
+require "minitest/autorun"