You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

284 line
14 KiB

7 年之前
7 年之前
7 年之前
  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. ModDir = filename:join([AppsDir, "apps", Name1, "alt", "nested"]),
  64. Mod = "-module(altmod). -export([main/0]). main() -> ok.",
  65. ec_file:mkdir_path(ModDir),
  66. ok = file:write_file(filename:join([ModDir, "altmod.erl"]), Mod),
  67. Ebin = filename:join([AppsDir, "_build", "default", "lib", Name1, "ebin", "altmod.beam"]),
  68. {ok, State} = rebar_test_utils:run_and_check(
  69. Config, RebarConfig, ["compile"],
  70. {ok, [{file, Ebin}]}
  71. ),
  72. ["alt/nested", "src"] = rebar_dir:src_dirs(rebar_state:opts(State)).
  73. src_dirs_with_opts(Config) ->
  74. RebarConfig = [{erl_opts, [{src_dirs, ["foo", "bar", "baz"]},
  75. {src_dirs, [{"foo",[{recursive,false}]}, "qux"]}]}],
  76. {ok, State} = rebar_test_utils:run_and_check(Config, RebarConfig, ["compile"], return),
  77. ["bar", "baz", "foo", "qux"] = rebar_dir:src_dirs(rebar_state:opts(State)).
  78. extra_src_dirs(Config) ->
  79. RebarConfig = [{erl_opts, [{extra_src_dirs, ["foo", "bar", "baz"]}]}],
  80. {ok, State} = rebar_test_utils:run_and_check(Config, RebarConfig, ["compile"], return),
  81. ["bar", "baz", "foo"] = rebar_dir:extra_src_dirs(rebar_state:opts(State)).
  82. all_src_dirs(Config) ->
  83. RebarConfig = [{erl_opts, [{src_dirs, ["foo", "bar"]}, {extra_src_dirs, ["baz", "qux"]}, {src_dirs, [{"foo", [{recursive,false}]}]}]}],
  84. {ok, State} = rebar_test_utils:run_and_check(Config, RebarConfig, ["compile"], return),
  85. ["bar", "baz", "foo", "qux"] = rebar_dir:all_src_dirs(rebar_state:opts(State)).
  86. src_dir_opts(Config) ->
  87. RebarConfig =
  88. [{erl_opts, [{src_dirs, [{"foo",[{recursive,true}]}, "bar"]},
  89. {extra_src_dirs, ["baz", {"foo", [{recursive,false}]}]},
  90. {src_dirs, [{"foo", [{recursive,false}]}]}]}],
  91. {ok, State} = rebar_test_utils:run_and_check(Config, RebarConfig,
  92. ["compile"], return),
  93. [{recursive,true}] = rebar_dir:src_dir_opts(rebar_state:opts(State), "foo"),
  94. [] = rebar_dir:src_dir_opts(rebar_state:opts(State), "bar"),
  95. [] = rebar_dir:src_dir_opts(rebar_state:opts(State), "nonexisting").
  96. recursive(Config) ->
  97. RebarConfig1 =
  98. [{erl_opts, [{src_dirs, ["foo", "bar"]},
  99. {extra_src_dirs, ["baz", {"foo", [{recursive,true}]}]},
  100. {src_dirs, [{"foo", [{recursive,false}]}]}]}],
  101. {ok, State1} = rebar_test_utils:run_and_check(Config, RebarConfig1,
  102. ["compile"], return),
  103. false = rebar_dir:recursive(rebar_state:opts(State1), "foo"),
  104. true = rebar_dir:recursive(rebar_state:opts(State1), "bar"),
  105. RebarConfig2 = [{erlc_compiler,[{recursive,false}]},
  106. {erl_opts,[{src_dirs,["foo",{"bar",[{recursive,true}]}]}]}],
  107. {ok, State2} = rebar_test_utils:run_and_check(Config, RebarConfig2,
  108. ["compile"], return),
  109. false = rebar_dir:recursive(rebar_state:opts(State2), "foo"),
  110. true = rebar_dir:recursive(rebar_state:opts(State2), "bar"),
  111. ok.
  112. top_src_dirs(Config) ->
  113. %% We can get the same result out of specifying src_dirs from the config root,
  114. %% not just the erl_opts
  115. RebarConfig = [{src_dirs, ["foo", "./bar", "bar", "bar/", "./bar/", "baz",
  116. "./", ".", "../", "..", "./../", "../.", ".././../"]}],
  117. {ok, State} = rebar_test_utils:run_and_check(Config, RebarConfig, ["compile"], return),
  118. [".", "..", "../..", "bar", "baz", "foo"] = rebar_dir:src_dirs(rebar_state:opts(State)).
  119. profile_src_dirs(Config) ->
  120. RebarConfig = [
  121. {erl_opts, [{src_dirs, ["foo", "bar"]}]},
  122. {profiles, [
  123. {more, [{erl_opts, [{src_dirs, ["baz", "qux"]}]}]}
  124. ]}
  125. ],
  126. {ok, State} = rebar_test_utils:run_and_check(Config, RebarConfig, ["as", "more", "compile"], return),
  127. R = lists:sort(["foo", "bar", "baz", "qux"]),
  128. R = rebar_dir:src_dirs(rebar_state:opts(State)).
  129. profile_extra_src_dirs(Config) ->
  130. RebarConfig = [
  131. {erl_opts, [{extra_src_dirs, ["foo", "bar"]}]},
  132. {profiles, [
  133. {more, [{erl_opts, [{extra_src_dirs, ["baz", "qux"]}]}]}
  134. ]}
  135. ],
  136. {ok, State} = rebar_test_utils:run_and_check(Config, RebarConfig, ["as", "more", "compile"], return),
  137. R = lists:sort(["foo", "bar", "baz", "qux"]),
  138. R = rebar_dir:extra_src_dirs(rebar_state:opts(State)).
  139. profile_all_src_dirs(Config) ->
  140. RebarConfig = [
  141. {erl_opts, [{src_dirs, ["foo"]}, {extra_src_dirs, ["bar"]}]},
  142. {profiles, [
  143. {more, [{erl_opts, [{src_dirs, ["baz"]}, {extra_src_dirs, ["qux"]}]}]}
  144. ]}
  145. ],
  146. {ok, State} = rebar_test_utils:run_and_check(Config, RebarConfig, ["as", "more", "compile"], return),
  147. R = lists:sort(["foo", "bar", "baz", "qux"]),
  148. R = rebar_dir:all_src_dirs(rebar_state:opts(State)).
  149. profile_src_dir_opts(Config) ->
  150. RebarConfig = [
  151. {erl_opts, [{src_dirs, ["foo"]},
  152. {extra_src_dirs, [{"bar",[recursive]}]}]},
  153. {profiles, [
  154. {more, [{erl_opts, [{src_dirs, [{"bar",[{recursive,false}]}]},
  155. {extra_src_dirs, ["qux"]}]}]}
  156. ]}
  157. ],
  158. {ok, State} = rebar_test_utils:run_and_check(Config, RebarConfig,
  159. ["as", "more", "compile"],
  160. return),
  161. [{recursive,false}] = rebar_dir:src_dir_opts(rebar_state:opts(State),"bar"),
  162. {ok, State1} = rebar_test_utils:run_and_check(Config, RebarConfig,
  163. ["compile"], return),
  164. [{recursive,true}] = rebar_dir:src_dir_opts(rebar_state:opts(State1),"bar").
  165. retarget_path(Config) ->
  166. {ok, State} = rebar_test_utils:run_and_check(Config, [], ["compile"], return),
  167. BaseDir = rebar_dir:base_dir(State),
  168. Name1 = ?config(app_one, Config),
  169. Name2 = ?config(app_two, Config),
  170. ?assertEqual(filename:join([BaseDir, "lib", Name1, "test"]),
  171. rebar_dir:retarget_path(State, filename:join([rebar_dir:root_dir(State), "apps", Name1, "test"]))),
  172. ?assertEqual(filename:join([BaseDir, "lib", Name2, "test"]),
  173. rebar_dir:retarget_path(State, filename:join([rebar_dir:root_dir(State), "apps", Name2, "test"]))),
  174. ?assertEqual(filename:join([BaseDir, "lib", Name1, "more_test"]),
  175. rebar_dir:retarget_path(State, filename:join([rebar_dir:root_dir(State), "apps", Name1, "more_test"]))),
  176. ?assertEqual(filename:join([BaseDir, "test"]),
  177. rebar_dir:retarget_path(State, filename:join([rebar_dir:root_dir(State), "test"]))),
  178. ?assertEqual(filename:join([BaseDir, "some_other_dir"]),
  179. rebar_dir:retarget_path(State, filename:join([rebar_dir:root_dir(State), "some_other_dir"]))),
  180. ?assertEqual("/somewhere/outside/the/project",
  181. rebar_dir:retarget_path(State, "/somewhere/outside/the/project")).
  182. alt_base_dir_abs(Config) ->
  183. AltName = lists:flatten(io_lib:format("~p", [os:timestamp()])),
  184. AltBaseDir = filename:join(?config(priv_dir, Config), AltName),
  185. RebarConfig = [{base_dir, AltBaseDir}],
  186. {ok, State} = rebar_test_utils:run_and_check(Config, RebarConfig, ["compile"], return),
  187. BaseDir = rebar_dir:base_dir(State),
  188. ?assertEqual(filename:join(AltBaseDir, "default"), BaseDir),
  189. Name1 = ?config(app_one, Config),
  190. Name2 = ?config(app_two, Config),
  191. ?assert(filelib:is_dir(filename:join([BaseDir, "lib", Name1, "ebin"]))),
  192. ?assert(filelib:is_file(filename:join([BaseDir, "lib", Name1, "ebin", Name1++".app"]))),
  193. ?assert(filelib:is_file(filename:join([BaseDir, "lib", Name1, "ebin", Name1++".beam"]))),
  194. ?assert(filelib:is_dir(filename:join([BaseDir, "lib", Name2, "ebin"]))),
  195. ?assert(filelib:is_file(filename:join([BaseDir, "lib", Name2, "ebin", Name2++".app"]))),
  196. ?assert(filelib:is_file(filename:join([BaseDir, "lib", Name2, "ebin", Name2++".beam"]))).
  197. alt_base_dir_rel(Config) ->
  198. AltName = lists:flatten(io_lib:format("~p", [os:timestamp()])),
  199. AltBaseDir = filename:join("..", AltName),
  200. RebarConfig = [{base_dir, AltBaseDir}],
  201. {ok, State} = rebar_test_utils:run_and_check(Config, RebarConfig, ["compile"], return),
  202. BaseDir = rebar_dir:base_dir(State),
  203. Name1 = ?config(app_one, Config),
  204. Name2 = ?config(app_two, Config),
  205. ?assert(filelib:is_dir(filename:join([BaseDir, "lib", Name1, "ebin"]))),
  206. ?assert(filelib:is_file(filename:join([BaseDir, "lib", Name1, "ebin", Name1++".app"]))),
  207. ?assert(filelib:is_file(filename:join([BaseDir, "lib", Name1, "ebin", Name1++".beam"]))),
  208. ?assert(filelib:is_dir(filename:join([BaseDir, "lib", Name2, "ebin"]))),
  209. ?assert(filelib:is_file(filename:join([BaseDir, "lib", Name2, "ebin", Name2++".app"]))),
  210. ?assert(filelib:is_file(filename:join([BaseDir, "lib", Name2, "ebin", Name2++".beam"]))).
  211. global_cache_dir(Config) ->
  212. RebarConfig = [{erl_opts, []}],
  213. {ok, State} = rebar_test_utils:run_and_check(Config, RebarConfig, ["compile"], return),
  214. DataDir = ?config(priv_dir, Config),
  215. Expected = filename:join([DataDir, "cache"]),
  216. ?assertEqual(Expected, rebar_dir:global_cache_dir(rebar_state:opts(State))).
  217. default_global_cache_dir(Config) ->
  218. RebarConfig = [{erl_opts, []}],
  219. {ok, State} = rebar_test_utils:run_and_check(Config, RebarConfig, ["compile"], return),
  220. Expected = filename:join([rebar_dir:home_dir(), ".cache", "rebar3"]),
  221. ?assertEqual(Expected, rebar_dir:global_cache_dir(rebar_state:opts(State))).
  222. overwrite_default_global_cache_dir(Config) ->
  223. RebarConfig = [{erl_opts, []}],
  224. {ok, State} = rebar_test_utils:run_and_check(Config, RebarConfig, ["compile"], return),
  225. Expected = ?config(priv_dir, Config),
  226. ?assertEqual(Expected, rebar_dir:global_cache_dir(rebar_state:opts(State))).