選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

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