Browse Source

Fix keep_logs option crashing when having more than N directories

rebar3 would crash if the option `{keep_logs, N}` was present and
there were more than N run directories in the log dir, so if the
option is found we keep the N-1 newest directories and delete the
rest.

While we are at it, as this should fix the crash, let's limit the
number of logs runs to keep from test runs in rebar3 to 5.
pull/2320/head
Pablo Costas 4 years ago
parent
commit
47a6b5981d
2 changed files with 29 additions and 0 deletions
  1. +3
    -0
      rebar.config
  2. +26
    -0
      src/rebar_prv_common_test.erl

+ 3
- 0
rebar.config View File

@ -49,6 +49,9 @@
{exclude_mods, [rebar_prv_alias]}
]}.
%% Keep only the logs of the last 5 runs
{ct_opts, [{keep_logs, 5}]}.
%% Profiles
{profiles, [{test, [
{deps, [{meck, "0.8.13"}]},

+ 26
- 0
src/rebar_prv_common_test.erl View File

@ -686,12 +686,38 @@ translate(State, [], Path) ->
{error, badparent} -> Path
end.
-spec handle_keep_logs(file:filename(), pos_integer()) -> ok.
handle_keep_logs(LogDir, N) ->
case file:list_dir(LogDir) of
{ok, Filenames} ->
Dirs = lists:filter(fun(File) ->
filelib:is_dir(filename:join([LogDir, File]))
end, Filenames) -- ["last"], %% we ignore the symlink as we later handle it
case Dirs of
%% first time running the tests, there are no logs to delete
[] -> ok;
_ ->
SortedDirs = lists:reverse(lists:sort(Dirs)),
%% sort the log dirs and keep the N - 1 newest
{_Keep, Discard} = lists:split(N - 1, SortedDirs),
?DEBUG("Removing the following directories because keep_logs option was found: ~p", [Discard]),
[rebar_file_utils:rm_rf(filename:join([LogDir, Dir])) || Dir <- Discard],
ok
end;
_ -> ok
end.
setup_logdir(State, Opts) ->
Logdir = case proplists:get_value(logdir, Opts) of
undefined -> filename:join([rebar_dir:base_dir(State), "logs"]);
Dir -> Dir
end,
filelib:ensure_dir(filename:join([Logdir, "dummy.beam"])),
case proplists:get_value(keep_logs, Opts) of
all -> ok;
undefined -> ok;
N -> handle_keep_logs(Logdir, N)
end,
[{logdir, Logdir}|lists:keydelete(logdir, 1, Opts)].
turn_off_auto_compile(Opts) ->

Loading…
Cancel
Save