-module(startest@locator). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]). -export([locate_test_files/1, identify_tests/2]). -export_type([test_file/0, test_source_file/0, test_function/0]). -type test_file() :: {test_file, binary(), binary(), list(startest@test_tree:test_tree()), birl@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_())}. -spec filepath_to_module_name(binary()) -> binary(). filepath_to_module_name(Filepath) -> _pipe = Filepath, _pipe@1 = gleam@string:slice( _pipe, gleam@string:length(<<"test/"/utf8>>), gleam@string:length(Filepath) ), gleam@string:replace(_pipe@1, <<".gleam"/utf8>>, <<""/utf8>>). -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:nil_error(_pipe) end, fun(Test_files) -> _pipe@1 = Test_files, _pipe@2 = gleam@list:filter( _pipe@1, fun(Filepath) -> gleam@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 ). -spec is_standalone_test(test_function(), startest@context:context()) -> boolean(). is_standalone_test(Test_function, Ctx) -> _pipe = erlang:element(3, Test_function), gleam@regex:check(erlang:element(4, erlang:element(2, Ctx)), _pipe). -spec is_test_suite(test_function(), startest@context:context()) -> boolean(). is_test_suite(Test_function, Ctx) -> _pipe = erlang:element(3, Test_function), gleam@regex:check(erlang:element(3, erlang:element(2, Ctx)), _pipe). -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 = gleam@dynamic:from(_pipe@2), gleam@dynamic: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 = startest@test_tree:decode_test_tree(_pipe@7), gleam@result:map_error( _pipe@8, fun(Error) -> startest@logger:error( erlang:element(4, Ctx), gleam@string:inspect(Error) ) end ) end ) end, Tests = gleam@list:concat([Test_suites@1, Standalone_tests@1]), case Tests of [] -> {error, nil}; Tests@1 -> Collect_duration = begin _pipe@9 = bigben@clock:now(erlang:element(3, Ctx)), birl:difference(_pipe@9, Started_at) end, {ok, {test_file, erlang:element(2, Test_file), erlang:element(3, Test_file), Tests@1, Collect_duration}} end. -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 ).