From 0b21243eaed49500bcb27654d394aa4f110bb723 Mon Sep 17 00:00:00 2001 From: alisdair sullivan Date: Wed, 14 Jan 2015 13:22:54 -0800 Subject: add functions `system_tmpdir/0,1` and `reset_dir/1` (in `rebar_file_utils`) --- src/rebar_file_utils.erl | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/rebar_file_utils.erl b/src/rebar_file_utils.erl index 0fc1403..8fe8965 100644 --- a/src/rebar_file_utils.erl +++ b/src/rebar_file_utils.erl @@ -30,7 +30,10 @@ cp_r/2, mv/2, delete_each/1, - write_file_if_contents_differ/2]). + write_file_if_contents_differ/2, + system_tmpdir/0, + system_tmpdir/1, + reset_dir/1]). -include("rebar.hrl"). @@ -123,6 +126,33 @@ write_file_if_contents_differ(Filename, Bytes) -> file:write_file(Filename, ToWrite) end. +%% returns an os appropriate tmpdir given a path +-spec system_tmpdir() -> file:filename(). +-spec system_tmpdir(PathComponents) -> file:filename() when + PathComponents :: [file:name()]. + +system_tmpdir() -> system_tmpdir([]). +system_tmpdir(PathComponents) -> + Tmp = case erlang:system_info(system_architecture) of + "win32" -> + "./tmp"; + _SysArch -> + "/tmp" + end, + filename:join([Tmp|PathComponents]). + +%% recursively removes a directory and then recreates the same +%% directory but empty +-spec reset_dir(Path) -> ok | {error, Reason} when + Path :: file:name(), + Reason :: file:posix(). + +reset_dir(Path) -> + %% delete the directory if it exists + _ = ec_file:remove(Path, [recursive]), + %% recreate the directory + filelib:ensure_dir(filename:join([Path, "dummy.beam"])). + %% =================================================================== %% Internal functions %% =================================================================== -- cgit v1.1 From 00e03d70efa697bcd6cba53d62efc461035a4d82 Mon Sep 17 00:00:00 2001 From: alisdair sullivan Date: Thu, 15 Jan 2015 09:48:18 -0800 Subject: use new `rebar_file_utils` functions in `eunit` and `ct` providers --- src/rebar_prv_common_test.erl | 12 ++---------- src/rebar_prv_eunit.erl | 12 ++---------- 2 files changed, 4 insertions(+), 20 deletions(-) (limited to 'src') diff --git a/src/rebar_prv_common_test.erl b/src/rebar_prv_common_test.erl index 72cb0b0..9e17f38 100644 --- a/src/rebar_prv_common_test.erl +++ b/src/rebar_prv_common_test.erl @@ -182,19 +182,11 @@ split_ct_dirs(State, RawOpts) -> {InDirs, OutDir}. default_test_dir(State) -> - Tmp = case erlang:system_info(system_architecture) of - "win32" -> - "./tmp"; - _SysArch -> - "/tmp" - end, + Tmp = rebar_file_utils:system_tmpdir(), Root = filename:join([rebar_state:dir(State), Tmp]), Project = filename:basename(rebar_state:dir(State)), OutDir = filename:join([Root, Project ++ "_rebar3_ct"]), - %% delete the directory if it exists so tests run with clean state - _ = ec_file:remove(OutDir, [recursive]), - %% recreate the directory - ok = filelib:ensure_dir(filename:join([OutDir, "dummy.beam"])), + ok = rebar_file_utils:reset_dir(OutDir), OutDir. transform_opts(Opts) -> diff --git a/src/rebar_prv_eunit.erl b/src/rebar_prv_eunit.erl index be554f3..a434fad 100644 --- a/src/rebar_prv_eunit.erl +++ b/src/rebar_prv_eunit.erl @@ -102,19 +102,11 @@ filter_checkouts([App|Rest], Acc) -> end. default_test_dir(State) -> - Tmp = case erlang:system_info(system_architecture) of - "win32" -> - "./tmp"; - _SysArch -> - "/tmp" - end, + Tmp = rebar_file_utils:system_tmpdir(), Root = filename:join([rebar_state:dir(State), Tmp]), Project = filename:basename(rebar_state:dir(State)), OutDir = filename:join([Root, Project ++ "_rebar3_eunit"]), - %% delete the directory if it exists so tests run with clean state - _ = ec_file:remove(OutDir, [recursive]), - %% recreate the directory - ok = filelib:ensure_dir(filename:join([OutDir, "dummy.beam"])), + ok = rebar_file_utils:reset_dir(OutDir), OutDir. test_state(State, TmpDir) -> -- cgit v1.1