Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

1753 wiersze
71 KiB

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