summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTristan Sloughter <tristan.sloughter@gmail.com>2015-01-19 08:31:15 -0600
committerTristan Sloughter <tristan.sloughter@gmail.com>2015-01-19 08:31:15 -0600
commit488d70d2f249835a61053469dcc6d8dd89a77f96 (patch)
treeaeaaa24d0991eb0443aef9f4dbfbc5ca610ce92c
parent5528007751fa534a320c27f46552c15b3ec3028d (diff)
parentf94108e1641d22b66e236e7c29638e5e0f2452ab (diff)
Merge pull request #101 from talentdeficit/file_utils
generalize some temporary directory handling and add it to `rebar_file_utils`
-rw-r--r--src/rebar_file_utils.erl32
-rw-r--r--src/rebar_prv_common_test.erl12
-rw-r--r--src/rebar_prv_eunit.erl12
-rw-r--r--test/rebar_file_utils_SUITE.erl86
4 files changed, 121 insertions, 21 deletions
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
%% ===================================================================
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) ->
diff --git a/test/rebar_file_utils_SUITE.erl b/test/rebar_file_utils_SUITE.erl
new file mode 100644
index 0000000..03a2d54
--- /dev/null
+++ b/test/rebar_file_utils_SUITE.erl
@@ -0,0 +1,86 @@
+-module(rebar_file_utils_SUITE).
+
+-export([all/0,
+ groups/0,
+ init_per_group/2,
+ end_per_group/2,
+ raw_tmpdir/1,
+ empty_tmpdir/1,
+ simple_tmpdir/1,
+ multi_tmpdir/1,
+ reset_nonexistent_dir/1,
+ reset_empty_dir/1,
+ reset_dir/1]).
+
+-include_lib("common_test/include/ct.hrl").
+-include_lib("eunit/include/eunit.hrl").
+-include_lib("kernel/include/file.hrl").
+
+
+all() ->
+ [{group, tmpdir},
+ {group, reset_dir}].
+
+groups() ->
+ [{tmpdir, [], [raw_tmpdir, empty_tmpdir, simple_tmpdir, multi_tmpdir]},
+ {reset_dir, [], [reset_nonexistent_dir, reset_empty_dir, reset_dir]}].
+
+init_per_group(reset_dir, Config) ->
+ TmpDir = rebar_file_utils:system_tmpdir(["rebar_file_utils_SUITE", "resetable"]),
+ [{tmpdir, TmpDir}|Config];
+init_per_group(_, Config) -> Config.
+end_per_group(_, Config) -> Config.
+
+raw_tmpdir(_Config) ->
+ case rebar_file_utils:system_tmpdir() of
+ "/tmp" -> ok;
+ "./tmp" -> ok
+ end.
+
+empty_tmpdir(_Config) ->
+ case rebar_file_utils:system_tmpdir([]) of
+ "/tmp" -> ok;
+ "./tmp" -> ok
+ end.
+
+simple_tmpdir(_Config) ->
+ case rebar_file_utils:system_tmpdir(["test"]) of
+ "/tmp/test" -> ok;
+ "./tmp/test" -> ok
+ end.
+
+multi_tmpdir(_Config) ->
+ case rebar_file_utils:system_tmpdir(["a", "b", "c"]) of
+ "/tmp/a/b/c" -> ok;
+ "./tmp/a/b/c" -> ok
+ end.
+
+reset_nonexistent_dir(Config) ->
+ TmpDir = ?config(tmpdir, Config),
+ _ = ec_file:remove(TmpDir, [recursive]),
+ ?assertNot(filelib:is_dir(TmpDir)),
+ ok = rebar_file_utils:reset_dir(TmpDir),
+ ?assert(filelib:is_dir(TmpDir)),
+ {ok, []} = file:list_dir(TmpDir).
+
+reset_empty_dir(Config) ->
+ TmpDir = ?config(tmpdir, Config),
+ _ = ec_file:remove(TmpDir, [recursive]),
+ _ = filelib:ensure_dir(filename:join([TmpDir, "dummy.beam"])),
+ ?assert(filelib:is_dir(TmpDir)),
+ ok = rebar_file_utils:reset_dir(TmpDir),
+ ?assert(filelib:is_dir(TmpDir)),
+ {ok, []} = file:list_dir(TmpDir).
+
+reset_dir(Config) ->
+ TmpDir = ?config(tmpdir, Config),
+ _ = ec_file:remove(TmpDir, [recursive]),
+ _ = filelib:ensure_dir(filename:join([TmpDir, "dummy.beam"])),
+ ?assert(filelib:is_dir(TmpDir)),
+ lists:foreach(fun(Name) -> file:write_file(filename:join([TmpDir, Name]), <<>>) end,
+ ["a", "b", "c"]),
+ lists:foreach(fun(File) -> ?assert(filelib:is_file(filename:join([TmpDir, File]))) end,
+ ["a", "b", "c"]),
+ ok = rebar_file_utils:reset_dir(TmpDir),
+ ?assert(filelib:is_dir(TmpDir)),
+ {ok, []} = file:list_dir(TmpDir).