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.

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