您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

1453 行
58 KiB

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