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.

372 lines
15 KiB

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