From b7911065ec52432ed5f04ad49b1e601ec34d6672 Mon Sep 17 00:00:00 2001 From: Eric Avdey Date: Thu, 4 Jan 2018 10:57:48 -0400 Subject: Cleanup data dirs in eunit_plugin before test run We use `unique_monotonic_integer` to name the test databases in unit testing. That means that on erlang R > 18 we are always creating databases with same names. When unit tests crashing or don't properly cleaning up on teardown, they are leaving behind old database files, son on a next run tests' setup fails with `database exists` exception. This fix cleans up old database files making sure we are always running unit tests from same blank state. --- rel/plugins/eunit_plugin.erl | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'rel/plugins') diff --git a/rel/plugins/eunit_plugin.erl b/rel/plugins/eunit_plugin.erl index bbf83d2ec..1de20b394 100644 --- a/rel/plugins/eunit_plugin.erl +++ b/rel/plugins/eunit_plugin.erl @@ -32,8 +32,28 @@ build_eunit_config(Config0, AppFile) -> Cwd = filename:absname(rebar_utils:get_cwd()), DataDir = Cwd ++ "/tmp/data", ViewIndexDir = Cwd ++ "/tmp/data", + TmpDataDir = Cwd ++ "/tmp/tmp_data", + cleanup_dirs([DataDir, TmpDataDir]), Config1 = rebar_config:set_global(Config0, template, "setup_eunit"), Config2 = rebar_config:set_global(Config1, prefix, Cwd), Config3 = rebar_config:set_global(Config2, data_dir, DataDir), Config = rebar_config:set_global(Config3, view_index_dir, ViewIndexDir), rebar_templater:create(Config, AppFile). + + +cleanup_dirs(Dirs) -> + lists:foreach(fun(Dir) -> + case filelib:is_dir(Dir) of + true -> del_dir(Dir); + false -> ok + end + end, Dirs). + + +del_dir(Dir) -> + All = filelib:wildcard(Dir ++ "/**"), + {Dirs, Files} = lists:partition(fun filelib:is_dir/1, All), + ok = lists:foreach(fun file:delete/1, Files), + SortedDirs = lists:sort(fun(A, B) -> length(A) > length(B) end, Dirs), + ok = lists:foreach(fun file:del_dir/1, SortedDirs), + ok = file:del_dir(Dir). -- cgit v1.2.1