Packages

Type-safe environment variables for Gleam. Zero dependencies.

Current section

Files

Jump to
envie src envie@testing.erl
Raw

src/envie@testing.erl

-module(envie@testing).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/envie/testing.gleam").
-export([isolated/1, with_env/2, with_dotenv_content/2]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
-file("src/envie/testing.gleam", 35).
?DOC(" Run `test_fn` with an empty environment; original env is restored afterwards.\n").
-spec isolated(fun(() -> EHF)) -> EHF.
isolated(Test_fn) ->
Original_env = envie:all(),
_pipe = maps:keys(Original_env),
gleam@list:each(_pipe, fun envie:unset/1),
Result = envie_ffi:rescue(Test_fn),
_pipe@1 = maps:to_list(Original_env),
gleam@list:each(
_pipe@1,
fun(Pair) ->
{Key, Value} = Pair,
envie:set(Key, Value)
end
),
case Result of
{ok, Value@1} ->
Value@1;
{error, Msg} ->
erlang:error(#{gleam_error => panic,
message => Msg,
file => <<?FILEPATH/utf8>>,
module => <<"envie/testing"/utf8>>,
function => <<"isolated"/utf8>>,
line => 51})
end.
-file("src/envie/testing.gleam", 63).
-spec save_env(list({binary(), binary()})) -> gleam@dict:dict(binary(), gleam@option:option(binary())).
save_env(Vars) ->
_pipe = Vars,
_pipe@1 = gleam@list:map(
_pipe,
fun(Pair) ->
{Key, _} = Pair,
Original = case envie:get(Key) of
{ok, Value} ->
{some, Value};
{error, _} ->
none
end,
{Key, Original}
end
),
maps:from_list(_pipe@1).
-file("src/envie/testing.gleam", 76).
-spec restore_env(gleam@dict:dict(binary(), gleam@option:option(binary()))) -> nil.
restore_env(Saved) ->
_pipe = maps:to_list(Saved),
gleam@list:each(
_pipe,
fun(Pair) ->
{Key, Maybe_value} = Pair,
case Maybe_value of
{some, Value} ->
envie:set(Key, Value);
none ->
envie:unset(Key)
end
end
).
-file("src/envie/testing.gleam", 16).
?DOC(
" Run `test_fn` with the given environment variables set.\n"
" Previous values are saved and restored afterwards (panic-safe).\n"
).
-spec with_env(list({binary(), binary()}), fun(() -> EHE)) -> EHE.
with_env(Vars, Test_fn) ->
Original = save_env(Vars),
gleam@list:each(
Vars,
fun(Pair) ->
{Key, Value} = Pair,
envie:set(Key, Value)
end
),
Result = envie_ffi:rescue(Test_fn),
restore_env(Original),
case Result of
{ok, Value@1} ->
Value@1;
{error, Msg} ->
erlang:error(#{gleam_error => panic,
message => Msg,
file => <<?FILEPATH/utf8>>,
module => <<"envie/testing"/utf8>>,
function => <<"with_env"/utf8>>,
line => 30})
end.
-file("src/envie/testing.gleam", 56).
?DOC(" Parse `content` as `.env`, set the variables for `test_fn`, then restore the env.\n").
-spec with_dotenv_content(binary(), fun(() -> EHG)) -> EHG.
with_dotenv_content(Content, Test_fn) ->
Vars@1 = case envie:parse_dotenv(Content) of
{ok, Vars} -> Vars;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
file => <<?FILEPATH/utf8>>,
module => <<"envie/testing"/utf8>>,
function => <<"with_dotenv_content"/utf8>>,
line => 57,
value => _assert_fail,
start => 1549,
'end' => 1598,
pattern_start => 1560,
pattern_end => 1568})
end,
with_env(Vars@1, Test_fn).