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

1692 行
68 KiB

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