From d685a784fc9758a182ef666724ee72e1cbf1ca05 Mon Sep 17 00:00:00 2001 From: Igor Toporkov Date: Sat, 9 Oct 2021 23:02:59 +0200 Subject: [PATCH 1/3] Cleanup no longer present test suites from _build --- src/rebar_prv_common_test.erl | 38 +++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/src/rebar_prv_common_test.erl b/src/rebar_prv_common_test.erl index 00e970cbf..9353a30b8 100644 --- a/src/rebar_prv_common_test.erl +++ b/src/rebar_prv_common_test.erl @@ -45,6 +45,12 @@ do(State) -> case compile(State, Tests) of %% successfully compiled apps {ok, S} -> + {ok, T} = Tests, + TestSources = proplists:get_value(dir, T), + AllDeps = rebar_state:code_paths(S, all_deps), + IsTestDir = fun(Path) -> string:slice(Path, length(Path) - 4, 4) == "test" end, + CompiledTestsDirs = lists:filter(IsTestDir, AllDeps), + cleanup_unused_test_files(CompiledTestsDirs, TestSources), {RawOpts, _} = rebar_state:command_parsed_args(S), case proplists:get_value(compile_only, RawOpts, false) of true -> @@ -86,6 +92,38 @@ do(State, Tests) -> Error end. +cleanup_unused_test_files(CompiledTestsDirs, TestSources) -> + GetModulesFun = fun(Dir, Acc) -> + Suites = rebar_utils:find_files(Dir, ".*_SUITE\.erl\$", false), + Modules = sets:from_list(lists:map(fun rebar_utils:erl_to_mod/1, Suites)), + sets:union(Acc, Modules) + end, + PresentTestModules = lists:foldl(GetModulesFun, sets:new(), TestSources), + CleanupFun = fun(Dir) -> + cleanup_unused_test_files_dir(Dir, PresentTestModules) + end, + lists:foreach(CleanupFun, CompiledTestsDirs). + +cleanup_unused_test_files_dir(Dir, PresentTestModules) -> + Mapping = create_module_to_files_mapping(Dir), + RemoveFun = fun(Module, {SrcPath, BeamPath}) -> + case sets:is_element(Module, PresentTestModules) of + true -> ok; + false -> file:delete(SrcPath), file:delete(BeamPath) + end + end, + _ = maps:map(RemoveFun, Mapping), + ok. + +create_module_to_files_mapping(Dir) -> + SrcFiles = rebar_utils:find_files(Dir, ".*_SUITE\.erl\$", false), + MappingFun = fun(SrcFile, Acc) -> + Module = rebar_utils:erl_to_mod(SrcFile), + BeamFile = filename:join([Dir, Module]) ++ ".beam", + Acc#{Module => {SrcFile, BeamFile}} + end, + lists:foldl(MappingFun, #{}, SrcFiles). + run_tests(State, Opts) -> T = translate_paths(State, Opts), Opts1 = setup_logdir(State, T), From 21a2ce0246c076ac97dd1cb03d0643526eeb2504 Mon Sep 17 00:00:00 2001 From: Igor Toporkov Date: Sun, 10 Oct 2021 13:36:52 +0200 Subject: [PATCH 2/3] Handle case when there are no test sources --- src/rebar_prv_common_test.erl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/rebar_prv_common_test.erl b/src/rebar_prv_common_test.erl index 9353a30b8..a14c5ab10 100644 --- a/src/rebar_prv_common_test.erl +++ b/src/rebar_prv_common_test.erl @@ -46,7 +46,7 @@ do(State) -> %% successfully compiled apps {ok, S} -> {ok, T} = Tests, - TestSources = proplists:get_value(dir, T), + TestSources = proplists:get_value(dir, T, []), AllDeps = rebar_state:code_paths(S, all_deps), IsTestDir = fun(Path) -> string:slice(Path, length(Path) - 4, 4) == "test" end, CompiledTestsDirs = lists:filter(IsTestDir, AllDeps), From 356c0aa620db58d71365e316429f4c3a34073cde Mon Sep 17 00:00:00 2001 From: Igor Toporkov Date: Tue, 12 Oct 2021 21:20:30 +0200 Subject: [PATCH 3/3] Delete unreachable clause in rebar_prv_common_test:do/2 --- src/rebar_prv_common_test.erl | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/src/rebar_prv_common_test.erl b/src/rebar_prv_common_test.erl index a14c5ab10..3a93fca2f 100644 --- a/src/rebar_prv_common_test.erl +++ b/src/rebar_prv_common_test.erl @@ -73,22 +73,17 @@ do(State, Tests) -> %% Run ct provider pre hooks for all project apps and top level project hooks rebar_hooks:run_project_and_app_hooks(Cwd, pre, ?PROVIDER, Providers, State), - case Tests of - {ok, T} -> - case run_tests(State, T) of - ok -> - %% Run ct provider post hooks for all project apps and top level project hooks - rebar_hooks:run_project_and_app_hooks(Cwd, post, ?PROVIDER, Providers, State), - rebar_paths:set_paths([plugins, deps], State), - symlink_to_last_ct_logs(State, T), - {ok, State}; - Error -> - rebar_paths:set_paths([plugins, deps], State), - symlink_to_last_ct_logs(State, T), - Error - end; + {ok, T} = Tests, + case run_tests(State, T) of + ok -> + %% Run ct provider post hooks for all project apps and top level project hooks + rebar_hooks:run_project_and_app_hooks(Cwd, post, ?PROVIDER, Providers, State), + rebar_paths:set_paths([plugins, deps], State), + symlink_to_last_ct_logs(State, T), + {ok, State}; Error -> rebar_paths:set_paths([plugins, deps], State), + symlink_to_last_ct_logs(State, T), Error end.