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.

173 lines
5.3 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, rebar_test_utils: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. setup_project(Case, Config0, Deps) ->
  103. DepsType = ?config(deps_type, Config0),
  104. Config = rebar_test_utils:init_rebar_state(
  105. Config0,
  106. atom_to_list(Case)++"_"++atom_to_list(DepsType)++"_"
  107. ),
  108. AppDir = ?config(apps, Config),
  109. rebar_test_utils:create_app(AppDir, "A", "0.0.0", [kernel, stdlib]),
  110. TopDeps = rebar_test_utils:top_level_deps(Deps),
  111. RebarConf = rebar_test_utils:create_config(AppDir, [{deps, TopDeps}]),
  112. case DepsType of
  113. git ->
  114. mock_git_resource:mock([{deps, rebar_test_utils:flat_deps(Deps)}]);
  115. pkg ->
  116. mock_pkg_resource:mock([{pkgdeps, rebar_test_utils:flat_pkgdeps(Deps)}])
  117. end,
  118. [{rebarconfig, RebarConf} | Config].
  119. mock_warnings() ->
  120. %% just let it do its thing, we check warnings through
  121. %% the call log.
  122. meck:new(rebar_log, [no_link, passthrough]).
  123. %%% TESTS %%%
  124. flat(Config) -> run(Config).
  125. pick_highest_left(Config) -> run(Config).
  126. pick_highest_right(Config) -> run(Config).
  127. pick_smallest1(Config) -> run(Config).
  128. pick_smallest2(Config) -> run(Config).
  129. circular1(Config) -> run(Config).
  130. circular2(Config) -> run(Config).
  131. circular_skip(Config) -> run(Config).
  132. run(Config) ->
  133. {ok, RebarConfig} = file:consult(?config(rebarconfig, Config)),
  134. rebar_test_utils:run_and_check(
  135. Config, RebarConfig, ["install_deps"], ?config(expect, Config)
  136. ),
  137. check_warnings(warning_calls(), ?config(warnings, Config), ?config(deps_type, Config)).
  138. warning_calls() ->
  139. History = meck:history(rebar_log),
  140. [{Str, Args} || {_, {rebar_log, log, [warn, Str, Args]}, _} <- History].
  141. check_warnings(_, [], _) ->
  142. ok;
  143. check_warnings(Warns, [{Name, Vsn} | Rest], Type) ->
  144. ct:pal("Checking for warning ~p in ~p", [{Name,Vsn},Warns]),
  145. ?assert(in_warnings(Type, Warns, Name, Vsn)),
  146. check_warnings(Warns, Rest, Type).
  147. in_warnings(git, Warns, NameRaw, VsnRaw) ->
  148. Name = iolist_to_binary(NameRaw),
  149. 1 =< length([1 || {_, [AppName, {git, _, {_, Vsn}}]} <- Warns,
  150. AppName =:= Name, Vsn =:= VsnRaw]);
  151. in_warnings(pkg, Warns, NameRaw, VsnRaw) ->
  152. Name = iolist_to_binary(NameRaw),
  153. Vsn = iolist_to_binary(VsnRaw),
  154. 1 =< length([1 || {_, [AppName, AppVsn]} <- Warns,
  155. AppName =:= Name, AppVsn =:= Vsn]).