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

334 行
12 KiB

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