diff options
author | Eric Avdey <eiri@eiri.ca> | 2018-01-04 10:57:48 -0400 |
---|---|---|
committer | Eric Avdey <eiri@eiri.ca> | 2018-01-04 10:57:48 -0400 |
commit | b7911065ec52432ed5f04ad49b1e601ec34d6672 (patch) | |
tree | 24609be75941f8812f5c136ecf9365eafc04c377 | |
parent | 3c90cc3aa72090ad6db8675167a1af17ed5182f4 (diff) | |
download | couchdb-b7911065ec52432ed5f04ad49b1e601ec34d6672.tar.gz |
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.
-rw-r--r-- | rel/plugins/eunit_plugin.erl | 20 |
1 files changed, 20 insertions, 0 deletions
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). |