Packages

This is a fork of gleeunit that allows it to be called as a library/function with a list of test modules instead of just via CLI.

Current section

Files

Jump to
glacier_gleeunit src gleeunit.erl
Raw

src/gleeunit.erl

-module(gleeunit).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([main/0, run/2]).
-export_type([atom_/0, encoding/0, report_module_name/0, gleeunit_progress_option/0, eunit_option/0]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
-type atom_() :: any().
-type encoding() :: utf8.
-type report_module_name() :: gleeunit_progress.
-type gleeunit_progress_option() :: {colored, boolean()}.
-type eunit_option() :: verbose |
no_tty |
{report, {report_module_name(), list(gleeunit_progress_option())}}.
-file("src/gleeunit.gleam", 93).
-spec find_matching_test_module_files(list(binary())) -> list(binary()).
find_matching_test_module_files(Test_module_files) ->
_pipe = Test_module_files,
_pipe@2 = gleam@list:filter(
_pipe,
fun(Module_name) ->
begin
_pipe@1 = Module_name,
gleam_stdlib:string_ends_with(_pipe@1, <<".gleam"/utf8>>)
end
=:= true
end
),
gleam@list:filter(
_pipe@2,
fun(Module_name@1) ->
Absolute_module_file_name = <<<<(gleeunit_glacier_ffi:get_cwd_as_binary(
))/binary,
"/"/utf8>>/binary,
Module_name@1/binary>>,
_pipe@3 = filelib:is_regular(Absolute_module_file_name),
gleam@function:tap(_pipe@3, fun(Exists) -> case Exists of
true ->
nil;
false ->
gleam_stdlib:println_error(
<<"Error: Could not find "/utf8,
Absolute_module_file_name/binary>>
)
end end)
end
).
-file("src/gleeunit.gleam", 142).
-spec gleam_to_erlang_module_name(binary()) -> binary().
gleam_to_erlang_module_name(Path) ->
_pipe = Path,
_pipe@1 = gleam@string:replace(_pipe, <<".gleam"/utf8>>, <<""/utf8>>),
_pipe@2 = gleam@string:replace(_pipe@1, <<".erl"/utf8>>, <<""/utf8>>),
gleam@string:replace(_pipe@2, <<"/"/utf8>>, <<"@"/utf8>>).
-file("src/gleeunit.gleam", 27).
-spec detect_all_test_modules() -> list(binary()).
detect_all_test_modules() ->
_pipe = gleeunit_ffi:find_files(
<<"**/*.{erl,gleam}"/utf8>>,
<<"test"/utf8>>
),
gleam@list:map(
_pipe,
fun(Test_module_file_name) ->
<<"test/"/utf8, Test_module_file_name/binary>>
end
).
-file("src/gleeunit.gleam", 35).
-spec run_suite(list(binary()), boolean()) -> nil.
run_suite(Test_module_files, Halts_on_error) ->
Options = [verbose,
no_tty,
{report, {gleeunit_progress, [{colored, true}]}}],
Result = begin
_pipe = Test_module_files,
_pipe@1 = gleam@list:map(
_pipe,
fun(Test_module_file) ->
_assert_subject = gleam@string:split_once(
Test_module_file,
<<"test/"/utf8>>
),
{ok, {_, Test_module_file@1}} = case _assert_subject of
{ok, {_, _}} -> _assert_subject;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
value => _assert_fail,
module => <<"gleeunit"/utf8>>,
function => <<"run_suite"/utf8>>,
line => 44})
end,
Test_module_file@1
end
),
_pipe@2 = gleam@list:map(_pipe@1, fun gleam_to_erlang_module_name/1),
_pipe@3 = gleam@list:map(
_pipe@2,
fun(_capture) -> erlang:binary_to_atom(_capture, utf8) end
),
gleeunit_ffi:run_eunit(_pipe@3, Options)
end,
Code = case Result of
{ok, _} ->
0;
{error, _} ->
1
end,
case {Halts_on_error, Code} of
{true, Code@1} ->
erlang:halt(Code@1);
{false, 0} ->
nil;
{false, _} ->
nil
end.
-file("src/gleeunit.gleam", 16).
?DOC(
" Find and run all test functions for the current project using Erlang's EUnit\n"
" test framework.\n"
"\n"
" Any Erlang or Gleam function in the `test` directory with a name ending in\n"
" `_test` is considered a test function and will be run.\n"
"\n"
" If running on JavaScript tests will be run with a custom test runner.\n"
).
-spec main() -> nil.
main() ->
_pipe = detect_all_test_modules(),
run_suite(_pipe, true).
-file("src/gleeunit.gleam", 84).
?DOC(
" Similar to `main()` but meant to be called as a function not from cli:\n"
" - Allows to specificity a list of test modules\n"
" - Allows customization of the halt behavior\n"
).
-spec run(list(binary()), boolean()) -> nil.
run(Test_module_files, Halts_on_error) ->
_pipe = Test_module_files,
_pipe@1 = find_matching_test_module_files(_pipe),
run_suite(_pipe@1, Halts_on_error).