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.

188 line
7.2 KiB

  1. %%% Mock a package resource and create an app magically for each URL submitted.
  2. -module(mock_pkg_resource).
  3. -export([mock/0, mock/1, unmock/0]).
  4. -define(MOD, rebar_pkg_resource).
  5. -include("rebar.hrl").
  6. %%%%%%%%%%%%%%%%%
  7. %%% Interface %%%
  8. %%%%%%%%%%%%%%%%%
  9. %% @doc same as `mock([])'.
  10. mock() -> mock([]).
  11. %% @doc Mocks a fake version of the git resource fetcher that creates
  12. %% empty applications magically, rather than trying to download them.
  13. %% Specific config options are explained in each of the private functions.
  14. -spec mock(Opts) -> ok when
  15. Opts :: [Option],
  16. Option :: {upgrade, [App]}
  17. | {cache_dir, string()}
  18. | {default_vsn, Vsn}
  19. | {override_vsn, [{App, Vsn}]}
  20. | {not_in_index, [{App, Vsn}]}
  21. | {pkgdeps, [{{App,Vsn}, [Dep]}]},
  22. App :: string(),
  23. Dep :: {App, string(), {pkg, App, Vsn, Hash}},
  24. Vsn :: string(),
  25. Hash :: string() | undefined.
  26. mock(Opts) ->
  27. meck:new(?MOD, [no_link, passthrough]),
  28. mock_lock(Opts),
  29. mock_update(Opts),
  30. mock_vsn(Opts),
  31. mock_download(Opts),
  32. mock_pkg_index(Opts),
  33. ok.
  34. unmock() ->
  35. meck:unload(?MOD),
  36. meck:unload(rebar_packages).
  37. %%%%%%%%%%%%%%%
  38. %%% Private %%%
  39. %%%%%%%%%%%%%%%
  40. %% @doc creates values for a lock file.
  41. mock_lock(_) ->
  42. meck:expect(?MOD, lock, fun(_AppDir, {pkg, Name, Vsn, Hash, _RepoConfig}) -> {pkg, Name, Vsn, Hash} end).
  43. %% @doc The config passed to the `mock/2' function can specify which apps
  44. %% should be updated on a per-name basis: `{update, ["App1", "App3"]}'.
  45. mock_update(Opts) ->
  46. ToUpdate = proplists:get_value(upgrade, Opts, []),
  47. meck:expect(
  48. ?MOD, needs_update,
  49. fun(_Dir, {pkg, App, _Vsn, _Hash, _}) ->
  50. lists:member(binary_to_list(App), ToUpdate)
  51. end).
  52. %% @doc Replicated an unsupported call.
  53. mock_vsn(_Opts) ->
  54. meck:expect(
  55. ?MOD, make_vsn,
  56. fun(_Dir) ->
  57. {error, "Replacing version of type pkg not supported."}
  58. end).
  59. %% @doc For each app to download, create a dummy app on disk instead.
  60. %% The configuration for this one (passed in from `mock/1') includes:
  61. %%
  62. %% - Specify a version with `{pkg, _, Vsn, _}'
  63. %% - Dependencies for each application must be passed of the form:
  64. %% `{pkgdeps, [{"app1", [{app2, ".*", {pkg, ...}}]}]}' -- basically
  65. %% the `pkgdeps' option takes a key/value list of terms to output directly
  66. %% into a `rebar.config' file to describe dependencies.
  67. mock_download(Opts) ->
  68. Deps = proplists:get_value(pkgdeps, Opts, []),
  69. Config = proplists:get_value(config, Opts, []),
  70. meck:expect(
  71. ?MOD, download,
  72. fun (Dir, {pkg, AppBin, Vsn, _, _}, _) ->
  73. App = binary_to_list(AppBin),
  74. filelib:ensure_dir(Dir),
  75. AppDeps = proplists:get_value({App,Vsn}, Deps, []),
  76. {ok, AppInfo} = rebar_test_utils:create_app(
  77. Dir, App, binary_to_list(Vsn),
  78. [kernel, stdlib] ++ [element(1,D) || D <- AppDeps]
  79. ),
  80. rebar_test_utils:create_config(Dir, [{deps, AppDeps}]++Config),
  81. TarApp = App++"-"++binary_to_list(Vsn)++".tar",
  82. Tarball = filename:join([Dir, TarApp]),
  83. Contents = filename:join([Dir, "contents.tar.gz"]),
  84. Files = all_files(rebar_app_info:dir(AppInfo)),
  85. ok = erl_tar:create(Contents,
  86. archive_names(Dir, App, Vsn, Files),
  87. [compressed]),
  88. ok = erl_tar:create(Tarball,
  89. [{"contents.tar.gz", Contents}],
  90. []),
  91. Cache = proplists:get_value(cache_dir, Opts, filename:join(Dir,"cache")),
  92. Cached = filename:join([Cache, TarApp]),
  93. filelib:ensure_dir(Cached),
  94. rebar_file_utils:mv(Tarball, Cached),
  95. {ok, true}
  96. end).
  97. %% @doc On top of the pkg resource mocking, we need to mock the package
  98. %% index.
  99. %%
  100. %% A special option, `{not_in_index, [App]}' lets the index leave out
  101. %% specific applications otherwise listed.
  102. mock_pkg_index(Opts) ->
  103. Deps = proplists:get_value(pkgdeps, Opts, []),
  104. Repos = proplists:get_value(repos, Opts, [<<"hexpm">>]),
  105. Skip = proplists:get_value(not_in_index, Opts, []),
  106. %% Dict: {App, Vsn}: [{<<"link">>, <<>>}, {<<"deps">>, []}]
  107. %% Index: all apps and deps in the index
  108. Dict = find_parts(Deps, Skip),
  109. to_index(Deps, Dict, Repos),
  110. meck:new(rebar_packages, [passthrough, no_link]),
  111. meck:expect(rebar_packages, update_package,
  112. fun(_, _, _State) -> ok end),
  113. meck:expect(rebar_packages, verify_table,
  114. fun(_State) -> true end).
  115. %%%%%%%%%%%%%%%
  116. %%% Helpers %%%
  117. %%%%%%%%%%%%%%%
  118. all_files(Dir) ->
  119. filelib:wildcard(filename:join([Dir, "**"])).
  120. archive_names(Dir, _App, _Vsn, Files) ->
  121. [{(F -- Dir) -- "/", F} || F <- Files].
  122. find_parts(Apps, Skip) -> find_parts(Apps, Skip, dict:new()).
  123. find_parts([], _, Acc) -> Acc;
  124. find_parts([{AppName, Deps}|Rest], Skip, Acc) ->
  125. case lists:member(AppName, Skip) orelse dict:is_key(AppName,Acc) of
  126. true -> find_parts(Rest, Skip, Acc);
  127. false ->
  128. AccNew = dict:store(AppName,
  129. Deps,
  130. Acc),
  131. find_parts(Rest, Skip, AccNew)
  132. end.
  133. parse_deps(Deps) ->
  134. [{maps:get(app, D, Name), {pkg, Name, Constraint, undefined}} || D=#{package := Name,
  135. requirement := Constraint} <- Deps].
  136. to_index(AllDeps, Dict, Repos) ->
  137. catch ets:delete(?PACKAGE_TABLE),
  138. rebar_packages:new_package_table(),
  139. dict:fold(
  140. fun({N, V}, Deps, _) ->
  141. DepsList = [#{package => DKB,
  142. app => DKB,
  143. requirement => DVB,
  144. source => {pkg, DKB, DVB, undefined}}
  145. || {DK, DV} <- Deps,
  146. DKB <- [ec_cnv:to_binary(DK)],
  147. DVB <- [ec_cnv:to_binary(DV)]],
  148. Repo = rebar_test_utils:random_element(Repos),
  149. ets:insert(?PACKAGE_TABLE, #package{key={N, V, Repo},
  150. dependencies=parse_deps(DepsList),
  151. retired=false,
  152. checksum = <<"checksum">>})
  153. end, ok, Dict),
  154. lists:foreach(fun({{Name, Vsn}, _}) ->
  155. case lists:any(fun(R) ->
  156. ets:member(?PACKAGE_TABLE, {ec_cnv:to_binary(Name), Vsn, R})
  157. end, Repos) of
  158. false ->
  159. Repo = rebar_test_utils:random_element(Repos),
  160. ets:insert(?PACKAGE_TABLE, #package{key={ec_cnv:to_binary(Name), Vsn, Repo},
  161. dependencies=[],
  162. retired=false,
  163. checksum = <<"checksum">>});
  164. true ->
  165. ok
  166. end
  167. end, AllDeps).