diff options
-rw-r--r-- | src/rebar_dir.erl | 31 | ||||
-rw-r--r-- | test/rebar_dir_SUITE.erl | 43 |
2 files changed, 70 insertions, 4 deletions
diff --git a/src/rebar_dir.erl b/src/rebar_dir.erl index 09e3114..3729704 100644 --- a/src/rebar_dir.erl +++ b/src/rebar_dir.erl @@ -121,8 +121,37 @@ processing_base_dir(State, Dir) -> AbsDir = filename:absname(Dir), AbsDir =:= rebar_state:get(State, base_dir). +make_absolute_path(Path) -> + case filename:pathtype(Path) of + absolute -> + Path; + relative -> + {ok, Dir} = file:get_cwd(), + filename:join([Dir, Path]); + volumerelative -> + Volume = hd(filename:split(Path)), + {ok, Dir} = file:get_cwd(Volume), + filename:join([Dir, Path]) + end. + +make_normalized_path(Path) -> + AbsPath = make_absolute_path(Path), + Components = filename:split(AbsPath), + make_normalized_path(Components, []). + +make_normalized_path([], NormalizedPath) -> + filename:join(lists:reverse(NormalizedPath)); +make_normalized_path([H|T], NormalizedPath) -> + case H of + "." -> make_normalized_path(T, NormalizedPath); + ".." -> make_normalized_path(T, tl(NormalizedPath)); + _ -> make_normalized_path(T, [H|NormalizedPath]) + end. + make_relative_path(Source, Target) -> - do_make_relative_path(filename:split(Source), filename:split(Target)). + AbsSource = make_normalized_path(Source), + AbsTarget = make_normalized_path(Target), + do_make_relative_path(filename:split(AbsSource), filename:split(AbsTarget)). do_make_relative_path([H|T1], [H|T2]) -> do_make_relative_path(T1, T2); diff --git a/test/rebar_dir_SUITE.erl b/test/rebar_dir_SUITE.erl index 526f827..1221db7 100644 --- a/test/rebar_dir_SUITE.erl +++ b/test/rebar_dir_SUITE.erl @@ -5,7 +5,7 @@ -export([default_src_dirs/1, default_extra_src_dirs/1, default_all_src_dirs/1]). -export([src_dirs/1, extra_src_dirs/1, all_src_dirs/1]). -export([profile_src_dirs/1, profile_extra_src_dirs/1, profile_all_src_dirs/1]). --export([retarget_path/1]). +-export([retarget_path/1, alt_base_dir_abs/1, alt_base_dir_rel/1]). -include_lib("common_test/include/ct.hrl"). -include_lib("eunit/include/eunit.hrl"). @@ -15,7 +15,7 @@ all() -> [default_src_dirs, default_extra_src_dirs, default_all_src_dirs, src_dirs, extra_src_dirs, all_src_dirs, profile_src_dirs, profile_extra_src_dirs, profile_all_src_dirs, - retarget_path]. + retarget_path, alt_base_dir_abs, alt_base_dir_rel]. init_per_testcase(_, Config) -> C = rebar_test_utils:init_rebar_state(Config), @@ -124,4 +124,41 @@ retarget_path(Config) -> ?assertEqual(filename:join([BaseDir, "some_other_dir"]), rebar_dir:retarget_path(State, filename:join([rebar_dir:root_dir(State), "some_other_dir"]))), ?assertEqual("/somewhere/outside/the/project", - rebar_dir:retarget_path(State, "/somewhere/outside/the/project")).
\ No newline at end of file + rebar_dir:retarget_path(State, "/somewhere/outside/the/project")). + +alt_base_dir_abs(Config) -> + AltName = lists:flatten(io_lib:format("~p", [os:timestamp()])), + AltBaseDir = filename:join(?config(priv_dir, Config), AltName), + RebarConfig = [{base_dir, AltBaseDir}], + {ok, State} = rebar_test_utils:run_and_check(Config, RebarConfig, ["compile"], return), + + BaseDir = rebar_dir:base_dir(State), + ?assertEqual(filename:join(AltBaseDir, "default"), BaseDir), + + Name1 = ?config(app_one, Config), + Name2 = ?config(app_two, Config), + + ?assert(filelib:is_dir(filename:join([BaseDir, "lib", Name1, "ebin"]))), + ?assert(filelib:is_file(filename:join([BaseDir, "lib", Name1, "ebin", Name1++".app"]))), + ?assert(filelib:is_file(filename:join([BaseDir, "lib", Name1, "ebin", Name1++".beam"]))), + ?assert(filelib:is_dir(filename:join([BaseDir, "lib", Name2, "ebin"]))), + ?assert(filelib:is_file(filename:join([BaseDir, "lib", Name2, "ebin", Name2++".app"]))), + ?assert(filelib:is_file(filename:join([BaseDir, "lib", Name2, "ebin", Name2++".beam"]))). + +alt_base_dir_rel(Config) -> + AltName = lists:flatten(io_lib:format("~p", [os:timestamp()])), + AltBaseDir = filename:join("..", AltName), + RebarConfig = [{base_dir, AltBaseDir}], + {ok, State} = rebar_test_utils:run_and_check(Config, RebarConfig, ["compile"], return), + + BaseDir = rebar_dir:base_dir(State), + + Name1 = ?config(app_one, Config), + Name2 = ?config(app_two, Config), + + ?assert(filelib:is_dir(filename:join([BaseDir, "lib", Name1, "ebin"]))), + ?assert(filelib:is_file(filename:join([BaseDir, "lib", Name1, "ebin", Name1++".app"]))), + ?assert(filelib:is_file(filename:join([BaseDir, "lib", Name1, "ebin", Name1++".beam"]))), + ?assert(filelib:is_dir(filename:join([BaseDir, "lib", Name2, "ebin"]))), + ?assert(filelib:is_file(filename:join([BaseDir, "lib", Name2, "ebin", Name2++".app"]))), + ?assert(filelib:is_file(filename:join([BaseDir, "lib", Name2, "ebin", Name2++".beam"]))). |