Compare commits

..

2 Commits

Author SHA1 Message Date
c848ca17ad Gave up trying to reset env var - set to "*" after test
And after doctest. it stayed at "*" after the last test anyway, letting
the doctest fail.

doctests are not recommended for side effectful tests anyway https://hexdocs.pm/ex_unit/main/ExUnit.DocTest.html
2025-09-14 21:30:08 +01:00
ca4eaf6f54 Add workaround for failing doctest
on_exit in test does not work properly as teardown - the variable is
unset, but doctest apparently runs after tests and it is not cleaned up
2025-09-14 20:19:18 +01:00
2 changed files with 21 additions and 21 deletions

View File

@ -18,8 +18,12 @@ defmodule BasicSignupWhitelist do
## Examples ## Examples
iex> System.delete_env("SIGNUP_ALLOWED_EMAILS")
iex> BasicSignupWhitelist.mail_whitelisted("joe@example.com") iex> BasicSignupWhitelist.mail_whitelisted("joe@example.com")
false false
iex> System.put_env("SIGNUP_ALLOWED_EMAILS","*")
iex> BasicSignupWhitelist.mail_whitelisted("joe@example.com")
true
""" """
def mail_whitelisted(email) do def mail_whitelisted(email) do

View File

@ -1,33 +1,29 @@
defmodule BasicSignupWhitelistTest do defmodule BasicSignupWhitelistTest do
use ExUnit.Case use ExUnit.Case, async: false
doctest BasicSignupWhitelist doctest BasicSignupWhitelist
defp allow_signups_for(whitelist) do defp allow_signups_for(whitelist) do
System.put_env("SIGNUP_ALLOWED_EMAILS", whitelist) System.put_env("SIGNUP_ALLOWED_EMAILS", whitelist)
end end
setup do describe "setup in describe block" do
# Save original env var setup do
original_env = System.get_env("SIGNUP_ALLOWED_EMAILS") on_exit(fn ->
# resetting values did not work, so 'just' set it to the effect without this module configured.
System.put_env("SIGNUP_ALLOWED_EMAILS", "*")
end)
on_exit(fn -> :ok
# Restore original env var after each test end
case original_env do
nil -> System.delete_env("SIGNUP_ALLOWED_EMAILS")
value -> System.put_env("SIGNUP_ALLOWED_EMAILS", value)
end
end)
:ok test "When not set, not whitelisted" do
end System.delete_env("SIGNUP_ALLOWED_EMAILS")
refute BasicSignupWhitelist.mail_whitelisted("joe@example.com")
end
test "When not set, not whitelisted" do test "When set to star, whitelisted" do
System.delete_env("SIGNUP_ALLOWED_EMAILS") allow_signups_for("*")
refute BasicSignupWhitelist.mail_whitelisted("joe@example.com") assert BasicSignupWhitelist.mail_whitelisted("joe@example.com")
end end
test "When set to star, whitelisted" do
allow_signups_for("*")
assert BasicSignupWhitelist.mail_whitelisted("joe@example.com")
end end
end end