Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

448 Zeilen
17 KiB

vor 10 Jahren
vor 10 Jahren
vor 10 Jahren
vor 10 Jahren
vor 10 Jahren
vor 10 Jahren
vor 10 Jahren
vor 10 Jahren
vor 10 Jahren
vor 10 Jahren
vor 10 Jahren
vor 10 Jahren
vor 10 Jahren
vor 10 Jahren
vor 10 Jahren
  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. all/0,
  8. build_basic_app/1,
  9. build_release_apps/1,
  10. build_checkout_apps/1,
  11. build_checkout_deps/1,
  12. build_all_srcdirs/1,
  13. recompile_when_hrl_changes/1,
  14. recompile_when_opts_change/1,
  15. dont_recompile_when_opts_dont_change/1,
  16. dont_recompile_yrl_or_xrl/1,
  17. deps_in_path/1,
  18. delete_beam_if_source_deleted/1,
  19. checkout_priority/1,
  20. compile_plugins/1,
  21. highest_version_of_pkg_dep/1]).
  22. -include_lib("common_test/include/ct.hrl").
  23. -include_lib("eunit/include/eunit.hrl").
  24. -include_lib("kernel/include/file.hrl").
  25. suite() ->
  26. [].
  27. init_per_suite(Config) ->
  28. Config.
  29. end_per_suite(_Config) ->
  30. ok.
  31. init_per_testcase(_, Config) ->
  32. rebar_test_utils:init_rebar_state(Config).
  33. end_per_testcase(_, _Config) ->
  34. catch meck:unload().
  35. all() ->
  36. [build_basic_app, build_release_apps,
  37. build_checkout_apps, build_checkout_deps,
  38. build_all_srcdirs, recompile_when_hrl_changes,
  39. recompile_when_opts_change, dont_recompile_when_opts_dont_change,
  40. dont_recompile_yrl_or_xrl, delete_beam_if_source_deleted,
  41. deps_in_path, checkout_priority, compile_plugins, highest_version_of_pkg_dep].
  42. build_basic_app(Config) ->
  43. AppDir = ?config(apps, Config),
  44. Name = rebar_test_utils:create_random_name("app1_"),
  45. Vsn = rebar_test_utils:create_random_vsn(),
  46. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  47. rebar_test_utils:run_and_check(Config, [], ["compile"], {ok, [{app, Name}]}).
  48. build_release_apps(Config) ->
  49. AppDir = ?config(apps, Config),
  50. Name1 = rebar_test_utils:create_random_name("relapp1_"),
  51. Vsn1 = rebar_test_utils:create_random_vsn(),
  52. rebar_test_utils:create_app(filename:join([AppDir,Name1]), Name1, Vsn1, [kernel, stdlib]),
  53. Name2 = rebar_test_utils:create_random_name("relapp2_"),
  54. Vsn2 = rebar_test_utils:create_random_vsn(),
  55. rebar_test_utils:create_app(filename:join([AppDir,Name2]), Name2, Vsn2, [kernel, stdlib]),
  56. rebar_test_utils:run_and_check(
  57. Config, [], ["compile"],
  58. {ok, [{app, Name1}, {app, Name2}]}
  59. ).
  60. build_checkout_apps(Config) ->
  61. AppDir = ?config(apps, Config),
  62. CheckoutsDir = ?config(checkouts, Config),
  63. Name1 = rebar_test_utils:create_random_name("checkapp1_"),
  64. Vsn1 = rebar_test_utils:create_random_vsn(),
  65. rebar_test_utils:create_app(filename:join([AppDir,Name1]), Name1, Vsn1, [kernel, stdlib]),
  66. Name2 = rebar_test_utils:create_random_name("checkapp2_"),
  67. Vsn2 = rebar_test_utils:create_random_vsn(),
  68. rebar_test_utils:create_app(filename:join([CheckoutsDir,Name2]), Name2, Vsn2, [kernel, stdlib]),
  69. rebar_test_utils:run_and_check(
  70. Config, [], ["compile"],
  71. {ok, [{app, Name1}, {checkout, Name2}]}
  72. ).
  73. build_checkout_deps(Config) ->
  74. AppDir = ?config(apps, Config),
  75. CheckoutsDir = ?config(checkouts, Config),
  76. DepsDir = filename:join([AppDir, "_build", "default", "lib"]),
  77. Name1 = rebar_test_utils:create_random_name("checkapp1_"),
  78. Vsn1 = rebar_test_utils:create_random_vsn(),
  79. rebar_test_utils:create_app(filename:join([AppDir,Name1]), Name1, Vsn1, [kernel, stdlib]),
  80. Name2 = rebar_test_utils:create_random_name("checkapp2_"),
  81. Vsn2 = rebar_test_utils:create_random_vsn(),
  82. rebar_test_utils:create_app(filename:join([CheckoutsDir,Name2]), Name2, Vsn2, [kernel, stdlib]),
  83. rebar_test_utils:create_app(filename:join([DepsDir,Name2]), Name2, Vsn1, [kernel, stdlib]),
  84. Deps = [{list_to_atom(Name2), Vsn2, {git, "", ""}}],
  85. {ok, RebarConfig} = file:consult(rebar_test_utils:create_config(AppDir, [{deps, Deps}])),
  86. rebar_test_utils:run_and_check(
  87. Config, RebarConfig, ["compile"],
  88. {ok, [{app, Name1}, {checkout, Name2}]}
  89. ),
  90. ok = application:load(list_to_atom(Name2)),
  91. Loaded = application:loaded_applications(),
  92. {_, _, Vsn2} = lists:keyfind(list_to_atom(Name2), 1, Loaded).
  93. build_all_srcdirs(Config) ->
  94. AppDir = ?config(apps, Config),
  95. RebarConfig = [{erl_opts, [{src_dirs, ["src", "extra"]}]}],
  96. Name = rebar_test_utils:create_random_name("app1_"),
  97. Vsn = rebar_test_utils:create_random_vsn(),
  98. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  99. ExtraSrc = <<"-module(extra_src).\n"
  100. "-export([ok/0]).\n"
  101. "ok() -> ok.\n">>,
  102. ok = filelib:ensure_dir(filename:join([AppDir, "extra", "dummy"])),
  103. ok = file:write_file(filename:join([AppDir, "extra", "extra_src.erl"]), ExtraSrc),
  104. rebar_test_utils:run_and_check(Config, RebarConfig, ["compile"], {ok, [{app, Name}]}),
  105. %% check a beam corresponding to the src in the extra src_dir exists in ebin
  106. EbinDir = filename:join([AppDir, "_build", "default", "lib", Name, "ebin"]),
  107. true = filelib:is_file(filename:join([EbinDir, "extra_src.beam"])),
  108. %% check the extra src_dir was linked into the _build dir
  109. true = filelib:is_dir(filename:join([AppDir, "_build", "default", "lib", Name, "extra"])).
  110. recompile_when_hrl_changes(Config) ->
  111. AppDir = ?config(apps, Config),
  112. Name = rebar_test_utils:create_random_name("app1_"),
  113. Vsn = rebar_test_utils:create_random_vsn(),
  114. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  115. ExtraSrc = <<"-module(test_header_include).\n"
  116. "-export([main/0]).\n"
  117. "-include(\"test_header_include.hrl\").\n"
  118. "main() -> ?SOME_DEFINE.\n">>,
  119. ExtraHeader = <<"-define(SOME_DEFINE, true).\n">>,
  120. HeaderFile = filename:join([AppDir, "src", "test_header_include.hrl"]),
  121. ok = file:write_file(filename:join([AppDir, "src", "test_header_include.erl"]), ExtraSrc),
  122. ok = file:write_file(HeaderFile, ExtraHeader),
  123. rebar_test_utils:run_and_check(Config, [], ["compile"], {ok, [{app, Name}]}),
  124. EbinDir = filename:join([AppDir, "_build", "default", "lib", Name, "ebin"]),
  125. {ok, Files} = file:list_dir(EbinDir),
  126. ModTime = [filelib:last_modified(filename:join([EbinDir, F]))
  127. || F <- Files, filename:extension(F) == ".beam"],
  128. timer:sleep(1000),
  129. os:cmd("touch " ++ HeaderFile),
  130. rebar_test_utils:run_and_check(Config, [], ["compile"], {ok, [{app, Name}]}),
  131. {ok, NewFiles} = file:list_dir(EbinDir),
  132. NewModTime = [filelib:last_modified(filename:join([EbinDir, F]))
  133. || F <- NewFiles, filename:extension(F) == ".beam"],
  134. ?assert(ModTime =/= NewModTime).
  135. recompile_when_opts_change(Config) ->
  136. AppDir = ?config(apps, Config),
  137. Name = rebar_test_utils:create_random_name("app1_"),
  138. Vsn = rebar_test_utils:create_random_vsn(),
  139. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  140. rebar_test_utils:run_and_check(Config, [], ["compile"], {ok, [{app, Name}]}),
  141. EbinDir = filename:join([AppDir, "_build", "default", "lib", Name, "ebin"]),
  142. {ok, Files} = file:list_dir(EbinDir),
  143. ModTime = [filelib:last_modified(filename:join([EbinDir, F]))
  144. || F <- Files, filename:extension(F) == ".beam"],
  145. timer:sleep(1000),
  146. rebar_test_utils:create_config(AppDir, [{erl_opts, [{d, some_define}]}]),
  147. rebar_test_utils:run_and_check(Config, [], ["compile"], {ok, [{app, Name}]}),
  148. {ok, NewFiles} = file:list_dir(EbinDir),
  149. NewModTime = [filelib:last_modified(filename:join([EbinDir, F]))
  150. || F <- NewFiles, filename:extension(F) == ".beam"],
  151. ?assert(ModTime =/= NewModTime).
  152. dont_recompile_when_opts_dont_change(Config) ->
  153. AppDir = ?config(apps, Config),
  154. Name = rebar_test_utils:create_random_name("app1_"),
  155. Vsn = rebar_test_utils:create_random_vsn(),
  156. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  157. rebar_test_utils:run_and_check(Config, [], ["compile"], {ok, [{app, Name}]}),
  158. EbinDir = filename:join([AppDir, "_build", "default", "lib", Name, "ebin"]),
  159. {ok, Files} = file:list_dir(EbinDir),
  160. ModTime = [filelib:last_modified(filename:join([EbinDir, F]))
  161. || F <- Files, filename:extension(F) == ".beam"],
  162. timer:sleep(1000),
  163. rebar_test_utils:run_and_check(Config, [], ["compile"], {ok, [{app, Name}]}),
  164. {ok, NewFiles} = file:list_dir(EbinDir),
  165. NewModTime = [filelib:last_modified(filename:join([EbinDir, F]))
  166. || F <- NewFiles, filename:extension(F) == ".beam"],
  167. ?assert(ModTime == NewModTime).
  168. dont_recompile_yrl_or_xrl(Config) ->
  169. AppDir = ?config(apps, Config),
  170. Name = rebar_test_utils:create_random_name("app1_"),
  171. Vsn = rebar_test_utils:create_random_vsn(),
  172. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  173. Xrl = filename:join([AppDir, "src", "not_a_real_xrl_" ++ Name ++ ".xrl"]),
  174. ok = filelib:ensure_dir(Xrl),
  175. XrlBody =
  176. "Definitions."
  177. "\n\n"
  178. "D = [0-9]"
  179. "\n\n"
  180. "Rules."
  181. "\n\n"
  182. "{D}+ :"
  183. " {token,{integer,TokenLine,list_to_integer(TokenChars)}}."
  184. "\n\n"
  185. "{D}+\\.{D}+((E|e)(\\+|\\-)?{D}+)? :"
  186. " {token,{float,TokenLine,list_to_float(TokenChars)}}."
  187. "\n\n"
  188. "Erlang code.",
  189. ok = ec_file:write(Xrl, XrlBody),
  190. XrlBeam = filename:join([AppDir, "ebin", filename:basename(Xrl, ".xrl") ++ ".beam"]),
  191. rebar_test_utils:run_and_check(Config, [], ["compile"], {ok, [{app, Name}]}),
  192. ModTime = filelib:last_modified(XrlBeam),
  193. timer:sleep(1000),
  194. rebar_test_utils:run_and_check(Config, [], ["compile"], {ok, [{app, Name}]}),
  195. NewModTime = filelib:last_modified(XrlBeam),
  196. ?assert(ModTime == NewModTime).
  197. delete_beam_if_source_deleted(Config) ->
  198. AppDir = ?config(apps, Config),
  199. Name = rebar_test_utils:create_random_name("app1_"),
  200. Vsn = rebar_test_utils:create_random_vsn(),
  201. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  202. rebar_test_utils:run_and_check(Config, [], ["compile"], {ok, [{app, Name}]}),
  203. EbinDir = filename:join([AppDir, "_build", "default", "lib", Name, "ebin"]),
  204. SrcDir = filename:join([AppDir, "_build", "default", "lib", Name, "src"]),
  205. ?assert(filelib:is_regular(filename:join(EbinDir, "not_a_real_src_" ++ Name ++ ".beam"))),
  206. file:delete(filename:join(SrcDir, "not_a_real_src_" ++ Name ++ ".erl")),
  207. rebar_test_utils:run_and_check(Config, [], ["compile"], {ok, [{app, Name}]}),
  208. ?assertNot(filelib:is_regular(filename:join(EbinDir, "not_a_real_src_" ++ Name ++ ".beam"))).
  209. deps_in_path(Config) ->
  210. AppDir = ?config(apps, Config),
  211. StartPaths = code:get_path(),
  212. Name = rebar_test_utils:create_random_name("app1_"),
  213. Vsn = rebar_test_utils:create_random_vsn(),
  214. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  215. DepName = rebar_test_utils:create_random_name("dep1_"),
  216. PkgName = rebar_test_utils:create_random_name("pkg1_"),
  217. mock_git_resource:mock([]),
  218. mock_pkg_resource:mock([
  219. {pkgdeps, [{{iolist_to_binary(PkgName), iolist_to_binary(Vsn)}, []}]}
  220. ]),
  221. RConfFile = rebar_test_utils:create_config(AppDir, [{deps, [
  222. {list_to_atom(DepName), {git, "http://site.com/user/"++DepName++".git", {tag, Vsn}}},
  223. {list_to_atom(PkgName), Vsn}
  224. ]}]),
  225. {ok, RConf} = file:consult(RConfFile),
  226. %% Make sure apps we look for are not visible
  227. %% Hope not to find src name
  228. ?assertEqual([], [Path || Path <- code:get_path(),
  229. {match, _} <- [re:run(Path, DepName)]]),
  230. %% Hope not to find pkg name in there
  231. ?assertEqual([], [Path || Path <- code:get_path(),
  232. {match, _} <- [re:run(Path, PkgName)]]),
  233. %% Build things
  234. rebar_test_utils:run_and_check(
  235. Config, RConf, ["compile"],
  236. {ok, [{app, Name}, {dep, DepName}, {dep, PkgName}]}
  237. ),
  238. %% Find src name in there
  239. ?assertNotEqual([], [Path || Path <- code:get_path(),
  240. {match, _} <- [re:run(Path, DepName)]]),
  241. %% find pkg name in there
  242. ?assertNotEqual([], [Path || Path <- code:get_path(),
  243. {match, _} <- [re:run(Path, PkgName)]]),
  244. code:set_path(StartPaths),
  245. %% Make sure apps we look for are not visible again
  246. %% Hope not to find src name
  247. ?assertEqual([], [Path || Path <- code:get_path(),
  248. {match, _} <- [re:run(Path, DepName)]]),
  249. %% Hope not to find pkg name in there
  250. ?assertEqual([], [Path || Path <- code:get_path(),
  251. {match, _} <- [re:run(Path, PkgName)]]),
  252. %% Rebuild
  253. rebar_test_utils:run_and_check(
  254. Config, RConf, ["compile"],
  255. {ok, [{app, Name}, {dep, DepName}, {dep, PkgName}]}
  256. ),
  257. %% Find src name in there
  258. ?assertNotEqual([], [Path || Path <- code:get_path(),
  259. {match, _} <- [re:run(Path, DepName)]]),
  260. %% find pkg name in there
  261. ?assertNotEqual([], [Path || Path <- code:get_path(),
  262. {match, _} <- [re:run(Path, PkgName)]]).
  263. checkout_priority(Config) ->
  264. AppDir = ?config(apps, Config),
  265. CheckoutsDir = ?config(checkouts, Config),
  266. StartPaths = code:get_path(),
  267. Name = rebar_test_utils:create_random_name("app1_"),
  268. Vsn = rebar_test_utils:create_random_vsn(),
  269. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  270. DepName = rebar_test_utils:create_random_name("dep1_"),
  271. PkgName = rebar_test_utils:create_random_name("pkg1_"),
  272. mock_git_resource:mock([]),
  273. mock_pkg_resource:mock([
  274. {pkgdeps, [{{iolist_to_binary(PkgName), iolist_to_binary(Vsn)}, []}]}
  275. ]),
  276. RConfFile = rebar_test_utils:create_config(AppDir, [{deps, [
  277. {list_to_atom(DepName), {git, "http://site.com/user/"++DepName++".git", {tag, Vsn}}},
  278. {list_to_atom(PkgName), Vsn}
  279. ]}]),
  280. {ok, RConf} = file:consult(RConfFile),
  281. %% Build with deps.
  282. rebar_test_utils:run_and_check(
  283. Config, RConf, ["compile"],
  284. {ok, [{app, Name}, {dep, DepName}, {dep, PkgName}]}
  285. ),
  286. %% Build two checkout apps similar to dependencies to be fetched,
  287. %% but on a different version
  288. Vsn2 = rebar_test_utils:create_random_vsn(),
  289. rebar_test_utils:create_app(filename:join([CheckoutsDir,DepName]), DepName, Vsn2, [kernel, stdlib]),
  290. rebar_test_utils:create_app(filename:join([CheckoutsDir,PkgName]), PkgName, Vsn2, [kernel, stdlib]),
  291. %% Rebuild and make sure the checkout apps are in path
  292. code:set_path(StartPaths),
  293. rebar_test_utils:run_and_check(
  294. Config, RConf, ["compile"],
  295. {ok, [{app, Name}, {checkout, DepName}, {checkout, PkgName}]}
  296. ),
  297. [DepPath] = [Path || Path <- code:get_path(),
  298. {match, _} <- [re:run(Path, DepName)]],
  299. [PkgPath] = [Path || Path <- code:get_path(),
  300. {match, _} <- [re:run(Path, PkgName)]],
  301. {ok, [DepApp]} = file:consult(filename:join([DepPath, DepName ++ ".app"])),
  302. {ok, [PkgApp]} = file:consult(filename:join([PkgPath, PkgName ++ ".app"])),
  303. {application, _, DepProps} = DepApp,
  304. {application, _, PkgProps} = PkgApp,
  305. ?assertEqual(Vsn2, proplists:get_value(vsn, DepProps)),
  306. ?assertEqual(Vsn2, proplists:get_value(vsn, PkgProps)).
  307. %% Tests that compiling a project installs and compiles the plugins of deps
  308. compile_plugins(Config) ->
  309. AppDir = ?config(apps, Config),
  310. Name = rebar_test_utils:create_random_name("app1_"),
  311. Vsn = rebar_test_utils:create_random_vsn(),
  312. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  313. DepName = rebar_test_utils:create_random_name("dep1_"),
  314. PluginName = rebar_test_utils:create_random_name("plugin1_"),
  315. mock_git_resource:mock([{config, [{plugins, [
  316. {list_to_atom(PluginName), Vsn}
  317. ]}]}]),
  318. mock_pkg_resource:mock([
  319. {pkgdeps, [{{iolist_to_binary(PluginName), iolist_to_binary(Vsn)}, []}]}
  320. ]),
  321. RConfFile =
  322. rebar_test_utils:create_config(AppDir,
  323. [{deps, [
  324. {list_to_atom(DepName), {git, "http://site.com/user/"++DepName++".git", {tag, Vsn}}}
  325. ]}]),
  326. {ok, RConf} = file:consult(RConfFile),
  327. %% Build with deps.
  328. rebar_test_utils:run_and_check(
  329. Config, RConf, ["compile"],
  330. {ok, [{app, Name}, {plugin, PluginName}, {dep, DepName}]}
  331. ).
  332. highest_version_of_pkg_dep(Config) ->
  333. AppDir = ?config(apps, Config),
  334. Name = rebar_test_utils:create_random_name("app1_"),
  335. Vsn = rebar_test_utils:create_random_vsn(),
  336. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  337. PkgName = rebar_test_utils:create_random_name("pkg1_"),
  338. mock_git_resource:mock([]),
  339. mock_pkg_resource:mock([
  340. {pkgdeps, [{{iolist_to_binary(PkgName), <<"0.1.0">>}, []},
  341. {{iolist_to_binary(PkgName), <<"0.0.1">>}, []},
  342. {{iolist_to_binary(PkgName), <<"0.1.3">>}, []},
  343. {{iolist_to_binary(PkgName), <<"0.1.1">>}, []}]}
  344. ]),
  345. RConfFile = rebar_test_utils:create_config(AppDir, [{deps, [list_to_atom(PkgName)]}]),
  346. {ok, RConf} = file:consult(RConfFile),
  347. %% Build with deps.
  348. rebar_test_utils:run_and_check(
  349. Config, RConf, ["compile"],
  350. {ok, [{app, Name}, {dep, PkgName, <<"0.1.3">>}]}
  351. ).