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

1352 行
54 KiB

10年前
10年前
10年前
10年前
10年前
10年前
10年前
  1. -module(rebar_compile_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. init_per_group/2,
  8. end_per_group/2,
  9. all/0,
  10. groups/0,
  11. build_basic_app/1, paths_basic_app/1, clean_basic_app/1,
  12. build_release_apps/1, paths_release_apps/1, clean_release_apps/1,
  13. build_checkout_apps/1, paths_checkout_apps/1,
  14. build_checkout_deps/1, paths_checkout_deps/1,
  15. build_basic_srcdirs/1, paths_basic_srcdirs/1,
  16. build_release_srcdirs/1, paths_release_srcdirs/1,
  17. build_unbalanced_srcdirs/1, paths_unbalanced_srcdirs/1,
  18. build_basic_extra_dirs/1, paths_basic_extra_dirs/1, clean_basic_extra_dirs/1,
  19. build_release_extra_dirs/1, paths_release_extra_dirs/1, clean_release_extra_dirs/1,
  20. build_unbalanced_extra_dirs/1, paths_unbalanced_extra_dirs/1,
  21. build_extra_dirs_in_project_root/1,
  22. paths_extra_dirs_in_project_root/1,
  23. clean_extra_dirs_in_project_root/1,
  24. recompile_when_hrl_changes/1,
  25. recompile_when_included_hrl_changes/1,
  26. recompile_when_opts_change/1,
  27. dont_recompile_when_opts_dont_change/1,
  28. dont_recompile_yrl_or_xrl/1,
  29. deps_in_path/1,
  30. delete_beam_if_source_deleted/1,
  31. checkout_priority/1,
  32. highest_version_of_pkg_dep/1,
  33. parse_transform_test/1,
  34. erl_first_files_test/1,
  35. mib_test/1,
  36. umbrella_mib_first_test/1,
  37. only_default_transitive_deps/1,
  38. clean_all/1,
  39. override_deps/1,
  40. profile_override_deps/1,
  41. deps_build_in_prod/1,
  42. include_file_relative_to_working_directory/1,
  43. include_file_in_src/1,
  44. always_recompile_when_erl_compiler_options_set/1]).
  45. -include_lib("common_test/include/ct.hrl").
  46. -include_lib("eunit/include/eunit.hrl").
  47. -include_lib("kernel/include/file.hrl").
  48. suite() ->
  49. [].
  50. all() ->
  51. [{group, basic_app}, {group, release_apps},
  52. {group, checkout_apps}, {group, checkout_deps},
  53. {group, basic_srcdirs}, {group, release_srcdirs}, {group, unbalanced_srcdirs},
  54. {group, basic_extras}, {group, release_extras}, {group, unbalanced_extras},
  55. {group, root_extras},
  56. recompile_when_hrl_changes, recompile_when_included_hrl_changes,
  57. recompile_when_opts_change,
  58. dont_recompile_when_opts_dont_change, dont_recompile_yrl_or_xrl,
  59. delete_beam_if_source_deleted,
  60. deps_in_path, checkout_priority, highest_version_of_pkg_dep,
  61. parse_transform_test, erl_first_files_test, mib_test,
  62. umbrella_mib_first_test, only_default_transitive_deps,
  63. clean_all, override_deps, profile_override_deps, deps_build_in_prod,
  64. include_file_relative_to_working_directory, include_file_in_src] ++
  65. case erlang:function_exported(os, unsetenv, 1) of
  66. true -> [always_recompile_when_erl_compiler_options_set];
  67. false -> []
  68. end.
  69. groups() ->
  70. [{basic_app, [], [build_basic_app, paths_basic_app, clean_basic_app]},
  71. {release_apps, [], [build_release_apps, paths_release_apps, clean_release_apps]},
  72. {checkout_apps, [], [build_checkout_apps, paths_checkout_apps]},
  73. {checkout_deps, [], [build_checkout_deps, paths_checkout_deps]},
  74. {basic_srcdirs, [], [build_basic_srcdirs, paths_basic_srcdirs]},
  75. {release_srcdirs, [], [build_release_srcdirs,
  76. paths_release_srcdirs]},
  77. {unbalanced_srcdirs, [], [build_unbalanced_srcdirs,
  78. paths_unbalanced_srcdirs]},
  79. {basic_extras, [], [build_basic_extra_dirs,
  80. paths_basic_extra_dirs,
  81. clean_basic_extra_dirs]},
  82. {release_extras, [], [build_release_extra_dirs,
  83. paths_release_extra_dirs,
  84. clean_release_extra_dirs]},
  85. {unbalanced_extras, [], [build_unbalanced_extra_dirs,
  86. paths_unbalanced_extra_dirs]},
  87. {root_extras, [], [build_extra_dirs_in_project_root,
  88. paths_extra_dirs_in_project_root,
  89. clean_extra_dirs_in_project_root]}].
  90. init_per_group(basic_app, Config) ->
  91. NewConfig = rebar_test_utils:init_rebar_state(Config, "basic_app_"),
  92. AppDir = ?config(apps, NewConfig),
  93. Name = rebar_test_utils:create_random_name("app1"),
  94. Vsn = rebar_test_utils:create_random_vsn(),
  95. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  96. [{app_names, [Name]}, {vsns, [Vsn]}|NewConfig];
  97. init_per_group(release_apps, Config) ->
  98. NewConfig = rebar_test_utils:init_rebar_state(Config, "release_apps_"),
  99. AppDir = ?config(apps, NewConfig),
  100. Name1 = rebar_test_utils:create_random_name("relapp1_"),
  101. Vsn1 = rebar_test_utils:create_random_vsn(),
  102. rebar_test_utils:create_app(filename:join([AppDir,"apps",Name1]), Name1, Vsn1, [kernel, stdlib]),
  103. Name2 = rebar_test_utils:create_random_name("relapp2_"),
  104. Vsn2 = rebar_test_utils:create_random_vsn(),
  105. rebar_test_utils:create_app(filename:join([AppDir,"apps",Name2]), Name2, Vsn2, [kernel, stdlib]),
  106. [{app_names, [Name1, Name2]}, {vsns, [Vsn1, Vsn2]}|NewConfig];
  107. init_per_group(checkout_apps, Config) ->
  108. NewConfig = rebar_test_utils:init_rebar_state(Config, "checkout_apps_"),
  109. AppDir = ?config(apps, NewConfig),
  110. CheckoutsDir = ?config(checkouts, NewConfig),
  111. Name1 = rebar_test_utils:create_random_name("checkapp1_"),
  112. Vsn1 = rebar_test_utils:create_random_vsn(),
  113. rebar_test_utils:create_app(AppDir, Name1, Vsn1, [kernel, stdlib]),
  114. Name2 = rebar_test_utils:create_random_name("checkapp2_"),
  115. Vsn2 = rebar_test_utils:create_random_vsn(),
  116. rebar_test_utils:create_app(filename:join([CheckoutsDir,Name2]), Name2, Vsn2, [kernel, stdlib]),
  117. [{app_names, [Name1, Name2]}, {vsns, [Vsn1, Vsn2]}|NewConfig];
  118. init_per_group(checkout_deps, Config) ->
  119. NewConfig = rebar_test_utils:init_rebar_state(Config, "checkout_deps_"),
  120. AppDir = ?config(apps, NewConfig),
  121. CheckoutsDir = ?config(checkouts, NewConfig),
  122. DepsDir = filename:join([AppDir, "_build", "default", "lib"]),
  123. Name1 = rebar_test_utils:create_random_name("checkapp1_"),
  124. Vsn1 = rebar_test_utils:create_random_vsn(),
  125. rebar_test_utils:create_app(AppDir, Name1, Vsn1, [kernel, stdlib]),
  126. Name2 = rebar_test_utils:create_random_name("checkapp2_"),
  127. Vsn2 = rebar_test_utils:create_random_vsn(),
  128. rebar_test_utils:create_app(filename:join([CheckoutsDir,Name2]), Name2, Vsn2, [kernel, stdlib]),
  129. rebar_test_utils:create_app(filename:join([DepsDir,Name2]), Name2, Vsn1, [kernel, stdlib]),
  130. [{app_names, [Name1, Name2]}, {vsns, [Vsn1, Vsn2]}|NewConfig];
  131. init_per_group(Group, Config) when Group == basic_srcdirs; Group == basic_extras ->
  132. NewConfig = rebar_test_utils:init_rebar_state(Config, "basic_srcdirs_"),
  133. AppDir = ?config(apps, NewConfig),
  134. Name = rebar_test_utils:create_random_name("app1_"),
  135. Vsn = rebar_test_utils:create_random_vsn(),
  136. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  137. ExtraSrc = io_lib:format("-module(~ts_extra).\n-export([ok/0]).\nok() -> ok.\n", [Name]),
  138. ok = filelib:ensure_dir(filename:join([AppDir, "extra", "dummy"])),
  139. ok = file:write_file(filename:join([AppDir, "extra", io_lib:format("~ts_extra.erl", [Name])]),
  140. ExtraSrc),
  141. [{app_names, [Name]}, {vsns, [Vsn]}|NewConfig];
  142. init_per_group(Group, Config) when Group == release_srcdirs; Group == release_extras ->
  143. NewConfig = rebar_test_utils:init_rebar_state(Config, "release_srcdirs_"),
  144. AppDir = ?config(apps, NewConfig),
  145. Name1 = rebar_test_utils:create_random_name("relapp1_"),
  146. Vsn1 = rebar_test_utils:create_random_vsn(),
  147. rebar_test_utils:create_app(filename:join([AppDir, "apps", Name1]), Name1, Vsn1, [kernel, stdlib]),
  148. Name2 = rebar_test_utils:create_random_name("relapp2_"),
  149. Vsn2 = rebar_test_utils:create_random_vsn(),
  150. rebar_test_utils:create_app(filename:join([AppDir, "apps", Name2]), Name2, Vsn2, [kernel, stdlib]),
  151. ExtraOne = io_lib:format("-module(~ts_extra).\n-export([ok/0]).\nok() -> ok.\n", [Name1]),
  152. ok = filelib:ensure_dir(filename:join([AppDir, "apps", Name1, "extra", "dummy"])),
  153. ok = file:write_file(filename:join([AppDir, "apps", Name1, "extra",
  154. io_lib:format("~ts_extra.erl", [Name1])]),
  155. ExtraOne),
  156. ExtraTwo = io_lib:format("-module(~ts_extra).\n-export([ok/0]).\nok() -> ok.\n", [Name2]),
  157. ok = filelib:ensure_dir(filename:join([AppDir, "apps", Name2, "extra", "dummy"])),
  158. ok = file:write_file(filename:join([AppDir, "apps", Name2, "extra",
  159. io_lib:format("~ts_extra.erl", [Name2])]),
  160. ExtraTwo),
  161. [{app_names, [Name1, Name2]}, {vsns, [Vsn1, Vsn2]}|NewConfig];
  162. init_per_group(Group, Config) when Group == unbalanced_srcdirs; Group == unbalanced_extras ->
  163. NewConfig = rebar_test_utils:init_rebar_state(Config, "unbalanced_srcdirs_"),
  164. AppDir = ?config(apps, NewConfig),
  165. Name1 = rebar_test_utils:create_random_name("relapp1_"),
  166. Vsn1 = rebar_test_utils:create_random_vsn(),
  167. rebar_test_utils:create_app(filename:join([AppDir, "apps", Name1]), Name1, Vsn1, [kernel, stdlib]),
  168. Name2 = rebar_test_utils:create_random_name("relapp2_"),
  169. Vsn2 = rebar_test_utils:create_random_vsn(),
  170. rebar_test_utils:create_app(filename:join([AppDir, "apps", Name2]), Name2, Vsn2, [kernel, stdlib]),
  171. ExtraOne = io_lib:format("-module(~ts_extra).\n-export([ok/0]).\nok() -> ok.\n", [Name1]),
  172. ok = filelib:ensure_dir(filename:join([AppDir, "apps", Name1, "extra", "dummy"])),
  173. ok = file:write_file(filename:join([AppDir, "apps", Name1, "extra",
  174. io_lib:format("~ts_extra.erl", [Name1])]),
  175. ExtraOne),
  176. [{app_names, [Name1, Name2]}, {vsns, [Vsn1, Vsn2]}|NewConfig];
  177. init_per_group(root_extras, Config) ->
  178. NewConfig = rebar_test_utils:init_rebar_state(Config, "root_extras_"),
  179. AppDir = ?config(apps, NewConfig),
  180. Name1 = rebar_test_utils:create_random_name("relapp1_"),
  181. Vsn1 = rebar_test_utils:create_random_vsn(),
  182. rebar_test_utils:create_app(filename:join([AppDir, "apps", Name1]), Name1, Vsn1, [kernel, stdlib]),
  183. Name2 = rebar_test_utils:create_random_name("relapp2_"),
  184. Vsn2 = rebar_test_utils:create_random_vsn(),
  185. rebar_test_utils:create_app(filename:join([AppDir, "apps", Name2]), Name2, Vsn2, [kernel, stdlib]),
  186. Extra = <<"-module(extra).\n-export([ok/0]).\nok() -> ok.\n">>,
  187. ok = filelib:ensure_dir(filename:join([AppDir, "extra", "dummy"])),
  188. ok = file:write_file(filename:join([AppDir, "extra", "extra.erl"]), Extra),
  189. [{app_names, [Name1, Name2]}, {vsns, [Vsn1, Vsn2]}|NewConfig].
  190. end_per_group(_Group, _Config) ->
  191. ok.
  192. init_per_suite(Config) ->
  193. Config.
  194. end_per_suite(_Config) ->
  195. ok.
  196. init_per_testcase(_, Config) ->
  197. case ?config(apps, Config) of
  198. undefined -> rebar_test_utils:init_rebar_state(Config);
  199. _ -> Config
  200. end.
  201. end_per_testcase(_, _Config) ->
  202. catch meck:unload().
  203. %% test cases
  204. build_basic_app(Config) ->
  205. [Name] = ?config(app_names, Config),
  206. rebar_test_utils:run_and_check(Config, [], ["compile"], {ok, [{app, Name}]}).
  207. build_release_apps(Config) ->
  208. [Name1, Name2] = ?config(app_names, Config),
  209. rebar_test_utils:run_and_check(
  210. Config, [], ["compile"],
  211. {ok, [{app, Name1}, {app, Name2}]}
  212. ).
  213. build_checkout_apps(Config) ->
  214. [Name1, Name2] = ?config(app_names, Config),
  215. rebar_test_utils:run_and_check(
  216. Config, [], ["compile"],
  217. {ok, [{app, Name1}, {checkout, Name2}]}
  218. ).
  219. build_checkout_deps(Config) ->
  220. AppDir = ?config(apps, Config),
  221. [Name1, Name2] = ?config(app_names, Config),
  222. [_, Vsn2] = ?config(vsns, Config),
  223. Deps = [{list_to_atom(Name2), Vsn2, {git, "", ""}}],
  224. {ok, RebarConfig} = file:consult(rebar_test_utils:create_config(AppDir, [{deps, Deps}])),
  225. rebar_test_utils:run_and_check(
  226. Config, RebarConfig, ["compile"],
  227. {ok, [{app, Name1}, {checkout, Name2}]}
  228. ).
  229. build_basic_srcdirs(Config) ->
  230. AppDir = ?config(apps, Config),
  231. [Name] = ?config(app_names, Config),
  232. RebarConfig = [{erl_opts, [{src_dirs, ["src", "extra"]}]}],
  233. %% check a beam corresponding to the src in the extra src_dir exists
  234. ExtraBeam = filename:join([AppDir, "_build", "default", "lib", Name, "ebin",
  235. io_lib:format("~ts_extra.beam", [Name])]),
  236. %% check the extra src_dir was copied/linked into the _build dir
  237. ExtraDir = filename:join([AppDir, "_build", "default", "lib", Name, "extra"]),
  238. rebar_test_utils:run_and_check(
  239. Config, RebarConfig, ["compile"],
  240. {ok, [{app, Name}, {file, ExtraBeam}, {dir, ExtraDir}]}
  241. ).
  242. build_release_srcdirs(Config) ->
  243. AppDir = ?config(apps, Config),
  244. [Name1, Name2] = ?config(app_names, Config),
  245. RebarConfig = [{erl_opts, [{src_dirs, ["src", "extra"]}]}],
  246. %% check a beam corresponding to the src in the extra src_dir exists
  247. Extra1Beam = filename:join([AppDir, "_build", "default", "lib", Name1, "ebin",
  248. io_lib:format("~ts_extra.beam", [Name1])]),
  249. Extra2Beam = filename:join([AppDir, "_build", "default", "lib", Name2, "ebin",
  250. io_lib:format("~ts_extra.beam", [Name2])]),
  251. %% check the extra src_dir was copied/linked into the _build dir
  252. Extra1Dir = filename:join([AppDir, "_build", "default", "lib", Name1, "extra"]),
  253. Extra2Dir = filename:join([AppDir, "_build", "default", "lib", Name2, "extra"]),
  254. rebar_test_utils:run_and_check(
  255. Config, RebarConfig, ["compile"],
  256. {ok, [{app, Name1}, {app, Name2},
  257. {file, Extra1Beam}, {file, Extra2Beam},
  258. {dir, Extra1Dir}, {dir, Extra2Dir}]}
  259. ).
  260. build_unbalanced_srcdirs(Config) ->
  261. AppDir = ?config(apps, Config),
  262. [Name1, Name2] = ?config(app_names, Config),
  263. RebarConfig = [{erl_opts, [{src_dirs, ["src", "extra"]}]}],
  264. %% check a beam corresponding to the src in the extra src_dir exists
  265. Extra1Beam = filename:join([AppDir, "_build", "default", "lib", Name1, "ebin",
  266. io_lib:format("~ts_extra.beam", [Name1])]),
  267. %% check the extra src_dir was copied/linked into the _build dir
  268. Extra1Dir = filename:join([AppDir, "_build", "default", "lib", Name1, "extra"]),
  269. rebar_test_utils:run_and_check(
  270. Config, RebarConfig, ["compile"],
  271. {ok, [{app, Name1}, {app, Name2}, {file, Extra1Beam}, {dir, Extra1Dir}]}
  272. ),
  273. %% check no extra src_dir were copied/linked into the _build dir
  274. Extra2Dir = filename:join([AppDir, "_build", "default", "lib", Name2, "extra"]),
  275. false = filelib:is_dir(Extra2Dir),
  276. %% check only expected beams are in the ebin dir
  277. {ok, Files} = rebar_utils:list_dir(filename:join([AppDir, "_build", "default", "lib", Name2, "ebin"])),
  278. lists:all(fun(Beam) -> lists:member(Beam, [Name2 ++ ".app", "not_a_real_src_" ++ Name2 ++ ".beam"]) end,
  279. Files).
  280. build_basic_extra_dirs(Config) ->
  281. AppDir = ?config(apps, Config),
  282. [Name] = ?config(app_names, Config),
  283. RebarConfig = [{erl_opts, [{extra_src_dirs, ["extra"]}]}],
  284. %% check a beam corresponding to the src in the extra src_dir exists
  285. ExtraBeam = filename:join([AppDir, "_build", "default", "lib", Name, "extra",
  286. io_lib:format("~ts_extra.beam", [Name])]),
  287. rebar_test_utils:run_and_check(
  288. Config, RebarConfig, ["compile"],
  289. {ok, [{app, Name}, {file, ExtraBeam}]}
  290. ).
  291. build_release_extra_dirs(Config) ->
  292. AppDir = ?config(apps, Config),
  293. [Name1, Name2] = ?config(app_names, Config),
  294. RebarConfig = [{erl_opts, [{extra_src_dirs, ["extra"]}]}],
  295. %% check a beam corresponding to the src in the extra src_dir exists
  296. Extra1Beam = filename:join([AppDir, "_build", "default", "lib", Name1, "extra",
  297. io_lib:format("~ts_extra.beam", [Name1])]),
  298. Extra2Beam = filename:join([AppDir, "_build", "default", "lib", Name2, "extra",
  299. io_lib:format("~ts_extra.beam", [Name2])]),
  300. rebar_test_utils:run_and_check(
  301. Config, RebarConfig, ["compile"],
  302. {ok, [{app, Name1}, {app, Name2}, {file, Extra1Beam}, {file, Extra2Beam}]}
  303. ).
  304. build_unbalanced_extra_dirs(Config) ->
  305. AppDir = ?config(apps, Config),
  306. [Name1, Name2] = ?config(app_names, Config),
  307. RebarConfig = [{erl_opts, [{extra_src_dirs, ["extra"]}]}],
  308. %% check a beam corresponding to the src in the extra src_dir exists
  309. Extra1Beam = filename:join([AppDir, "_build", "default", "lib", Name1, "extra",
  310. io_lib:format("~ts_extra.beam", [Name1])]),
  311. rebar_test_utils:run_and_check(
  312. Config, RebarConfig, ["compile"],
  313. {ok, [{app, Name1}, {app, Name2}, {file, Extra1Beam}]}
  314. ),
  315. %% check no extra src_dir were copied/linked into the _build dir
  316. false = filelib:is_dir(filename:join([AppDir, "_build", "default", "lib", Name2, "extra"])),
  317. %% check only expected beams are in the ebin dir
  318. {ok, Files} = rebar_utils:list_dir(filename:join([AppDir, "_build", "default", "lib", Name2, "ebin"])),
  319. lists:all(fun(Beam) -> lists:member(Beam, [Name2 ++ ".app", "not_a_real_src_" ++ Name2 ++ ".beam"]) end,
  320. Files).
  321. build_extra_dirs_in_project_root(Config) ->
  322. AppDir = ?config(apps, Config),
  323. [Name1, Name2] = ?config(app_names, Config),
  324. RebarConfig = [{erl_opts, [{extra_src_dirs, ["extra"]}]}],
  325. %% check a beam corresponding to the src in the extra src_dir exists
  326. ExtraBeam = filename:join([AppDir, "_build", "default", "extras", "extra", "extra.beam"]),
  327. rebar_test_utils:run_and_check(
  328. Config, RebarConfig, ["compile"],
  329. {ok, [{app, Name1}, {app, Name2}, {file, ExtraBeam}]}
  330. ).
  331. paths_basic_app(Config) ->
  332. [Name] = ?config(app_names, Config),
  333. [Vsn] = ?config(vsns, Config),
  334. {ok, State} = rebar_test_utils:run_and_check(Config, [], ["compile"], return),
  335. code:add_paths(rebar_state:code_paths(State, all_deps)),
  336. ok = application:load(list_to_atom(Name)),
  337. Loaded = application:loaded_applications(),
  338. {_, _, Vsn} = lists:keyfind(list_to_atom(Name), 1, Loaded).
  339. paths_release_apps(Config) ->
  340. [Name1, Name2] = ?config(app_names, Config),
  341. [Vsn1, Vsn2] = ?config(vsns, Config),
  342. {ok, State} = rebar_test_utils:run_and_check(Config, [], ["compile"], return),
  343. code:add_paths(rebar_state:code_paths(State, all_deps)),
  344. ok = application:load(list_to_atom(Name1)),
  345. ok = application:load(list_to_atom(Name2)),
  346. Loaded = application:loaded_applications(),
  347. {_, _, Vsn1} = lists:keyfind(list_to_atom(Name1), 1, Loaded),
  348. {_, _, Vsn2} = lists:keyfind(list_to_atom(Name2), 1, Loaded).
  349. paths_checkout_apps(Config) ->
  350. [Name1, _Name2] = ?config(app_names, Config),
  351. [Vsn1, _Vsn2] = ?config(vsns, Config),
  352. {ok, State} = rebar_test_utils:run_and_check(Config, [], ["compile"], return),
  353. code:add_paths(rebar_state:code_paths(State, all_deps)),
  354. ok = application:load(list_to_atom(Name1)),
  355. Loaded = application:loaded_applications(),
  356. {_, _, Vsn1} = lists:keyfind(list_to_atom(Name1), 1, Loaded).
  357. paths_checkout_deps(Config) ->
  358. AppDir = ?config(apps, Config),
  359. [_Name1, Name2] = ?config(app_names, Config),
  360. [_Vsn1, Vsn2] = ?config(vsns, Config),
  361. %% rebar_test_utils:init_rebar_state/1,2 uses rebar_state:new/3 which
  362. %% maybe incorrectly sets deps to [] (based on `rebar.lock`) instead of
  363. %% to the checkapps
  364. %% until that is sorted out the lock file has to be removed before
  365. %% this test will pass
  366. file:delete(filename:join([AppDir, "rebar.lock"])),
  367. {ok, RebarConfig} = file:consult(filename:join([AppDir, "rebar.config"])),
  368. {ok, State} = rebar_test_utils:run_and_check(Config, RebarConfig, ["compile"], return),
  369. code:add_paths(rebar_state:code_paths(State, all_deps)),
  370. ok = application:load(list_to_atom(Name2)),
  371. Loaded = application:loaded_applications(),
  372. {_, _, Vsn2} = lists:keyfind(list_to_atom(Name2), 1, Loaded).
  373. paths_basic_srcdirs(Config) ->
  374. AppDir = ?config(apps, Config),
  375. [Name] = ?config(app_names, Config),
  376. RebarConfig = [{erl_opts, [{src_dirs, ["src", "extra"]}]}],
  377. {ok, State} = rebar_test_utils:run_and_check(Config, RebarConfig, ["compile"], return),
  378. code:add_paths(rebar_state:code_paths(State, all_deps)),
  379. Mod = list_to_atom(lists:flatten(io_lib:format("~ts_extra", [Name]))),
  380. {module, Mod} = code:ensure_loaded(Mod),
  381. Expect = filename:join([AppDir, "_build", "default", "lib", Name, "ebin",
  382. io_lib:format("~ts_extra.beam", [Name])]),
  383. Expect = code:which(Mod).
  384. paths_release_srcdirs(Config) ->
  385. AppDir = ?config(apps, Config),
  386. [Name1, Name2] = ?config(app_names, Config),
  387. RebarConfig = [{erl_opts, [{src_dirs, ["src", "extra"]}]}],
  388. {ok, State} = rebar_test_utils:run_and_check(Config, RebarConfig, ["compile"], return),
  389. code:add_paths(rebar_state:code_paths(State, all_deps)),
  390. Mod1 = list_to_atom(lists:flatten(io_lib:format("~ts_extra", [Name1]))),
  391. {module, Mod1} = code:ensure_loaded(Mod1),
  392. Mod2 = list_to_atom(lists:flatten(io_lib:format("~ts_extra", [Name2]))),
  393. {module, Mod2} = code:ensure_loaded(Mod2),
  394. ExpectOne = filename:join([AppDir, "_build", "default", "lib", Name1, "ebin",
  395. io_lib:format("~ts_extra.beam", [Name1])]),
  396. ExpectOne = code:which(Mod1),
  397. ExpectTwo = filename:join([AppDir, "_build", "default", "lib", Name2, "ebin",
  398. io_lib:format("~ts_extra.beam", [Name2])]),
  399. ExpectTwo = code:which(Mod2).
  400. paths_unbalanced_srcdirs(Config) ->
  401. AppDir = ?config(apps, Config),
  402. [Name1, Name2] = ?config(app_names, Config),
  403. RebarConfig = [{erl_opts, [{src_dirs, ["src", "extra"]}]}],
  404. {ok, State} = rebar_test_utils:run_and_check(Config, RebarConfig, ["compile"], return),
  405. code:add_paths(rebar_state:code_paths(State, all_deps)),
  406. Mod1 = list_to_atom(lists:flatten(io_lib:format("~ts_extra", [Name1]))),
  407. {module, Mod1} = code:ensure_loaded(Mod1),
  408. Mod2 = list_to_atom(lists:flatten(io_lib:format("~ts_extra", [Name2]))),
  409. {error, nofile} = code:ensure_loaded(Mod2),
  410. ExpectOne = filename:join([AppDir, "_build", "default", "lib", Name1, "ebin",
  411. io_lib:format("~ts_extra.beam", [Name1])]),
  412. ExpectOne = code:which(Mod1).
  413. paths_basic_extra_dirs(Config) ->
  414. AppDir = ?config(apps, Config),
  415. [Name] = ?config(app_names, Config),
  416. RebarConfig = [{erl_opts, [{extra_src_dirs, ["extra"]}]}],
  417. {ok, State} = rebar_test_utils:run_and_check(Config, RebarConfig, ["compile"], return),
  418. code:add_paths(rebar_state:code_paths(State, all_deps)),
  419. Mod = list_to_atom(lists:flatten(io_lib:format("~ts_extra", [Name]))),
  420. {module, Mod} = code:ensure_loaded(Mod),
  421. Expect = filename:join([AppDir, "_build", "default", "lib", Name, "extra",
  422. io_lib:format("~ts_extra.beam", [Name])]),
  423. Expect = code:which(Mod).
  424. paths_release_extra_dirs(Config) ->
  425. AppDir = ?config(apps, Config),
  426. [Name1, Name2] = ?config(app_names, Config),
  427. RebarConfig = [{erl_opts, [{extra_src_dirs, ["extra"]}]}],
  428. {ok, State} = rebar_test_utils:run_and_check(Config, RebarConfig, ["compile"], return),
  429. code:add_paths(rebar_state:code_paths(State, all_deps)),
  430. Mod1 = list_to_atom(lists:flatten(io_lib:format("~ts_extra", [Name1]))),
  431. {module, Mod1} = code:ensure_loaded(Mod1),
  432. Mod2 = list_to_atom(lists:flatten(io_lib:format("~ts_extra", [Name2]))),
  433. {module, Mod2} = code:ensure_loaded(Mod2),
  434. ExpectOne = filename:join([AppDir, "_build", "default", "lib", Name1, "extra",
  435. io_lib:format("~ts_extra.beam", [Name1])]),
  436. ExpectOne = code:which(Mod1),
  437. ExpectTwo = filename:join([AppDir, "_build", "default", "lib", Name2, "extra",
  438. io_lib:format("~ts_extra.beam", [Name2])]),
  439. ExpectTwo = code:which(Mod2).
  440. paths_unbalanced_extra_dirs(Config) ->
  441. AppDir = ?config(apps, Config),
  442. [Name1, Name2] = ?config(app_names, Config),
  443. RebarConfig = [{erl_opts, [{extra_src_dirs, ["extra"]}]}],
  444. {ok, State} = rebar_test_utils:run_and_check(Config, RebarConfig, ["compile"], return),
  445. code:add_paths(rebar_state:code_paths(State, all_deps)),
  446. Mod1 = list_to_atom(lists:flatten(io_lib:format("~ts_extra", [Name1]))),
  447. {module, Mod1} = code:ensure_loaded(Mod1),
  448. Mod2 = list_to_atom(lists:flatten(io_lib:format("~ts_extra", [Name2]))),
  449. {error, nofile} = code:ensure_loaded(Mod2),
  450. ExpectOne = filename:join([AppDir, "_build", "default", "lib", Name1, "extra",
  451. io_lib:format("~ts_extra.beam", [Name1])]),
  452. ExpectOne = code:which(Mod1).
  453. paths_extra_dirs_in_project_root(Config) ->
  454. AppDir = ?config(apps, Config),
  455. RebarConfig = [{erl_opts, [{extra_src_dirs, ["extra"]}]}],
  456. {ok, State} = rebar_test_utils:run_and_check(Config, RebarConfig, ["compile"], return),
  457. code:add_paths(rebar_state:code_paths(State, all_deps)),
  458. {module, extra} = code:ensure_loaded(extra),
  459. Expect = filename:join([AppDir, "_build", "default", "extras", "extra", "extra.beam"]),
  460. Expect = code:which(extra).
  461. clean_basic_app(Config) ->
  462. [Name] = ?config(app_names, Config),
  463. rebar_test_utils:run_and_check(Config, [], ["clean"], {ok, [{app, Name, invalid}]}).
  464. clean_release_apps(Config) ->
  465. [Name1, Name2] = ?config(app_names, Config),
  466. rebar_test_utils:run_and_check(Config, [], ["clean"],
  467. {ok, [{app, Name1, invalid}, {app, Name2, invalid}]}).
  468. clean_basic_extra_dirs(Config) ->
  469. AppDir = ?config(apps, Config),
  470. [Name] = ?config(app_names, Config),
  471. rebar_test_utils:run_and_check(Config, [], ["clean"], {ok, [{app, Name, invalid}]}),
  472. Beam = lists:flatten(io_lib:format("~ts_extra", [Name])),
  473. false = ec_file:exists(filename:join([AppDir, "_build", "default", "lib", Name, "extras", Beam])).
  474. clean_release_extra_dirs(Config) ->
  475. AppDir = ?config(apps, Config),
  476. [Name1, Name2] = ?config(app_names, Config),
  477. rebar_test_utils:run_and_check(Config, [], ["clean"],
  478. {ok, [{app, Name1, invalid}, {app, Name2, invalid}]}),
  479. Beam1 = lists:flatten(io_lib:format("~ts_extra", [Name1])),
  480. false = ec_file:exists(filename:join([AppDir, "_build", "default", "lib", Name1, "extras", Beam1])),
  481. Beam2 = lists:flatten(io_lib:format("~ts_extra", [Name2])),
  482. false = ec_file:exists(filename:join([AppDir, "_build", "default", "lib", Name2, "extras", Beam2])).
  483. clean_extra_dirs_in_project_root(Config) ->
  484. AppDir = ?config(apps, Config),
  485. [Name1, Name2] = ?config(app_names, Config),
  486. rebar_test_utils:run_and_check(Config, [], ["clean"],
  487. {ok, [{app, Name1, invalid}, {app, Name2, invalid}]}),
  488. false = ec_file:exists(filename:join([AppDir, "_build", "default", "extras"])).
  489. recompile_when_hrl_changes(Config) ->
  490. AppDir = ?config(apps, Config),
  491. Name = rebar_test_utils:create_random_name("app1_"),
  492. Vsn = rebar_test_utils:create_random_vsn(),
  493. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  494. ExtraSrc = <<"-module(test_header_include).\n"
  495. "-export([main/0]).\n"
  496. "-include(\"test_header_include.hrl\").\n"
  497. "main() -> ?SOME_DEFINE.\n">>,
  498. ExtraHeader = <<"-define(SOME_DEFINE, true).\n">>,
  499. HeaderFile = filename:join([AppDir, "src", "test_header_include.hrl"]),
  500. ok = file:write_file(filename:join([AppDir, "src", "test_header_include.erl"]), ExtraSrc),
  501. ok = file:write_file(HeaderFile, ExtraHeader),
  502. rebar_test_utils:run_and_check(Config, [], ["compile"], {ok, [{app, Name}]}),
  503. EbinDir = filename:join([AppDir, "_build", "default", "lib", Name, "ebin"]),
  504. {ok, Files} = rebar_utils:list_dir(EbinDir),
  505. ModTime = [filelib:last_modified(filename:join([EbinDir, F]))
  506. || F <- Files, filename:extension(F) == ".beam"],
  507. timer:sleep(1000),
  508. NewExtraHeader = <<"-define(SOME_DEFINE, false).\n">>,
  509. ok = file:write_file(HeaderFile, NewExtraHeader),
  510. rebar_test_utils:run_and_check(Config, [], ["compile"], {ok, [{app, Name}]}),
  511. {ok, NewFiles} = rebar_utils:list_dir(EbinDir),
  512. NewModTime = [filelib:last_modified(filename:join([EbinDir, F]))
  513. || F <- NewFiles, filename:extension(F) == ".beam"],
  514. ?assert(ModTime =/= NewModTime).
  515. recompile_when_included_hrl_changes(Config) ->
  516. AppDir = ?config(apps, Config),
  517. Name = rebar_test_utils:create_random_name("app1_"),
  518. Vsn = rebar_test_utils:create_random_vsn(),
  519. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  520. ExtraSrc = <<"-module(test_header_include).\n"
  521. "-export([main/0]).\n"
  522. "-include(\"test_header_include.hrl\").\n"
  523. "main() -> ?SOME_DEFINE.\n">>,
  524. ExtraHeader = <<"-define(SOME_DEFINE, true).\n">>,
  525. ok = filelib:ensure_dir(filename:join([AppDir, "include", "dummy"])),
  526. HeaderFile = filename:join([AppDir, "include", "test_header_include.hrl"]),
  527. ok = file:write_file(filename:join([AppDir, "src", "test_header_include.erl"]), ExtraSrc),
  528. ok = file:write_file(HeaderFile, ExtraHeader),
  529. rebar_test_utils:run_and_check(Config, [], ["compile"], {ok, [{app, Name}]}),
  530. EbinDir = filename:join([AppDir, "_build", "default", "lib", Name, "ebin"]),
  531. {ok, Files} = rebar_utils:list_dir(EbinDir),
  532. ModTime = [filelib:last_modified(filename:join([EbinDir, F]))
  533. || F <- Files, filename:extension(F) == ".beam"],
  534. timer:sleep(1000),
  535. NewExtraHeader = <<"-define(SOME_DEFINE, false).\n">>,
  536. ok = file:write_file(HeaderFile, NewExtraHeader),
  537. rebar_test_utils:run_and_check(Config, [], ["compile"], {ok, [{app, Name}]}),
  538. {ok, NewFiles} = rebar_utils:list_dir(EbinDir),
  539. NewModTime = [filelib:last_modified(filename:join([EbinDir, F]))
  540. || F <- NewFiles, filename:extension(F) == ".beam"],
  541. ?assert(ModTime =/= NewModTime).
  542. recompile_when_opts_change(Config) ->
  543. AppDir = ?config(apps, Config),
  544. Name = rebar_test_utils:create_random_name("app1_"),
  545. Vsn = rebar_test_utils:create_random_vsn(),
  546. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  547. rebar_test_utils:run_and_check(Config, [], ["compile"], {ok, [{app, Name}]}),
  548. EbinDir = filename:join([AppDir, "_build", "default", "lib", Name, "ebin"]),
  549. {ok, Files} = rebar_utils:list_dir(EbinDir),
  550. ModTime = [filelib:last_modified(filename:join([EbinDir, F]))
  551. || F <- Files, filename:extension(F) == ".beam"],
  552. timer:sleep(1000),
  553. rebar_test_utils:create_config(AppDir, [{erl_opts, [{d, some_define}]}]),
  554. rebar_test_utils:run_and_check(Config, [], ["compile"], {ok, [{app, Name}]}),
  555. {ok, NewFiles} = rebar_utils:list_dir(EbinDir),
  556. NewModTime = [filelib:last_modified(filename:join([EbinDir, F]))
  557. || F <- NewFiles, filename:extension(F) == ".beam"],
  558. ?assert(ModTime =/= NewModTime).
  559. dont_recompile_when_opts_dont_change(Config) ->
  560. AppDir = ?config(apps, Config),
  561. Name = rebar_test_utils:create_random_name("app1_"),
  562. Vsn = rebar_test_utils:create_random_vsn(),
  563. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  564. rebar_test_utils:run_and_check(Config, [], ["compile"], {ok, [{app, Name}]}),
  565. EbinDir = filename:join([AppDir, "_build", "default", "lib", Name, "ebin"]),
  566. {ok, Files} = rebar_utils:list_dir(EbinDir),
  567. ModTime = [filelib:last_modified(filename:join([EbinDir, F]))
  568. || F <- Files, filename:extension(F) == ".beam"],
  569. timer:sleep(1000),
  570. rebar_test_utils:run_and_check(Config, [], ["compile"], {ok, [{app, Name}]}),
  571. {ok, NewFiles} = rebar_utils:list_dir(EbinDir),
  572. NewModTime = [filelib:last_modified(filename:join([EbinDir, F]))
  573. || F <- NewFiles, filename:extension(F) == ".beam"],
  574. ?assert(ModTime == NewModTime).
  575. dont_recompile_yrl_or_xrl(Config) ->
  576. AppDir = ?config(apps, Config),
  577. Name = rebar_test_utils:create_random_name("app1_"),
  578. Vsn = rebar_test_utils:create_random_vsn(),
  579. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  580. Xrl = filename:join([AppDir, "src", "not_a_real_xrl_" ++ Name ++ ".xrl"]),
  581. ok = filelib:ensure_dir(Xrl),
  582. XrlBody =
  583. "Definitions."
  584. "\n\n"
  585. "D = [0-9]"
  586. "\n\n"
  587. "Rules."
  588. "\n\n"
  589. "{D}+ :"
  590. " {token,{integer,TokenLine,list_to_integer(TokenChars)}}."
  591. "\n\n"
  592. "{D}+\\.{D}+((E|e)(\\+|\\-)?{D}+)? :"
  593. " {token,{float,TokenLine,list_to_float(TokenChars)}}."
  594. "\n\n"
  595. "Erlang code.",
  596. ok = ec_file:write(Xrl, XrlBody),
  597. XrlBeam = filename:join([AppDir, "ebin", filename:basename(Xrl, ".xrl") ++ ".beam"]),
  598. rebar_test_utils:run_and_check(Config, [], ["compile"], {ok, [{app, Name}]}),
  599. ModTime = filelib:last_modified(XrlBeam),
  600. timer:sleep(1000),
  601. rebar_test_utils:run_and_check(Config, [], ["compile"], {ok, [{app, Name}]}),
  602. NewModTime = filelib:last_modified(XrlBeam),
  603. ?assert(ModTime == NewModTime).
  604. delete_beam_if_source_deleted(Config) ->
  605. AppDir = ?config(apps, Config),
  606. Name = rebar_test_utils:create_random_name("app1_"),
  607. Vsn = rebar_test_utils:create_random_vsn(),
  608. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  609. rebar_test_utils:run_and_check(Config, [], ["compile"], {ok, [{app, Name}]}),
  610. EbinDir = filename:join([AppDir, "_build", "default", "lib", Name, "ebin"]),
  611. _SrcDir = filename:join([AppDir, "_build", "default", "lib", Name, "src"]),
  612. ?assert(filelib:is_regular(filename:join(EbinDir, "not_a_real_src_" ++ Name ++ ".beam"))),
  613. file:delete(filename:join([AppDir, "src", "not_a_real_src_" ++ Name ++ ".erl"])),
  614. rebar_test_utils:run_and_check(Config, [], ["compile"], {ok, [{app, Name}]}),
  615. ?assertNot(filelib:is_regular(filename:join(EbinDir, "not_a_real_src_" ++ Name ++ ".beam"))).
  616. deps_in_path(Config) ->
  617. AppDir = ?config(apps, Config),
  618. StartPaths = code:get_path(),
  619. Name = rebar_test_utils:create_random_name("app1_"),
  620. Vsn = rebar_test_utils:create_random_vsn(),
  621. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  622. DepName = rebar_test_utils:create_random_name("dep1_"),
  623. PkgName = rebar_test_utils:create_random_name("pkg1_"),
  624. mock_git_resource:mock([]),
  625. mock_pkg_resource:mock([
  626. {pkgdeps, [{{iolist_to_binary(PkgName), iolist_to_binary(Vsn)}, []}]}
  627. ]),
  628. RConfFile = rebar_test_utils:create_config(AppDir, [{deps, [
  629. {list_to_atom(DepName), {git, "http://site.com/user/"++DepName++".git", {tag, Vsn}}},
  630. {list_to_atom(PkgName), Vsn}
  631. ]}]),
  632. {ok, RConf} = file:consult(RConfFile),
  633. %% Make sure apps we look for are not visible
  634. %% Hope not to find src name
  635. ?assertEqual([], [Path || Path <- code:get_path(),
  636. {match, _} <- [re:run(Path, DepName)]]),
  637. %% Hope not to find pkg name in there
  638. ?assertEqual([], [Path || Path <- code:get_path(),
  639. {match, _} <- [re:run(Path, PkgName)]]),
  640. %% Build things
  641. {ok, State} = rebar_test_utils:run_and_check(
  642. Config, RConf, ["compile"],
  643. {ok, [{app, Name}, {dep, DepName}, {dep, PkgName}]}
  644. ),
  645. code:add_paths(rebar_state:code_paths(State, all_deps)),
  646. %% Find src name in there
  647. ?assertNotEqual([], [Path || Path <- code:get_path(),
  648. {match, _} <- [re:run(Path, DepName)]]),
  649. %% find pkg name in there
  650. ?assertNotEqual([], [Path || Path <- code:get_path(),
  651. {match, _} <- [re:run(Path, PkgName)]]),
  652. true = code:set_path(lists:filter(fun(P) -> ec_file:exists(P) end, StartPaths)),
  653. %% Make sure apps we look for are not visible again
  654. %% Hope not to find src name
  655. ?assertEqual([], [Path || Path <- code:get_path(),
  656. {match, _} <- [re:run(Path, DepName)]]),
  657. %% Hope not to find pkg name in there
  658. ?assertEqual([], [Path || Path <- code:get_path(),
  659. {match, _} <- [re:run(Path, PkgName)]]),
  660. %% Rebuild
  661. {ok, State1} = rebar_test_utils:run_and_check(
  662. Config, RConf, ["compile"],
  663. {ok, [{app, Name}, {dep, DepName}, {dep, PkgName}]}
  664. ),
  665. %% Find src name in there
  666. code:add_paths(rebar_state:code_paths(State1, all_deps)),
  667. ?assertNotEqual([], [Path || Path <- code:get_path(),
  668. {match, _} <- [re:run(Path, DepName)]]),
  669. %% find pkg name in there
  670. ?assertNotEqual([], [Path || Path <- code:get_path(),
  671. {match, _} <- [re:run(Path, PkgName)]]).
  672. checkout_priority(Config) ->
  673. AppDir = ?config(apps, Config),
  674. CheckoutsDir = ?config(checkouts, Config),
  675. StartPaths = code:get_path(),
  676. Name = rebar_test_utils:create_random_name("app1_"),
  677. Vsn = rebar_test_utils:create_random_vsn(),
  678. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  679. DepName = rebar_test_utils:create_random_name("dep1_"),
  680. PkgName = rebar_test_utils:create_random_name("pkg1_"),
  681. mock_git_resource:mock([]),
  682. mock_pkg_resource:mock([
  683. {pkgdeps, [{{iolist_to_binary(PkgName), iolist_to_binary(Vsn)}, []}]}
  684. ]),
  685. RConfFile = rebar_test_utils:create_config(AppDir, [{deps, [
  686. {list_to_atom(DepName), {git, "http://site.com/user/"++DepName++".git", {tag, Vsn}}},
  687. {list_to_atom(PkgName), Vsn}
  688. ]}]),
  689. {ok, RConf} = file:consult(RConfFile),
  690. %% Build with deps.
  691. rebar_test_utils:run_and_check(
  692. Config, RConf, ["compile"],
  693. {ok, [{app, Name}, {dep, DepName}, {dep, PkgName}]}
  694. ),
  695. %% Build two checkout apps similar to dependencies to be fetched,
  696. %% but on a different version
  697. Vsn2 = rebar_test_utils:create_random_vsn(),
  698. rebar_test_utils:create_app(filename:join([CheckoutsDir,DepName]), DepName, Vsn2, [kernel, stdlib]),
  699. rebar_test_utils:create_app(filename:join([CheckoutsDir,PkgName]), PkgName, Vsn2, [kernel, stdlib]),
  700. %% Rebuild and make sure the checkout apps are in path
  701. code:set_path(StartPaths),
  702. {ok, State} = rebar_test_utils:run_and_check(
  703. Config, RConf, ["compile"],
  704. {ok, [{app, Name}, {checkout, DepName}, {checkout, PkgName}]}
  705. ),
  706. code:add_paths(rebar_state:code_paths(State, all_deps)),
  707. [DepPath] = [Path || Path <- code:get_path(),
  708. {match, _} <- [re:run(Path, DepName)]],
  709. [PkgPath] = [Path || Path <- code:get_path(),
  710. {match, _} <- [re:run(Path, PkgName)]],
  711. {ok, [DepApp]} = file:consult(filename:join([DepPath, DepName ++ ".app"])),
  712. {ok, [PkgApp]} = file:consult(filename:join([PkgPath, PkgName ++ ".app"])),
  713. {application, _, DepProps} = DepApp,
  714. {application, _, PkgProps} = PkgApp,
  715. ?assertEqual(Vsn2, proplists:get_value(vsn, DepProps)),
  716. ?assertEqual(Vsn2, proplists:get_value(vsn, PkgProps)).
  717. highest_version_of_pkg_dep(Config) ->
  718. AppDir = ?config(apps, Config),
  719. Name = rebar_test_utils:create_random_name("app1_"),
  720. Vsn = rebar_test_utils:create_random_vsn(),
  721. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  722. PkgName = rebar_test_utils:create_random_name("pkg1_"),
  723. mock_git_resource:mock([]),
  724. mock_pkg_resource:mock([
  725. {pkgdeps, [{{iolist_to_binary(PkgName), <<"0.1.0">>}, []},
  726. {{iolist_to_binary(PkgName), <<"0.0.1">>}, []},
  727. {{iolist_to_binary(PkgName), <<"0.1.3">>}, []},
  728. {{iolist_to_binary(PkgName), <<"0.1.1">>}, []}]}
  729. ]),
  730. RConfFile = rebar_test_utils:create_config(AppDir, [{deps, [list_to_atom(PkgName)]}]),
  731. {ok, RConf} = file:consult(RConfFile),
  732. %% Build with deps.
  733. rebar_test_utils:run_and_check(
  734. Config, RConf, ["compile"],
  735. {ok, [{app, Name}, {dep, PkgName, <<"0.1.3">>}]}
  736. ).
  737. parse_transform_test(Config) ->
  738. AppDir = ?config(apps, Config),
  739. RebarConfig = [{erl_opts, [{parse_transform, pascal}]}],
  740. Name = rebar_test_utils:create_random_name("app1_"),
  741. Vsn = rebar_test_utils:create_random_vsn(),
  742. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  743. ExtraSrc = <<"-module(pascal). "
  744. "-export([parse_transform/2]). "
  745. "parse_transform(Forms, _Options) -> "
  746. "Forms.">>,
  747. ok = file:write_file(filename:join([AppDir, "src", "pascal.erl"]), ExtraSrc),
  748. rebar_test_utils:run_and_check(Config, RebarConfig, ["compile"], {ok, [{app, Name}]}),
  749. EbinDir = filename:join([AppDir, "_build", "default", "lib", Name, "ebin"]),
  750. true = filelib:is_file(filename:join([EbinDir, "pascal.beam"])).
  751. erl_first_files_test(Config) ->
  752. AppDir = ?config(apps, Config),
  753. RebarConfig = [{erl_opts, [{parse_transform, mark_time}]},
  754. {erl_first_files, ["src/mark_time.erl",
  755. "src/b.erl",
  756. "src/d.erl",
  757. "src/a.erl"]}],
  758. Name = rebar_test_utils:create_random_name("app1_"),
  759. Vsn = rebar_test_utils:create_random_vsn(),
  760. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  761. rebar_test_utils:write_src_file(AppDir, "a.erl"),
  762. rebar_test_utils:write_src_file(AppDir, "b.erl"),
  763. rebar_test_utils:write_src_file(AppDir, "d.erl"),
  764. rebar_test_utils:write_src_file(AppDir, "e.erl"),
  765. ExtraSrc = <<"-module(mark_time). "
  766. "-export([parse_transform/2]). "
  767. "parse_transform([Form={attribute,_,module,Mod}|Forms], Options) -> "
  768. " [Form, {attribute,1,number, os:timestamp()} | Forms];"
  769. "parse_transform([Form|Forms], Options) -> "
  770. " [Form | parse_transform(Forms, Options)].">>,
  771. ok = file:write_file(filename:join([AppDir, "src", "mark_time.erl"]), ExtraSrc),
  772. rebar_test_utils:run_and_check(Config, RebarConfig, ["compile"], {ok, [{app, Name}]}),
  773. EbinDir = filename:join([AppDir, "_build", "default", "lib", Name, "ebin"]),
  774. true = filelib:is_file(filename:join([EbinDir, "mark_time.beam"])),
  775. code:load_abs(filename:join([EbinDir, "a"])),
  776. code:load_abs(filename:join([EbinDir, "b"])),
  777. code:load_abs(filename:join([EbinDir, "d"])),
  778. code:load_abs(filename:join([EbinDir, "e"])),
  779. A = proplists:get_value(number, a:module_info(attributes)),
  780. B = proplists:get_value(number, b:module_info(attributes)),
  781. D = proplists:get_value(number, d:module_info(attributes)),
  782. E = proplists:get_value(number, e:module_info(attributes)),
  783. ?assertEqual([B,D,A,E], lists:sort([A,B,D,E])).
  784. mib_test(Config) ->
  785. AppDir = ?config(apps, Config),
  786. RebarConfig = [{mib_first_files, ["mibs/SIMPLE-MIB.mib"]}],
  787. Name = rebar_test_utils:create_random_name("app1_"),
  788. Vsn = rebar_test_utils:create_random_vsn(),
  789. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  790. MibsSrc = <<"-- SIMPLE-MIB.\n"
  791. "-- This is just a simple MIB used for testing!\n"
  792. "--\n"
  793. "SIMPLE-MIB DEFINITIONS ::= BEGIN\n"
  794. "IMPORTS\n"
  795. " MODULE-IDENTITY, enterprises\n"
  796. " FROM SNMPv2-SMI;\n"
  797. "\n"
  798. "ericsson MODULE-IDENTITY\n"
  799. " LAST-UPDATED\n"
  800. " \"201403060000Z\"\n"
  801. " ORGANIZATION\n"
  802. " \"rebar\"\n"
  803. " CONTACT-INFO\n"
  804. " \"rebar <rebar@example.com>\n"
  805. " or\n"
  806. " whoever is currently responsible for the SIMPLE\n"
  807. " enterprise MIB tree branch (enterprises.999).\"\n"
  808. " DESCRIPTION\n"
  809. " \"This very small module is made available\n"
  810. " for mib-compilation testing.\"\n"
  811. " ::= { enterprises 999 }\n"
  812. "END\n">>,
  813. ok = filelib:ensure_dir(filename:join([AppDir, "mibs", "dummy"])),
  814. ok = file:write_file(filename:join([AppDir, "mibs", "SIMPLE-MIB.mib"]), MibsSrc),
  815. rebar_test_utils:run_and_check(Config, RebarConfig, ["compile"], {ok, [{app, Name}]}),
  816. %% check a bin corresponding to the mib in the mibs dir exists in priv/mibs
  817. PrivMibsDir = filename:join([AppDir, "_build", "default", "lib", Name, "priv", "mibs"]),
  818. true = filelib:is_file(filename:join([PrivMibsDir, "SIMPLE-MIB.bin"])),
  819. %% check a hrl corresponding to the mib in the mibs dir exists in include
  820. true = filelib:is_file(filename:join([AppDir, "include", "SIMPLE-MIB.hrl"])),
  821. %% check the mibs dir was linked into the _build dir
  822. true = filelib:is_dir(filename:join([AppDir, "_build", "default", "lib", Name, "mibs"])).
  823. umbrella_mib_first_test(Config) ->
  824. AppsDir = ?config(apps, Config),
  825. Name = rebar_test_utils:create_random_name("app1_"),
  826. Vsn = rebar_test_utils:create_random_vsn(),
  827. AppDir = filename:join([AppsDir, "apps", Name]),
  828. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  829. MibsSrc = <<"-- SIMPLE-MIB.\n"
  830. "-- This is just a simple MIB used for testing!\n"
  831. "--\n"
  832. "SIMPLE-MIB DEFINITIONS ::= BEGIN\n"
  833. "IMPORTS\n"
  834. " MODULE-IDENTITY, enterprises\n"
  835. " FROM SNMPv2-SMI;\n"
  836. "\n"
  837. "ericsson MODULE-IDENTITY\n"
  838. " LAST-UPDATED\n"
  839. " \"201403060000Z\"\n"
  840. " ORGANIZATION\n"
  841. " \"rebar\"\n"
  842. " CONTACT-INFO\n"
  843. " \"rebar <rebar@example.com>\n"
  844. " or\n"
  845. " whoever is currently responsible for the SIMPLE\n"
  846. " enterprise MIB tree branch (enterprises.999).\"\n"
  847. " DESCRIPTION\n"
  848. " \"This very small module is made available\n"
  849. " for mib-compilation testing.\"\n"
  850. " ::= { enterprises 999 }\n"
  851. "END\n">>,
  852. ok = filelib:ensure_dir(filename:join([AppDir, "mibs", "dummy"])),
  853. ok = file:write_file(filename:join([AppDir, "mibs", "SIMPLE-MIB.mib"]), MibsSrc),
  854. RebarConfig = [{mib_first_files, ["mibs/SIMPLE-MIB.mib"]}],
  855. rebar_test_utils:run_and_check(Config, RebarConfig, ["compile"], {ok, [{app, Name}]}),
  856. %% check a bin corresponding to the mib in the mibs dir exists in priv/mibs
  857. PrivMibsDir = filename:join([AppsDir, "_build", "default", "lib", Name, "priv", "mibs"]),
  858. true = filelib:is_file(filename:join([PrivMibsDir, "SIMPLE-MIB.bin"])),
  859. %% check a hrl corresponding to the mib in the mibs dir exists in include
  860. true = filelib:is_file(filename:join([AppDir, "include", "SIMPLE-MIB.hrl"])),
  861. %% check the mibs dir was linked into the _build dir
  862. true = filelib:is_dir(filename:join([AppsDir, "_build", "default", "lib", Name, "mibs"])).
  863. only_default_transitive_deps(Config) ->
  864. AppDir = ?config(apps, Config),
  865. Name = rebar_test_utils:create_random_name("app1_"),
  866. Vsn = rebar_test_utils:create_random_vsn(),
  867. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  868. GitDeps = rebar_test_utils:expand_deps(git, [{"a", "1.0.0", []}]),
  869. PkgName = rebar_test_utils:create_random_name("pkg1_"),
  870. {SrcDeps, _} = rebar_test_utils:flat_deps(GitDeps),
  871. mock_git_resource:mock([{deps, SrcDeps},
  872. {config, [{profiles, [{test, [{deps, [list_to_atom(PkgName)]}]}]}]}]),
  873. mock_pkg_resource:mock([{pkgdeps, [{{iolist_to_binary(PkgName), <<"0.1.0">>}, []}]}]),
  874. Deps = rebar_test_utils:top_level_deps(GitDeps),
  875. RConfFile = rebar_test_utils:create_config(AppDir, [{deps, Deps}]),
  876. {ok, RConf} = file:consult(RConfFile),
  877. %% Build with deps.
  878. rebar_test_utils:run_and_check(
  879. Config, RConf, ["as", "test", "compile"],
  880. {ok, [{app, Name}, {dep, "a", <<"1.0.0">>}, {dep_not_exist, PkgName}]}
  881. ).
  882. clean_all(Config) ->
  883. AppDir = ?config(apps, Config),
  884. Name = rebar_test_utils:create_random_name("app1_"),
  885. Vsn = rebar_test_utils:create_random_vsn(),
  886. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  887. DepName = rebar_test_utils:create_random_name("dep1_"),
  888. PkgName = rebar_test_utils:create_random_name("pkg1_"),
  889. mock_git_resource:mock([]),
  890. mock_pkg_resource:mock([
  891. {pkgdeps, [{{iolist_to_binary(PkgName), iolist_to_binary(Vsn)}, []}]}
  892. ]),
  893. RConfFile = rebar_test_utils:create_config(AppDir, [{deps, [
  894. {list_to_atom(DepName), {git, "http://site.com/user/"++DepName++".git", {tag, Vsn}}},
  895. {list_to_atom(PkgName), Vsn}
  896. ]}]),
  897. {ok, RConf} = file:consult(RConfFile),
  898. %% Build things
  899. rebar_test_utils:run_and_check(
  900. Config, RConf, ["compile"],
  901. {ok, [{app, Name}, {app, DepName}, {app, PkgName}]}
  902. ),
  903. %% Clean all
  904. rebar_test_utils:run_and_check(Config, [], ["clean", "--all"],
  905. {ok, [{app, Name, invalid},
  906. {app, DepName, invalid},
  907. {app, PkgName, invalid}]}).
  908. override_deps(Config) ->
  909. mock_git_resource:mock([{deps, [{some_dep, "0.0.1"},{other_dep, "0.0.1"}]}]),
  910. Deps = rebar_test_utils:expand_deps(git, [{"some_dep", "0.0.1", [{"other_dep", "0.0.1", []}]}]),
  911. TopDeps = rebar_test_utils:top_level_deps(Deps),
  912. RebarConfig = [
  913. {deps, TopDeps},
  914. {overrides, [
  915. {override, some_dep, [
  916. {deps, []}
  917. ]}
  918. ]}
  919. ],
  920. rebar_test_utils:run_and_check(
  921. Config, RebarConfig, ["compile"],
  922. {ok, [{dep, "some_dep"},{dep_not_exist, "other_dep"}]}
  923. ).
  924. profile_override_deps(Config) ->
  925. mock_git_resource:mock([{deps, [{some_dep, "0.0.1"},{other_dep, "0.0.1"}]}]),
  926. Deps = rebar_test_utils:expand_deps(git, [{"some_dep", "0.0.1", [{"other_dep", "0.0.1", []}]}]),
  927. TopDeps = rebar_test_utils:top_level_deps(Deps),
  928. RebarConfig = [
  929. {deps, TopDeps},
  930. {profiles, [{a,
  931. [{overrides, [
  932. {override, some_dep, [
  933. {deps, []}
  934. ]}
  935. ]}
  936. ]}
  937. ]}],
  938. rebar_test_utils:run_and_check(
  939. Config, RebarConfig, ["as", "a", "compile"],
  940. {ok, [{dep, "some_dep"},{dep_not_exist, "other_dep"}]}
  941. ).
  942. %% verify a deps prod profile is used
  943. %% tested by checking prod hooks run and outputs to default profile dir for dep
  944. %% and prod deps are installed for dep
  945. deps_build_in_prod(Config) ->
  946. AppDir = ?config(apps, Config),
  947. Name = rebar_test_utils:create_random_name("app1_"),
  948. Vsn = rebar_test_utils:create_random_vsn(),
  949. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  950. GitDeps = rebar_test_utils:expand_deps(git, [{"asdf", "1.0.0", []}]),
  951. PkgName = rebar_test_utils:create_random_name("pkg1_"),
  952. {SrcDeps, _} = rebar_test_utils:flat_deps(GitDeps),
  953. mock_git_resource:mock([{deps, SrcDeps},
  954. {config, [{profiles, [{prod, [{pre_hooks, [{compile, "echo whatsup > randomfile"}]},
  955. {deps, [list_to_atom(PkgName)]}]}]}]}]),
  956. mock_pkg_resource:mock([{pkgdeps, [{{iolist_to_binary(PkgName), <<"0.1.0">>}, []}]}]),
  957. Deps = rebar_test_utils:top_level_deps(GitDeps),
  958. RConfFile = rebar_test_utils:create_config(AppDir, [{deps, Deps}]),
  959. {ok, RConf} = file:consult(RConfFile),
  960. %% Build with deps.
  961. rebar_test_utils:run_and_check(
  962. Config, RConf, ["compile"],
  963. {ok, [{app, Name}, {dep, "asdf", <<"1.0.0">>}, {dep, PkgName},
  964. {file, filename:join([AppDir, "_build", "default", "lib", "asdf", "randomfile"])}]}
  965. ).
  966. %% verify that the proper include path is defined
  967. %% according the erlang doc which states:
  968. %% If the filename File is absolute (possibly after variable substitution),
  969. %% the include file with that name is included. Otherwise, the specified file
  970. %% is searched for in the following directories, and in this order:
  971. %% * The current working directory
  972. %% * The directory where the module is being compiled
  973. %% * The directories given by the include option
  974. include_file_relative_to_working_directory(Config) ->
  975. AppDir = ?config(apps, Config),
  976. Name = rebar_test_utils:create_random_name("app1_"),
  977. Vsn = rebar_test_utils:create_random_vsn(),
  978. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  979. Src = <<"-module(test).\n"
  980. "\n"
  981. "-include(\"include/test.hrl\").\n"
  982. "\n"
  983. "test() -> ?TEST_MACRO.\n"
  984. "\n">>,
  985. Include = <<"-define(TEST_MACRO, test).\n">>,
  986. ok = filelib:ensure_dir(filename:join([AppDir, "src", "dummy"])),
  987. ok = file:write_file(filename:join([AppDir, "src", "test.erl"]), Src),
  988. ok = filelib:ensure_dir(filename:join([AppDir, "include", "dummy"])),
  989. ok = file:write_file(filename:join([AppDir, "include", "test.hrl"]), Include),
  990. RebarConfig = [],
  991. rebar_test_utils:run_and_check(Config, RebarConfig,
  992. ["compile"],
  993. {ok, [{app, Name}]}).
  994. include_file_in_src(Config) ->
  995. AppDir = ?config(apps, Config),
  996. Name = rebar_test_utils:create_random_name("app1_"),
  997. Vsn = rebar_test_utils:create_random_vsn(),
  998. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  999. Src = <<"-module(test).\n"
  1000. "\n"
  1001. "-include(\"test.hrl\").\n"
  1002. "\n"
  1003. "test() -> ?TEST_MACRO.\n"
  1004. "\n">>,
  1005. Include = <<"-define(TEST_MACRO, test).\n">>,
  1006. ok = filelib:ensure_dir(filename:join([AppDir, "src", "dummy"])),
  1007. ok = file:write_file(filename:join([AppDir, "src", "test.erl"]), Src),
  1008. ok = file:write_file(filename:join([AppDir, "src", "test.hrl"]), Include),
  1009. RebarConfig = [],
  1010. rebar_test_utils:run_and_check(Config, RebarConfig,
  1011. ["compile"],
  1012. {ok, [{app, Name}]}).
  1013. always_recompile_when_erl_compiler_options_set(Config) ->
  1014. %% save existing env to restore after test
  1015. ExistingEnv = os:getenv("ERL_COMPILER_OPTIONS"),
  1016. AppDir = ?config(apps, Config),
  1017. Name = rebar_test_utils:create_random_name("erl_compiler_options_"),
  1018. Vsn = rebar_test_utils:create_random_vsn(),
  1019. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  1020. true = os:unsetenv("ERL_COMPILER_OPTIONS"),
  1021. rebar_test_utils:run_and_check(Config, [], ["compile"], {ok, [{app, Name}]}),
  1022. EbinDir = filename:join([AppDir, "_build", "default", "lib", Name, "ebin"]),
  1023. {ok, Files} = rebar_utils:list_dir(EbinDir),
  1024. ModTime = [filelib:last_modified(filename:join([EbinDir, F]))
  1025. || F <- Files, filename:extension(F) == ".beam"],
  1026. timer:sleep(1000),
  1027. true = os:putenv("ERL_COMPILER_OPTIONS", "[{d, some_macro}]"),
  1028. rebar_test_utils:run_and_check(Config, [], ["compile"], {ok, [{app, Name}]}),
  1029. {ok, NewFiles} = rebar_utils:list_dir(EbinDir),
  1030. NewModTime = [filelib:last_modified(filename:join([EbinDir, F]))
  1031. || F <- NewFiles, filename:extension(F) == ".beam"],
  1032. ?assert(ModTime =/= NewModTime),
  1033. %% restore existing env
  1034. case ExistingEnv of
  1035. false -> ok;
  1036. _ -> os:putenv("ERL_COMPILER_OPTIONS", ExistingEnv)
  1037. end.