您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

257 行
12 KiB

  1. -module(rebar_dir_SUITE).
  2. -export([all/0, init_per_testcase/2, end_per_testcase/2]).
  3. -export([default_src_dirs/1, default_extra_src_dirs/1, default_all_src_dirs/1]).
  4. -export([src_dirs/1, src_dirs_with_opts/1, extra_src_dirs/1, all_src_dirs/1]).
  5. -export([src_dir_opts/1, recursive/1]).
  6. -export([profile_src_dirs/1, profile_extra_src_dirs/1, profile_all_src_dirs/1]).
  7. -export([profile_src_dir_opts/1]).
  8. -export([retarget_path/1, alt_base_dir_abs/1, alt_base_dir_rel/1]).
  9. -export([global_cache_dir/1, default_global_cache_dir/1, overwrite_default_global_cache_dir/1]).
  10. -include_lib("common_test/include/ct.hrl").
  11. -include_lib("eunit/include/eunit.hrl").
  12. -include_lib("kernel/include/file.hrl").
  13. all() -> [default_src_dirs, default_extra_src_dirs, default_all_src_dirs,
  14. src_dirs, extra_src_dirs, all_src_dirs, src_dir_opts, recursive,
  15. profile_src_dirs, profile_extra_src_dirs, profile_all_src_dirs,
  16. profile_src_dir_opts,
  17. retarget_path, alt_base_dir_abs, alt_base_dir_rel, global_cache_dir,
  18. default_global_cache_dir, overwrite_default_global_cache_dir].
  19. init_per_testcase(default_global_cache_dir, Config) ->
  20. [{apps, AppsDir}, {checkouts, CheckoutsDir}, {state, _State} | Config] = rebar_test_utils:init_rebar_state(Config),
  21. NewState = rebar_state:new([{base_dir, filename:join([AppsDir, "_build"])}
  22. ,{root_dir, AppsDir}]),
  23. [{apps, AppsDir}, {checkouts, CheckoutsDir}, {state, NewState} | Config];
  24. init_per_testcase(overwrite_default_global_cache_dir, Config) ->
  25. os:putenv("REBAR_CACHE_DIR", ?config(priv_dir, Config)),
  26. [{apps, AppsDir}, {checkouts, CheckoutsDir}, {state, _State} | Config] = rebar_test_utils:init_rebar_state(Config),
  27. NewState = rebar_state:new([{base_dir, filename:join([AppsDir, "_build"])}
  28. ,{root_dir, AppsDir}]),
  29. [{apps, AppsDir}, {checkouts, CheckoutsDir}, {state, NewState} | Config];
  30. init_per_testcase(_, Config) ->
  31. C = rebar_test_utils:init_rebar_state(Config),
  32. AppDir = ?config(apps, C),
  33. Name1 = rebar_test_utils:create_random_name("app1_"),
  34. Vsn1 = rebar_test_utils:create_random_vsn(),
  35. rebar_test_utils:create_app(filename:join([AppDir,"apps",Name1]), Name1, Vsn1, [kernel, stdlib]),
  36. Name2 = rebar_test_utils:create_random_name("app2_"),
  37. Vsn2 = rebar_test_utils:create_random_vsn(),
  38. rebar_test_utils:create_app(filename:join([AppDir,"apps",Name2]), Name2, Vsn2, [kernel, stdlib]),
  39. [{app_one, Name1}, {app_two, Name2}] ++ C.
  40. end_per_testcase(_, _Config) -> ok.
  41. default_src_dirs(Config) ->
  42. {ok, State} = rebar_test_utils:run_and_check(Config, [], ["compile"], return),
  43. [] = rebar_dir:src_dirs(rebar_state:opts(State)),
  44. ["src"] = rebar_dir:src_dirs(rebar_state:opts(State), ["src"]).
  45. default_extra_src_dirs(Config) ->
  46. {ok, State} = rebar_test_utils:run_and_check(Config, [], ["compile"], return),
  47. [] = rebar_dir:extra_src_dirs(rebar_state:opts(State)),
  48. ["src"] = rebar_dir:extra_src_dirs(rebar_state:opts(State), ["src"]).
  49. default_all_src_dirs(Config) ->
  50. {ok, State} = rebar_test_utils:run_and_check(Config, [], ["compile"], return),
  51. [] = rebar_dir:all_src_dirs(rebar_state:opts(State)),
  52. ["src", "test"] = rebar_dir:all_src_dirs(rebar_state:opts(State), ["src"], ["test"]).
  53. src_dirs(Config) ->
  54. RebarConfig = [{erl_opts, [{src_dirs, ["foo", "./bar", "bar", "bar/", "./bar/", "baz",
  55. "./", ".", "../", "..", "./../", "../.", ".././../"]}]}],
  56. {ok, State} = rebar_test_utils:run_and_check(Config, RebarConfig, ["compile"], return),
  57. [".", "..", "../..", "bar", "baz", "foo"] = rebar_dir:src_dirs(rebar_state:opts(State)).
  58. src_dirs_with_opts(Config) ->
  59. RebarConfig = [{erl_opts, [{src_dirs, ["foo", "bar", "baz"]},
  60. {src_dirs, [{"foo",[{recursive,false}]}, "qux"]}]}],
  61. {ok, State} = rebar_test_utils:run_and_check(Config, RebarConfig, ["compile"], return),
  62. ["bar", "baz", "foo", "qux"] = rebar_dir:src_dirs(rebar_state:opts(State)).
  63. extra_src_dirs(Config) ->
  64. RebarConfig = [{erl_opts, [{extra_src_dirs, ["foo", "bar", "baz"]}]}],
  65. {ok, State} = rebar_test_utils:run_and_check(Config, RebarConfig, ["compile"], return),
  66. ["bar", "baz", "foo"] = rebar_dir:extra_src_dirs(rebar_state:opts(State)).
  67. all_src_dirs(Config) ->
  68. RebarConfig = [{erl_opts, [{src_dirs, ["foo", "bar"]}, {extra_src_dirs, ["baz", "qux"]}, {src_dirs, [{"foo", [{recursive,false}]}]}]}],
  69. {ok, State} = rebar_test_utils:run_and_check(Config, RebarConfig, ["compile"], return),
  70. ["bar", "baz", "foo", "qux"] = rebar_dir:all_src_dirs(rebar_state:opts(State)).
  71. src_dir_opts(Config) ->
  72. RebarConfig =
  73. [{erl_opts, [{src_dirs, [{"foo",[{recursive,true}]}, "bar"]},
  74. {extra_src_dirs, ["baz", {"foo", [{recursive,false}]}]},
  75. {src_dirs, [{"foo", [{recursive,false}]}]}]}],
  76. {ok, State} = rebar_test_utils:run_and_check(Config, RebarConfig,
  77. ["compile"], return),
  78. [{recursive,true}] = rebar_dir:src_dir_opts(rebar_state:opts(State), "foo"),
  79. [] = rebar_dir:src_dir_opts(rebar_state:opts(State), "bar"),
  80. [] = rebar_dir:src_dir_opts(rebar_state:opts(State), "nonexisting").
  81. recursive(Config) ->
  82. RebarConfig1 =
  83. [{erl_opts, [{src_dirs, ["foo", "bar"]},
  84. {extra_src_dirs, ["baz", {"foo", [{recursive,true}]}]},
  85. {src_dirs, [{"foo", [{recursive,false}]}]}]}],
  86. {ok, State1} = rebar_test_utils:run_and_check(Config, RebarConfig1,
  87. ["compile"], return),
  88. false = rebar_dir:recursive(rebar_state:opts(State1), "foo"),
  89. true = rebar_dir:recursive(rebar_state:opts(State1), "bar"),
  90. RebarConfig2 = [{erlc_compiler,[{recursive,false}]},
  91. {erl_opts,[{src_dirs,["foo",{"bar",[{recursive,true}]}]}]}],
  92. {ok, State2} = rebar_test_utils:run_and_check(Config, RebarConfig2,
  93. ["compile"], return),
  94. false = rebar_dir:recursive(rebar_state:opts(State2), "foo"),
  95. true = rebar_dir:recursive(rebar_state:opts(State2), "bar"),
  96. ok.
  97. profile_src_dirs(Config) ->
  98. RebarConfig = [
  99. {erl_opts, [{src_dirs, ["foo", "bar"]}]},
  100. {profiles, [
  101. {more, [{erl_opts, [{src_dirs, ["baz", "qux"]}]}]}
  102. ]}
  103. ],
  104. {ok, State} = rebar_test_utils:run_and_check(Config, RebarConfig, ["as", "more", "compile"], return),
  105. R = lists:sort(["foo", "bar", "baz", "qux"]),
  106. R = rebar_dir:src_dirs(rebar_state:opts(State)).
  107. profile_extra_src_dirs(Config) ->
  108. RebarConfig = [
  109. {erl_opts, [{extra_src_dirs, ["foo", "bar"]}]},
  110. {profiles, [
  111. {more, [{erl_opts, [{extra_src_dirs, ["baz", "qux"]}]}]}
  112. ]}
  113. ],
  114. {ok, State} = rebar_test_utils:run_and_check(Config, RebarConfig, ["as", "more", "compile"], return),
  115. R = lists:sort(["foo", "bar", "baz", "qux"]),
  116. R = rebar_dir:extra_src_dirs(rebar_state:opts(State)).
  117. profile_all_src_dirs(Config) ->
  118. RebarConfig = [
  119. {erl_opts, [{src_dirs, ["foo"]}, {extra_src_dirs, ["bar"]}]},
  120. {profiles, [
  121. {more, [{erl_opts, [{src_dirs, ["baz"]}, {extra_src_dirs, ["qux"]}]}]}
  122. ]}
  123. ],
  124. {ok, State} = rebar_test_utils:run_and_check(Config, RebarConfig, ["as", "more", "compile"], return),
  125. R = lists:sort(["foo", "bar", "baz", "qux"]),
  126. R = rebar_dir:all_src_dirs(rebar_state:opts(State)).
  127. profile_src_dir_opts(Config) ->
  128. RebarConfig = [
  129. {erl_opts, [{src_dirs, ["foo"]},
  130. {extra_src_dirs, [{"bar",[recursive]}]}]},
  131. {profiles, [
  132. {more, [{erl_opts, [{src_dirs, [{"bar",[{recursive,false}]}]},
  133. {extra_src_dirs, ["qux"]}]}]}
  134. ]}
  135. ],
  136. {ok, State} = rebar_test_utils:run_and_check(Config, RebarConfig,
  137. ["as", "more", "compile"],
  138. return),
  139. [{recursive,false}] = rebar_dir:src_dir_opts(rebar_state:opts(State),"bar"),
  140. {ok, State1} = rebar_test_utils:run_and_check(Config, RebarConfig,
  141. ["compile"], return),
  142. [{recursive,true}] = rebar_dir:src_dir_opts(rebar_state:opts(State1),"bar").
  143. retarget_path(Config) ->
  144. {ok, State} = rebar_test_utils:run_and_check(Config, [], ["compile"], return),
  145. BaseDir = rebar_dir:base_dir(State),
  146. Name1 = ?config(app_one, Config),
  147. Name2 = ?config(app_two, Config),
  148. ?assertEqual(filename:join([BaseDir, "lib", Name1, "test"]),
  149. rebar_dir:retarget_path(State, filename:join([rebar_dir:root_dir(State), "apps", Name1, "test"]))),
  150. ?assertEqual(filename:join([BaseDir, "lib", Name2, "test"]),
  151. rebar_dir:retarget_path(State, filename:join([rebar_dir:root_dir(State), "apps", Name2, "test"]))),
  152. ?assertEqual(filename:join([BaseDir, "lib", Name1, "more_test"]),
  153. rebar_dir:retarget_path(State, filename:join([rebar_dir:root_dir(State), "apps", Name1, "more_test"]))),
  154. ?assertEqual(filename:join([BaseDir, "test"]),
  155. rebar_dir:retarget_path(State, filename:join([rebar_dir:root_dir(State), "test"]))),
  156. ?assertEqual(filename:join([BaseDir, "some_other_dir"]),
  157. rebar_dir:retarget_path(State, filename:join([rebar_dir:root_dir(State), "some_other_dir"]))),
  158. ?assertEqual("/somewhere/outside/the/project",
  159. rebar_dir:retarget_path(State, "/somewhere/outside/the/project")).
  160. alt_base_dir_abs(Config) ->
  161. AltName = lists:flatten(io_lib:format("~p", [os:timestamp()])),
  162. AltBaseDir = filename:join(?config(priv_dir, Config), AltName),
  163. RebarConfig = [{base_dir, AltBaseDir}],
  164. {ok, State} = rebar_test_utils:run_and_check(Config, RebarConfig, ["compile"], return),
  165. BaseDir = rebar_dir:base_dir(State),
  166. ?assertEqual(filename:join(AltBaseDir, "default"), BaseDir),
  167. Name1 = ?config(app_one, Config),
  168. Name2 = ?config(app_two, Config),
  169. ?assert(filelib:is_dir(filename:join([BaseDir, "lib", Name1, "ebin"]))),
  170. ?assert(filelib:is_file(filename:join([BaseDir, "lib", Name1, "ebin", Name1++".app"]))),
  171. ?assert(filelib:is_file(filename:join([BaseDir, "lib", Name1, "ebin", Name1++".beam"]))),
  172. ?assert(filelib:is_dir(filename:join([BaseDir, "lib", Name2, "ebin"]))),
  173. ?assert(filelib:is_file(filename:join([BaseDir, "lib", Name2, "ebin", Name2++".app"]))),
  174. ?assert(filelib:is_file(filename:join([BaseDir, "lib", Name2, "ebin", Name2++".beam"]))).
  175. alt_base_dir_rel(Config) ->
  176. AltName = lists:flatten(io_lib:format("~p", [os:timestamp()])),
  177. AltBaseDir = filename:join("..", AltName),
  178. RebarConfig = [{base_dir, AltBaseDir}],
  179. {ok, State} = rebar_test_utils:run_and_check(Config, RebarConfig, ["compile"], return),
  180. BaseDir = rebar_dir:base_dir(State),
  181. Name1 = ?config(app_one, Config),
  182. Name2 = ?config(app_two, Config),
  183. ?assert(filelib:is_dir(filename:join([BaseDir, "lib", Name1, "ebin"]))),
  184. ?assert(filelib:is_file(filename:join([BaseDir, "lib", Name1, "ebin", Name1++".app"]))),
  185. ?assert(filelib:is_file(filename:join([BaseDir, "lib", Name1, "ebin", Name1++".beam"]))),
  186. ?assert(filelib:is_dir(filename:join([BaseDir, "lib", Name2, "ebin"]))),
  187. ?assert(filelib:is_file(filename:join([BaseDir, "lib", Name2, "ebin", Name2++".app"]))),
  188. ?assert(filelib:is_file(filename:join([BaseDir, "lib", Name2, "ebin", Name2++".beam"]))).
  189. global_cache_dir(Config) ->
  190. RebarConfig = [{erl_opts, []}],
  191. {ok, State} = rebar_test_utils:run_and_check(Config, RebarConfig, ["compile"], return),
  192. DataDir = ?config(priv_dir, Config),
  193. Expected = filename:join([DataDir, "cache"]),
  194. ?assertEqual(Expected, rebar_dir:global_cache_dir(rebar_state:opts(State))).
  195. default_global_cache_dir(Config) ->
  196. RebarConfig = [{erl_opts, []}],
  197. {ok, State} = rebar_test_utils:run_and_check(Config, RebarConfig, ["compile"], return),
  198. Expected = filename:join([rebar_dir:home_dir(), ".cache", "rebar3"]),
  199. ?assertEqual(Expected, rebar_dir:global_cache_dir(rebar_state:opts(State))).
  200. overwrite_default_global_cache_dir(Config) ->
  201. RebarConfig = [{erl_opts, []}],
  202. {ok, State} = rebar_test_utils:run_and_check(Config, RebarConfig, ["compile"], return),
  203. Expected = ?config(priv_dir, Config),
  204. ?assertEqual(Expected, rebar_dir:global_cache_dir(rebar_state:opts(State))).