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

451 wiersze
17 KiB

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