Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

257 linhas
11 KiB

  1. %% Test suite for the handling hexpm repo configurations
  2. -module(rebar_pkg_repos_SUITE).
  3. -compile(export_all).
  4. -include_lib("common_test/include/ct.hrl").
  5. -include_lib("eunit/include/eunit.hrl").
  6. -include("rebar.hrl").
  7. all() ->
  8. [default_repo, repo_merging, repo_replacing, {group, resolve_version}].
  9. groups() ->
  10. [{resolve_version, [use_first_repo_match, use_exact_with_hash, fail_repo_update,
  11. ignore_match_in_excluded_repo]}].
  12. init_per_group(resolve_version, Config) ->
  13. Repo1 = <<"test-repo-1">>,
  14. Repo2 = <<"test-repo-2">>,
  15. Repo3 = <<"test-repo-3">>,
  16. Hexpm = <<"hexpm">>,
  17. Repos = [Repo1, Repo2, Repo3, Hexpm],
  18. Deps = [{"A", "0.1.1", <<"good checksum">>, Repo1},
  19. {"A", "0.1.1", <<"good checksum">>, Repo2},
  20. {"B", "1.0.0", Repo1},
  21. {"B", "2.0.0", Repo2},
  22. {"B", "1.4.0", Repo3},
  23. {"B", "1.4.3", Hexpm},
  24. {"C", "1.3.1", <<"bad checksum">>, Repo1},
  25. {"C", "1.3.1", <<"good checksum">>, Repo2}],
  26. [{deps, Deps}, {repos, Repos} | Config];
  27. init_per_group(_, Config) ->
  28. Config.
  29. end_per_group(_, _) ->
  30. ok.
  31. init_per_testcase(use_first_repo_match, Config) ->
  32. Deps = ?config(deps, Config),
  33. Repos = ?config(repos, Config),
  34. State = setup_deps_and_repos(Deps, Repos),
  35. meck:new(rebar_packages, [passthrough, no_link]),
  36. %% fail when the first repo is updated since it doesn't have a matching package
  37. %% should continue anyway
  38. meck:expect(rebar_packages, update_package,
  39. fun(_, _, _State) -> ok end),
  40. meck:expect(rebar_packages, verify_table,
  41. fun(_State) -> true end),
  42. [{state, State} | Config];
  43. init_per_testcase(use_exact_with_hash, Config) ->
  44. Deps = ?config(deps, Config),
  45. Repos = ?config(repos, Config),
  46. State = setup_deps_and_repos(Deps, Repos),
  47. meck:new(rebar_packages, [passthrough, no_link]),
  48. %% fail when the first repo is updated since it doesn't have a matching package
  49. %% should continue anyway
  50. meck:expect(rebar_packages, update_package,
  51. fun(_, _, _State) -> ok end),
  52. meck:expect(rebar_packages, verify_table,
  53. fun(_State) -> true end),
  54. [{state, State} | Config];
  55. init_per_testcase(fail_repo_update, Config) ->
  56. Deps = ?config(deps, Config),
  57. Repos = ?config(repos, Config),
  58. State = setup_deps_and_repos(Deps, Repos),
  59. meck:new(rebar_packages, [passthrough, no_link]),
  60. %% fail when the first repo is updated since it doesn't have a matching package
  61. %% should continue anyway
  62. [Repo1 | _] = Repos,
  63. meck:expect(rebar_packages, update_package,
  64. fun(_, #{name := Repo}, _State) when Repo =:= Repo1 -> fail;
  65. (_, _, _State) -> ok end),
  66. meck:expect(rebar_packages, verify_table,
  67. fun(_State) -> true end),
  68. [{state, State} | Config];
  69. init_per_testcase(ignore_match_in_excluded_repo, Config) ->
  70. Deps = ?config(deps, Config),
  71. Repos = [Repo1, _, Repo3 | _] = ?config(repos, Config),
  72. %% drop repo1 and repo2 from the repos to be used by the pkg resource
  73. State = setup_deps_and_repos(Deps, [R || R <- Repos, R =/= Repo3, R =/= Repo1]),
  74. meck:new(rebar_packages, [passthrough, no_link]),
  75. %% fail when the first repo is updated since it doesn't have a matching package
  76. %% should continue anyway
  77. [_, _, Repo3 | _] = Repos,
  78. meck:expect(rebar_packages, update_package,
  79. fun(_, _, _State) -> ok end),
  80. meck:expect(rebar_packages, verify_table,
  81. fun(_State) -> true end),
  82. [{state, State} | Config];
  83. init_per_testcase(_, Config) ->
  84. Config.
  85. end_per_testcase(Case, _Config) when Case =:= use_first_repo_match ;
  86. Case =:= use_exact_with_hash ;
  87. Case =:= fail_repo_update ;
  88. Case =:= ignore_match_in_excluded_repo ->
  89. meck:unload(rebar_packages);
  90. end_per_testcase(_, _) ->
  91. ok.
  92. default_repo(_Config) ->
  93. Repo1 = #{name => <<"hexpm">>,
  94. api_key => <<"asdf">>},
  95. MergedRepos = rebar_pkg_resource:repos([{repos, [Repo1]}]),
  96. ?assertMatch([#{name := <<"hexpm">>,
  97. api_key := <<"asdf">>,
  98. api_url := <<"https://hex.pm/api">>}], MergedRepos).
  99. repo_merging(_Config) ->
  100. Repo1 = #{name => <<"repo-1">>,
  101. api_url => <<"repo-1/api">>},
  102. Repo2 = #{name => <<"repo-2">>,
  103. repo_url => <<"repo-2/repo">>,
  104. repo_verify => false},
  105. Result = rebar_pkg_resource:merge_repos([Repo1, Repo2,
  106. #{name => <<"repo-2">>,
  107. api_url => <<"repo-2/api">>,
  108. repo_url => <<"bad url">>,
  109. repo_verify => true},
  110. #{name => <<"repo-1">>,
  111. api_url => <<"bad url">>,
  112. repo_verify => true},
  113. #{name => <<"repo-2">>,
  114. organization => <<"repo-2-org">>,
  115. api_url => <<"repo-2/api-2">>,
  116. repo_url => <<"other/repo">>}]),
  117. ?assertMatch([#{name := <<"repo-1">>,
  118. api_url := <<"repo-1/api">>,
  119. repo_verify := true},
  120. #{name := <<"repo-2">>,
  121. api_url := <<"repo-2/api">>,
  122. repo_url := <<"repo-2/repo">>,
  123. organization := <<"repo-2-org">>,
  124. repo_verify := false}], Result).
  125. repo_replacing(_Config) ->
  126. Repo1 = #{name => <<"repo-1">>,
  127. api_url => <<"repo-1/api">>},
  128. Repo2 = #{name => <<"repo-2">>,
  129. repo_url => <<"repo-2/repo">>,
  130. repo_verify => false},
  131. ?assertMatch([Repo1, Repo2, #{name := <<"hexpm">>}],
  132. rebar_pkg_resource:repos([{repos, [Repo1]},
  133. {repos, [Repo2]}])),
  134. %% use of replace is ignored if found in later entries than the first
  135. ?assertMatch([Repo1, Repo2, #{name := <<"hexpm">>}],
  136. rebar_pkg_resource:repos([{repos, [Repo1]},
  137. {repos, replace, [Repo2]}])),
  138. ?assertMatch([Repo1],
  139. rebar_pkg_resource:repos([{repos, replace, [Repo1]},
  140. {repos, [Repo2]}])).
  141. use_first_repo_match(Config) ->
  142. State = ?config(state, Config),
  143. ?assertMatch({ok,{package,{<<"B">>, <<"2.0.0">>, Repo2},
  144. <<"some checksum">>, false, []},
  145. #{name := Repo2,
  146. http_adapter_config := #{profile := rebar}}},
  147. rebar_packages:resolve_version(<<"B">>, <<"> 1.4.0">>, undefined,
  148. ?PACKAGE_TABLE, State)),
  149. ?assertMatch({ok,{package,{<<"B">>, <<"1.4.0">>, Repo3},
  150. <<"some checksum">>, false, []},
  151. #{name := Repo3,
  152. http_adapter_config := #{profile := rebar}}},
  153. rebar_packages:resolve_version(<<"B">>, <<"~> 1.4.0">>, undefined,
  154. ?PACKAGE_TABLE, State)).
  155. %% tests that even though an easier repo has C-1.3.1 it doesn't use it since its hash is different
  156. use_exact_with_hash(Config) ->
  157. State = ?config(state, Config),
  158. ?assertMatch({ok,{package,{<<"C">>, <<"1.3.1">>, Repo2},
  159. <<"good checksum">>, false, []},
  160. #{name := Repo2,
  161. http_adapter_config := #{profile := rebar}}},
  162. rebar_packages:resolve_version(<<"C">>, <<"1.3.1">>, <<"good checksum">>,
  163. ?PACKAGE_TABLE, State)).
  164. fail_repo_update(Config) ->
  165. State = ?config(state, Config),
  166. ?assertMatch({ok,{package,{<<"B">>, <<"1.4.0">>, Repo3},
  167. <<"some checksum">>, false, []},
  168. #{name := Repo3,
  169. http_adapter_config := #{profile := rebar}}},
  170. rebar_packages:resolve_version(<<"B">>, <<"~> 1.4.0">>, undefined,
  171. ?PACKAGE_TABLE, State)).
  172. ignore_match_in_excluded_repo(Config) ->
  173. State = ?config(state, Config),
  174. Repos = ?config(repos, Config),
  175. ?assertMatch({ok,{package,{<<"B">>, <<"1.4.3">>, Hexpm},
  176. <<"some checksum">>, false, []},
  177. #{name := Hexpm,
  178. http_adapter_config := #{profile := rebar}}},
  179. rebar_packages:resolve_version(<<"B">>, <<"~> 1.4.0">>, undefined,
  180. ?PACKAGE_TABLE, State)),
  181. [_, Repo2 | _] = Repos,
  182. ?assertMatch({ok,{package,{<<"A">>, <<"0.1.1">>, Repo2},
  183. <<"good checksum">>, false, []},
  184. #{name := Repo2,
  185. http_adapter_config := #{profile := rebar}}},
  186. rebar_packages:resolve_version(<<"A">>, <<"0.1.1">>, <<"good checksum">>,
  187. ?PACKAGE_TABLE, State)).
  188. %%
  189. setup_deps_and_repos(Deps, Repos) ->
  190. true = rebar_packages:new_package_table(),
  191. insert_deps(Deps),
  192. State = rebar_state:new([{hex, [{repos, [#{name => R} || R <- Repos]}]}]),
  193. rebar_state:create_resources([{pkg, rebar_pkg_resource}], State).
  194. insert_deps(Deps) ->
  195. lists:foreach(fun({Name, Version, Repo}) ->
  196. ets:insert(?PACKAGE_TABLE, #package{key={rebar_utils:to_binary(Name),
  197. rebar_utils:to_binary(Version),
  198. rebar_utils:to_binary(Repo)},
  199. dependencies=[],
  200. retired=false,
  201. checksum = <<"some checksum">>});
  202. ({Name, Version, Checksum, Repo}) ->
  203. ets:insert(?PACKAGE_TABLE, #package{key={rebar_utils:to_binary(Name),
  204. rebar_utils:to_binary(Version),
  205. rebar_utils:to_binary(Repo)},
  206. dependencies=[],
  207. retired=false,
  208. checksum = Checksum})
  209. end, Deps).