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.

326 line
13 KiB

  1. -module(rebar_src_dirs_SUITE).
  2. -export([suite/0,
  3. init_per_suite/1,
  4. end_per_suite/1,
  5. init_per_testcase/2,
  6. end_per_testcase/2,
  7. all/0,
  8. src_dirs_at_root/1,
  9. extra_src_dirs_at_root/1,
  10. src_dirs_in_erl_opts/1,
  11. extra_src_dirs_in_erl_opts/1,
  12. src_dirs_at_root_and_in_erl_opts/1,
  13. dupe_src_dirs_at_root_and_in_erl_opts/1,
  14. extra_src_dirs_at_root_and_in_erl_opts/1,
  15. build_basic_app/1,
  16. build_multi_apps/1,
  17. src_dir_takes_precedence_over_extra/1,
  18. src_dir_checkout_dep/1,
  19. app_src_info/1]).
  20. -include_lib("common_test/include/ct.hrl").
  21. -include_lib("eunit/include/eunit.hrl").
  22. suite() ->
  23. [].
  24. init_per_suite(Config) ->
  25. Config.
  26. end_per_suite(_Config) ->
  27. ok.
  28. init_per_testcase(_, Config) ->
  29. rebar_test_utils:init_rebar_state(Config).
  30. end_per_testcase(_, _Config) -> ok.
  31. all() ->
  32. [src_dirs_at_root, extra_src_dirs_at_root,
  33. src_dirs_in_erl_opts, extra_src_dirs_in_erl_opts,
  34. src_dirs_at_root_and_in_erl_opts,
  35. dupe_src_dirs_at_root_and_in_erl_opts,
  36. extra_src_dirs_at_root_and_in_erl_opts,
  37. build_basic_app, build_multi_apps, src_dir_takes_precedence_over_extra,
  38. src_dir_checkout_dep, app_src_info].
  39. src_dirs_at_root(Config) ->
  40. AppDir = ?config(apps, Config),
  41. Name = rebar_test_utils:create_random_name("app1_"),
  42. Vsn = rebar_test_utils:create_random_vsn(),
  43. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  44. RebarConfig = [{src_dirs, ["foo", "bar", "baz"]}],
  45. {ok, State} = rebar_test_utils:run_and_check(Config, RebarConfig, ["compile"], return),
  46. ["bar", "baz", "foo"] = rebar_dir:src_dirs(rebar_state:opts(State), []).
  47. extra_src_dirs_at_root(Config) ->
  48. AppDir = ?config(apps, Config),
  49. Name = rebar_test_utils:create_random_name("app1_"),
  50. Vsn = rebar_test_utils:create_random_vsn(),
  51. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  52. RebarConfig = [{extra_src_dirs, ["foo", "bar", "baz"]}],
  53. {ok, State} = rebar_test_utils:run_and_check(Config, RebarConfig, ["compile"], return),
  54. ["bar", "baz", "foo"] = rebar_dir:extra_src_dirs(rebar_state:opts(State), []).
  55. src_dirs_in_erl_opts(Config) ->
  56. AppDir = ?config(apps, Config),
  57. Name = rebar_test_utils:create_random_name("app1_"),
  58. Vsn = rebar_test_utils:create_random_vsn(),
  59. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  60. RebarConfig = [{erl_opts, [{src_dirs, ["foo", "bar", "baz"]}]}],
  61. {ok, State} = rebar_test_utils:run_and_check(Config, RebarConfig, ["compile"], return),
  62. ["bar", "baz", "foo"] = rebar_dir:src_dirs(rebar_state:opts(State), []).
  63. extra_src_dirs_in_erl_opts(Config) ->
  64. AppDir = ?config(apps, Config),
  65. Name = rebar_test_utils:create_random_name("app1_"),
  66. Vsn = rebar_test_utils:create_random_vsn(),
  67. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  68. RebarConfig = [{erl_opts, [{extra_src_dirs, ["foo", "bar", "baz"]}]}],
  69. {ok, State} = rebar_test_utils:run_and_check(Config, RebarConfig, ["compile"], return),
  70. ["bar", "baz", "foo"] = rebar_dir:extra_src_dirs(rebar_state:opts(State), []).
  71. src_dirs_at_root_and_in_erl_opts(Config) ->
  72. AppDir = ?config(apps, Config),
  73. Name = rebar_test_utils:create_random_name("src_dirs_root_erlopts_"),
  74. Vsn = rebar_test_utils:create_random_vsn(),
  75. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  76. RebarConfig = [{erl_opts, [{src_dirs, ["foo", "bar"]}]}, {src_dirs, ["baz", "qux"]}],
  77. %% move the .app.src file to one of the subdirs, out of src/
  78. filelib:ensure_dir(filename:join([AppDir, "qux", "fake"])),
  79. rebar_file_utils:mv(filename:join([AppDir, "src", Name ++ ".app.src"]),
  80. filename:join([AppDir, "qux", Name ++ ".app.src"])),
  81. {ok, State} = rebar_test_utils:run_and_check(Config, RebarConfig, ["compile"], return),
  82. ["bar", "baz", "foo", "qux"] = rebar_dir:src_dirs(rebar_state:opts(State), []),
  83. rebar_test_utils:run_and_check(Config, RebarConfig, ["compile"],
  84. {ok, [{app, Name}]}),
  85. ok.
  86. dupe_src_dirs_at_root_and_in_erl_opts(Config) ->
  87. AppDir = ?config(apps, Config),
  88. Name = rebar_test_utils:create_random_name("dupe_src_dirs_root_erlopts_"),
  89. Vsn = rebar_test_utils:create_random_vsn(),
  90. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  91. RebarConfig = [{erl_opts, [{src_dirs, ["foo", "bar"]}]}, {src_dirs, ["baz", "qux"]}],
  92. %% move the .app.src file to one of the subdirs, out of src/
  93. filelib:ensure_dir(filename:join([AppDir, "qux", "fake"])),
  94. filelib:ensure_dir(filename:join([AppDir, "foo", "fake"])),
  95. Src1 = filename:join([AppDir, "qux", Name ++ ".app.src"]),
  96. Src2 = filename:join([AppDir, "foo", Name ++ ".app.src"]),
  97. rebar_file_utils:mv(filename:join([AppDir, "src", Name ++ ".app.src"]),
  98. Src1),
  99. %% Then copy it over to create a conflict with dupes
  100. file:copy(Src1, Src2),
  101. {error, {rebar_prv_app_discovery, {multiple_app_files, [Src2, Src1]}}} =
  102. rebar_test_utils:run_and_check(Config, RebarConfig, ["compile"], return),
  103. ok.
  104. extra_src_dirs_at_root_and_in_erl_opts(Config) ->
  105. AppDir = ?config(apps, Config),
  106. Name = rebar_test_utils:create_random_name("app1_"),
  107. Vsn = rebar_test_utils:create_random_vsn(),
  108. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  109. RebarConfig = [{erl_opts, [{extra_src_dirs, ["foo", "bar"]}]}, {extra_src_dirs, ["baz", "qux"]}],
  110. {ok, State} = rebar_test_utils:run_and_check(Config, RebarConfig, ["compile"], return),
  111. ["bar", "baz", "foo", "qux"] = rebar_dir:extra_src_dirs(rebar_state:opts(State), []).
  112. build_basic_app(Config) ->
  113. AppDir = ?config(apps, Config),
  114. Name = rebar_test_utils:create_random_name("app1_"),
  115. Vsn = rebar_test_utils:create_random_vsn(),
  116. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  117. Extra = filename:join([AppDir, "extra", "extra.erl"]),
  118. ok = filelib:ensure_dir(Extra),
  119. Src = io_lib:format("-module(extra).~n-export([x/0]).~nx() -> ok.", []),
  120. ok = ec_file:write(Extra, Src),
  121. RebarConfig = [{erl_opts, [{extra_src_dirs, ["extra"]}]}],
  122. rebar_test_utils:run_and_check(Config, RebarConfig, ["compile"], {ok, [{app, Name}]}),
  123. %% check that `extra.erl` was compiled to the `extra` dir
  124. ExtraOut = filename:join([AppDir, "_build", "default", "lib", Name, "extra"]),
  125. true = filelib:is_file(filename:join([ExtraOut, "extra.beam"])),
  126. %% check that `extra.erl` is not in the `modules` key of the app
  127. {ok, App} = file:consult(filename:join([AppDir,
  128. "_build",
  129. "default",
  130. "lib",
  131. Name,
  132. "ebin",
  133. Name ++ ".app"])),
  134. [{application, _, KVs}] = App,
  135. Mods = proplists:get_value(modules, KVs),
  136. false = lists:member(extra, Mods).
  137. build_multi_apps(Config) ->
  138. AppDir = ?config(apps, Config),
  139. Name1 = rebar_test_utils:create_random_name("app1_"),
  140. Vsn1 = rebar_test_utils:create_random_vsn(),
  141. rebar_test_utils:create_app(filename:join([AppDir,"apps",Name1]), Name1, Vsn1, [kernel, stdlib]),
  142. Name2 = rebar_test_utils:create_random_name("app2_"),
  143. Vsn2 = rebar_test_utils:create_random_vsn(),
  144. rebar_test_utils:create_app(filename:join([AppDir,"apps",Name2]), Name2, Vsn2, [kernel, stdlib]),
  145. Extra1 = filename:join([AppDir, "apps", Name1, "extra", "extra1.erl"]),
  146. ok = filelib:ensure_dir(Extra1),
  147. Src1 = io_lib:format("-module(extra1).~n-export([x/0]).~nx() -> ok.", []),
  148. ok = ec_file:write(Extra1, Src1),
  149. Extra2 = filename:join([AppDir, "apps", Name2, "extra", "extra2.erl"]),
  150. ok = filelib:ensure_dir(Extra2),
  151. Src2 = io_lib:format("-module(extra2).~n-export([x/0]).~nx() -> ok.", []),
  152. ok = ec_file:write(Extra2, Src2),
  153. RebarConfig = [{erl_opts, [{extra_src_dirs, ["extra"]}]}],
  154. rebar_test_utils:run_and_check(
  155. Config, RebarConfig, ["compile"],
  156. {ok, [{app, Name1}, {app, Name2}]}
  157. ),
  158. %% check that `extraX.erl` was compiled to the `ebin` dir
  159. ExtraOut1 = filename:join([AppDir, "_build", "default", "lib", Name1, "extra"]),
  160. true = filelib:is_file(filename:join([ExtraOut1, "extra1.beam"])),
  161. ExtraOut2 = filename:join([AppDir, "_build", "default", "lib", Name2, "extra"]),
  162. true = filelib:is_file(filename:join([ExtraOut2, "extra2.beam"])),
  163. %% check that `extraX.erl` is not in the `modules` key of the app
  164. {ok, App1} = file:consult(filename:join([AppDir,
  165. "_build",
  166. "default",
  167. "lib",
  168. Name1,
  169. "ebin",
  170. Name1 ++ ".app"])),
  171. [{application, _, KVs1}] = App1,
  172. Mods1 = proplists:get_value(modules, KVs1),
  173. false = lists:member(extra1, Mods1),
  174. {ok, App2} = file:consult(filename:join([AppDir,
  175. "_build",
  176. "default",
  177. "lib",
  178. Name2,
  179. "ebin",
  180. Name2 ++ ".app"])),
  181. [{application, _, KVs2}] = App2,
  182. Mods2 = proplists:get_value(modules, KVs2),
  183. false = lists:member(extra2, Mods2).
  184. src_dir_takes_precedence_over_extra(Config) ->
  185. AppDir = ?config(apps, Config),
  186. Name = rebar_test_utils:create_random_name("app1_"),
  187. Vsn = rebar_test_utils:create_random_vsn(),
  188. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  189. Extra = filename:join([AppDir, "extra", "extra.erl"]),
  190. ok = filelib:ensure_dir(Extra),
  191. Src = io_lib:format("-module(extra).~n-export([x/0]).~nx() -> ok.", []),
  192. ok = ec_file:write(Extra, Src),
  193. RebarConfig = [{erl_opts, [{src_dirs, ["src", "extra"]}, {extra_src_dirs, ["extra"]}]}],
  194. rebar_test_utils:run_and_check(Config, RebarConfig, ["compile"], {ok, [{app, Name}]}),
  195. %% check that `extra.erl` was compiled to the `extra` dir
  196. ExtraOut = filename:join([AppDir, "_build", "default", "lib", Name, "extra"]),
  197. true = filelib:is_file(filename:join([ExtraOut, "extra.beam"])),
  198. %% check that `extra.erl` is in the `modules` key of the app
  199. {ok, App} = file:consult(filename:join([AppDir,
  200. "_build",
  201. "default",
  202. "lib",
  203. Name,
  204. "ebin",
  205. Name ++ ".app"])),
  206. [{application, _, KVs}] = App,
  207. Mods = proplists:get_value(modules, KVs),
  208. true = lists:member(extra, Mods).
  209. src_dir_checkout_dep(Config) ->
  210. AppDir = ?config(apps, Config),
  211. AppName = rebar_test_utils:create_random_name("src_dir_checkout_app"),
  212. DepName = rebar_test_utils:create_random_name("src_dir_checkout_dep"),
  213. AtomDep = list_to_atom(DepName),
  214. Vsn = rebar_test_utils:create_random_vsn(),
  215. rebar_test_utils:create_app(AppDir, AppName, Vsn, [kernel, stdlib]),
  216. RebarConfig = [{deps, [AtomDep]}],
  217. DepDir = filename:join([?config(checkouts, Config), DepName]),
  218. ct:pal("checkouts dir: ~p", [DepDir]),
  219. rebar_test_utils:create_app(DepDir, DepName, Vsn, [kernel, stdlib]),
  220. %% move the .app.src file to one of the subdirs, out of src/
  221. rebar_file_utils:mv(filename:join([DepDir, "src"]),
  222. filename:join([DepDir, "qux"])),
  223. DepRebarConfig = [{erl_opts, [{src_dirs, ["foo", "bar"]}]},
  224. {src_dirs, ["baz", "qux"]}],
  225. file:write_file(filename:join([DepDir, "rebar.config"]),
  226. io_lib:format("~p.~n~p.~n", DepRebarConfig)),
  227. rebar_test_utils:run_and_check(
  228. Config, RebarConfig, ["compile"],
  229. {ok, [{checkout, DepName}, {app, AppName}]}
  230. ),
  231. ok.
  232. app_src_info(Config) ->
  233. PrivDir = ?config(priv_dir, Config),
  234. AppName1 = rebar_test_utils:create_random_name("app_src_info"),
  235. AppDir1 = filename:join(PrivDir, AppName1),
  236. {ok, Info1} = rebar_app_info:new(AppName1, "1.0.0", AppDir1),
  237. AppSrc1 = filename:join([AppDir1, "src", AppName1 ++ ".app.src"]),
  238. ok = filelib:ensure_dir(AppSrc1),
  239. ok = file:write_file(AppSrc1, "[]."),
  240. ?assertEqual(AppSrc1, rebar_app_info:app_file_src(Info1)),
  241. AppName2 = rebar_test_utils:create_random_name("app_src_info"),
  242. AppDir2 = filename:join(PrivDir, AppName2),
  243. {ok, Info2Tmp} = rebar_app_info:new(AppName2, "1.0.0", AppDir2),
  244. Info2 = rebar_app_info:set(Info2Tmp, src_dirs, ["foo", "bar", "baz"]),
  245. AppSrc2 = filename:join([AppDir2, "bar", AppName2 ++ ".app.src"]),
  246. ok = filelib:ensure_dir(AppSrc2),
  247. ok = file:write_file(AppSrc2, "[]."),
  248. ?assertEqual(AppSrc2, rebar_app_info:app_file_src(Info2)),
  249. ok.