Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

1704 rader
69 KiB

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