     end
 
     test "application environment overwrites :running_in_container config on Heroku" do
-      Application.put_env :appsignal, :config, running_in_container: false
-      config = default_configuration()
-      |> Map.put(:running_in_container, false)
-      |> Map.put(:log, "stdout")
-      assert config == init_config()
+      assert with_config(%{running_in_container: false}, &init_config/0) ==
+        default_configuration()
+        |> Map.put(:running_in_container, false)
+        |> Map.put(:log, "stdout")
     end
 
     test "application environment overwrites :log config on Heroku" do
-      Application.put_env :appsignal, :config, log: "file"
-      config = default_configuration()
-      |> Map.put(:running_in_container, true)
-      |> Map.put(:log, "file")
-      assert config == init_config()
+      assert with_config(%{log: "file"}, &init_config/0) ==
+        default_configuration()
+        |> Map.put(:running_in_container, true)
+        |> Map.put(:log, "file")
     end
   end
 
   describe "reset_environment_config!" do
     test "deletes existing configuration in environment" do
-      System.put_env("_APPSIGNAL_APP_NAME", "foo")
-      Appsignal.Config.reset_environment_config!
-      assert System.get_env("_APPSIGNAL_APP_NAME") == nil
+      assert with_env(
+        %{"_APPSIGNAL_APP_NAME" => "foo"},
+        fn() ->
+          Appsignal.Config.reset_environment_config!
+          System.get_env("_APPSIGNAL_APP_NAME")
+        end
+      ) == nil
     end
   end
 
   describe "write_to_environment" do
-    setup do
-      Application.put_env(:appsignal, :config, [])
-    end
-
     defp write_to_environment do
       init_config()
       Appsignal.Config.write_to_environment


  describe "active?" do
    test "when active and valid" do
      Application.put_env(:appsignal, :config, %{active: true, valid: true})
      assert Config.active?
    end

    test "when active but not valid" do
      Application.put_env(:appsignal, :config, %{active: true, valid: false})
      refute Config.active?
    end

    test "when not active and not valid" do
      Application.put_env(:appsignal, :config, %{active: false, valid: false})
      refute Config.active?
    end
  end

  describe "using the application environment" do
    setup do: Application.put_env(:appsignal, :config, [])

    test "active" do
      add_to_application_env(:active, true)
      assert default_configuration() |> Map.put(:active, true) == init_config()
    end

    test "ca_file_path" do
      add_to_application_env(:ca_file_path, "/foo/bar/zab.ca")
      assert default_configuration() |> Map.put(:ca_file_path, "/foo/bar/zab.ca") == init_config()
    end

    test "debug" do
      add_to_application_env(:debug, true)
      assert default_configuration() |> Map.put(:debug, true) == init_config()
    end

    test "enable_host_metrics" do
      add_to_application_env(:enable_host_metrics, false)
      assert default_configuration() |> Map.put(:enable_host_metrics, false) == init_config()
    end

    test "endpoint" do
      add_to_application_env(:endpoint, "https://push.staging.lol")
      assert default_configuration() |> Map.put(:endpoint, "https://push.staging.lol") == init_config()
    end

    test "env" do
      add_to_application_env(:env, :prod)
      assert default_configuration() |> Map.put(:env, :prod) == init_config()
    end

    test "filter_parameters" do
      add_to_application_env(:filter_parameters, ~w(password secret))
      assert default_configuration() |> Map.put(:filter_parameters, ~w(password secret)) == init_config()
    end

    test "frontend_error_catching_path" do
      add_to_application_env(:frontend_error_catching_path, "/appsignal_error_catcher")
      assert default_configuration() |> Map.put(:frontend_error_catching_path, "/appsignal_error_catcher") == init_config()
    end

    test "hostname" do
      add_to_application_env(:hostname, "Bobs-MPB.example.com")
      assert default_configuration() |> Map.put(:hostname, "Bobs-MPB.example.com") == init_config()
    end

    test "http_proxy" do
      add_to_application_env(:http_proxy, "http://10.10.10.10:8888")
      assert default_configuration() |> Map.put(:http_proxy, "http://10.10.10.10:8888") == init_config()
    end

    test "ignore_actions" do
      actions = ~w(
        ExampleApplication.PageController#ignored
        ExampleApplication.PageController#also_ignored
      )
      add_to_application_env(:ignore_actions, actions)
      assert default_configuration() |> Map.put(:ignore_actions, actions) == init_config()
    end

    test "ignore_errors" do
      errors = ~w(VerySpecificError AnotherError)
      add_to_application_env(:ignore_errors, errors)
      assert default_configuration() |> Map.put(:ignore_errors, errors) == init_config()
    end

    test "log" do
      add_to_application_env(:log, "stdout")
      assert default_configuration() |> Map.put(:log, "stdout") == init_config()
    end

    test "log_path" do
      add_to_application_env(:log_path, "log/appsignal.log")
      assert default_configuration() |> Map.put(:log_path, "log/appsignal.log") == init_config()
    end

    test "name" do
      add_to_application_env(:name, "my application")
      assert default_configuration() |> Map.put(:name, "my application") == init_config()
    end

    test "push_api_key" do
      add_to_application_env(:push_api_key, "00000000-0000-0000-0000-000000000000")
      assert valid_configuration() |> Map.put(:active, false) == init_config()
    end

    test "running_in_container" do
      add_to_application_env(:running_in_container, true)
      assert default_configuration() |> Map.put(:running_in_container, true) == init_config()
    end

    test "send_params" do
      add_to_application_env(:send_params, true)
      assert default_configuration() |> Map.put(:send_params, true) == init_config()
    end

    test "skip_session_data" do
      add_to_application_env(:skip_session_data, true)
      assert default_configuration() |> Map.put(:skip_session_data, true) == init_config()
    end

    test "working_dir_path" do
      add_to_application_env(:working_dir_path, "/tmp/appsignal")
      assert default_configuration() |> Map.put(:working_dir_path, "/tmp/appsignal") == init_config()
    end
  end

  describe "using the system environment" do
    test "active" do
      System.put_env("APPSIGNAL_ACTIVE", "true")
      assert default_configuration() |> Map.put(:active, true) == init_config()
    end

    test "ca_file_path" do
      System.put_env("APPSIGNAL_CA_FILE_PATH", "/foo/bar/baz.ca")
      assert default_configuration() |> Map.put(:ca_file_path, "/foo/bar/baz.ca") == init_config()
    end

    test "debug" do
      System.put_env("APPSIGNAL_DEBUG", "true")
      assert default_configuration() |> Map.put(:debug, true) == init_config()
    end

    test "enable_host_metrics" do
      System.put_env("APPSIGNAL_ENABLE_HOST_METRICS", "false")
      assert default_configuration() |> Map.put(:enable_host_metrics, false) == init_config()
    end

    test "endpoint" do
      System.put_env("APPSIGNAL_PUSH_API_ENDPOINT", "https://push.staging.lol")
      assert default_configuration() |> Map.put(:endpoint, "https://push.staging.lol") == init_config()
    end

    test "env" do
      System.put_env("APPSIGNAL_APP_ENV", "prod")
      assert default_configuration() |> Map.put(:env, :prod) == init_config()
    end

    test "filter_parameters" do
      System.put_env("APPSIGNAL_FILTER_PARAMETERS", "password,secret")
      assert default_configuration() |> Map.put(:filter_parameters, ~w(password secret)) == init_config()
    end

    test "frontend_error_catching_path" do
      System.put_env("APPSIGNAL_FRONTEND_ERROR_CATCHING_PATH", "/appsignal_error_catcher")
      assert default_configuration() |> Map.put(:frontend_error_catching_path, "/appsignal_error_catcher") == init_config()
    end

    test "hostname" do
      System.put_env("APPSIGNAL_HOSTNAME", "Bobs-MBP.example.com")
      assert default_configuration() |> Map.put(:hostname, "Bobs-MBP.example.com") == init_config()
    end

    test "http_proxy" do
      System.put_env("APPSIGNAL_HTTP_PROXY", "http://10.10.10.10:8888")
      assert default_configuration() |> Map.put(:http_proxy, "http://10.10.10.10:8888") == init_config()
    end

    test "ignore_actions" do
      System.put_env("APPSIGNAL_IGNORE_ACTIONS", "ExampleApplication.PageController#ignored,ExampleApplication.PageController#also_ignored")
      actions = ~w(
          ExampleApplication.PageController#ignored
          ExampleApplication.PageController#also_ignored
      )
      assert default_configuration() |> Map.put(:ignore_actions, actions) == init_config()
    end

    test "ignore_errors" do
      System.put_env("APPSIGNAL_IGNORE_ERRORS", "VerySpecificError,AnotherError")
      errors = ~w(VerySpecificError AnotherError)
      assert default_configuration() |> Map.put(:ignore_errors, errors) == init_config()
    end

    test "log" do
      System.put_env("APPSIGNAL_LOG", "stdout")
      assert default_configuration() |> Map.put(:log, "stdout") == init_config()
    end

    test "log_path" do
      System.put_env("APPSIGNAL_LOG_PATH", "log/appsignal.log")
      assert default_configuration() |> Map.put(:log_path, "log/appsignal.log") == init_config()
    end

    test "name" do
      System.put_env("APPSIGNAL_APP_NAME", "my_application")
      assert default_configuration() |> Map.put(:name, "my_application") == init_config()
    end

    test "push_api_key" do
      System.put_env("APPSIGNAL_PUSH_API_KEY", "00000000-0000-0000-0000-000000000000")
      assert valid_configuration() == init_config()
      assert init_config()[:active] == true
    end

    test "running_in_container" do
      System.put_env("APPSIGNAL_RUNNING_IN_CONTAINER", "true")
      assert default_configuration() |> Map.put(:running_in_container, true) == init_config()
    end

    test "send_params" do
      System.put_env("APPSIGNAL_SEND_PARAMS", "true")
      assert default_configuration() |> Map.put(:send_params, true) == init_config()
    end

    test "skip_session_data" do
      System.put_env("APPSIGNAL_SKIP_SESSION_DATA", "true")
      assert default_configuration() |> Map.put(:skip_session_data, true) == init_config()
    end

    test "working_dir_path" do
      System.put_env("APPSIGNAL_WORKING_DIR_PATH", "/tmp/appsignal")
      assert default_configuration() |> Map.put(:working_dir_path, "/tmp/appsignal") == init_config()
    end
  end

  test "system environment overwrites application environment configuration" do
    System.put_env("APPSIGNAL_PUSH_API_KEY", "00000000-0000-0000-0000-000000000000")
    assert valid_configuration() |> Map.put(:active, true) == init_config()

    clear_env()

    Application.put_env(:appsignal, :config, active: false)
    System.put_env("APPSIGNAL_PUSH_API_KEY", "00000000-0000-0000-0000-000000000000")
    assert valid_configuration() |> Map.put(:active, false) == init_config()
  end

  describe "when on Heroku" do
    setup do
      System.put_env("DYNO", "web.1")
    end

    test ":running_in_container and :log" do

    |> Map.put(:push_api_key, "00000000-0000-0000-0000-000000000000")
  end

  defp init_config() do
    Config.initialize()
    Application.get_all_env(:appsignal)[:config]
