From 4667ec488af225690635f2f6ceaa3640c75c8a32 Mon Sep 17 00:00:00 2001 From: Andrew Dryga Date: Mon, 26 Aug 2024 14:07:29 -0600 Subject: [PATCH] Fix coverage --- lib/logger_json/formatter/metadata.ex | 2 +- test/logger_json/formatter/metadata_test.exs | 43 ++++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/lib/logger_json/formatter/metadata.ex b/lib/logger_json/formatter/metadata.ex index ed00741..2345ea3 100644 --- a/lib/logger_json/formatter/metadata.ex +++ b/lib/logger_json/formatter/metadata.ex @@ -18,7 +18,7 @@ defmodule LoggerJSON.Formatter.Metadata do end def update_metadata_selector({:from_application_env, other}, _processed_keys) do - raise """ + raise ArgumentError, """ Invalid value for `:metadata` option: `{:from_application_env, #{inspect(other)}}`. The value must be a tuple with the application and module name, diff --git a/test/logger_json/formatter/metadata_test.exs b/test/logger_json/formatter/metadata_test.exs index 653685b..1ee3576 100644 --- a/test/logger_json/formatter/metadata_test.exs +++ b/test/logger_json/formatter/metadata_test.exs @@ -3,6 +3,49 @@ defmodule LoggerJSON.Formatter.MetadataTest do import LoggerJSON.Formatter.Metadata describe "update_metadata_selector/2" do + # test this + # def update_metadata_selector({:from_application_env, {app, module}, path}, processed_keys) do + # Application.fetch_env!(app, module) + # |> get_in(path) + # |> update_metadata_selector(processed_keys) + # end + + # def update_metadata_selector({:from_application_env, {app, module}}, processed_keys) do + # Application.fetch_env!(app, module) + # |> update_metadata_selector(processed_keys) + # end + + # def update_metadata_selector({:from_application_env, other}, _processed_keys) do + # raise """ + # Invalid value for `:metadata` option: `{:from_application_env, #{inspect(other)}}`. + + # The value must be a tuple with the application and module name, + # and an optional path to the metadata option. + + # Eg.: `{:from_application_env, {:logger, :default_formatter}, [:metadata]}` + # """ + # end + + test "takes metadata from application env" do + Application.put_env(:logger_json, :test_metadata_key, [:foo]) + + assert update_metadata_selector({:from_application_env, {:logger_json, :test_metadata_key}}, []) == + [:foo] + + Application.put_env(:logger_json, :test_metadata_key, %{metadata: [:foo]}) + + assert update_metadata_selector({:from_application_env, {:logger_json, :test_metadata_key}, [:metadata]}, []) == + [:foo] + end + + test "raises if metadata is not a tuple with the application and module name" do + message = ~r/Invalid value for `:metadata` option: `{:from_application_env, :foo}`./ + + assert_raise ArgumentError, message, fn -> + update_metadata_selector({:from_application_env, :foo}, []) + end + end + test "takes metadata :all rule and updates it to exclude the given keys" do assert update_metadata_selector(:all, [:ansi_color]) == {:all_except, [:ansi_color]} end