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.

1267 lines
51 KiB

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