Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

422 rindas
16 KiB

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