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.

335 lines
16 KiB

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