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.

221 line
6.9 KiB

  1. -module(rebar_install_deps_SUITE).
  2. -compile(export_all).
  3. -include_lib("common_test/include/ct.hrl").
  4. -include_lib("eunit/include/eunit.hrl").
  5. all() -> [{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(Case, Config) ->
  26. {Deps, Warnings, Expect} = deps(Case),
  27. Expected = case Expect of
  28. {ok, List} -> {ok, format_expected_deps(List)};
  29. {error, Reason} -> {error, Reason}
  30. end,
  31. DepsType = ?config(deps_type, Config),
  32. mock_warnings(),
  33. [{expect, Expected},
  34. {warnings, Warnings}
  35. | setup_project(Case, Config, expand_deps(DepsType, Deps))].
  36. end_per_testcase(_, Config) ->
  37. meck:unload(),
  38. Config.
  39. format_expected_deps(Deps) ->
  40. [case Dep of
  41. {N,V} -> {dep, N, V};
  42. N -> {dep, N}
  43. end || Dep <- Deps].
  44. %% format:
  45. %% {Spec,
  46. %% [Warning],
  47. %% {ok, Result} | {error, Reason}}
  48. %%
  49. %% Spec is a list of levelled dependencies of two possible forms:
  50. %% - {"Name", Spec}
  51. %% - {"Name", "Vsn", Spec}
  52. %%
  53. %% Warnings are going to match on mocked ?WARN(...)
  54. %% calls to be evaluated. An empty list means we do not care about
  55. %% warnings, not that no warnings will be printed. This means
  56. %% the list of warning isn't interpreted to be exhaustive, and more
  57. %% warnings may be generated than are listed.
  58. deps(flat) ->
  59. {[{"B", []},
  60. {"C", []}],
  61. [],
  62. {ok, ["B", "C"]}};
  63. deps(pick_highest_left) ->
  64. {[{"B", [{"C", "2", []}]},
  65. {"C", "1", []}],
  66. [{"C","2"}],
  67. {ok, ["B", {"C", "1"}]}};
  68. deps(pick_highest_right) ->
  69. {[{"B", "1", []},
  70. {"C", [{"B", "2", []}]}],
  71. [{"B","2"}],
  72. {ok, [{"B","1"}, "C"]}};
  73. deps(pick_smallest1) ->
  74. {[{"B", [{"D", "1", []}]},
  75. {"C", [{"D", "2", []}]}],
  76. [{"D","2"}],
  77. %% we pick D1 because B < C
  78. {ok, ["B","C",{"D","1"}]}};
  79. deps(pick_smallest2) ->
  80. {[{"C", [{"D", "2", []}]},
  81. {"B", [{"D", "1", []}]}],
  82. [{"D","2"}],
  83. %% we pick D1 because B < C
  84. {ok, ["B","C",{"D","1"}]}};
  85. deps(circular1) ->
  86. {[{"B", [{"A", []}]}, % A is the top-level app
  87. {"C", []}],
  88. [],
  89. {error, {rebar_prv_install_deps, {cycles, [[<<"A">>,<<"B">>]]}}}};
  90. deps(circular2) ->
  91. {[{"B", [{"C", [{"B", []}]}]},
  92. {"C", []}],
  93. [],
  94. {error, {rebar_prv_install_deps, {cycles, [[<<"B">>,<<"C">>]]}}}};
  95. deps(circular_skip) ->
  96. %% Never spot the circular dep due to being to low in the deps tree
  97. %% in source deps
  98. {[{"B", [{"C", "2", [{"B", []}]}]},
  99. {"C", "1", [{"D",[]}]}],
  100. [{"C","2"}],
  101. {ok, ["B", {"C","1"}, "D"]}}.
  102. expand_deps(_, []) -> [];
  103. expand_deps(git, [{Name, Deps} | Rest]) ->
  104. Dep = {Name, ".*", {git, "https://example.org/user/"++Name++".git", "master"}},
  105. [{Dep, expand_deps(git, Deps)} | expand_deps(git, Rest)];
  106. expand_deps(git, [{Name, Vsn, Deps} | Rest]) ->
  107. Dep = {Name, Vsn, {git, "https://example.org/user/"++Name++".git", {tag, Vsn}}},
  108. [{Dep, expand_deps(git, Deps)} | expand_deps(git, Rest)];
  109. expand_deps(pkg, [{Name, Deps} | Rest]) ->
  110. Dep = {pkg, Name, "0.0.0", "https://example.org/user/"++Name++".tar.gz"},
  111. [{Dep, expand_deps(pkg, Deps)} | expand_deps(pkg, Rest)];
  112. expand_deps(pkg, [{Name, Vsn, Deps} | Rest]) ->
  113. Dep = {pkg, Name, Vsn, "https://example.org/user/"++Name++".tar.gz"},
  114. [{Dep, expand_deps(pkg, Deps)} | expand_deps(pkg, Rest)].
  115. setup_project(Case, Config0, Deps) ->
  116. DepsType = ?config(deps_type, Config0),
  117. Config = rebar_test_utils:init_rebar_state(
  118. Config0,
  119. atom_to_list(Case)++"_"++atom_to_list(DepsType)++"_"
  120. ),
  121. AppDir = ?config(apps, Config),
  122. rebar_test_utils:create_app(AppDir, "A", "0.0.0", [kernel, stdlib]),
  123. TopDeps = top_level_deps(Deps),
  124. RebarConf = rebar_test_utils:create_config(AppDir, [{deps, TopDeps}]),
  125. case DepsType of
  126. git ->
  127. mock_git_resource:mock([{deps, flat_deps(Deps)}]);
  128. pkg ->
  129. mock_pkg_resource:mock([{pkgdeps, flat_pkgdeps(Deps)}])
  130. end,
  131. [{rebarconfig, RebarConf} | Config].
  132. flat_deps([]) -> [];
  133. flat_deps([{{Name,_Vsn,Ref}, Deps} | Rest]) ->
  134. [{{Name,vsn_from_ref(Ref)}, top_level_deps(Deps)}]
  135. ++
  136. flat_deps(Deps)
  137. ++
  138. flat_deps(Rest).
  139. vsn_from_ref({git, _, {_, Vsn}}) -> Vsn;
  140. vsn_from_ref({git, _, Vsn}) -> Vsn.
  141. flat_pkgdeps([]) -> [];
  142. flat_pkgdeps([{{pkg, Name, Vsn, _Url}, Deps} | Rest]) ->
  143. [{{iolist_to_binary(Name),iolist_to_binary(Vsn)}, top_level_deps(Deps)}]
  144. ++
  145. flat_pkgdeps(Deps)
  146. ++
  147. flat_pkgdeps(Rest).
  148. top_level_deps([]) -> [];
  149. top_level_deps([{{Name, Vsn, Ref}, _} | Deps]) ->
  150. [{list_to_atom(Name), Vsn, Ref} | top_level_deps(Deps)];
  151. top_level_deps([{{pkg, Name, Vsn, _URL}, _} | Deps]) ->
  152. [{list_to_atom(Name), Vsn} | top_level_deps(Deps)].
  153. app_vsn([]) -> [];
  154. app_vsn([{Source, Deps} | Rest]) ->
  155. {Name, Vsn} = case Source of
  156. {N,V,_Ref} -> {N,V};
  157. {pkg, N, V, _} -> {N,V}
  158. end,
  159. [{Name, Vsn}] ++ app_vsn(Deps) ++ app_vsn(Rest).
  160. mock_warnings() ->
  161. %% just let it do its thing, we check warnings through
  162. %% the call log.
  163. meck:new(rebar_log, [no_link, passthrough]).
  164. %%% TESTS %%%
  165. flat(Config) -> run(Config).
  166. pick_highest_left(Config) -> run(Config).
  167. pick_highest_right(Config) -> run(Config).
  168. pick_smallest1(Config) -> run(Config).
  169. pick_smallest2(Config) -> run(Config).
  170. circular1(Config) -> run(Config).
  171. circular2(Config) -> run(Config).
  172. circular_skip(Config) -> run(Config).
  173. run(Config) ->
  174. {ok, RebarConfig} = file:consult(?config(rebarconfig, Config)),
  175. rebar_test_utils:run_and_check(
  176. Config, RebarConfig, ["install_deps"], ?config(expect, Config)
  177. ),
  178. check_warnings(warning_calls(), ?config(warnings, Config), ?config(deps_type, Config)).
  179. warning_calls() ->
  180. History = meck:history(rebar_log),
  181. [{Str, Args} || {_, {rebar_log, log, [warn, Str, Args]}, _} <- History].
  182. check_warnings(_, [], _) ->
  183. ok;
  184. check_warnings(Warns, [{Name, Vsn} | Rest], Type) ->
  185. ct:pal("Checking for warning ~p in ~p", [{Name,Vsn},Warns]),
  186. ?assert(in_warnings(Type, Warns, Name, Vsn)),
  187. check_warnings(Warns, Rest, Type).
  188. in_warnings(git, Warns, NameRaw, VsnRaw) ->
  189. Name = iolist_to_binary(NameRaw),
  190. 1 =< length([1 || {_, [AppName, {git, _, {_, Vsn}}]} <- Warns,
  191. AppName =:= Name, Vsn =:= VsnRaw]);
  192. in_warnings(pkg, Warns, NameRaw, VsnRaw) ->
  193. Name = iolist_to_binary(NameRaw),
  194. Vsn = iolist_to_binary(VsnRaw),
  195. 1 =< length([1 || {_, [AppName, AppVsn]} <- Warns,
  196. AppName =:= Name, AppVsn =:= Vsn]).