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.

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