Skip to content

Commit

Permalink
✅ Use capture_log instead of with_log
Browse files Browse the repository at this point in the history
with_log wasn't introduced until Elixir 1.13, but we still run CI on 1.12.
  • Loading branch information
randycoulman committed Jan 10, 2024
1 parent ef0a042 commit 50f8ba2
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 28 deletions.
8 changes: 3 additions & 5 deletions test/config_cat_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -175,13 +175,11 @@ defmodule ConfigCatTest do
warning? = unquote(warning?)
user = if user_id, do: User.new(user_id)

{value, logs} =
with_log(fn ->
ConfigCat.get_value(key, default_value, user, client: client)
logs =
capture_log(fn ->
assert expected_value == ConfigCat.get_value(key, default_value, user, client: client)
end)

assert value == expected_value

if warning? do
default_type = SettingType.infer_elixir_type(default_value)
expected_type = SettingType.infer_elixir_type(expected_value)
Expand Down
7 changes: 3 additions & 4 deletions test/evaluation_log_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -108,17 +108,16 @@ defmodule ConfigCat.EvaluationLogTest do
# test_name = Path.basename(expected_log_file, ".txt")
expected_log = File.read!(Path.join(suite_sub_dir, expected_log_file))

{value, log} =
with_log(fn ->
ConfigCat.get_value(key, default_value, user, client: client)
log =
capture_log(fn ->
assert return_value == ConfigCat.get_value(key, default_value, user, client: client)
end)

{expected_clean_log, expected_user} = extract_logged_user(expected_log)
{clean_log, actual_user} = extract_logged_user(log)

assert clean_log == expected_clean_log
assert actual_user == expected_user
assert value == return_value
end

defp build_user(nil), do: nil
Expand Down
32 changes: 13 additions & 19 deletions test/rollout_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -172,13 +172,11 @@ defmodule ConfigCat.RolloutTest do

key = "boolTextEqualsNumber"

{value, logs} =
with_log(fn ->
ConfigCat.get_value(key, nil, user, client: client)
logs =
capture_log(fn ->
assert ConfigCat.get_value(key, nil, user, client: client)
end)

assert value

expected_log =
"warning [3005] Evaluation of condition (User.#{custom_attribute_name} EQUALS '#{custom_attribute_value}') " <>
"for setting '#{key}' may not produce the expected result (the User.#{custom_attribute_name} attribute is not a string value, " <>
Expand All @@ -201,13 +199,11 @@ defmodule ConfigCat.RolloutTest do
}
""")

{details, logs} =
with_log(fn ->
Rollout.evaluate("test", nil, false, "default_variation_id", config)
logs =
capture_log(fn ->
assert %EvaluationDetails{value: false} = Rollout.evaluate("test", nil, false, "default_variation_id", config)
end)

assert %EvaluationDetails{value: false} = details

expected_log =
"error [2001] Failed to evaluate setting 'test'. " <>
"(Setting value is not of the expected type String.t())"
Expand Down Expand Up @@ -325,12 +321,12 @@ defmodule ConfigCat.RolloutTest do
|> LocalFileDataSource.new(:local_only)
|> OverrideDataSource.overrides()

{details, logs} =
with_log(fn ->
Rollout.evaluate(key, nil, "default_value", "default_variation_id", config)
logs =
capture_log(fn ->
assert %EvaluationDetails{value: "default_value"} =
Rollout.evaluate(key, nil, "default_value", "default_variation_id", config)
end)

assert %EvaluationDetails{value: "default_value"} = details
assert logs =~ "Circular dependency detected"
assert logs =~ dependency_cycle
end
Expand Down Expand Up @@ -378,13 +374,11 @@ defmodule ConfigCat.RolloutTest do

{:ok, client} = start_config_cat(sdk_key, flag_overrides: flag_overrides)

{value, logs} =
with_log(fn ->
ConfigCat.get_value(key, nil, client: client)
logs =
capture_log(fn ->
assert expected_value == ConfigCat.get_value(key, nil, client: client)
end)

assert value == expected_value

unless expected_value do
flag_value_type = SettingType.infer_elixir_type(flag_value)

Expand Down

0 comments on commit 50f8ba2

Please sign in to comment.