Skip to content

Commit

Permalink
add regression case for pruning artifacts
Browse files Browse the repository at this point in the history
This discovered a bug where dangling artifact files could be left in the
DAG and bloat it over time. This was unlikely to be a real problem, but
it's nice to be clean.
  • Loading branch information
ferd committed May 22, 2022
1 parent 27b3c9e commit c13ed8f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/rebar_compiler_dag.erl
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,7 @@ maybe_rm_artifact_and_edge(G, OutDir, SrcExt, Ext, Source) ->
lists:foreach(fun(Target) ->
?DIAGNOSTIC("Source ~ts is gone, deleting artifact ~ts "
"if it exists", [Source, Target]),
digraph:del_vertex(G, Target),
file:delete(Target)
end, Targets)
end,
Expand Down
36 changes: 35 additions & 1 deletion test/rebar_compiler_dag_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
-include_lib("kernel/include/file.hrl").

all() ->
[exists, {group, with_project}].
[exists, {group, with_project}, prune_preserve_artifacts].

groups() ->
%% The tests in this group are dirty, the order is specific
Expand Down Expand Up @@ -365,6 +365,40 @@ propagate_app2_ptrans_hrl(Config) ->
matches(Matches, FileStamps),
ok.

prune_preserve_artifacts() ->
[{doc, "Build artifacts are kept through the pruning process, even with multiple "
"artifact types."}].
prune_preserve_artifacts(Config) ->
Priv = ?config(priv_dir, Config),
Source = filename:join(Priv, "file.src"),
%% The source file must exist not to get the artifacts pruned
ok = file:write_file(Source, <<"hello, world!">>),
Opts = [some_option, {with, terms}],
G = digraph:new([acyclic]),
digraph:add_vertex(G, Source, 123),
rebar_compiler_dag:store_artifact(G, Source, "artifact.type1", Opts),
rebar_compiler_dag:store_artifact(G, Source, "artifact.type2", Opts),
rebar_compiler_dag:store_artifact(G, Source, "derived.type3", Opts),
AppPaths = [{Priv, filename:join(Priv, "out")}],
ct:pal("all vertices: ~p~n", [digraph:vertices(G)]),
%% Prune with all types being valid aside from the source file;
%% expect all of the artifacts to be kept
rebar_compiler_dag:prune(G, ".src", [".type1",".type2",".type3"], [Source], AppPaths),
?assertEqual([Source, "artifact.type1", "artifact.type2", "derived.type3"],
lists:sort(digraph:vertices(G) -- ['$r3_dirty_bit'])),
%% Prune artifacts that no longer belong to the compiler definition
rebar_compiler_dag:prune(G, ".src", [".type1",".type2"], [Source], AppPaths),
?assertEqual([Source, "artifact.type1", "artifact.type2"],
lists:sort(digraph:vertices(G) -- ['$r3_dirty_bit'])),
%% if the source file is gone, prune everything
ok = file:delete(Source),
rebar_compiler_dag:prune(G, ".src", [".type1",".type2"], [Source], AppPaths),
?assertEqual([Source, "artifact.type1", "artifact.type2"],
lists:sort(digraph:vertices(G) -- ['$r3_dirty_bit'])),
rebar_compiler_dag:prune(G, ".src", [".type1",".type2"], [], AppPaths),
?assertEqual([], lists:sort(digraph:vertices(G) -- ['$r3_dirty_bit'])),
ok.

%%%%%%%%%%%%%%%
%%% HELPERS %%%
%%%%%%%%%%%%%%%
Expand Down

0 comments on commit c13ed8f

Please sign in to comment.