You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

283 line
11 KiB

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