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

318 行
11 KiB

  1. -module(rebar_deps_SUITE).
  2. -compile(export_all).
  3. -include_lib("common_test/include/ct.hrl").
  4. -include_lib("eunit/include/eunit.hrl").
  5. all() -> [sub_app_deps, newly_added_dep, http_proxy_settings, https_proxy_settings, {group, git}, {group, pkg}].
  6. groups() ->
  7. [{all, [], [flat, pick_highest_left, pick_highest_right,
  8. pick_smallest1, pick_smallest2,
  9. circular1, circular2, circular_skip]},
  10. {git, [], [{group, all}]},
  11. {pkg, [], [{group, all}]}].
  12. init_per_suite(Config) ->
  13. application:start(meck),
  14. Config.
  15. end_per_suite(_Config) ->
  16. application:stop(meck).
  17. init_per_group(git, Config) ->
  18. [{deps_type, git} | Config];
  19. init_per_group(pkg, Config) ->
  20. [{deps_type, pkg} | Config];
  21. init_per_group(_, Config) ->
  22. Config.
  23. end_per_group(_, Config) ->
  24. Config.
  25. init_per_testcase(newly_added_dep, Config) ->
  26. rebar_test_utils:init_rebar_state(Config);
  27. init_per_testcase(sub_app_deps, Config) ->
  28. rebar_test_utils:init_rebar_state(Config);
  29. init_per_testcase(http_proxy_settings, Config) ->
  30. %% Create private rebar.config
  31. Priv = ?config(priv_dir, Config),
  32. GlobalDir = filename:join(Priv, "global"),
  33. GlobalConfigDir = filename:join([GlobalDir, ".config", "rebar3"]),
  34. GlobalConfig = filename:join([GlobalDir, ".config", "rebar3", "rebar.config"]),
  35. meck:new(rebar_dir, [passthrough]),
  36. meck:expect(rebar_dir, global_config, fun() -> GlobalConfig end),
  37. meck:expect(rebar_dir, global_cache_dir, fun(_) -> GlobalDir end),
  38. %% Insert proxy variables into config
  39. rebar_test_utils:create_config(GlobalConfigDir,
  40. [{http_proxy, "http://localhost:1234"}
  41. ]),
  42. rebar_test_utils:init_rebar_state(Config);
  43. init_per_testcase(https_proxy_settings, Config) ->
  44. SupportsHttpsProxy = case erlang:system_info(otp_release) of
  45. "R16"++_ -> true;
  46. "R"++_ -> false;
  47. _ -> true % 17 and up don't have a "R" in the version
  48. end,
  49. if not SupportsHttpsProxy ->
  50. {skip, https_proxy_unsupported_before_R16};
  51. SupportsHttpsProxy ->
  52. %% Create private rebar.config
  53. Priv = ?config(priv_dir, Config),
  54. GlobalDir = filename:join(Priv, "global"),
  55. GlobalConfigDir = filename:join([GlobalDir, ".config", "rebar3"]),
  56. GlobalConfig = filename:join([GlobalDir, ".config", "rebar3", "rebar.config"]),
  57. meck:new(rebar_dir, [passthrough]),
  58. meck:expect(rebar_dir, global_config, fun() -> GlobalConfig end),
  59. meck:expect(rebar_dir, global_cache_dir, fun(_) -> GlobalDir end),
  60. %% Insert proxy variables into config
  61. rebar_test_utils:create_config(GlobalConfigDir,
  62. [{https_proxy, "http://localhost:1234"}
  63. ]),
  64. rebar_test_utils:init_rebar_state(Config)
  65. end;
  66. init_per_testcase(Case, Config) ->
  67. {Deps, Warnings, Expect} = deps(Case),
  68. Expected = case Expect of
  69. {ok, List} -> {ok, format_expected_deps(List)};
  70. {error, Reason} -> {error, Reason}
  71. end,
  72. DepsType = ?config(deps_type, Config),
  73. mock_warnings(),
  74. [{expect, Expected},
  75. {warnings, Warnings}
  76. | setup_project(Case, Config, rebar_test_utils:expand_deps(DepsType, Deps))].
  77. end_per_testcase(https_proxy_settings, Config) ->
  78. meck:unload(rebar_dir),
  79. Config;
  80. end_per_testcase(http_proxy_settings, Config) ->
  81. meck:unload(rebar_dir),
  82. Config;
  83. end_per_testcase(_, Config) ->
  84. meck:unload(),
  85. Config.
  86. format_expected_deps(Deps) ->
  87. [case Dep of
  88. {N,V} -> {dep, N, V};
  89. N -> {dep, N}
  90. end || Dep <- Deps].
  91. %% format:
  92. %% {Spec,
  93. %% [Warning],
  94. %% {ok, Result} | {error, Reason}}
  95. %%
  96. %% Spec is a list of levelled dependencies of two possible forms:
  97. %% - {"Name", Spec}
  98. %% - {"Name", "Vsn", Spec}
  99. %%
  100. %% Warnings are going to match on mocked ?WARN(...)
  101. %% calls to be evaluated. An empty list means we do not care about
  102. %% warnings, not that no warnings will be printed. This means
  103. %% the list of warning isn't interpreted to be exhaustive, and more
  104. %% warnings may be generated than are listed.
  105. deps(flat) ->
  106. {[{"B", []},
  107. {"C", []}],
  108. [],
  109. {ok, ["B", "C"]}};
  110. deps(pick_highest_left) ->
  111. {[{"B", [{"C", "2", []}]},
  112. {"C", "1", []}],
  113. [{"C","2"}],
  114. {ok, ["B", {"C", "1"}]}};
  115. deps(pick_highest_right) ->
  116. {[{"B", "1", []},
  117. {"C", [{"B", "2", []}]}],
  118. [{"B","2"}],
  119. {ok, [{"B","1"}, "C"]}};
  120. deps(pick_smallest1) ->
  121. {[{"B", [{"D", "1", []}]},
  122. {"C", [{"D", "2", []}]}],
  123. [{"D","2"}],
  124. %% we pick D1 because B < C
  125. {ok, ["B","C",{"D","1"}]}};
  126. deps(pick_smallest2) ->
  127. {[{"C", [{"D", "2", []}]},
  128. {"B", [{"D", "1", []}]}],
  129. [{"D","2"}],
  130. %% we pick D1 because B < C
  131. {ok, ["B","C",{"D","1"}]}};
  132. deps(circular1) ->
  133. {[{"B", [{"A", []}]}, % A is the top-level app
  134. {"C", []}],
  135. [],
  136. {error, {rebar_prv_install_deps, {cycles, [[<<"A">>,<<"B">>]]}}}};
  137. deps(circular2) ->
  138. {[{"B", [{"C", [{"B", []}]}]},
  139. {"C", []}],
  140. [],
  141. {error, {rebar_prv_install_deps, {cycles, [[<<"B">>,<<"C">>]]}}}};
  142. deps(circular_skip) ->
  143. %% Never spot the circular dep due to being to low in the deps tree
  144. %% in source deps
  145. {[{"B", [{"C", "2", [{"B", []}]}]},
  146. {"C", "1", [{"D",[]}]}],
  147. [{"C","2"}],
  148. {ok, ["B", {"C","1"}, "D"]}}.
  149. setup_project(Case, Config0, Deps) ->
  150. DepsType = ?config(deps_type, Config0),
  151. Config = rebar_test_utils:init_rebar_state(
  152. Config0,
  153. atom_to_list(Case)++"_"++atom_to_list(DepsType)++"_"
  154. ),
  155. AppDir = ?config(apps, Config),
  156. rebar_test_utils:create_app(AppDir, "A", "0.0.0", [kernel, stdlib]),
  157. TopDeps = rebar_test_utils:top_level_deps(Deps),
  158. RebarConf = rebar_test_utils:create_config(AppDir, [{deps, TopDeps}]),
  159. case DepsType of
  160. git ->
  161. mock_git_resource:mock([{deps, rebar_test_utils:flat_deps(Deps)}]);
  162. pkg ->
  163. mock_pkg_resource:mock([{pkgdeps, flat_pkgdeps(Deps)}])
  164. end,
  165. [{rebarconfig, RebarConf} | Config].
  166. flat_pkgdeps([]) -> [];
  167. flat_pkgdeps([{{pkg, Name, Vsn}, Deps} | Rest]) ->
  168. [{{iolist_to_binary(Name),iolist_to_binary(Vsn)}, rebar_test_utils:top_level_deps(Deps)}]
  169. ++
  170. flat_pkgdeps(Deps)
  171. ++
  172. flat_pkgdeps(Rest).
  173. app_vsn([]) -> [];
  174. app_vsn([{Source, Deps} | Rest]) ->
  175. {Name, Vsn} = case Source of
  176. {pkg, N, V} -> {N,V};
  177. {N,V,_Ref} -> {N,V}
  178. end,
  179. [{Name, Vsn}] ++ app_vsn(Deps) ++ app_vsn(Rest).
  180. mock_warnings() ->
  181. %% just let it do its thing, we check warnings through
  182. %% the call log.
  183. meck:new(rebar_log, [no_link, passthrough]).
  184. %%% TESTS %%%
  185. flat(Config) -> run(Config).
  186. pick_highest_left(Config) -> run(Config).
  187. pick_highest_right(Config) -> run(Config).
  188. pick_smallest1(Config) -> run(Config).
  189. pick_smallest2(Config) -> run(Config).
  190. circular1(Config) -> run(Config).
  191. circular2(Config) -> run(Config).
  192. circular_skip(Config) -> run(Config).
  193. %% Test that the deps of project apps that have their own rebar.config
  194. %% are included, but that top level rebar.config deps take precedence
  195. sub_app_deps(Config) ->
  196. AppDir = ?config(apps, Config),
  197. Deps = rebar_test_utils:expand_deps(git, [{"a", "1.0.0", []}
  198. ,{"b", "1.0.0", []}
  199. ,{"b", "2.0.0", []}]),
  200. mock_git_resource:mock([{deps, rebar_test_utils:flat_deps(Deps)}]),
  201. Name = rebar_test_utils:create_random_name("sub_app1_"),
  202. Vsn = rebar_test_utils:create_random_vsn(),
  203. SubAppsDir = filename:join([AppDir, "apps", Name]),
  204. SubDeps = rebar_test_utils:top_level_deps(rebar_test_utils:expand_deps(git, [{"a", "1.0.0", []}
  205. ,{"b", "2.0.0", []}])),
  206. rebar_test_utils:create_app(SubAppsDir, Name, Vsn, [kernel, stdlib]),
  207. rebar_test_utils:create_config(SubAppsDir, [{deps, SubDeps}]),
  208. TopDeps = rebar_test_utils:top_level_deps(rebar_test_utils:expand_deps(git, [{"b", "1.0.0", []}])),
  209. {ok, RebarConfig} = file:consult(rebar_test_utils:create_config(AppDir, [{deps, TopDeps}])),
  210. rebar_test_utils:run_and_check(
  211. Config, RebarConfig, ["compile"],
  212. {ok, [{app, Name}, {dep, "a"}, {dep, "b", "1.0.0"}]}).
  213. %% Newly added dependency after locking
  214. newly_added_dep(Config) ->
  215. AppDir = ?config(apps, Config),
  216. Deps = rebar_test_utils:expand_deps(git, [{"a", "1.0.0", []}
  217. ,{"b", "1.0.0", [{"c", "1.0.0", []}]}
  218. ,{"c", "2.0.0", []}]),
  219. mock_git_resource:mock([{deps, rebar_test_utils:flat_deps(Deps)}]),
  220. Name = rebar_test_utils:create_random_name("app_"),
  221. Vsn = rebar_test_utils:create_random_vsn(),
  222. SubAppsDir = filename:join([AppDir, "apps", Name]),
  223. rebar_test_utils:create_app(SubAppsDir, Name, Vsn, [kernel, stdlib]),
  224. TopDeps = rebar_test_utils:top_level_deps(rebar_test_utils:expand_deps(git, [{"b", "1.0.0", []}])),
  225. {ok, RebarConfig} = file:consult(rebar_test_utils:create_config(AppDir, [{deps, TopDeps}])),
  226. rebar_test_utils:run_and_check(
  227. Config, RebarConfig, ["compile"],
  228. {ok, [{app, Name}, {dep, "b", "1.0.0"}, {dep, "c", "1.0.0"}]}),
  229. %% Add a and c to top level
  230. TopDeps2 = rebar_test_utils:top_level_deps(rebar_test_utils:expand_deps(git, [{"a", "1.0.0", []}
  231. ,{"c", "2.0.0", []}
  232. ,{"b", "1.0.0", []}])),
  233. {ok, RebarConfig2} = file:consult(rebar_test_utils:create_config(AppDir, [{deps, TopDeps2}])),
  234. LockFile = filename:join(AppDir, "rebar.lock"),
  235. RebarConfig3 = rebar_config:merge_locks(RebarConfig2,
  236. rebar_config:consult_lock_file(LockFile)),
  237. %% a should now be installed and c should not change
  238. rebar_test_utils:run_and_check(
  239. Config, RebarConfig3, ["compile"],
  240. {ok, [{app, Name}, {dep, "a"}, {dep, "b", "1.0.0"}, {dep, "c", "1.0.0"}]}).
  241. http_proxy_settings(_Config) ->
  242. %% Load config
  243. rebar_utils:set_httpc_options(),
  244. rebar3:init_config(),
  245. %% Assert variable is right
  246. ?assertEqual({ok,{{"localhost", 1234}, []}},
  247. httpc:get_option(proxy, rebar)).
  248. https_proxy_settings(_Config) ->
  249. %% Load config
  250. rebar_utils:set_httpc_options(),
  251. rebar3:init_config(),
  252. %% Assert variable is right
  253. ?assertEqual({ok,{{"localhost", 1234}, []}},
  254. httpc:get_option(https_proxy, rebar)).
  255. run(Config) ->
  256. {ok, RebarConfig} = file:consult(?config(rebarconfig, Config)),
  257. rebar_test_utils:run_and_check(
  258. Config, RebarConfig, ["install_deps"], ?config(expect, Config)
  259. ),
  260. check_warnings(warning_calls(), ?config(warnings, Config), ?config(deps_type, Config)).
  261. warning_calls() ->
  262. History = meck:history(rebar_log),
  263. [{Str, Args} || {_, {rebar_log, log, [warn, Str, Args]}, _} <- History].
  264. check_warnings(_, [], _) ->
  265. ok;
  266. check_warnings(Warns, [{Name, Vsn} | Rest], Type) ->
  267. ct:pal("Checking for warning ~p in ~p", [{Name,Vsn},Warns]),
  268. ?assert(in_warnings(Type, Warns, Name, Vsn)),
  269. check_warnings(Warns, Rest, Type).
  270. in_warnings(git, Warns, NameRaw, VsnRaw) ->
  271. Name = iolist_to_binary(NameRaw),
  272. 1 =< length([1 || {_, [AppName, {git, _, {_, Vsn}}]} <- Warns,
  273. AppName =:= Name, Vsn =:= VsnRaw]);
  274. in_warnings(pkg, Warns, NameRaw, VsnRaw) ->
  275. Name = iolist_to_binary(NameRaw),
  276. Vsn = iolist_to_binary(VsnRaw),
  277. 1 =< length([1 || {_, [AppName, AppVsn]} <- Warns,
  278. AppName =:= Name, AppVsn =:= Vsn]).