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

331 行
13 KiB

  1. -module(rebar_plugins_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. compile_plugins/1,
  9. compile_global_plugins/1,
  10. complex_plugins/1,
  11. list/1,
  12. upgrade/1,
  13. sub_app_plugins/1,
  14. sub_app_plugin_overrides/1,
  15. project_plugins/1]).
  16. -include_lib("common_test/include/ct.hrl").
  17. -include_lib("eunit/include/eunit.hrl").
  18. -include_lib("kernel/include/file.hrl").
  19. suite() ->
  20. [].
  21. init_per_suite(Config) ->
  22. Config.
  23. end_per_suite(_Config) ->
  24. ok.
  25. init_per_testcase(_, Config) ->
  26. rebar_test_utils:init_rebar_state(Config).
  27. end_per_testcase(_, _Config) ->
  28. catch meck:unload().
  29. all() ->
  30. [compile_plugins, compile_global_plugins, complex_plugins, list, upgrade, sub_app_plugins, sub_app_plugin_overrides, project_plugins].
  31. %% Tests that compiling a project installs and compiles the plugins of deps
  32. compile_plugins(Config) ->
  33. AppDir = ?config(apps, Config),
  34. Name = rebar_test_utils:create_random_name("app1_"),
  35. Vsn = rebar_test_utils:create_random_vsn(),
  36. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  37. DepName = rebar_test_utils:create_random_name("dep1_"),
  38. PluginName = rebar_test_utils:create_random_name("plugin1_"),
  39. Plugins = rebar_test_utils:expand_deps(git, [{PluginName, Vsn, []}]),
  40. {SrcDeps, _} = rebar_test_utils:flat_deps(Plugins),
  41. mock_git_resource:mock([{deps, SrcDeps}]),
  42. mock_pkg_resource:mock([{pkgdeps, [{{list_to_binary(DepName), list_to_binary(Vsn)}, []}]},
  43. {config, [{plugins, [
  44. {list_to_atom(PluginName),
  45. {git, "http://site.com/user/"++PluginName++".git",
  46. {tag, Vsn}}}]}]}]),
  47. RConfFile =
  48. rebar_test_utils:create_config(AppDir,
  49. [{deps, [
  50. list_to_atom(DepName)
  51. ]}]),
  52. {ok, RConf} = file:consult(RConfFile),
  53. %% Build with deps.
  54. rebar_test_utils:run_and_check(
  55. Config, RConf, ["compile"],
  56. {ok, [{app, Name}, {plugin, PluginName}, {dep, DepName}]}
  57. ).
  58. %% Tests that compiling a project installs and compiles the global plugins
  59. compile_global_plugins(Config) ->
  60. AppDir = ?config(apps, Config),
  61. GlobalDir = filename:join(AppDir, "global"),
  62. GlobalConfigDir = filename:join([GlobalDir, ".config", "rebar3"]),
  63. GlobalConfig = filename:join([GlobalDir, ".config", "rebar3", "rebar.config"]),
  64. meck:new(rebar_dir, [passthrough]),
  65. meck:expect(rebar_dir, global_config, fun() -> GlobalConfig end),
  66. meck:expect(rebar_dir, global_cache_dir, fun(_) -> GlobalDir end),
  67. Name = rebar_test_utils:create_random_name("app1_"),
  68. Vsn = rebar_test_utils:create_random_vsn(),
  69. Vsn2 = rebar_test_utils:create_random_vsn(),
  70. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  71. DepName = rebar_test_utils:create_random_name("dep1_"),
  72. PluginName = rebar_test_utils:create_random_name("plugin1_"),
  73. mock_git_resource:mock([{deps, [{list_to_atom(PluginName), Vsn},
  74. {list_to_atom(PluginName), Vsn2},
  75. {{iolist_to_binary(DepName), iolist_to_binary(Vsn)}, []}]}]),
  76. rebar_test_utils:create_config(GlobalConfigDir,
  77. [{plugins, [
  78. {list_to_atom(PluginName), {git, "http://site.com/user/"++PluginName++".git", {tag, Vsn}}}
  79. ]}]),
  80. RConfFile =
  81. rebar_test_utils:create_config(AppDir,
  82. [{deps, [
  83. {list_to_atom(DepName), {git, "http://site.com/user/"++DepName++".git", {tag, Vsn}}}
  84. ]},
  85. {plugins, [
  86. {list_to_atom(PluginName), {git, "http://site.com/user/"++PluginName++".git", {tag, Vsn2}}}
  87. ]}]),
  88. {ok, RConf} = file:consult(RConfFile),
  89. %% Runs global plugin install
  90. rebar3:init_config(),
  91. %% Build with deps.
  92. rebar_test_utils:run_and_check(
  93. Config, RConf, ["compile"],
  94. {ok, [{app, Name},
  95. {global_plugin, PluginName, Vsn},
  96. {plugin, PluginName, Vsn2},
  97. {dep, DepName}]}
  98. ),
  99. meck:unload(rebar_dir).
  100. %% Tests installing of plugin with transitive deps
  101. complex_plugins(Config) ->
  102. AppDir = ?config(apps, Config),
  103. meck:new(rebar_dir, [passthrough]),
  104. Name = rebar_test_utils:create_random_name("app1_"),
  105. Vsn = rebar_test_utils:create_random_vsn(),
  106. Vsn2 = rebar_test_utils:create_random_vsn(),
  107. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  108. DepName = rebar_test_utils:create_random_name("dep1_"),
  109. DepName2 = rebar_test_utils:create_random_name("dep2_"),
  110. DepName3 = rebar_test_utils:create_random_name("dep3_"),
  111. PluginName = rebar_test_utils:create_random_name("plugin1_"),
  112. Deps = rebar_test_utils:expand_deps(git, [{PluginName, Vsn2, [{DepName2, Vsn,
  113. [{DepName3, Vsn, []}]}]}
  114. ,{DepName, Vsn, []}]),
  115. {SrcDeps, _} = rebar_test_utils:flat_deps(Deps),
  116. mock_git_resource:mock([{deps, SrcDeps}]),
  117. RConfFile =
  118. rebar_test_utils:create_config(AppDir,
  119. [{deps, [
  120. {list_to_atom(DepName), {git, "http://site.com/user/"++DepName++".git", {tag, Vsn}}}
  121. ]},
  122. {plugins, [
  123. {list_to_atom(PluginName), {git, "http://site.com/user/"++PluginName++".git", {tag, Vsn2}}}
  124. ]}]),
  125. {ok, RConf} = file:consult(RConfFile),
  126. %% Build with deps.
  127. rebar_test_utils:run_and_check(
  128. Config, RConf, ["compile"],
  129. {ok, [{app, Name},
  130. {plugin, PluginName, Vsn2},
  131. {plugin, DepName2},
  132. {plugin, DepName3},
  133. {dep, DepName}]}
  134. ),
  135. meck:unload(rebar_dir).
  136. list(Config) ->
  137. rebar_test_utils:run_and_check(
  138. Config, [], ["plugins", "list"],
  139. {ok, []}
  140. ).
  141. upgrade(Config) ->
  142. AppDir = ?config(apps, Config),
  143. Name = rebar_test_utils:create_random_name("app1_"),
  144. Vsn = rebar_test_utils:create_random_vsn(),
  145. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  146. PkgName = rebar_test_utils:create_random_name("pkg1_"),
  147. mock_git_resource:mock([]),
  148. mock_pkg_resource:mock([
  149. {pkgdeps, [{{iolist_to_binary(PkgName), <<"0.1.0">>}, []},
  150. {{iolist_to_binary(PkgName), <<"0.0.1">>}, []},
  151. {{iolist_to_binary(PkgName), <<"0.1.1">>}, []}]}
  152. ]),
  153. RConfFile = rebar_test_utils:create_config(AppDir, [{plugins, [list_to_atom(PkgName)]}]),
  154. {ok, RConf} = file:consult(RConfFile),
  155. %% Build with deps.
  156. rebar_test_utils:run_and_check(
  157. Config, RConf, ["compile"],
  158. {ok, [{app, Name}, {plugin, PkgName, <<"0.1.1">>}]}
  159. ),
  160. catch mock_pkg_resource:unmock(),
  161. mock_pkg_resource:mock([
  162. {pkgdeps, [{{iolist_to_binary(PkgName), <<"0.1.0">>}, []},
  163. {{iolist_to_binary(PkgName), <<"0.0.1">>}, []},
  164. {{iolist_to_binary(PkgName), <<"0.1.3">>}, []},
  165. {{iolist_to_binary(PkgName), <<"0.1.1">>}, []}]},
  166. {upgrade, [PkgName]}
  167. ]),
  168. %% Build with deps.
  169. rebar_test_utils:run_and_check(
  170. Config, RConf, ["plugins", "upgrade", PkgName],
  171. {ok, [{app, Name}, {plugin, PkgName, <<"0.1.3">>}]}
  172. ).
  173. sub_app_plugins(Config) ->
  174. AppDir = ?config(apps, Config),
  175. Name = rebar_test_utils:create_random_name("sub_app1_"),
  176. Vsn = rebar_test_utils:create_random_vsn(),
  177. DepName = rebar_test_utils:create_random_name("dep1_"),
  178. PluginName = rebar_test_utils:create_random_name("plugin1_"),
  179. mock_pkg_resource:mock([{pkgdeps, [{{list_to_binary(DepName), list_to_binary(Vsn)}, []},
  180. {{list_to_binary(PluginName), list_to_binary(Vsn)}, []}]}]),
  181. SubAppsDir = filename:join([AppDir, "apps", Name]),
  182. rebar_test_utils:create_app(SubAppsDir, Name, Vsn, [kernel, stdlib]),
  183. rebar_test_utils:create_config(SubAppsDir, [{deps, [{list_to_binary(DepName), list_to_binary(Vsn)}]},
  184. {plugins, [list_to_atom(PluginName)]}]),
  185. RConfFile =
  186. rebar_test_utils:create_config(AppDir,
  187. [{deps, [
  188. list_to_atom(DepName)
  189. ]}]),
  190. {ok, RConf} = file:consult(RConfFile),
  191. %% Build with deps.
  192. rebar_test_utils:run_and_check(
  193. Config, RConf, ["compile"],
  194. {ok, [{app, Name}, {dep, DepName}, {plugin, PluginName}]}
  195. ).
  196. %% Tests that overrides in a dep that includes a plugin are applied to plugin fetching
  197. sub_app_plugin_overrides(Config) ->
  198. AppDir = ?config(apps, Config),
  199. Name = rebar_test_utils:create_random_name("sub_app1_"),
  200. Vsn = rebar_test_utils:create_random_vsn(),
  201. Dep2Name = rebar_test_utils:create_random_name("dep2_"),
  202. DepName = rebar_test_utils:create_random_name("dep1_"),
  203. PluginName = rebar_test_utils:create_random_name("plugin1_"),
  204. Vsn2 = rebar_test_utils:create_random_vsn(),
  205. Deps = rebar_test_utils:expand_deps(git, [{PluginName, Vsn, [{DepName, Vsn, []}]},
  206. {DepName, Vsn, []}]),
  207. {SrcDeps, _} = rebar_test_utils:flat_deps(Deps),
  208. mock_git_resource:mock([{deps, SrcDeps}]),
  209. mock_pkg_resource:mock([{pkgdeps, [{{list_to_binary(Dep2Name), list_to_binary(Vsn)}, []}]},
  210. {config, [{plugins, [{list_to_atom(PluginName),
  211. {git, "http://site.com/user/"++PluginName++".git",
  212. {tag, Vsn}}}]},
  213. %% Dep2 overrides the plugin's deps to have vsn2 of dep1
  214. {overrides, [{override, list_to_atom(PluginName),
  215. [{deps, [{list_to_atom(DepName),
  216. {git, "http://site.com/user/"++DepName++".git",
  217. {tag, Vsn2}}}]}]}]}]}]),
  218. SubAppsDir = filename:join([AppDir, "apps", Name]),
  219. rebar_test_utils:create_app(SubAppsDir, Name, Vsn, [kernel, stdlib]),
  220. RConfFile = rebar_test_utils:create_config(AppDir, [{deps, [{list_to_binary(Dep2Name), list_to_binary(Vsn)}]}]),
  221. {ok, RConf} = file:consult(RConfFile),
  222. %% Build with deps.
  223. rebar_test_utils:run_and_check(
  224. Config, RConf, ["compile"],
  225. {ok, [{app, Name}, {dep, Dep2Name, Vsn}, {plugin, DepName, Vsn2}, {plugin, PluginName}]}
  226. ).
  227. %% Check that project plugins are first in providers even if they override defaults but that
  228. %% normal plugins do not
  229. project_plugins(Config) ->
  230. AppDir = ?config(apps, Config),
  231. Name = rebar_test_utils:create_random_name("app1_"),
  232. Vsn = rebar_test_utils:create_random_vsn(),
  233. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  234. DepName = rebar_test_utils:create_random_name("dep1_"),
  235. PluginName = "compile",
  236. PluginName2 = "release",
  237. Plugins = rebar_test_utils:expand_deps(git, [{PluginName, Vsn, []}, {PluginName2, Vsn, []}]),
  238. {SrcDeps, _} = rebar_test_utils:flat_deps(Plugins),
  239. mock_git_resource:mock([{deps, SrcDeps}], create_plugin),
  240. mock_pkg_resource:mock([{pkgdeps, [{{list_to_binary(DepName), list_to_binary(Vsn)}, []}]},
  241. {config, [{plugins, [
  242. {list_to_atom(PluginName),
  243. {git, "http://site.com/user/"++PluginName++".git",
  244. {tag, Vsn}}}]}]}]),
  245. RConfFile =
  246. rebar_test_utils:create_config(AppDir,
  247. [{deps, [
  248. list_to_atom(DepName)
  249. ]},
  250. {project_plugins, [
  251. {list_to_atom(PluginName2),
  252. {git, "http://site.com/user/"++PluginName2++".git",
  253. {tag, Vsn}}}]}]),
  254. {ok, RConf} = file:consult(RConfFile),
  255. %% Build with deps.
  256. {ok, State} = rebar_test_utils:run_and_check(
  257. Config, RConf, ["compile"],
  258. {ok, [{app, Name}, {plugin, PluginName}, {plugin, PluginName2}, {dep, DepName}]}
  259. ),
  260. %% Should have 2 release providers but only 1 compile provider
  261. Release = [P || P <- rebar_state:providers(State), providers:impl(P) =:= release, providers:namespace(P) =:= default],
  262. Compile = [P || P <- rebar_state:providers(State), providers:impl(P) =:= compile, providers:namespace(P) =:= default],
  263. ?assertEqual(length(Release), 2),
  264. ?assertEqual(length(Compile), 1).