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

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