25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

222 lines
6.9 KiB

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