Skip to content

Commit

Permalink
Clean up Sentry.Config
Browse files Browse the repository at this point in the history
  • Loading branch information
whatyouhide committed Sep 14, 2023
1 parent d33bf19 commit f996c7d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 29 deletions.
12 changes: 6 additions & 6 deletions lib/sentry/config.ex
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,8 @@ defmodule Sentry.Config do
end

defp get_config_from_app_or_system_env(app_key, system_env_key) do
case Application.fetch_env(:sentry, app_key) do
{:ok, {:system, env_key}} ->
case Application.get_env(:sentry, app_key, nil) do
{:system, env_key} ->
raise ArgumentError, """
using {:system, env} as a configuration value is not supported since v9.0.0 of this \
library. Move the configuration for #{inspect(app_key)} to config/runtime.exs, \
Expand All @@ -244,16 +244,16 @@ defmodule Sentry.Config do
"""

{:ok, value} ->
value

:error ->
nil ->
if value = System.get_env(system_env_key) do
Application.put_env(:sentry, app_key, value)
value
else
nil
end

value ->
value
end
end
end
48 changes: 25 additions & 23 deletions test/config_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,49 +3,51 @@ defmodule Sentry.ConfigTest do
import Sentry.TestEnvironmentHelper
alias Sentry.Config

test "retrieves from application environment" do
dsn = "https://public:secret@app.getsentry.com/1"
modify_env(:sentry, dsn: dsn)
assert dsn == Config.dsn()
end

test "retrieves from system environment" do
dsn = "https://public:secret@app.getsentry.com/1"
modify_system_env(%{"SENTRY_DSN" => dsn})
describe "dsn/0" do
test "retrieves from application environment" do
dsn = "https://public:secret@app.getsentry.com/1"
modify_env(:sentry, dsn: dsn)
assert Config.dsn() == dsn
end

assert dsn == Config.dsn()
end
test "retrieves from system environment" do
dsn = "https://public:secret@app.getsentry.com/1"
modify_system_env(%{"SENTRY_DSN" => dsn})
assert Config.dsn() == dsn
end

test "sets application env if found in system env" do
dsn = "https://public:secret@app.getsentry.com/1"
modify_system_env(%{"SENTRY_DSN" => dsn})
test "sets application env if found in system env" do
dsn = "https://public:secret@app.getsentry.com/1"
modify_system_env(%{"SENTRY_DSN" => dsn})

assert Config.dsn() == dsn
assert Application.get_env(:sentry, :dsn) == dsn
assert Config.dsn() == dsn
assert Application.get_env(:sentry, :dsn) == dsn
end
end

describe "source_code_path_pattern" do
describe "source_code_path_pattern/0" do
test "returns default when not configured" do
assert "**/*.ex" == Config.source_code_path_pattern()
assert Config.source_code_path_pattern() == "**/*.ex"
end
end

describe "included_environments" do
describe "included_environments/0" do
test "retrieves from app env" do
modify_env(:sentry, included_environments: [:test, :dev])
assert ["test", "dev"] == Config.included_environments()
assert Config.included_environments() == ["test", "dev"]
end
end

describe "environment_name" do
describe "environment_name/0" do
test "retrieves from app env" do
modify_env(:sentry, environment_name: "test")
assert "test" == Config.environment_name()
assert Config.environment_name() == "test"
end

test "retrieves from system env" do
modify_env(:sentry, environment_name: nil)
modify_system_env(%{"SENTRY_ENVIRONMENT" => "test"})
assert :test == Config.environment_name()
assert Config.environment_name() == "test"
end

test "raises if not set" do
Expand Down

0 comments on commit f996c7d

Please sign in to comment.