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.

2136 lines
83 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
  1. -module(rebar_compile_SUITE).
  2. -compile(export_all).
  3. -include_lib("common_test/include/ct.hrl").
  4. -include_lib("eunit/include/eunit.hrl").
  5. -include_lib("kernel/include/file.hrl").
  6. suite() ->
  7. [].
  8. all() ->
  9. [{group, basic_app}, {group, release_apps},
  10. {group, checkout_apps}, {group, checkout_deps},
  11. {group, basic_srcdirs}, {group, release_srcdirs}, {group, unbalanced_srcdirs},
  12. {group, basic_extras}, {group, release_extras}, {group, unbalanced_extras},
  13. {group, root_extras},
  14. recompile_when_hrl_changes, recompile_when_included_hrl_changes,
  15. recompile_when_opts_included_hrl_changes,
  16. recompile_when_opts_change,
  17. dont_recompile_when_opts_dont_change, dont_recompile_yrl_or_xrl,
  18. delete_beam_if_source_deleted,
  19. deps_in_path, checkout_priority, highest_version_of_pkg_dep,
  20. parse_transform_test, erl_first_files_test, mib_test,
  21. umbrella_mib_first_test, only_default_transitive_deps, clean_all,
  22. profile_deps, deps_build_in_prod,
  23. override_deps, override_add_deps, override_del_deps,
  24. override_opts, override_add_opts, override_del_opts,
  25. apply_overrides_exactly_once,
  26. profile_override_deps, profile_override_add_deps, profile_override_del_deps,
  27. profile_override_opts, profile_override_add_opts, profile_override_del_opts,
  28. include_file_relative_to_working_directory, include_file_in_src,
  29. include_file_relative_to_working_directory_test, include_file_in_src_test,
  30. include_file_in_src_test_multiapp,
  31. recompile_when_parse_transform_as_opt_changes,
  32. recompile_when_parse_transform_inline_changes,
  33. regex_filter_skip, regex_filter_regression,
  34. recursive, no_recursive,
  35. always_recompile_when_erl_compiler_options_set,
  36. dont_recompile_when_erl_compiler_options_env_does_not_change,
  37. recompile_when_erl_compiler_options_env_changes].
  38. groups() ->
  39. [{basic_app, [], [build_basic_app, paths_basic_app, clean_basic_app]},
  40. {release_apps, [], [build_release_apps, paths_release_apps, clean_release_apps]},
  41. {checkout_apps, [], [build_checkout_apps, paths_checkout_apps]},
  42. {checkout_deps, [], [build_checkout_deps, paths_checkout_deps]},
  43. {basic_srcdirs, [], [build_basic_srcdirs, paths_basic_srcdirs]},
  44. {release_srcdirs, [], [build_release_srcdirs,
  45. paths_release_srcdirs]},
  46. {unbalanced_srcdirs, [], [build_unbalanced_srcdirs,
  47. paths_unbalanced_srcdirs]},
  48. {basic_extras, [], [build_basic_extra_dirs,
  49. paths_basic_extra_dirs,
  50. clean_basic_extra_dirs]},
  51. {release_extras, [], [build_release_extra_dirs,
  52. paths_release_extra_dirs,
  53. clean_release_extra_dirs]},
  54. {unbalanced_extras, [], [build_unbalanced_extra_dirs,
  55. paths_unbalanced_extra_dirs]},
  56. {root_extras, [], [build_extra_dirs_in_project_root,
  57. paths_extra_dirs_in_project_root,
  58. clean_extra_dirs_in_project_root]}].
  59. init_per_group(basic_app, Config) ->
  60. NewConfig = rebar_test_utils:init_rebar_state(Config, "basic_app_"),
  61. AppDir = ?config(apps, NewConfig),
  62. Name = rebar_test_utils:create_random_name("app1"),
  63. Vsn = rebar_test_utils:create_random_vsn(),
  64. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  65. [{app_names, [Name]}, {vsns, [Vsn]}|NewConfig];
  66. init_per_group(release_apps, Config) ->
  67. NewConfig = rebar_test_utils:init_rebar_state(Config, "release_apps_"),
  68. AppDir = ?config(apps, NewConfig),
  69. Name1 = rebar_test_utils:create_random_name("relapp1_"),
  70. Vsn1 = rebar_test_utils:create_random_vsn(),
  71. rebar_test_utils:create_app(filename:join([AppDir,"apps",Name1]), Name1, Vsn1, [kernel, stdlib]),
  72. Name2 = rebar_test_utils:create_random_name("relapp2_"),
  73. Vsn2 = rebar_test_utils:create_random_vsn(),
  74. rebar_test_utils:create_app(filename:join([AppDir,"apps",Name2]), Name2, Vsn2, [kernel, stdlib]),
  75. [{app_names, [Name1, Name2]}, {vsns, [Vsn1, Vsn2]}|NewConfig];
  76. init_per_group(checkout_apps, Config) ->
  77. NewConfig = rebar_test_utils:init_rebar_state(Config, "checkout_apps_"),
  78. AppDir = ?config(apps, NewConfig),
  79. CheckoutsDir = ?config(checkouts, NewConfig),
  80. Name1 = rebar_test_utils:create_random_name("checkapp1_"),
  81. Vsn1 = rebar_test_utils:create_random_vsn(),
  82. rebar_test_utils:create_app(AppDir, Name1, Vsn1, [kernel, stdlib]),
  83. Name2 = rebar_test_utils:create_random_name("checkapp2_"),
  84. Vsn2 = rebar_test_utils:create_random_vsn(),
  85. rebar_test_utils:create_app(filename:join([CheckoutsDir,Name2]), Name2, Vsn2, [kernel, stdlib]),
  86. [{app_names, [Name1, Name2]}, {vsns, [Vsn1, Vsn2]}|NewConfig];
  87. init_per_group(checkout_deps, Config) ->
  88. NewConfig = rebar_test_utils:init_rebar_state(Config, "checkout_deps_"),
  89. AppDir = ?config(apps, NewConfig),
  90. CheckoutsDir = ?config(checkouts, NewConfig),
  91. DepsDir = filename:join([AppDir, "_build", "default", "lib"]),
  92. Name1 = rebar_test_utils:create_random_name("checkapp1_"),
  93. Vsn1 = rebar_test_utils:create_random_vsn(),
  94. rebar_test_utils:create_app(AppDir, Name1, Vsn1, [kernel, stdlib]),
  95. Name2 = rebar_test_utils:create_random_name("checkapp2_"),
  96. Vsn2 = rebar_test_utils:create_random_vsn(),
  97. rebar_test_utils:create_app(filename:join([CheckoutsDir,Name2]), Name2, Vsn2, [kernel, stdlib]),
  98. rebar_test_utils:create_app(filename:join([DepsDir,Name2]), Name2, Vsn1, [kernel, stdlib]),
  99. [{app_names, [Name1, Name2]}, {vsns, [Vsn1, Vsn2]}|NewConfig];
  100. init_per_group(Group, Config) when Group == basic_srcdirs; Group == basic_extras ->
  101. NewConfig = rebar_test_utils:init_rebar_state(Config, "basic_srcdirs_"),
  102. AppDir = ?config(apps, NewConfig),
  103. Name = rebar_test_utils:create_random_name("app1_"),
  104. Vsn = rebar_test_utils:create_random_vsn(),
  105. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  106. ExtraSrc = io_lib:format("-module(~ts_extra).\n-export([ok/0]).\nok() -> ok.\n", [Name]),
  107. ok = filelib:ensure_dir(filename:join([AppDir, "extra", "dummy"])),
  108. ok = file:write_file(filename:join([AppDir, "extra", io_lib:format("~ts_extra.erl", [Name])]),
  109. ExtraSrc),
  110. [{app_names, [Name]}, {vsns, [Vsn]}|NewConfig];
  111. init_per_group(Group, Config) when Group == release_srcdirs; Group == release_extras ->
  112. NewConfig = rebar_test_utils:init_rebar_state(Config, "release_srcdirs_"),
  113. AppDir = ?config(apps, NewConfig),
  114. Name1 = rebar_test_utils:create_random_name("relapp1_"),
  115. Vsn1 = rebar_test_utils:create_random_vsn(),
  116. rebar_test_utils:create_app(filename:join([AppDir, "apps", Name1]), Name1, Vsn1, [kernel, stdlib]),
  117. Name2 = rebar_test_utils:create_random_name("relapp2_"),
  118. Vsn2 = rebar_test_utils:create_random_vsn(),
  119. rebar_test_utils:create_app(filename:join([AppDir, "apps", Name2]), Name2, Vsn2, [kernel, stdlib]),
  120. ExtraOne = io_lib:format("-module(~ts_extra).\n-export([ok/0]).\nok() -> ok.\n", [Name1]),
  121. ok = filelib:ensure_dir(filename:join([AppDir, "apps", Name1, "extra", "dummy"])),
  122. ok = file:write_file(filename:join([AppDir, "apps", Name1, "extra",
  123. io_lib:format("~ts_extra.erl", [Name1])]),
  124. ExtraOne),
  125. ExtraTwo = io_lib:format("-module(~ts_extra).\n-export([ok/0]).\nok() -> ok.\n", [Name2]),
  126. ok = filelib:ensure_dir(filename:join([AppDir, "apps", Name2, "extra", "dummy"])),
  127. ok = file:write_file(filename:join([AppDir, "apps", Name2, "extra",
  128. io_lib:format("~ts_extra.erl", [Name2])]),
  129. ExtraTwo),
  130. [{app_names, [Name1, Name2]}, {vsns, [Vsn1, Vsn2]}|NewConfig];
  131. init_per_group(Group, Config) when Group == unbalanced_srcdirs; Group == unbalanced_extras ->
  132. NewConfig = rebar_test_utils:init_rebar_state(Config, "unbalanced_srcdirs_"),
  133. AppDir = ?config(apps, NewConfig),
  134. Name1 = rebar_test_utils:create_random_name("relapp1_"),
  135. Vsn1 = rebar_test_utils:create_random_vsn(),
  136. rebar_test_utils:create_app(filename:join([AppDir, "apps", Name1]), Name1, Vsn1, [kernel, stdlib]),
  137. Name2 = rebar_test_utils:create_random_name("relapp2_"),
  138. Vsn2 = rebar_test_utils:create_random_vsn(),
  139. rebar_test_utils:create_app(filename:join([AppDir, "apps", Name2]), Name2, Vsn2, [kernel, stdlib]),
  140. ExtraOne = io_lib:format("-module(~ts_extra).\n-export([ok/0]).\nok() -> ok.\n", [Name1]),
  141. ok = filelib:ensure_dir(filename:join([AppDir, "apps", Name1, "extra", "dummy"])),
  142. ok = file:write_file(filename:join([AppDir, "apps", Name1, "extra",
  143. io_lib:format("~ts_extra.erl", [Name1])]),
  144. ExtraOne),
  145. [{app_names, [Name1, Name2]}, {vsns, [Vsn1, Vsn2]}|NewConfig];
  146. init_per_group(root_extras, Config) ->
  147. NewConfig = rebar_test_utils:init_rebar_state(Config, "root_extras_"),
  148. AppDir = ?config(apps, NewConfig),
  149. Name1 = rebar_test_utils:create_random_name("relapp1_"),
  150. Vsn1 = rebar_test_utils:create_random_vsn(),
  151. rebar_test_utils:create_app(filename:join([AppDir, "apps", Name1]), Name1, Vsn1, [kernel, stdlib]),
  152. Name2 = rebar_test_utils:create_random_name("relapp2_"),
  153. Vsn2 = rebar_test_utils:create_random_vsn(),
  154. rebar_test_utils:create_app(filename:join([AppDir, "apps", Name2]), Name2, Vsn2, [kernel, stdlib]),
  155. Extra = <<"-module(extra).\n-export([ok/0]).\nok() -> ok.\n">>,
  156. ok = filelib:ensure_dir(filename:join([AppDir, "extra", "dummy"])),
  157. ok = file:write_file(filename:join([AppDir, "extra", "extra.erl"]), Extra),
  158. [{app_names, [Name1, Name2]}, {vsns, [Vsn1, Vsn2]}|NewConfig].
  159. end_per_group(_Group, _Config) ->
  160. ok.
  161. init_per_suite(Config) ->
  162. Config.
  163. end_per_suite(_Config) ->
  164. ok.
  165. init_per_testcase(Test, Config) when
  166. Test == dont_recompile_when_erl_compiler_options_env_does_not_change
  167. orelse
  168. Test == recompile_when_erl_compiler_options_env_changes ->
  169. _ = code:ensure_loaded(os),
  170. UnSetEnv = erlang:function_exported(os, unsetenv, 1),
  171. _ = code:ensure_loaded(compile),
  172. EnvOpts = erlang:function_exported(compile, env_compiler_options, 0),
  173. case {UnSetEnv, EnvOpts} of
  174. {true, true} -> maybe_init_config(Config);
  175. _ -> {skip, "compile:env_compiler_options/0 unavailable"}
  176. end;
  177. init_per_testcase(always_recompile_when_erl_compiler_options_set, Config) ->
  178. _ = code:ensure_loaded(os),
  179. UnSetEnv = erlang:function_exported(os, unsetenv, 1),
  180. _ = code:ensure_loaded(compile),
  181. EnvOpts = erlang:function_exported(compile, env_compiler_options, 0),
  182. case {UnSetEnv, EnvOpts} of
  183. {true, true} -> {skip, "compile:env_compiler_options/0 available"};
  184. {true, false} -> maybe_init_config(Config);
  185. _ -> {skip, "os:unsetenv/1 unavailable"}
  186. end;
  187. init_per_testcase(_, Config) -> maybe_init_config(Config).
  188. maybe_init_config(Config) ->
  189. case ?config(apps, Config) of
  190. undefined -> rebar_test_utils:init_rebar_state(Config);
  191. _ -> Config
  192. end.
  193. end_per_testcase(_, _Config) ->
  194. catch meck:unload().
  195. %% test cases
  196. build_basic_app(Config) ->
  197. [Name] = ?config(app_names, Config),
  198. rebar_test_utils:run_and_check(Config, [], ["compile"], {ok, [{app, Name}]}).
  199. build_release_apps(Config) ->
  200. [Name1, Name2] = ?config(app_names, Config),
  201. rebar_test_utils:run_and_check(
  202. Config, [], ["compile"],
  203. {ok, [{app, Name1}, {app, Name2}]}
  204. ).
  205. build_checkout_apps(Config) ->
  206. [Name1, Name2] = ?config(app_names, Config),
  207. rebar_test_utils:run_and_check(
  208. Config, [], ["compile"],
  209. {ok, [{app, Name1}, {checkout, Name2}]}
  210. ).
  211. build_checkout_deps(Config) ->
  212. AppDir = ?config(apps, Config),
  213. [Name1, Name2] = ?config(app_names, Config),
  214. [_, Vsn2] = ?config(vsns, Config),
  215. Deps = [{list_to_atom(Name2), Vsn2, {git, "", ""}}],
  216. {ok, RebarConfig} = file:consult(rebar_test_utils:create_config(AppDir, [{deps, Deps}])),
  217. rebar_test_utils:run_and_check(
  218. Config, RebarConfig, ["compile"],
  219. {ok, [{app, Name1}, {checkout, Name2}]}
  220. ).
  221. build_basic_srcdirs(Config) ->
  222. AppDir = ?config(apps, Config),
  223. [Name] = ?config(app_names, Config),
  224. RebarConfig = [{erl_opts, [{src_dirs, ["src", "extra"]}]}],
  225. %% check a beam corresponding to the src in the extra src_dir exists
  226. ExtraBeam = filename:join([AppDir, "_build", "default", "lib", Name, "ebin",
  227. io_lib:format("~ts_extra.beam", [Name])]),
  228. %% check the extra src_dir was copied/linked into the _build dir
  229. ExtraDir = filename:join([AppDir, "_build", "default", "lib", Name, "extra"]),
  230. rebar_test_utils:run_and_check(
  231. Config, RebarConfig, ["compile"],
  232. {ok, [{app, Name}, {file, ExtraBeam}, {dir, ExtraDir}]}
  233. ).
  234. build_release_srcdirs(Config) ->
  235. AppDir = ?config(apps, Config),
  236. [Name1, Name2] = ?config(app_names, Config),
  237. RebarConfig = [{erl_opts, [{src_dirs, ["src", "extra"]}]}],
  238. %% check a beam corresponding to the src in the extra src_dir exists
  239. Extra1Beam = filename:join([AppDir, "_build", "default", "lib", Name1, "ebin",
  240. io_lib:format("~ts_extra.beam", [Name1])]),
  241. Extra2Beam = filename:join([AppDir, "_build", "default", "lib", Name2, "ebin",
  242. io_lib:format("~ts_extra.beam", [Name2])]),
  243. %% check the extra src_dir was copied/linked into the _build dir
  244. Extra1Dir = filename:join([AppDir, "_build", "default", "lib", Name1, "extra"]),
  245. Extra2Dir = filename:join([AppDir, "_build", "default", "lib", Name2, "extra"]),
  246. rebar_test_utils:run_and_check(
  247. Config, RebarConfig, ["compile"],
  248. {ok, [{app, Name1}, {app, Name2},
  249. {file, Extra1Beam}, {file, Extra2Beam},
  250. {dir, Extra1Dir}, {dir, Extra2Dir}]}
  251. ).
  252. build_unbalanced_srcdirs(Config) ->
  253. AppDir = ?config(apps, Config),
  254. [Name1, Name2] = ?config(app_names, Config),
  255. RebarConfig = [{erl_opts, [{src_dirs, ["src", "extra"]}]}],
  256. %% check a beam corresponding to the src in the extra src_dir exists
  257. Extra1Beam = filename:join([AppDir, "_build", "default", "lib", Name1, "ebin",
  258. io_lib:format("~ts_extra.beam", [Name1])]),
  259. %% check the extra src_dir was copied/linked into the _build dir
  260. Extra1Dir = filename:join([AppDir, "_build", "default", "lib", Name1, "extra"]),
  261. rebar_test_utils:run_and_check(
  262. Config, RebarConfig, ["compile"],
  263. {ok, [{app, Name1}, {app, Name2}, {file, Extra1Beam}, {dir, Extra1Dir}]}
  264. ),
  265. %% check no extra src_dir were copied/linked into the _build dir
  266. Extra2Dir = filename:join([AppDir, "_build", "default", "lib", Name2, "extra"]),
  267. false = filelib:is_dir(Extra2Dir),
  268. %% check only expected beams are in the ebin dir
  269. {ok, Files} = rebar_utils:list_dir(filename:join([AppDir, "_build", "default", "lib", Name2, "ebin"])),
  270. lists:all(fun(Beam) -> lists:member(Beam, [Name2 ++ ".app", "not_a_real_src_" ++ Name2 ++ ".beam"]) end,
  271. Files).
  272. build_basic_extra_dirs(Config) ->
  273. AppDir = ?config(apps, Config),
  274. [Name] = ?config(app_names, Config),
  275. RebarConfig = [{erl_opts, [{extra_src_dirs, ["extra"]}]}],
  276. %% check a beam corresponding to the src in the extra src_dir exists
  277. ExtraBeam = filename:join([AppDir, "_build", "default", "lib", Name, "extra",
  278. io_lib:format("~ts_extra.beam", [Name])]),
  279. rebar_test_utils:run_and_check(
  280. Config, RebarConfig, ["compile"],
  281. {ok, [{app, Name}, {file, ExtraBeam}]}
  282. ).
  283. build_release_extra_dirs(Config) ->
  284. AppDir = ?config(apps, Config),
  285. [Name1, Name2] = ?config(app_names, Config),
  286. RebarConfig = [{erl_opts, [{extra_src_dirs, ["extra"]}]}],
  287. %% check a beam corresponding to the src in the extra src_dir exists
  288. Extra1Beam = filename:join([AppDir, "_build", "default", "lib", Name1, "extra",
  289. io_lib:format("~ts_extra.beam", [Name1])]),
  290. Extra2Beam = filename:join([AppDir, "_build", "default", "lib", Name2, "extra",
  291. io_lib:format("~ts_extra.beam", [Name2])]),
  292. rebar_test_utils:run_and_check(
  293. Config, RebarConfig, ["compile"],
  294. {ok, [{app, Name1}, {app, Name2}, {file, Extra1Beam}, {file, Extra2Beam}]}
  295. ).
  296. build_unbalanced_extra_dirs(Config) ->
  297. AppDir = ?config(apps, Config),
  298. [Name1, Name2] = ?config(app_names, Config),
  299. RebarConfig = [{erl_opts, [{extra_src_dirs, ["extra"]}]}],
  300. %% check a beam corresponding to the src in the extra src_dir exists
  301. Extra1Beam = filename:join([AppDir, "_build", "default", "lib", Name1, "extra",
  302. io_lib:format("~ts_extra.beam", [Name1])]),
  303. rebar_test_utils:run_and_check(
  304. Config, RebarConfig, ["compile"],
  305. {ok, [{app, Name1}, {app, Name2}, {file, Extra1Beam}]}
  306. ),
  307. %% check no extra src_dir were copied/linked into the _build dir
  308. false = filelib:is_dir(filename:join([AppDir, "_build", "default", "lib", Name2, "extra"])),
  309. %% check only expected beams are in the ebin dir
  310. {ok, Files} = rebar_utils:list_dir(filename:join([AppDir, "_build", "default", "lib", Name2, "ebin"])),
  311. lists:all(fun(Beam) -> lists:member(Beam, [Name2 ++ ".app", "not_a_real_src_" ++ Name2 ++ ".beam"]) end,
  312. Files).
  313. build_extra_dirs_in_project_root(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. ExtraBeam = filename:join([AppDir, "_build", "default", "extras", "extra", "extra.beam"]),
  319. rebar_test_utils:run_and_check(
  320. Config, RebarConfig, ["compile"],
  321. {ok, [{app, Name1}, {app, Name2}, {file, ExtraBeam}]}
  322. ).
  323. paths_basic_app(Config) ->
  324. [Name] = ?config(app_names, Config),
  325. [Vsn] = ?config(vsns, Config),
  326. {ok, State} = rebar_test_utils:run_and_check(Config, [], ["compile"], return),
  327. code:add_paths(rebar_state:code_paths(State, all_deps)),
  328. ok = application:load(list_to_atom(Name)),
  329. Loaded = application:loaded_applications(),
  330. {_, _, Vsn} = lists:keyfind(list_to_atom(Name), 1, Loaded).
  331. paths_release_apps(Config) ->
  332. [Name1, Name2] = ?config(app_names, Config),
  333. [Vsn1, Vsn2] = ?config(vsns, Config),
  334. {ok, State} = rebar_test_utils:run_and_check(Config, [], ["compile"], return),
  335. code:add_paths(rebar_state:code_paths(State, all_deps)),
  336. ok = application:load(list_to_atom(Name1)),
  337. ok = application:load(list_to_atom(Name2)),
  338. Loaded = application:loaded_applications(),
  339. {_, _, Vsn1} = lists:keyfind(list_to_atom(Name1), 1, Loaded),
  340. {_, _, Vsn2} = lists:keyfind(list_to_atom(Name2), 1, Loaded).
  341. paths_checkout_apps(Config) ->
  342. [Name1, _Name2] = ?config(app_names, Config),
  343. [Vsn1, _Vsn2] = ?config(vsns, Config),
  344. {ok, State} = rebar_test_utils:run_and_check(Config, [], ["compile"], return),
  345. code:add_paths(rebar_state:code_paths(State, all_deps)),
  346. ok = application:load(list_to_atom(Name1)),
  347. Loaded = application:loaded_applications(),
  348. {_, _, Vsn1} = lists:keyfind(list_to_atom(Name1), 1, Loaded).
  349. paths_checkout_deps(Config) ->
  350. AppDir = ?config(apps, Config),
  351. [_Name1, Name2] = ?config(app_names, Config),
  352. [_Vsn1, Vsn2] = ?config(vsns, Config),
  353. %% rebar_test_utils:init_rebar_state/1,2 uses rebar_state:new/3 which
  354. %% maybe incorrectly sets deps to [] (based on `rebar.lock`) instead of
  355. %% to the checkapps
  356. %% until that is sorted out the lock file has to be removed before
  357. %% this test will pass
  358. file:delete(filename:join([AppDir, "rebar.lock"])),
  359. {ok, RebarConfig} = file:consult(filename:join([AppDir, "rebar.config"])),
  360. {ok, State} = rebar_test_utils:run_and_check(Config, RebarConfig, ["compile"], return),
  361. code:add_paths(rebar_state:code_paths(State, all_deps)),
  362. ok = application:load(list_to_atom(Name2)),
  363. Loaded = application:loaded_applications(),
  364. {_, _, Vsn2} = lists:keyfind(list_to_atom(Name2), 1, Loaded).
  365. paths_basic_srcdirs(Config) ->
  366. AppDir = ?config(apps, Config),
  367. [Name] = ?config(app_names, Config),
  368. RebarConfig = [{erl_opts, [{src_dirs, ["src", "extra"]}]}],
  369. {ok, State} = rebar_test_utils:run_and_check(Config, RebarConfig, ["compile"], return),
  370. code:add_paths(rebar_state:code_paths(State, all_deps)),
  371. Mod = list_to_atom(lists:flatten(io_lib:format("~ts_extra", [Name]))),
  372. {module, Mod} = code:ensure_loaded(Mod),
  373. Expect = filename:join([AppDir, "_build", "default", "lib", Name, "ebin",
  374. io_lib:format("~ts_extra.beam", [Name])]),
  375. Expect = code:which(Mod).
  376. paths_release_srcdirs(Config) ->
  377. AppDir = ?config(apps, Config),
  378. [Name1, Name2] = ?config(app_names, Config),
  379. RebarConfig = [{erl_opts, [{src_dirs, ["src", "extra"]}]}],
  380. {ok, State} = rebar_test_utils:run_and_check(Config, RebarConfig, ["compile"], return),
  381. code:add_paths(rebar_state:code_paths(State, all_deps)),
  382. Mod1 = list_to_atom(lists:flatten(io_lib:format("~ts_extra", [Name1]))),
  383. {module, Mod1} = code:ensure_loaded(Mod1),
  384. Mod2 = list_to_atom(lists:flatten(io_lib:format("~ts_extra", [Name2]))),
  385. {module, Mod2} = code:ensure_loaded(Mod2),
  386. ExpectOne = filename:join([AppDir, "_build", "default", "lib", Name1, "ebin",
  387. io_lib:format("~ts_extra.beam", [Name1])]),
  388. ExpectOne = code:which(Mod1),
  389. ExpectTwo = filename:join([AppDir, "_build", "default", "lib", Name2, "ebin",
  390. io_lib:format("~ts_extra.beam", [Name2])]),
  391. ExpectTwo = code:which(Mod2).
  392. paths_unbalanced_srcdirs(Config) ->
  393. AppDir = ?config(apps, Config),
  394. [Name1, Name2] = ?config(app_names, Config),
  395. RebarConfig = [{erl_opts, [{src_dirs, ["src", "extra"]}]}],
  396. {ok, State} = rebar_test_utils:run_and_check(Config, RebarConfig, ["compile"], return),
  397. code:add_paths(rebar_state:code_paths(State, all_deps)),
  398. Mod1 = list_to_atom(lists:flatten(io_lib:format("~ts_extra", [Name1]))),
  399. {module, Mod1} = code:ensure_loaded(Mod1),
  400. Mod2 = list_to_atom(lists:flatten(io_lib:format("~ts_extra", [Name2]))),
  401. {error, nofile} = code:ensure_loaded(Mod2),
  402. ExpectOne = filename:join([AppDir, "_build", "default", "lib", Name1, "ebin",
  403. io_lib:format("~ts_extra.beam", [Name1])]),
  404. ExpectOne = code:which(Mod1).
  405. paths_basic_extra_dirs(Config) ->
  406. AppDir = ?config(apps, Config),
  407. [Name] = ?config(app_names, Config),
  408. RebarConfig = [{erl_opts, [{extra_src_dirs, ["extra"]}]}],
  409. {ok, State} = rebar_test_utils:run_and_check(Config, RebarConfig, ["compile"], return),
  410. code:add_paths(rebar_state:code_paths(State, all_deps)),
  411. Mod = list_to_atom(lists:flatten(io_lib:format("~ts_extra", [Name]))),
  412. {module, Mod} = code:ensure_loaded(Mod),
  413. Expect = filename:join([AppDir, "_build", "default", "lib", Name, "extra",
  414. io_lib:format("~ts_extra.beam", [Name])]),
  415. Expect = code:which(Mod).
  416. paths_release_extra_dirs(Config) ->
  417. AppDir = ?config(apps, Config),
  418. [Name1, Name2] = ?config(app_names, Config),
  419. RebarConfig = [{erl_opts, [{extra_src_dirs, ["extra"]}]}],
  420. {ok, State} = rebar_test_utils:run_and_check(Config, RebarConfig, ["compile"], return),
  421. code:add_paths(rebar_state:code_paths(State, all_deps)),
  422. Mod1 = list_to_atom(lists:flatten(io_lib:format("~ts_extra", [Name1]))),
  423. {module, Mod1} = code:ensure_loaded(Mod1),
  424. Mod2 = list_to_atom(lists:flatten(io_lib:format("~ts_extra", [Name2]))),
  425. {module, Mod2} = code:ensure_loaded(Mod2),
  426. ExpectOne = filename:join([AppDir, "_build", "default", "lib", Name1, "extra",
  427. io_lib:format("~ts_extra.beam", [Name1])]),
  428. ExpectOne = code:which(Mod1),
  429. ExpectTwo = filename:join([AppDir, "_build", "default", "lib", Name2, "extra",
  430. io_lib:format("~ts_extra.beam", [Name2])]),
  431. ExpectTwo = code:which(Mod2).
  432. paths_unbalanced_extra_dirs(Config) ->
  433. AppDir = ?config(apps, Config),
  434. [Name1, Name2] = ?config(app_names, Config),
  435. RebarConfig = [{erl_opts, [{extra_src_dirs, ["extra"]}]}],
  436. {ok, State} = rebar_test_utils:run_and_check(Config, RebarConfig, ["compile"], return),
  437. code:add_paths(rebar_state:code_paths(State, all_deps)),
  438. Mod1 = list_to_atom(lists:flatten(io_lib:format("~ts_extra", [Name1]))),
  439. {module, Mod1} = code:ensure_loaded(Mod1),
  440. Mod2 = list_to_atom(lists:flatten(io_lib:format("~ts_extra", [Name2]))),
  441. {error, nofile} = code:ensure_loaded(Mod2),
  442. ExpectOne = filename:join([AppDir, "_build", "default", "lib", Name1, "extra",
  443. io_lib:format("~ts_extra.beam", [Name1])]),
  444. ExpectOne = code:which(Mod1).
  445. paths_extra_dirs_in_project_root(Config) ->
  446. AppDir = ?config(apps, Config),
  447. RebarConfig = [{erl_opts, [{extra_src_dirs, ["extra"]}]}],
  448. {ok, State} = rebar_test_utils:run_and_check(Config, RebarConfig, ["compile"], return),
  449. code:add_paths(rebar_state:code_paths(State, all_deps)),
  450. {module, extra} = code:ensure_loaded(extra),
  451. Expect = filename:join([AppDir, "_build", "default", "extras", "extra", "extra.beam"]),
  452. Expect = code:which(extra).
  453. clean_basic_app(Config) ->
  454. [Name] = ?config(app_names, Config),
  455. rebar_test_utils:run_and_check(Config, [], ["clean"], {ok, [{app, Name, invalid}]}).
  456. clean_release_apps(Config) ->
  457. [Name1, Name2] = ?config(app_names, Config),
  458. rebar_test_utils:run_and_check(Config, [], ["clean"],
  459. {ok, [{app, Name1, invalid}, {app, Name2, invalid}]}).
  460. clean_basic_extra_dirs(Config) ->
  461. AppDir = ?config(apps, Config),
  462. [Name] = ?config(app_names, Config),
  463. rebar_test_utils:run_and_check(Config, [], ["clean"], {ok, [{app, Name, invalid}]}),
  464. Beam = lists:flatten(io_lib:format("~ts_extra", [Name])),
  465. false = ec_file:exists(filename:join([AppDir, "_build", "default", "lib", Name, "extras", Beam])).
  466. clean_release_extra_dirs(Config) ->
  467. AppDir = ?config(apps, Config),
  468. [Name1, Name2] = ?config(app_names, Config),
  469. rebar_test_utils:run_and_check(Config, [], ["clean"],
  470. {ok, [{app, Name1, invalid}, {app, Name2, invalid}]}),
  471. Beam1 = lists:flatten(io_lib:format("~ts_extra", [Name1])),
  472. false = ec_file:exists(filename:join([AppDir, "_build", "default", "lib", Name1, "extras", Beam1])),
  473. Beam2 = lists:flatten(io_lib:format("~ts_extra", [Name2])),
  474. false = ec_file:exists(filename:join([AppDir, "_build", "default", "lib", Name2, "extras", Beam2])).
  475. clean_extra_dirs_in_project_root(Config) ->
  476. AppDir = ?config(apps, Config),
  477. [Name1, Name2] = ?config(app_names, Config),
  478. rebar_test_utils:run_and_check(Config, [], ["clean"],
  479. {ok, [{app, Name1, invalid}, {app, Name2, invalid}]}),
  480. false = ec_file:exists(filename:join([AppDir, "_build", "default", "extras"])).
  481. recompile_when_hrl_changes(Config) ->
  482. AppDir = ?config(apps, Config),
  483. Name = rebar_test_utils:create_random_name("app1_"),
  484. Vsn = rebar_test_utils:create_random_vsn(),
  485. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  486. ExtraSrc = <<"-module(test_header_include).\n"
  487. "-export([main/0]).\n"
  488. "-include(\"test_header_include.hrl\").\n"
  489. "main() -> ?SOME_DEFINE.\n">>,
  490. ExtraHeader = <<"-define(SOME_DEFINE, true).\n">>,
  491. HeaderFile = filename:join([AppDir, "src", "test_header_include.hrl"]),
  492. ok = file:write_file(filename:join([AppDir, "src", "test_header_include.erl"]), ExtraSrc),
  493. ok = file:write_file(HeaderFile, ExtraHeader),
  494. rebar_test_utils:run_and_check(Config, [], ["compile"], {ok, [{app, Name}]}),
  495. EbinDir = filename:join([AppDir, "_build", "default", "lib", Name, "ebin"]),
  496. {ok, Files} = rebar_utils:list_dir(EbinDir),
  497. ModTime = [filelib:last_modified(filename:join([EbinDir, F]))
  498. || F <- Files, filename:extension(F) == ".beam"],
  499. timer:sleep(1000),
  500. NewExtraHeader = <<"-define(SOME_DEFINE, false).\n">>,
  501. ok = file:write_file(HeaderFile, NewExtraHeader),
  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_included_hrl_changes(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. ExtraSrc = <<"-module(test_header_include).\n"
  513. "-export([main/0]).\n"
  514. "-include(\"test_header_include.hrl\").\n"
  515. "main() -> ?SOME_DEFINE.\n">>,
  516. ExtraHeader = <<"-define(SOME_DEFINE, true).\n">>,
  517. ok = filelib:ensure_dir(filename:join([AppDir, "include", "dummy"])),
  518. HeaderFile = filename:join([AppDir, "include", "test_header_include.hrl"]),
  519. ok = file:write_file(filename:join([AppDir, "src", "test_header_include.erl"]), ExtraSrc),
  520. ok = file:write_file(HeaderFile, ExtraHeader),
  521. rebar_test_utils:run_and_check(Config, [], ["compile"], {ok, [{app, Name}]}),
  522. EbinDir = filename:join([AppDir, "_build", "default", "lib", Name, "ebin"]),
  523. {ok, Files} = rebar_utils:list_dir(EbinDir),
  524. ModTime = [filelib:last_modified(filename:join([EbinDir, F]))
  525. || F <- Files, filename:extension(F) == ".beam"],
  526. timer:sleep(1000),
  527. NewExtraHeader = <<"-define(SOME_DEFINE, false).\n">>,
  528. ok = file:write_file(HeaderFile, NewExtraHeader),
  529. rebar_test_utils:run_and_check(Config, [], ["compile"], {ok, [{app, Name}]}),
  530. {ok, NewFiles} = rebar_utils:list_dir(EbinDir),
  531. NewModTime = [filelib:last_modified(filename:join([EbinDir, F]))
  532. || F <- NewFiles, filename:extension(F) == ".beam"],
  533. ?assert(ModTime =/= NewModTime).
  534. recompile_when_opts_included_hrl_changes(Config) ->
  535. AppsDir = ?config(apps, Config),
  536. Name = rebar_test_utils:create_random_name("app1_"),
  537. Vsn = rebar_test_utils:create_random_vsn(),
  538. AppDir = filename:join([AppsDir, "apps", Name]),
  539. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  540. ExtraSrc = <<"-module(test_header_include).\n"
  541. "-export([main/0]).\n"
  542. "-include(\"test_header_include.hrl\").\n"
  543. "main() -> ?SOME_DEFINE.\n">>,
  544. ExtraHeader = <<"-define(SOME_DEFINE, true).\n">>,
  545. ok = filelib:ensure_dir(filename:join([AppsDir, "include", "dummy"])),
  546. HeaderFile = filename:join([AppsDir, "include", "test_header_include.hrl"]),
  547. ok = file:write_file(filename:join([AppDir, "src", "test_header_include.erl"]), ExtraSrc),
  548. ok = file:write_file(HeaderFile, ExtraHeader),
  549. %% Using relative path from the project root
  550. RebarConfig = [{erl_opts, [{i, "include/"}]}],
  551. {ok,Cwd} = file:get_cwd(),
  552. ok = file:set_cwd(AppsDir),
  553. rebar_test_utils:run_and_check(Config, RebarConfig, ["compile"], {ok, [{app, Name}]}),
  554. EbinDir = filename:join([AppsDir, "_build", "default", "lib", Name, "ebin"]),
  555. {ok, Files} = rebar_utils:list_dir(EbinDir),
  556. ModTime = [filelib:last_modified(filename:join([EbinDir, F]))
  557. || F <- Files, filename:extension(F) == ".beam"],
  558. timer:sleep(1000),
  559. NewExtraHeader = <<"-define(SOME_DEFINE, false).\n">>,
  560. ok = file:write_file(HeaderFile, NewExtraHeader),
  561. rebar_test_utils:run_and_check(Config, RebarConfig, ["compile"], {ok, [{app, Name}]}),
  562. {ok, NewFiles} = rebar_utils:list_dir(EbinDir),
  563. NewModTime = [filelib:last_modified(filename:join([EbinDir, F]))
  564. || F <- NewFiles, filename:extension(F) == ".beam"],
  565. ok = file:set_cwd(Cwd),
  566. ?assert(ModTime =/= NewModTime).
  567. recompile_when_opts_change(Config) ->
  568. AppDir = ?config(apps, Config),
  569. Name = rebar_test_utils:create_random_name("app1_"),
  570. Vsn = rebar_test_utils:create_random_vsn(),
  571. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  572. rebar_test_utils:run_and_check(Config, [], ["compile"], {ok, [{app, Name}]}),
  573. EbinDir = filename:join([AppDir, "_build", "default", "lib", Name, "ebin"]),
  574. {ok, Files} = rebar_utils:list_dir(EbinDir),
  575. ModTime = [filelib:last_modified(filename:join([EbinDir, F]))
  576. || F <- Files, filename:extension(F) == ".beam"],
  577. timer:sleep(1000),
  578. rebar_test_utils:create_config(AppDir, [{erl_opts, [{d, some_define}]}]),
  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. ?assert(ModTime =/= NewModTime).
  584. dont_recompile_when_opts_dont_change(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. rebar_test_utils:run_and_check(Config, [], ["compile"], {ok, [{app, Name}]}),
  590. EbinDir = filename:join([AppDir, "_build", "default", "lib", Name, "ebin"]),
  591. {ok, Files} = rebar_utils:list_dir(EbinDir),
  592. ModTime = [filelib:last_modified(filename:join([EbinDir, F]))
  593. || F <- Files, filename:extension(F) == ".beam"],
  594. timer:sleep(1000),
  595. rebar_test_utils:run_and_check(Config, [], ["compile"], {ok, [{app, Name}]}),
  596. {ok, NewFiles} = rebar_utils:list_dir(EbinDir),
  597. NewModTime = [filelib:last_modified(filename:join([EbinDir, F]))
  598. || F <- NewFiles, filename:extension(F) == ".beam"],
  599. ?assertEqual(ModTime, NewModTime).
  600. dont_recompile_yrl_or_xrl(Config) ->
  601. AppDir = ?config(apps, Config),
  602. Name = rebar_test_utils:create_random_name("app1_"),
  603. Vsn = rebar_test_utils:create_random_vsn(),
  604. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  605. Xrl = filename:join([AppDir, "src", "not_a_real_xrl_" ++ Name ++ ".xrl"]),
  606. ok = filelib:ensure_dir(Xrl),
  607. XrlBody =
  608. "Definitions."
  609. "\n\n"
  610. "D = [0-9]"
  611. "\n\n"
  612. "Rules."
  613. "\n\n"
  614. "{D}+ :"
  615. " {token,{integer,TokenLine,list_to_integer(TokenChars)}}."
  616. "\n\n"
  617. "{D}+\\.{D}+((E|e)(\\+|\\-)?{D}+)? :"
  618. " {token,{float,TokenLine,list_to_float(TokenChars)}}."
  619. "\n\n"
  620. "Erlang code.",
  621. ok = ec_file:write(Xrl, XrlBody),
  622. XrlBeam = filename:join([AppDir, "ebin", filename:basename(Xrl, ".xrl") ++ ".beam"]),
  623. rebar_test_utils:run_and_check(Config, [], ["compile"], {ok, [{app, Name}]}),
  624. ModTime = filelib:last_modified(XrlBeam),
  625. timer:sleep(1000),
  626. rebar_test_utils:run_and_check(Config, [], ["compile"], {ok, [{app, Name}]}),
  627. NewModTime = filelib:last_modified(XrlBeam),
  628. ?assert(ModTime == NewModTime).
  629. delete_beam_if_source_deleted(Config) ->
  630. AppDir = ?config(apps, Config),
  631. Name = rebar_test_utils:create_random_name("app1_"),
  632. Vsn = rebar_test_utils:create_random_vsn(),
  633. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  634. rebar_test_utils:run_and_check(Config, [], ["compile"], {ok, [{app, Name}]}),
  635. EbinDir = filename:join([AppDir, "_build", "default", "lib", Name, "ebin"]),
  636. _SrcDir = filename:join([AppDir, "_build", "default", "lib", Name, "src"]),
  637. ?assert(filelib:is_regular(filename:join(EbinDir, "not_a_real_src_" ++ Name ++ ".beam"))),
  638. file:delete(filename:join([AppDir, "src", "not_a_real_src_" ++ Name ++ ".erl"])),
  639. rebar_test_utils:run_and_check(Config, [], ["compile"], {ok, [{app, Name}]}),
  640. ?assertNot(filelib:is_regular(filename:join(EbinDir, "not_a_real_src_" ++ Name ++ ".beam"))).
  641. deps_in_path(Config) ->
  642. AppDir = ?config(apps, Config),
  643. StartPaths = code:get_path(),
  644. Name = rebar_test_utils:create_random_name("app1_"),
  645. Vsn = rebar_test_utils:create_random_vsn(),
  646. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  647. DepName = rebar_test_utils:create_random_name("dep1_"),
  648. PkgName = rebar_test_utils:create_random_name("pkg1_"),
  649. mock_git_resource:mock([]),
  650. mock_pkg_resource:mock([
  651. {pkgdeps, [{{iolist_to_binary(PkgName), iolist_to_binary(Vsn)}, []}]}
  652. ]),
  653. RConfFile = rebar_test_utils:create_config(AppDir, [{deps, [
  654. {list_to_atom(DepName), {git, "http://site.com/user/"++DepName++".git", {tag, Vsn}}},
  655. {list_to_atom(PkgName), Vsn}
  656. ]}]),
  657. {ok, RConf} = file:consult(RConfFile),
  658. %% Make sure apps we look for are not visible
  659. %% Hope not to find src name
  660. ?assertEqual([], [Path || Path <- code:get_path(),
  661. {match, _} <- [re:run(Path, DepName)]]),
  662. %% Hope not to find pkg name in there
  663. ?assertEqual([], [Path || Path <- code:get_path(),
  664. {match, _} <- [re:run(Path, PkgName)]]),
  665. %% Build things
  666. {ok, State} = rebar_test_utils:run_and_check(
  667. Config, RConf, ["compile"],
  668. {ok, [{app, Name}, {dep, DepName}, {dep, PkgName}]}
  669. ),
  670. code:add_paths(rebar_state:code_paths(State, all_deps)),
  671. %% Find src name in there
  672. ?assertNotEqual([], [Path || Path <- code:get_path(),
  673. {match, _} <- [re:run(Path, DepName)]]),
  674. %% find pkg name in there
  675. ?assertNotEqual([], [Path || Path <- code:get_path(),
  676. {match, _} <- [re:run(Path, PkgName)]]),
  677. true = code:set_path(lists:filter(fun(P) -> ec_file:exists(P) end, StartPaths)),
  678. %% Make sure apps we look for are not visible again
  679. %% Hope not to find src name
  680. ?assertEqual([], [Path || Path <- code:get_path(),
  681. {match, _} <- [re:run(Path, DepName)]]),
  682. %% Hope not to find pkg name in there
  683. ?assertEqual([], [Path || Path <- code:get_path(),
  684. {match, _} <- [re:run(Path, PkgName)]]),
  685. %% Rebuild
  686. {ok, State1} = rebar_test_utils:run_and_check(
  687. Config, RConf, ["compile"],
  688. {ok, [{app, Name}, {dep, DepName}, {dep, PkgName}]}
  689. ),
  690. %% Find src name in there
  691. code:add_paths(rebar_state:code_paths(State1, all_deps)),
  692. ?assertNotEqual([], [Path || Path <- code:get_path(),
  693. {match, _} <- [re:run(Path, DepName)]]),
  694. %% find pkg name in there
  695. ?assertNotEqual([], [Path || Path <- code:get_path(),
  696. {match, _} <- [re:run(Path, PkgName)]]).
  697. checkout_priority(Config) ->
  698. AppDir = ?config(apps, Config),
  699. CheckoutsDir = ?config(checkouts, Config),
  700. StartPaths = code:get_path(),
  701. Name = rebar_test_utils:create_random_name("app1_"),
  702. Vsn = rebar_test_utils:create_random_vsn(),
  703. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  704. DepName = rebar_test_utils:create_random_name("dep1_"),
  705. PkgName = rebar_test_utils:create_random_name("pkg1_"),
  706. mock_git_resource:mock([]),
  707. mock_pkg_resource:mock([
  708. {pkgdeps, [{{iolist_to_binary(PkgName), iolist_to_binary(Vsn)}, []}]}
  709. ]),
  710. RConfFile = rebar_test_utils:create_config(AppDir, [{deps, [
  711. {list_to_atom(DepName), {git, "http://site.com/user/"++DepName++".git", {tag, Vsn}}},
  712. {list_to_atom(PkgName), Vsn}
  713. ]}]),
  714. {ok, RConf} = file:consult(RConfFile),
  715. %% Build with deps.
  716. rebar_test_utils:run_and_check(
  717. Config, RConf, ["compile"],
  718. {ok, [{app, Name}, {dep, DepName}, {dep, PkgName}]}
  719. ),
  720. %% Build two checkout apps similar to dependencies to be fetched,
  721. %% but on a different version
  722. Vsn2 = rebar_test_utils:create_random_vsn(),
  723. rebar_test_utils:create_app(filename:join([CheckoutsDir,DepName]), DepName, Vsn2, [kernel, stdlib]),
  724. rebar_test_utils:create_app(filename:join([CheckoutsDir,PkgName]), PkgName, Vsn2, [kernel, stdlib]),
  725. %% Rebuild and make sure the checkout apps are in path
  726. code:set_path(StartPaths),
  727. {ok, State} = rebar_test_utils:run_and_check(
  728. Config, RConf, ["compile"],
  729. {ok, [{app, Name}, {checkout, DepName}, {checkout, PkgName}]}
  730. ),
  731. code:add_paths(rebar_state:code_paths(State, all_deps)),
  732. [DepPath] = [Path || Path <- code:get_path(),
  733. {match, _} <- [re:run(Path, DepName)]],
  734. [PkgPath] = [Path || Path <- code:get_path(),
  735. {match, _} <- [re:run(Path, PkgName)]],
  736. {ok, [DepApp]} = file:consult(filename:join([DepPath, DepName ++ ".app"])),
  737. {ok, [PkgApp]} = file:consult(filename:join([PkgPath, PkgName ++ ".app"])),
  738. {application, _, DepProps} = DepApp,
  739. {application, _, PkgProps} = PkgApp,
  740. ?assertEqual(Vsn2, proplists:get_value(vsn, DepProps)),
  741. ?assertEqual(Vsn2, proplists:get_value(vsn, PkgProps)).
  742. highest_version_of_pkg_dep(Config) ->
  743. AppDir = ?config(apps, Config),
  744. Name = rebar_test_utils:create_random_name("app1_"),
  745. Vsn = rebar_test_utils:create_random_vsn(),
  746. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  747. PkgName = rebar_test_utils:create_random_name("pkg1_"),
  748. mock_git_resource:mock([]),
  749. mock_pkg_resource:mock([
  750. {pkgdeps, [{{iolist_to_binary(PkgName), <<"0.1.0">>}, []},
  751. {{iolist_to_binary(PkgName), <<"0.0.1">>}, []},
  752. {{iolist_to_binary(PkgName), <<"0.1.3">>}, []},
  753. {{iolist_to_binary(PkgName), <<"0.1.1">>}, []}]}
  754. ]),
  755. RConfFile = rebar_test_utils:create_config(AppDir, [{deps, [list_to_atom(PkgName)]}]),
  756. {ok, RConf} = file:consult(RConfFile),
  757. %% Build with deps.
  758. rebar_test_utils:run_and_check(
  759. Config, RConf, ["compile"],
  760. {ok, [{app, Name}, {dep, PkgName, <<"0.1.3">>}]}
  761. ).
  762. parse_transform_test(Config) ->
  763. AppDir = ?config(apps, Config),
  764. RebarConfig = [{erl_opts, [{parse_transform, pascal}]}],
  765. Name = rebar_test_utils:create_random_name("app1_"),
  766. Vsn = rebar_test_utils:create_random_vsn(),
  767. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  768. ExtraSrc = <<"-module(pascal). "
  769. "-export([parse_transform/2]). "
  770. "parse_transform(Forms, _Options) -> "
  771. "Forms.">>,
  772. ok = file:write_file(filename:join([AppDir, "src", "pascal.erl"]), ExtraSrc),
  773. rebar_test_utils:run_and_check(Config, RebarConfig, ["compile"], {ok, [{app, Name}]}),
  774. EbinDir = filename:join([AppDir, "_build", "default", "lib", Name, "ebin"]),
  775. true = filelib:is_file(filename:join([EbinDir, "pascal.beam"])).
  776. erl_first_files_test(Config) ->
  777. AppDir = ?config(apps, Config),
  778. RebarConfig = [{erl_opts, [{parse_transform, mark_time}]},
  779. {erl_first_files, ["src/mark_time.erl",
  780. "src/b.erl",
  781. "src/d.erl",
  782. "src/a.erl"]}],
  783. Name = rebar_test_utils:create_random_name("app1_"),
  784. Vsn = rebar_test_utils:create_random_vsn(),
  785. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  786. rebar_test_utils:write_src_file(AppDir, "a.erl"),
  787. rebar_test_utils:write_src_file(AppDir, "b.erl"),
  788. rebar_test_utils:write_src_file(AppDir, "d.erl"),
  789. rebar_test_utils:write_src_file(AppDir, "e.erl"),
  790. ExtraSrc = <<"-module(mark_time). "
  791. "-export([parse_transform/2]). "
  792. "parse_transform([Form={attribute,_,module,Mod}|Forms], Options) -> "
  793. " [Form, {attribute,1,number, os:timestamp()} | Forms];"
  794. "parse_transform([Form|Forms], Options) -> "
  795. " [Form | parse_transform(Forms, Options)].">>,
  796. ok = file:write_file(filename:join([AppDir, "src", "mark_time.erl"]), ExtraSrc),
  797. rebar_test_utils:run_and_check(Config, RebarConfig, ["compile"], {ok, [{app, Name}]}),
  798. EbinDir = filename:join([AppDir, "_build", "default", "lib", Name, "ebin"]),
  799. true = filelib:is_file(filename:join([EbinDir, "mark_time.beam"])),
  800. code:load_abs(filename:join([EbinDir, "a"])),
  801. code:load_abs(filename:join([EbinDir, "b"])),
  802. code:load_abs(filename:join([EbinDir, "d"])),
  803. code:load_abs(filename:join([EbinDir, "e"])),
  804. A = proplists:get_value(number, a:module_info(attributes)),
  805. B = proplists:get_value(number, b:module_info(attributes)),
  806. D = proplists:get_value(number, d:module_info(attributes)),
  807. E = proplists:get_value(number, e:module_info(attributes)),
  808. ?assertEqual([B,D,A,E], lists:sort([A,B,D,E])).
  809. mib_test(Config) ->
  810. AppDir = ?config(apps, Config),
  811. RebarConfig = [{mib_first_files, ["mibs/SIMPLE-MIB.mib"]}],
  812. Name = rebar_test_utils:create_random_name("app1_"),
  813. Vsn = rebar_test_utils:create_random_vsn(),
  814. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  815. MibsSrc = <<"-- SIMPLE-MIB.\n"
  816. "-- This is just a simple MIB used for testing!\n"
  817. "--\n"
  818. "SIMPLE-MIB DEFINITIONS ::= BEGIN\n"
  819. "IMPORTS\n"
  820. " MODULE-IDENTITY, enterprises\n"
  821. " FROM SNMPv2-SMI;\n"
  822. "\n"
  823. "ericsson MODULE-IDENTITY\n"
  824. " LAST-UPDATED\n"
  825. " \"201403060000Z\"\n"
  826. " ORGANIZATION\n"
  827. " \"rebar\"\n"
  828. " CONTACT-INFO\n"
  829. " \"rebar <rebar@example.com>\n"
  830. " or\n"
  831. " whoever is currently responsible for the SIMPLE\n"
  832. " enterprise MIB tree branch (enterprises.999).\"\n"
  833. " DESCRIPTION\n"
  834. " \"This very small module is made available\n"
  835. " for mib-compilation testing.\"\n"
  836. " ::= { enterprises 999 }\n"
  837. "END\n">>,
  838. ok = filelib:ensure_dir(filename:join([AppDir, "mibs", "dummy"])),
  839. ok = file:write_file(filename:join([AppDir, "mibs", "SIMPLE-MIB.mib"]), MibsSrc),
  840. rebar_test_utils:run_and_check(Config, RebarConfig, ["compile"], {ok, [{app, Name}]}),
  841. %% check a bin corresponding to the mib in the mibs dir exists in priv/mibs
  842. PrivMibsDir = filename:join([AppDir, "_build", "default", "lib", Name, "priv", "mibs"]),
  843. true = filelib:is_file(filename:join([PrivMibsDir, "SIMPLE-MIB.bin"])),
  844. %% check a hrl corresponding to the mib in the mibs dir exists in include
  845. true = filelib:is_file(filename:join([AppDir, "include", "SIMPLE-MIB.hrl"])),
  846. %% check the mibs dir was linked into the _build dir
  847. true = filelib:is_dir(filename:join([AppDir, "_build", "default", "lib", Name, "mibs"])).
  848. umbrella_mib_first_test(Config) ->
  849. AppsDir = ?config(apps, Config),
  850. Name = rebar_test_utils:create_random_name("app1_"),
  851. Vsn = rebar_test_utils:create_random_vsn(),
  852. AppDir = filename:join([AppsDir, "apps", Name]),
  853. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  854. MibsSrc = <<"-- SIMPLE-MIB.\n"
  855. "-- This is just a simple MIB used for testing!\n"
  856. "--\n"
  857. "SIMPLE-MIB DEFINITIONS ::= BEGIN\n"
  858. "IMPORTS\n"
  859. " MODULE-IDENTITY, enterprises\n"
  860. " FROM SNMPv2-SMI;\n"
  861. "\n"
  862. "ericsson MODULE-IDENTITY\n"
  863. " LAST-UPDATED\n"
  864. " \"201403060000Z\"\n"
  865. " ORGANIZATION\n"
  866. " \"rebar\"\n"
  867. " CONTACT-INFO\n"
  868. " \"rebar <rebar@example.com>\n"
  869. " or\n"
  870. " whoever is currently responsible for the SIMPLE\n"
  871. " enterprise MIB tree branch (enterprises.999).\"\n"
  872. " DESCRIPTION\n"
  873. " \"This very small module is made available\n"
  874. " for mib-compilation testing.\"\n"
  875. " ::= { enterprises 999 }\n"
  876. "END\n">>,
  877. ok = filelib:ensure_dir(filename:join([AppDir, "mibs", "dummy"])),
  878. ok = file:write_file(filename:join([AppDir, "mibs", "SIMPLE-MIB.mib"]), MibsSrc),
  879. RebarConfig = [{mib_first_files, ["mibs/SIMPLE-MIB.mib"]}],
  880. rebar_test_utils:run_and_check(Config, RebarConfig, ["compile"], {ok, [{app, Name}]}),
  881. %% check a bin corresponding to the mib in the mibs dir exists in priv/mibs
  882. PrivMibsDir = filename:join([AppsDir, "_build", "default", "lib", Name, "priv", "mibs"]),
  883. true = filelib:is_file(filename:join([PrivMibsDir, "SIMPLE-MIB.bin"])),
  884. %% check a hrl corresponding to the mib in the mibs dir exists in include
  885. true = filelib:is_file(filename:join([AppDir, "include", "SIMPLE-MIB.hrl"])),
  886. %% check the mibs dir was linked into the _build dir
  887. true = filelib:is_dir(filename:join([AppsDir, "_build", "default", "lib", Name, "mibs"])).
  888. only_default_transitive_deps(Config) ->
  889. AppDir = ?config(apps, Config),
  890. Name = rebar_test_utils:create_random_name("app1_"),
  891. Vsn = rebar_test_utils:create_random_vsn(),
  892. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  893. GitDeps = rebar_test_utils:expand_deps(git, [{"a", "1.0.0", []}]),
  894. PkgName = rebar_test_utils:create_random_name("pkg1_"),
  895. {SrcDeps, _} = rebar_test_utils:flat_deps(GitDeps),
  896. mock_git_resource:mock([{deps, SrcDeps},
  897. {config, [{profiles, [{test, [{deps, [list_to_atom(PkgName)]}]}]}]}]),
  898. mock_pkg_resource:mock([{pkgdeps, [{{iolist_to_binary(PkgName), <<"0.1.0">>}, []}]}]),
  899. Deps = rebar_test_utils:top_level_deps(GitDeps),
  900. RConfFile = rebar_test_utils:create_config(AppDir, [{deps, Deps}]),
  901. {ok, RConf} = file:consult(RConfFile),
  902. %% Build with deps.
  903. rebar_test_utils:run_and_check(
  904. Config, RConf, ["as", "test", "compile"],
  905. {ok, [{app, Name}, {dep, "a", <<"1.0.0">>}, {dep_not_exist, PkgName}]}
  906. ).
  907. clean_all(Config) ->
  908. AppDir = ?config(apps, Config),
  909. Name = rebar_test_utils:create_random_name("app1_"),
  910. Vsn = rebar_test_utils:create_random_vsn(),
  911. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  912. DepName = rebar_test_utils:create_random_name("dep1_"),
  913. PkgName = rebar_test_utils:create_random_name("pkg1_"),
  914. mock_git_resource:mock([]),
  915. mock_pkg_resource:mock([
  916. {pkgdeps, [{{iolist_to_binary(PkgName), iolist_to_binary(Vsn)}, []}]}
  917. ]),
  918. RConfFile = rebar_test_utils:create_config(AppDir, [{deps, [
  919. {list_to_atom(DepName), {git, "http://site.com/user/"++DepName++".git", {tag, Vsn}}},
  920. {list_to_atom(PkgName), Vsn}
  921. ]}]),
  922. {ok, RConf} = file:consult(RConfFile),
  923. %% Build things
  924. rebar_test_utils:run_and_check(
  925. Config, RConf, ["compile"],
  926. {ok, [{app, Name}, {app, DepName}, {app, PkgName}]}
  927. ),
  928. %% Clean all
  929. rebar_test_utils:run_and_check(Config, [], ["clean", "--all"],
  930. {ok, [{app, Name, invalid},
  931. {app, DepName, invalid},
  932. {app, PkgName, invalid}]}).
  933. override_deps(Config) ->
  934. Deps = rebar_test_utils:expand_deps(git, [{"some_dep", "0.0.1", [{"other_dep", "0.0.1", []}]}]),
  935. TopDeps = rebar_test_utils:top_level_deps(Deps),
  936. {SrcDeps, _} = rebar_test_utils:flat_deps(Deps),
  937. mock_git_resource:mock([{deps, SrcDeps}]),
  938. RebarConfig = [
  939. {deps, TopDeps},
  940. {overrides, [
  941. {override, some_dep, [
  942. {deps, []}
  943. ]}
  944. ]}
  945. ],
  946. rebar_test_utils:run_and_check(
  947. Config, RebarConfig, ["compile"],
  948. {ok, [{dep, "some_dep"},
  949. {dep_not_exist, "other_dep"}]}
  950. ).
  951. override_add_deps(Config) ->
  952. Deps = rebar_test_utils:expand_deps(git, [{"some_dep", "0.0.1", [{"other_dep", "0.0.1", []}]}]),
  953. TopDeps = rebar_test_utils:top_level_deps(Deps),
  954. DepA = {dep_a, "0.0.1", {git, "http://site.com/dep_a.git", {tag, "0.0.1"}}},
  955. DepB = {dep_b, "0.0.1", {git, "http://site.com/dep_b.git", {tag, "0.0.1"}}},
  956. DepC = {dep_c, "0.0.1", {git, "http://site.com/dep_c.git", {tag, "0.0.1"}}},
  957. {SrcDeps, _} = rebar_test_utils:flat_deps(Deps),
  958. mock_git_resource:mock([{deps, [DepA, DepB, DepC | SrcDeps]}]),
  959. RebarConfig = [
  960. {deps, TopDeps},
  961. {overrides, [
  962. {add, some_dep, [
  963. {deps, [DepA, DepB]}
  964. ]},
  965. {add, [
  966. {deps, [DepC]}
  967. ]}
  968. ]}
  969. ],
  970. rebar_test_utils:run_and_check(
  971. Config, RebarConfig, ["compile"],
  972. {ok, [{dep, "some_dep"},
  973. {dep, "other_dep"},
  974. {dep, "dep_a"},
  975. {dep, "dep_b"},
  976. {dep, "dep_c"}]}
  977. ).
  978. override_del_deps(Config) ->
  979. Deps = rebar_test_utils:expand_deps(git, [{"some_dep", "0.0.1", [{"dep_a", "0.0.1", []},
  980. {"dep_b", "0.0.1", []},
  981. {"dep_c", "0.0.1", []}]},
  982. {"other_dep", "0.0.1", [{"dep_c", "0.0.1", []},
  983. {"dep_d", "0.0.1", []}]}]),
  984. TopDeps = rebar_test_utils:top_level_deps(Deps),
  985. DepA = {dep_a, "0.0.1", {git, "https://example.org/user/dep_a.git", {tag, "0.0.1"}}},
  986. DepB = {dep_b, "0.0.1", {git, "https://example.org/user/dep_b.git", {tag, "0.0.1"}}},
  987. DepC = {dep_c, "0.0.1", {git, "https://example.org/user/dep_c.git", {tag, "0.0.1"}}},
  988. {SrcDeps, _} = rebar_test_utils:flat_deps(Deps),
  989. mock_git_resource:mock([{deps, SrcDeps}]),
  990. RebarConfig = [
  991. {deps, TopDeps},
  992. {overrides, [
  993. {del, some_dep, [
  994. {deps, [DepA, DepB]}
  995. ]},
  996. {del, [
  997. {deps, [DepC]}
  998. ]}
  999. ]}
  1000. ],
  1001. rebar_test_utils:run_and_check(
  1002. Config, RebarConfig, ["compile"],
  1003. {ok, [{dep, "some_dep"},
  1004. {dep, "other_dep"},
  1005. {dep_not_exist, "dep_a"},
  1006. {dep_not_exist, "dep_b"},
  1007. {dep_not_exist, "dep_c"},
  1008. {dep, "dep_d"}]}
  1009. ).
  1010. override_opts(Config) ->
  1011. AppDir = ?config(apps, Config),
  1012. Name = rebar_test_utils:create_random_name("app1_"),
  1013. Vsn = rebar_test_utils:create_random_vsn(),
  1014. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  1015. RebarConfig = [
  1016. {erl_opts, [
  1017. compressed,
  1018. warn_missing_spec
  1019. ]},
  1020. {overrides, [
  1021. {override, [
  1022. {erl_opts, [compressed]}
  1023. ]}
  1024. ]}
  1025. ],
  1026. rebar_test_utils:create_config(AppDir, RebarConfig),
  1027. rebar_test_utils:run_and_check(
  1028. Config, RebarConfig, ["compile"], {ok, [{app, Name}]}),
  1029. Path = filename:join([AppDir, "_build", "default", "lib", Name, "ebin"]),
  1030. code:add_patha(Path),
  1031. Mod = list_to_atom("not_a_real_src_" ++ Name),
  1032. true = lists:member(compressed, proplists:get_value(options, Mod:module_info(compile), [])),
  1033. false = lists:member(warn_missing_spec, proplists:get_value(options, Mod:module_info(compile), [])).
  1034. %% test for fix of https://github.com/erlang/rebar3/issues/1801
  1035. %% only apply overrides once
  1036. %% verify by having an override add the macro TEST to the dep some_dep
  1037. %% building under `ct` will fail if the `add` is applied more than once
  1038. apply_overrides_exactly_once(Config) ->
  1039. AppDir = ?config(apps, Config),
  1040. Deps = rebar_test_utils:expand_deps(git, [{"some_dep", "0.0.1", [{"other_dep", "0.0.1", []}]}]),
  1041. TopDeps = rebar_test_utils:top_level_deps(Deps),
  1042. {SrcDeps, _} = rebar_test_utils:flat_deps(Deps),
  1043. mock_git_resource:mock([{deps, SrcDeps}]),
  1044. Name = rebar_test_utils:create_random_name("app1_"),
  1045. Vsn = rebar_test_utils:create_random_vsn(),
  1046. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  1047. RebarConfig = [{deps, TopDeps},
  1048. {overrides, [
  1049. {add, some_dep, [
  1050. {erl_opts, [{d, 'TEST'}]}
  1051. ]}
  1052. ]}],
  1053. rebar_test_utils:create_config(AppDir, RebarConfig),
  1054. rebar_test_utils:run_and_check(
  1055. Config, RebarConfig, ["ct", "--compile_only"], {ok, [{app, Name}, {dep, "some_dep"}], "test"}).
  1056. override_add_opts(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. RebarConfig = [
  1062. {erl_opts, [
  1063. warn_missing_spec
  1064. ]},
  1065. {overrides, [
  1066. {add, [
  1067. {erl_opts, [compressed]}
  1068. ]}
  1069. ]}
  1070. ],
  1071. rebar_test_utils:create_config(AppDir, RebarConfig),
  1072. rebar_test_utils:run_and_check(
  1073. Config, RebarConfig, ["compile"], {ok, [{app, Name}]}),
  1074. Path = filename:join([AppDir, "_build", "default", "lib", Name, "ebin"]),
  1075. code:add_patha(Path),
  1076. Mod = list_to_atom("not_a_real_src_" ++ Name),
  1077. true = lists:member(compressed, proplists:get_value(options, Mod:module_info(compile), [])),
  1078. true = lists:member(warn_missing_spec, proplists:get_value(options, Mod:module_info(compile), [])).
  1079. override_del_opts(Config) ->
  1080. AppDir = ?config(apps, Config),
  1081. Name = rebar_test_utils:create_random_name("app1_"),
  1082. Vsn = rebar_test_utils:create_random_vsn(),
  1083. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  1084. RebarConfig = [
  1085. {erl_opts, [
  1086. compressed,
  1087. warn_missing_spec
  1088. ]},
  1089. {overrides, [
  1090. {del, [
  1091. {erl_opts, [warn_missing_spec]}
  1092. ]}
  1093. ]}
  1094. ],
  1095. rebar_test_utils:create_config(AppDir, RebarConfig),
  1096. rebar_test_utils:run_and_check(
  1097. Config, RebarConfig, ["compile"], {ok, [{app, Name}]}),
  1098. Path = filename:join([AppDir, "_build", "default", "lib", Name, "ebin"]),
  1099. code:add_patha(Path),
  1100. Mod = list_to_atom("not_a_real_src_" ++ Name),
  1101. true = lists:member(compressed, proplists:get_value(options, Mod:module_info(compile), [])),
  1102. false = lists:member(warn_missing_spec, proplists:get_value(options, Mod:module_info(compile), [])).
  1103. profile_override_deps(Config) ->
  1104. Deps = rebar_test_utils:expand_deps(git, [{"some_dep", "0.0.1", [{"other_dep", "0.0.1", []}]}]),
  1105. TopDeps = rebar_test_utils:top_level_deps(Deps),
  1106. {SrcDeps, _} = rebar_test_utils:flat_deps(Deps),
  1107. mock_git_resource:mock([{deps, SrcDeps}]),
  1108. RebarConfig = [
  1109. {deps, TopDeps},
  1110. {profiles, [
  1111. {a, [
  1112. {overrides, [
  1113. {override, some_dep, [
  1114. {deps, []}
  1115. ]}
  1116. ]}
  1117. ]}
  1118. ]}],
  1119. rebar_test_utils:run_and_check(
  1120. Config, RebarConfig, ["as", "a", "compile"],
  1121. {ok, [{dep, "some_dep"},
  1122. {dep_not_exist, "other_dep"}]}
  1123. ).
  1124. profile_override_add_deps(Config) ->
  1125. Deps = rebar_test_utils:expand_deps(git, [{"some_dep", "0.0.1", [{"other_dep", "0.0.1", []}]}]),
  1126. TopDeps = rebar_test_utils:top_level_deps(Deps),
  1127. DepA = {dep_a, "0.0.1", {git, "http://site.com/dep_a.git", {tag, "0.0.1"}}},
  1128. DepB = {dep_b, "0.0.1", {git, "http://site.com/dep_b.git", {tag, "0.0.1"}}},
  1129. DepC = {dep_c, "0.0.1", {git, "http://site.com/dep_c.git", {tag, "0.0.1"}}},
  1130. {SrcDeps, _} = rebar_test_utils:flat_deps(Deps),
  1131. mock_git_resource:mock([{deps, [DepA, DepB, DepC | SrcDeps]}]),
  1132. RebarConfig = [
  1133. {deps, TopDeps},
  1134. {profiles, [
  1135. {a, [
  1136. {overrides, [
  1137. {add, some_dep, [
  1138. {deps, [DepA, DepB]}
  1139. ]},
  1140. {add, [
  1141. {deps, [DepC]}
  1142. ]}
  1143. ]}
  1144. ]}
  1145. ]}
  1146. ],
  1147. rebar_test_utils:run_and_check(
  1148. Config, RebarConfig, ["as", "a", "compile"],
  1149. {ok, [{dep, "some_dep"},
  1150. {dep, "other_dep"},
  1151. {dep, "dep_a"},
  1152. {dep, "dep_b"},
  1153. {dep, "dep_c"}]}
  1154. ).
  1155. profile_override_del_deps(Config) ->
  1156. Deps = rebar_test_utils:expand_deps(git, [{"some_dep", "0.0.1", [{"dep_a", "0.0.1", []},
  1157. {"dep_b", "0.0.1", []},
  1158. {"dep_c", "0.0.1", []}]},
  1159. {"other_dep", "0.0.1", [{"dep_c", "0.0.1", []},
  1160. {"dep_d", "0.0.1", []}]}]),
  1161. TopDeps = rebar_test_utils:top_level_deps(Deps),
  1162. DepA = {dep_a, "0.0.1", {git, "https://example.org/user/dep_a.git", {tag, "0.0.1"}}},
  1163. DepB = {dep_b, "0.0.1", {git, "https://example.org/user/dep_b.git", {tag, "0.0.1"}}},
  1164. DepC = {dep_c, "0.0.1", {git, "https://example.org/user/dep_c.git", {tag, "0.0.1"}}},
  1165. {SrcDeps, _} = rebar_test_utils:flat_deps(Deps),
  1166. mock_git_resource:mock([{deps, SrcDeps}]),
  1167. RebarConfig = [
  1168. {deps, TopDeps},
  1169. {profiles, [
  1170. {a, [
  1171. {overrides, [
  1172. {del, some_dep, [
  1173. {deps, [DepA, DepB]}
  1174. ]},
  1175. {del, [
  1176. {deps, [DepC]}
  1177. ]}
  1178. ]}
  1179. ]}
  1180. ]}
  1181. ],
  1182. rebar_test_utils:run_and_check(
  1183. Config, RebarConfig, ["as", "a", "compile"],
  1184. {ok, [{dep, "some_dep"},
  1185. {dep, "other_dep"},
  1186. {dep_not_exist, "dep_a"},
  1187. {dep_not_exist, "dep_b"},
  1188. {dep_not_exist, "dep_c"},
  1189. {dep, "dep_d"}]}
  1190. ).
  1191. profile_override_opts(Config) ->
  1192. AppDir = ?config(apps, Config),
  1193. Name = rebar_test_utils:create_random_name("app1_"),
  1194. Vsn = rebar_test_utils:create_random_vsn(),
  1195. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  1196. RebarConfig = [
  1197. {erl_opts, [
  1198. compressed,
  1199. warn_missing_spec
  1200. ]},
  1201. {profiles, [
  1202. {a, [
  1203. {overrides, [
  1204. {override, [
  1205. {erl_opts, [compressed]}
  1206. ]}
  1207. ]}
  1208. ]}
  1209. ]}
  1210. ],
  1211. rebar_test_utils:create_config(AppDir, RebarConfig),
  1212. rebar_test_utils:run_and_check(
  1213. Config, RebarConfig, ["as", "a", "compile"], {ok, [{app, Name}]}),
  1214. Path = filename:join([AppDir, "_build", "a", "lib", Name, "ebin"]),
  1215. code:add_patha(Path),
  1216. Mod = list_to_atom("not_a_real_src_" ++ Name),
  1217. true = lists:member(compressed, proplists:get_value(options, Mod:module_info(compile), [])),
  1218. false = lists:member(warn_missing_spec, proplists:get_value(options, Mod:module_info(compile), [])).
  1219. profile_override_add_opts(Config) ->
  1220. AppDir = ?config(apps, Config),
  1221. Name = rebar_test_utils:create_random_name("app1_"),
  1222. Vsn = rebar_test_utils:create_random_vsn(),
  1223. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  1224. RebarConfig = [
  1225. {erl_opts, [
  1226. warn_missing_spec
  1227. ]},
  1228. {profiles, [
  1229. {a, [
  1230. {overrides, [
  1231. {add, [
  1232. {erl_opts, [compressed]}
  1233. ]}
  1234. ]}
  1235. ]}
  1236. ]}
  1237. ],
  1238. rebar_test_utils:create_config(AppDir, RebarConfig),
  1239. rebar_test_utils:run_and_check(
  1240. Config, RebarConfig, ["as", "a", "compile"], {ok, [{app, Name}]}),
  1241. Path = filename:join([AppDir, "_build", "a", "lib", Name, "ebin"]),
  1242. code:add_patha(Path),
  1243. Mod = list_to_atom("not_a_real_src_" ++ Name),
  1244. true = lists:member(compressed, proplists:get_value(options, Mod:module_info(compile), [])),
  1245. true = lists:member(warn_missing_spec, proplists:get_value(options, Mod:module_info(compile), [])).
  1246. profile_override_del_opts(Config) ->
  1247. AppDir = ?config(apps, Config),
  1248. Name = rebar_test_utils:create_random_name("app1_"),
  1249. Vsn = rebar_test_utils:create_random_vsn(),
  1250. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  1251. RebarConfig = [
  1252. {erl_opts, [
  1253. compressed,
  1254. warn_missing_spec
  1255. ]},
  1256. {profiles, [
  1257. {a, [
  1258. {overrides, [
  1259. {del, [
  1260. {erl_opts, [warn_missing_spec]}
  1261. ]}
  1262. ]}
  1263. ]}
  1264. ]}
  1265. ],
  1266. rebar_test_utils:create_config(AppDir, RebarConfig),
  1267. rebar_test_utils:run_and_check(
  1268. Config, RebarConfig, ["as", "a", "compile"], {ok, [{app, Name}]}),
  1269. Path = filename:join([AppDir, "_build", "a", "lib", Name, "ebin"]),
  1270. code:add_patha(Path),
  1271. Mod = list_to_atom("not_a_real_src_" ++ Name),
  1272. true = lists:member(compressed, proplists:get_value(options, Mod:module_info(compile), [])),
  1273. false = lists:member(warn_missing_spec, proplists:get_value(options, Mod:module_info(compile), [])).
  1274. profile_deps(Config) ->
  1275. Deps = rebar_test_utils:expand_deps(git, [{"some_dep", "0.0.1", [{"other_dep", "0.0.1", []}]}]),
  1276. TopDeps = rebar_test_utils:top_level_deps(Deps),
  1277. {SrcDeps, _} = rebar_test_utils:flat_deps(Deps),
  1278. mock_git_resource:mock([{deps, SrcDeps}]),
  1279. RebarConfig = [
  1280. {deps, TopDeps},
  1281. {profiles, [{a, []}]}],
  1282. rebar_test_utils:run_and_check(
  1283. Config, RebarConfig, ["as", "a", "compile"],
  1284. {ok, [{dep, "some_dep"},{dep, "other_dep"}]}
  1285. ).
  1286. %% verify a deps prod profile is used
  1287. %% tested by checking prod hooks run and outputs to default profile dir for dep
  1288. %% and prod deps are installed for dep
  1289. deps_build_in_prod(Config) ->
  1290. AppDir = ?config(apps, Config),
  1291. Name = rebar_test_utils:create_random_name("app1_"),
  1292. Vsn = rebar_test_utils:create_random_vsn(),
  1293. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  1294. GitDeps = rebar_test_utils:expand_deps(git, [{"asdf", "1.0.0", []}]),
  1295. PkgName = rebar_test_utils:create_random_name("pkg1_"),
  1296. {SrcDeps, _} = rebar_test_utils:flat_deps(GitDeps),
  1297. mock_git_resource:mock([{deps, SrcDeps},
  1298. {config, [{profiles, [{prod, [{pre_hooks, [{compile, "echo whatsup > randomfile"}]},
  1299. {deps, [list_to_atom(PkgName)]}]}]}]}]),
  1300. mock_pkg_resource:mock([{pkgdeps, [{{iolist_to_binary(PkgName), <<"0.1.0">>}, []}]}]),
  1301. Deps = rebar_test_utils:top_level_deps(GitDeps),
  1302. RConfFile = rebar_test_utils:create_config(AppDir, [{deps, Deps}]),
  1303. {ok, RConf} = file:consult(RConfFile),
  1304. %% Build with deps.
  1305. rebar_test_utils:run_and_check(
  1306. Config, RConf, ["compile"],
  1307. {ok, [{app, Name}, {dep, "asdf", <<"1.0.0">>}, {dep, PkgName},
  1308. {file, filename:join([AppDir, "_build", "default", "lib", "asdf", "randomfile"])}]}
  1309. ).
  1310. %% verify that the proper include path is defined
  1311. %% according the erlang doc which states:
  1312. %% If the filename File is absolute (possibly after variable substitution),
  1313. %% the include file with that name is included. Otherwise, the specified file
  1314. %% is searched for in the following directories, and in this order:
  1315. %% * The current working directory
  1316. %% * The directory where the module is being compiled
  1317. %% * The directories given by the include option
  1318. include_file_relative_to_working_directory(Config) ->
  1319. AppDir = ?config(apps, Config),
  1320. Name = rebar_test_utils:create_random_name("app1_"),
  1321. Vsn = rebar_test_utils:create_random_vsn(),
  1322. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  1323. Src = <<"-module(test).\n"
  1324. "\n"
  1325. "-include(\"include/test.hrl\").\n"
  1326. "\n"
  1327. "test() -> ?TEST_MACRO.\n"
  1328. "\n">>,
  1329. Include = <<"-define(TEST_MACRO, test).\n">>,
  1330. ok = filelib:ensure_dir(filename:join([AppDir, "src", "dummy"])),
  1331. ok = file:write_file(filename:join([AppDir, "src", "test.erl"]), Src),
  1332. ok = filelib:ensure_dir(filename:join([AppDir, "include", "dummy"])),
  1333. ok = file:write_file(filename:join([AppDir, "include", "test.hrl"]), Include),
  1334. RebarConfig = [],
  1335. rebar_test_utils:run_and_check(Config, RebarConfig,
  1336. ["compile"],
  1337. {ok, [{app, Name}]}).
  1338. include_file_in_src(Config) ->
  1339. AppDir = ?config(apps, Config),
  1340. Name = rebar_test_utils:create_random_name("app1_"),
  1341. Vsn = rebar_test_utils:create_random_vsn(),
  1342. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  1343. Src = <<"-module(test).\n"
  1344. "\n"
  1345. "-include(\"test.hrl\").\n"
  1346. "\n"
  1347. "test() -> ?TEST_MACRO.\n"
  1348. "\n">>,
  1349. Include = <<"-define(TEST_MACRO, test).\n">>,
  1350. ok = filelib:ensure_dir(filename:join([AppDir, "src", "dummy"])),
  1351. ok = file:write_file(filename:join([AppDir, "src", "test.erl"]), Src),
  1352. ok = file:write_file(filename:join([AppDir, "src", "test.hrl"]), Include),
  1353. RebarConfig = [],
  1354. rebar_test_utils:run_and_check(Config, RebarConfig,
  1355. ["compile"],
  1356. {ok, [{app, Name}]}).
  1357. %% verify that the proper include path is defined
  1358. %% according the erlang doc which states:
  1359. %% If the filename File is absolute (possibly after variable substitution),
  1360. %% the include file with that name is included. Otherwise, the specified file
  1361. %% is searched for in the following directories, and in this order:
  1362. %% * The current working directory
  1363. %% * The directory where the module is being compiled
  1364. %% * The directories given by the include option
  1365. %%
  1366. %% This test ensures that things keep working when additional directories
  1367. %% are used for apps, such as the test/ directory within the test profile.
  1368. include_file_relative_to_working_directory_test(Config) ->
  1369. AppDir = ?config(apps, Config),
  1370. Name = rebar_test_utils:create_random_name("app1_"),
  1371. Vsn = rebar_test_utils:create_random_vsn(),
  1372. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  1373. Src = <<"-module(test).\n"
  1374. "\n"
  1375. "-include(\"include/test.hrl\").\n"
  1376. "\n"
  1377. "test() -> ?TEST_MACRO.\n"
  1378. "\n">>,
  1379. Include = <<"-define(TEST_MACRO, test).\n">>,
  1380. ok = filelib:ensure_dir(filename:join([AppDir, "src", "dummy"])),
  1381. ok = filelib:ensure_dir(filename:join([AppDir, "test", "dummy"])),
  1382. ok = file:write_file(filename:join([AppDir, "test", "test.erl"]), Src),
  1383. ok = filelib:ensure_dir(filename:join([AppDir, "include", "dummy"])),
  1384. ok = file:write_file(filename:join([AppDir, "include", "test.hrl"]), Include),
  1385. RebarConfig = [],
  1386. rebar_test_utils:run_and_check(Config, RebarConfig,
  1387. ["as", "test", "compile"],
  1388. {ok, [{app, Name}]}).
  1389. %% Same as `include_file_in_src/1' but using the `test/' directory
  1390. %% within the test profile.
  1391. include_file_in_src_test(Config) ->
  1392. AppDir = ?config(apps, Config),
  1393. Name = rebar_test_utils:create_random_name("app1_"),
  1394. Vsn = rebar_test_utils:create_random_vsn(),
  1395. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  1396. Src = <<"-module(test).\n"
  1397. "\n"
  1398. "-include(\"test.hrl\").\n"
  1399. "\n"
  1400. "test() -> ?TEST_MACRO.\n"
  1401. "\n">>,
  1402. Include = <<"-define(TEST_MACRO, test).\n">>,
  1403. ok = filelib:ensure_dir(filename:join([AppDir, "src", "dummy"])),
  1404. ok = filelib:ensure_dir(filename:join([AppDir, "test", "dummy"])),
  1405. ok = file:write_file(filename:join([AppDir, "test", "test.erl"]), Src),
  1406. ok = file:write_file(filename:join([AppDir, "src", "test.hrl"]), Include),
  1407. RebarConfig = [],
  1408. rebar_test_utils:run_and_check(Config, RebarConfig,
  1409. ["as", "test", "compile"],
  1410. {ok, [{app, Name}]}).
  1411. %% Same as `include_file_in_src_test/1' but using multiple top-level
  1412. %% apps as dependencies.
  1413. include_file_in_src_test_multiapp(Config) ->
  1414. Name1 = rebar_test_utils:create_random_name("app2_"),
  1415. Name2 = rebar_test_utils:create_random_name("app1_"),
  1416. AppDir1 = filename:join([?config(apps, Config), "lib", Name1]),
  1417. AppDir2 = filename:join([?config(apps, Config), "lib", Name2]),
  1418. Vsn = rebar_test_utils:create_random_vsn(),
  1419. rebar_test_utils:create_app(AppDir1, Name1, Vsn, [kernel, stdlib]),
  1420. rebar_test_utils:create_app(AppDir2, Name2, Vsn, [kernel, stdlib]),
  1421. Src = "-module(test).\n"
  1422. "\n"
  1423. "-include_lib(\"" ++ Name2 ++ "/include/test.hrl\").\n"
  1424. "\n"
  1425. "test() -> ?TEST_MACRO.\n"
  1426. "\n",
  1427. Include = <<"-define(TEST_MACRO, test).\n">>,
  1428. ok = filelib:ensure_dir(filename:join([AppDir1, "src", "dummy"])),
  1429. ok = filelib:ensure_dir(filename:join([AppDir1, "test", "dummy"])),
  1430. ok = filelib:ensure_dir(filename:join([AppDir2, "src", "dummy"])),
  1431. ok = filelib:ensure_dir(filename:join([AppDir2, "include", "dummy"])),
  1432. ok = file:write_file(filename:join([AppDir1, "test", "test.erl"]), Src),
  1433. ok = file:write_file(filename:join([AppDir2, "include", "test.hrl"]), Include),
  1434. RebarConfig = [],
  1435. rebar_test_utils:run_and_check(Config, RebarConfig,
  1436. ["as", "test", "compile"],
  1437. {ok, [{app, Name1}]}).
  1438. %% this test sets the env var, compiles, records the file last modified timestamp,
  1439. %% recompiles and compares the file last modified timestamp to ensure it hasn't
  1440. %% changed. this test should run on 19.x+
  1441. dont_recompile_when_erl_compiler_options_env_does_not_change(Config) ->
  1442. %% save existing env to restore after test
  1443. ExistingEnv = os:getenv("ERL_COMPILER_OPTIONS"),
  1444. AppDir = ?config(apps, Config),
  1445. Name = rebar_test_utils:create_random_name("erl_compiler_options_"),
  1446. Vsn = rebar_test_utils:create_random_vsn(),
  1447. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  1448. true = os:unsetenv("ERL_COMPILER_OPTIONS"),
  1449. true = os:putenv("ERL_COMPILER_OPTIONS", "[{d, some_macro}]"),
  1450. rebar_test_utils:run_and_check(Config, [], ["compile"], {ok, [{app, Name}]}),
  1451. EbinDir = filename:join([AppDir, "_build", "default", "lib", Name, "ebin"]),
  1452. {ok, Files} = rebar_utils:list_dir(EbinDir),
  1453. ModTime = [filelib:last_modified(filename:join([EbinDir, F]))
  1454. || F <- Files, filename:extension(F) == ".beam"],
  1455. timer:sleep(1000),
  1456. rebar_test_utils:run_and_check(Config, [], ["compile"], {ok, [{app, Name}]}),
  1457. {ok, NewFiles} = rebar_utils:list_dir(EbinDir),
  1458. NewModTime = [filelib:last_modified(filename:join([EbinDir, F]))
  1459. || F <- NewFiles, filename:extension(F) == ".beam"],
  1460. ?assert(ModTime == NewModTime),
  1461. %% restore existing env
  1462. case ExistingEnv of
  1463. false -> ok;
  1464. _ -> os:putenv("ERL_COMPILER_OPTIONS", ExistingEnv)
  1465. end.
  1466. %% this test compiles, records the file last modified timestamp, sets the env
  1467. %% var, recompiles and compares the file last modified timestamp to ensure it
  1468. %% has changed. this test should run on 19.x+
  1469. recompile_when_erl_compiler_options_env_changes(Config) ->
  1470. %% save existing env to restore after test
  1471. ExistingEnv = os:getenv("ERL_COMPILER_OPTIONS"),
  1472. AppDir = ?config(apps, Config),
  1473. Name = rebar_test_utils:create_random_name("erl_compiler_options_"),
  1474. Vsn = rebar_test_utils:create_random_vsn(),
  1475. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  1476. true = os:unsetenv("ERL_COMPILER_OPTIONS"),
  1477. rebar_test_utils:run_and_check(Config, [], ["compile"], {ok, [{app, Name}]}),
  1478. EbinDir = filename:join([AppDir, "_build", "default", "lib", Name, "ebin"]),
  1479. {ok, Files} = rebar_utils:list_dir(EbinDir),
  1480. ModTime = [filelib:last_modified(filename:join([EbinDir, F]))
  1481. || F <- Files, filename:extension(F) == ".beam"],
  1482. timer:sleep(1000),
  1483. true = os:putenv("ERL_COMPILER_OPTIONS", "[{d, some_macro}]"),
  1484. rebar_test_utils:run_and_check(Config, [], ["compile"], {ok, [{app, Name}]}),
  1485. {ok, NewFiles} = rebar_utils:list_dir(EbinDir),
  1486. NewModTime = [filelib:last_modified(filename:join([EbinDir, F]))
  1487. || F <- NewFiles, filename:extension(F) == ".beam"],
  1488. ?assert(ModTime =/= NewModTime),
  1489. %% restore existing env
  1490. case ExistingEnv of
  1491. false -> ok;
  1492. _ -> os:putenv("ERL_COMPILER_OPTIONS", ExistingEnv)
  1493. end.
  1494. %% this test sets the env var, compiles, records the file last modified
  1495. %% timestamp, recompiles and compares the file last modified timestamp to
  1496. %% ensure it has changed. this test should run on 18.x
  1497. always_recompile_when_erl_compiler_options_set(Config) ->
  1498. %% save existing env to restore after test
  1499. ExistingEnv = os:getenv("ERL_COMPILER_OPTIONS"),
  1500. AppDir = ?config(apps, Config),
  1501. Name = rebar_test_utils:create_random_name("erl_compiler_options_"),
  1502. Vsn = rebar_test_utils:create_random_vsn(),
  1503. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  1504. true = os:unsetenv("ERL_COMPILER_OPTIONS"),
  1505. true = os:putenv("ERL_COMPILER_OPTIONS", "[{d, some_macro}]"),
  1506. rebar_test_utils:run_and_check(Config, [], ["compile"], {ok, [{app, Name}]}),
  1507. EbinDir = filename:join([AppDir, "_build", "default", "lib", Name, "ebin"]),
  1508. {ok, Files} = rebar_utils:list_dir(EbinDir),
  1509. ModTime = [filelib:last_modified(filename:join([EbinDir, F]))
  1510. || F <- Files, filename:extension(F) == ".beam"],
  1511. timer:sleep(1000),
  1512. rebar_test_utils:run_and_check(Config, [], ["compile"], {ok, [{app, Name}]}),
  1513. {ok, NewFiles} = rebar_utils:list_dir(EbinDir),
  1514. NewModTime = [filelib:last_modified(filename:join([EbinDir, F]))
  1515. || F <- NewFiles, filename:extension(F) == ".beam"],
  1516. ?assert(ModTime =/= NewModTime),
  1517. %% restore existing env
  1518. case ExistingEnv of
  1519. false -> ok;
  1520. _ -> os:putenv("ERL_COMPILER_OPTIONS", ExistingEnv)
  1521. end.
  1522. recompile_when_parse_transform_inline_changes(Config) ->
  1523. AppDir = ?config(apps, Config),
  1524. Name = rebar_test_utils:create_random_name("parse_transform_inline_"),
  1525. Vsn = rebar_test_utils:create_random_vsn(),
  1526. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  1527. ok = filelib:ensure_dir(filename:join([AppDir, "src", "dummy"])),
  1528. ModSrc = <<"-module(example).\n"
  1529. "-export([foo/2]).\n"
  1530. "-compile([{parse_transform, example_parse_transform}]).\n"
  1531. "foo(_, _) -> ok.">>,
  1532. ok = file:write_file(filename:join([AppDir, "src", "example.erl"]),
  1533. ModSrc),
  1534. ParseTransform = <<"-module(example_parse_transform).\n"
  1535. "-export([parse_transform/2]).\n"
  1536. "parse_transform(AST, _) -> AST.\n">>,
  1537. ok = file:write_file(filename:join([AppDir, "src", "example_parse_transform.erl"]),
  1538. ParseTransform),
  1539. rebar_test_utils:run_and_check(Config, [], ["compile"], {ok, [{app, Name}]}),
  1540. EbinDir = filename:join([AppDir, "_build", "default", "lib", Name, "ebin"]),
  1541. {ok, Files} = rebar_utils:list_dir(EbinDir),
  1542. ModTime = [filelib:last_modified(filename:join([EbinDir, F]))
  1543. || F <- Files, filename:basename(F, ".beam") == "example"],
  1544. timer:sleep(1000),
  1545. NewParseTransform = <<"-module(example_parse_transform).\n"
  1546. "-export([parse_transform/2]).\n"
  1547. "parse_transform(AST, _) -> identity(AST).\n"
  1548. "identity(AST) -> AST.\n">>,
  1549. ok = file:write_file(filename:join([AppDir, "src", "example_parse_transform.erl"]),
  1550. NewParseTransform),
  1551. rebar_test_utils:run_and_check(Config, [], ["compile"], {ok, [{app, Name}]}),
  1552. {ok, NewFiles} = rebar_utils:list_dir(EbinDir),
  1553. NewModTime = [filelib:last_modified(filename:join([EbinDir, F]))
  1554. || F <- NewFiles, filename:basename(F, ".beam") == "example"],
  1555. ?assert(ModTime =/= NewModTime).
  1556. recompile_when_parse_transform_as_opt_changes(Config) ->
  1557. AppDir = ?config(apps, Config),
  1558. Name = rebar_test_utils:create_random_name("parse_transform_opt_"),
  1559. Vsn = rebar_test_utils:create_random_vsn(),
  1560. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  1561. ok = filelib:ensure_dir(filename:join([AppDir, "src", "dummy"])),
  1562. ModSrc = <<"-module(example).\n"
  1563. "-export([foo/2]).\n"
  1564. "foo(_, _) -> ok.">>,
  1565. ok = file:write_file(filename:join([AppDir, "src", "example.erl"]),
  1566. ModSrc),
  1567. ParseTransform = <<"-module(example_parse_transform).\n"
  1568. "-export([parse_transform/2]).\n"
  1569. "parse_transform(AST, _) -> AST.">>,
  1570. ok = file:write_file(filename:join([AppDir, "src", "example_parse_transform.erl"]),
  1571. ParseTransform),
  1572. RebarConfig = [{erl_opts, [{parse_transform, example_parse_transform}]}],
  1573. rebar_test_utils:run_and_check(Config, RebarConfig, ["compile"], {ok, [{app, Name}]}),
  1574. EbinDir = filename:join([AppDir, "_build", "default", "lib", Name, "ebin"]),
  1575. {ok, Files} = rebar_utils:list_dir(EbinDir),
  1576. ModTime = [filelib:last_modified(filename:join([EbinDir, F]))
  1577. || F <- Files, filename:basename(F, ".beam") == "example"],
  1578. timer:sleep(1000),
  1579. NewParseTransform = <<"-module(example_parse_transform).\n"
  1580. "-export([parse_transform/2]).\n"
  1581. "parse_transform(AST, _) -> identity(AST).\n"
  1582. "identity(AST) -> AST.">>,
  1583. ok = file:write_file(filename:join([AppDir, "src", "example_parse_transform.erl"]),
  1584. NewParseTransform),
  1585. rebar_test_utils:run_and_check(Config, RebarConfig, ["compile"], {ok, [{app, Name}]}),
  1586. {ok, NewFiles} = rebar_utils:list_dir(EbinDir),
  1587. NewModTime = [filelib:last_modified(filename:join([EbinDir, F]))
  1588. || F <- NewFiles, filename:basename(F, ".beam") == "example"],
  1589. ?assert(ModTime =/= NewModTime).
  1590. recursive(Config) ->
  1591. AppDir = ?config(apps, Config),
  1592. Name = rebar_test_utils:create_random_name("app1_"),
  1593. Vsn = rebar_test_utils:create_random_vsn(),
  1594. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  1595. rebar_test_utils:write_src_file(filename:join(AppDir,src),"rec.erl"),
  1596. rebar_test_utils:run_and_check(Config, [], ["compile"], {ok, [{app, Name}]}),
  1597. EbinDir = filename:join([AppDir, "_build", "default", "lib", Name, "ebin"]),
  1598. {ok, Files} = rebar_utils:list_dir(EbinDir),
  1599. ?assert(lists:member("rec.beam",Files)).
  1600. no_recursive(Config) ->
  1601. AppDir = ?config(apps, Config),
  1602. Name = rebar_test_utils:create_random_name("app1_"),
  1603. Vsn = rebar_test_utils:create_random_vsn(),
  1604. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  1605. rebar_test_utils:write_src_file(filename:join(AppDir,src),"rec.erl"),
  1606. RebarConfig1 = [{erlc_compiler,[{recursive,false}]}],
  1607. rebar_test_utils:run_and_check(Config, RebarConfig1, ["compile"],
  1608. {ok, [{app, Name}]}),
  1609. EbinDir = filename:join([AppDir, "_build", "default", "lib", Name, "ebin"]),
  1610. {ok, Files1} = rebar_utils:list_dir(EbinDir),
  1611. ?assert(false==lists:member("rec.beam",Files1)),
  1612. RebarConfig2 = [{src_dirs,[{"src",[{recursive,false}]}]}],
  1613. rebar_test_utils:run_and_check(Config, RebarConfig2, ["compile"],
  1614. {ok, [{app, Name}]}),
  1615. {ok, Files2} = rebar_utils:list_dir(EbinDir),
  1616. ?assert(false==lists:member("rec.beam",Files2)),
  1617. ok.
  1618. regex_filter_skip(Config) ->
  1619. AppDir = ?config(apps, Config),
  1620. Name = rebar_test_utils:create_random_name("regex_skip"),
  1621. Vsn = rebar_test_utils:create_random_vsn(),
  1622. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  1623. rebar_test_utils:write_src_file(filename:join(AppDir,src),"._rec.erl"),
  1624. Expected = filename:join([AppDir, "_build", "default", "lib", Name, "ebin","._rec.beam"]),
  1625. RebarConfig = [],
  1626. try
  1627. rebar_test_utils:run_and_check(Config, RebarConfig, ["compile"],
  1628. {ok, [{file, Expected}]}),
  1629. throw(should_not_be_found)
  1630. catch
  1631. %% the file was not found, as desired!
  1632. error:{assertion_failed,_} -> %% OTP =< 17
  1633. ok;
  1634. error:{assert,_} -> %% OTP >= 18
  1635. ok
  1636. end.
  1637. regex_filter_regression(Config) ->
  1638. AppDir = ?config(apps, Config),
  1639. Name = rebar_test_utils:create_random_name("regex_regression"),
  1640. Vsn = rebar_test_utils:create_random_vsn(),
  1641. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  1642. rebar_test_utils:write_src_file(filename:join(AppDir,src),"r_f.erl"),
  1643. Expected = filename:join([AppDir, "_build", "default", "lib", Name, "ebin","r_f.beam"]),
  1644. RebarConfig = [],
  1645. rebar_test_utils:run_and_check(Config, RebarConfig, ["compile"],
  1646. {ok, [{file, Expected}]}),
  1647. ok.