選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

180 行
6.7 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]),
  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, Source) -> Source 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. Skip = proplists:get_value(not_in_index, Opts, []),
  105. %% Dict: {App, Vsn}: [{<<"link">>, <<>>}, {<<"deps">>, []}]
  106. %% Index: all apps and deps in the index
  107. Dict = find_parts(Deps, Skip),
  108. meck:new(rebar_packages, [passthrough, no_link]),
  109. %% meck:expect(rebar_packages, packages,
  110. %% fun(_State) -> to_index(Deps, Dict) end),
  111. meck:expect(rebar_packages, verify_table,
  112. fun(_State) -> to_index(Deps, Dict), true end).
  113. %%%%%%%%%%%%%%%
  114. %%% Helpers %%%
  115. %%%%%%%%%%%%%%%
  116. all_files(Dir) ->
  117. filelib:wildcard(filename:join([Dir, "**"])).
  118. archive_names(Dir, _App, _Vsn, Files) ->
  119. [{(F -- Dir) -- "/", F} || F <- Files].
  120. find_parts(Apps, Skip) -> find_parts(Apps, Skip, dict:new()).
  121. find_parts([], _, Acc) -> Acc;
  122. find_parts([{AppName, Deps}|Rest], Skip, Acc) ->
  123. case lists:member(AppName, Skip) orelse dict:is_key(AppName,Acc) of
  124. true -> find_parts(Rest, Skip, Acc);
  125. false ->
  126. AccNew = dict:store(AppName,
  127. Deps,
  128. Acc),
  129. find_parts(Rest, Skip, AccNew)
  130. end.
  131. parse_deps(Deps) ->
  132. [{maps:get(app, D, Name), {pkg, Name, Constraint, undefined}} || D=#{package := Name,
  133. requirement := Constraint} <- Deps].
  134. to_index(AllDeps, Dict) ->
  135. catch ets:delete(?PACKAGE_TABLE),
  136. rebar_packages:new_package_table(),
  137. dict:fold(
  138. fun(K, Deps, _) ->
  139. DepsList = [#{package => DKB,
  140. app => DKB,
  141. requirement => DVB,
  142. source => {pkg, DKB, DVB, undefined}}
  143. || {DK, DV} <- Deps,
  144. DKB <- [ec_cnv:to_binary(DK)],
  145. DVB <- [ec_cnv:to_binary(DV)]],
  146. ets:insert(?PACKAGE_TABLE, #package{key = K,
  147. dependencies = parse_deps(DepsList),
  148. checksum = <<"checksum">>})
  149. end, ok, Dict),
  150. lists:foreach(fun({{Name, Vsn}, _}) ->
  151. case ets:member(?PACKAGE_TABLE, {ec_cnv:to_binary(Name), Vsn}) of
  152. false ->
  153. ets:insert(?PACKAGE_TABLE, #package{key =
  154. {ec_cnv:to_binary(Name), Vsn},
  155. dependencies = [],
  156. checksum = <<"checksum">>});
  157. true ->
  158. ok
  159. end
  160. end, AllDeps).