Current section
Files
Jump to
Current section
Files
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", 34).
?DOC(" Run `test_fn` with an empty environment; original env is restored afterwards.\n").
-spec isolated(fun(() -> EWR)) -> EWR.
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),
Current_env = envie:all(),
_pipe@1 = maps:to_list(Current_env),
gleam@list:each(
_pipe@1,
fun(Pair) ->
{Key, _} = Pair,
case gleam_stdlib:map_get(Original_env, Key) of
{ok, _} ->
nil;
{error, _} ->
envie:unset(Key)
end
end
),
_pipe@2 = maps:to_list(Original_env),
gleam@list:each(
_pipe@2,
fun(Pair@1) ->
{Key@1, Value} = Pair@1,
envie:set(Key@1, 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 => 60})
end.
-file("src/envie/testing.gleam", 72).
-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", 85).
-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", 15).
?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(() -> EWQ)) -> EWQ.
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 => 29})
end.
-file("src/envie/testing.gleam", 65).
?DOC(" Parse `content` as `.env`, set the variables for `test_fn`, then restore the env.\n").
-spec with_dotenv_content(binary(), fun(() -> EWS)) -> EWS.
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 => 66,
value => _assert_fail,
start => 1628,
'end' => 1677,
pattern_start => 1639,
pattern_end => 1647})
end,
with_env(Vars@1, Test_fn).