Current section
Files
Jump to
Current section
Files
src/startest@locator.erl
-module(startest@locator).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-define(FILEPATH, "src/startest/locator.gleam").
-export([locate_test_files/1, identify_tests/2]).
-export_type([test_file/0, test_source_file/0, test_function/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 test_file() :: {test_file,
binary(),
binary(),
list(startest@test_tree:test_tree()),
gleam@time@duration:duration()}.
-type test_source_file() :: {test_source_file,
binary(),
binary(),
list(test_function())}.
-type test_function() :: {test_function,
binary(),
binary(),
fun(() -> gleam@dynamic:dynamic_())}.
-file("src/startest/locator.gleam", 72).
?DOC(" Returns the Gleam module name from the given filepath.\n").
-spec filepath_to_module_name(binary()) -> binary().
filepath_to_module_name(Filepath) ->
_pipe = Filepath,
_pipe@1 = gleam@string:slice(
_pipe,
string:length(<<"test/"/utf8>>),
string:length(Filepath)
),
gleam@string:replace(_pipe@1, <<".gleam"/utf8>>, <<""/utf8>>).
-file("src/startest/locator.gleam", 44).
?DOC(" Returns the list of files in the `test/` directory.\n").
-spec locate_test_files(startest@context:context()) -> {ok,
list(test_source_file())} |
{error, nil}.
locate_test_files(Ctx) ->
gleam@result:'try'(
begin
_pipe = simplifile:get_files(<<"test"/utf8>>),
gleam@result:replace_error(_pipe, nil)
end,
fun(Test_files) -> _pipe@1 = Test_files,
_pipe@2 = gleam@list:filter(
_pipe@1,
fun(Filepath) ->
gleam_stdlib:string_ends_with(Filepath, <<".gleam"/utf8>>)
end
),
_pipe@3 = gleam@list:filter(
_pipe@2,
fun(Filepath@1) ->
case erlang:element(5, erlang:element(2, Ctx)) of
[] ->
true;
Filters ->
gleam@list:any(
Filters,
fun(Filter) ->
gleam_stdlib:contains_string(
Filepath@1,
Filter
)
end
)
end
end
),
_pipe@4 = gleam@list:map(
_pipe@3,
fun(Filepath@2) ->
{test_source_file,
filepath_to_module_name(Filepath@2),
Filepath@2,
[]}
end
),
{ok, _pipe@4} end
).
-file("src/startest/locator.gleam", 149).
-spec is_standalone_test(test_function(), startest@context:context()) -> boolean().
is_standalone_test(Test_function, Ctx) ->
_pipe = erlang:element(3, Test_function),
gleam@regexp:check(erlang:element(4, erlang:element(2, Ctx)), _pipe).
-file("src/startest/locator.gleam", 154).
-spec is_test_suite(test_function(), startest@context:context()) -> boolean().
is_test_suite(Test_function, Ctx) ->
_pipe = erlang:element(3, Test_function),
gleam@regexp:check(erlang:element(3, erlang:element(2, Ctx)), _pipe).
-file("src/startest/locator.gleam", 96).
-spec identify_tests_in_file(test_source_file(), startest@context:context()) -> {ok,
test_file()} |
{error, nil}.
identify_tests_in_file(Test_file, Ctx) ->
Started_at = bigben@clock:now(erlang:element(3, Ctx)),
{Standalone_tests, Test_functions} = begin
_pipe = erlang:element(4, Test_file),
gleam@list:partition(
_pipe,
fun(_capture) -> is_standalone_test(_capture, Ctx) end
)
end,
Standalone_tests@1 = begin
_pipe@1 = Standalone_tests,
gleam@list:map(
_pipe@1,
fun(Test_function) ->
Function = begin
_pipe@2 = erlang:element(4, Test_function),
_pipe@3 = startest@internal@unsafe:from(_pipe@2),
startest@internal@unsafe:coerce(_pipe@3)
end,
_pipe@4 = {test,
erlang:element(3, Test_function),
Function,
false},
{test, _pipe@4}
end
)
end,
{Test_suites, _} = begin
_pipe@5 = Test_functions,
gleam@list:partition(
_pipe@5,
fun(_capture@1) -> is_test_suite(_capture@1, Ctx) end
)
end,
Test_suites@1 = begin
_pipe@6 = Test_suites,
gleam@list:filter_map(
_pipe@6,
fun(Test_function@1) ->
_pipe@7 = (erlang:element(4, Test_function@1))(),
_pipe@8 = gleam@dynamic@decode:run(
_pipe@7,
startest@test_tree:test_tree_decoder()
),
gleam@result:map_error(
_pipe@8,
fun(Error) ->
startest@logger:error(
erlang:element(4, Ctx),
gleam@string:inspect(Error)
)
end
)
end
)
end,
Tests = lists:append([Test_suites@1, Standalone_tests@1]),
case Tests of
[] ->
{error, nil};
Tests@1 ->
Collect_duration = begin
_pipe@9 = Started_at,
gleam@time@timestamp:difference(
_pipe@9,
bigben@clock:now(erlang:element(3, Ctx))
)
end,
{ok,
{test_file,
erlang:element(2, Test_file),
erlang:element(3, Test_file),
Tests@1,
Collect_duration}}
end.
-file("src/startest/locator.gleam", 88).
?DOC(
" Identifies all of the tests contained in the given list of `TestSourceFile`s.\n"
"\n"
" Any files that don't have any tests will be excluded from the result.\n"
).
-spec identify_tests(list(test_source_file()), startest@context:context()) -> list(test_file()).
identify_tests(Test_files, Ctx) ->
_pipe = Test_files,
gleam@list:filter_map(
_pipe,
fun(_capture) -> identify_tests_in_file(_capture, Ctx) end
).