You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1788 line
72 KiB

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