Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

298 řádky
12 KiB

  1. -module(rebar_test_utils).
  2. -include_lib("common_test/include/ct.hrl").
  3. -include_lib("eunit/include/eunit.hrl").
  4. -export([init_rebar_state/1, init_rebar_state/2, run_and_check/4]).
  5. -export([expand_deps/2, flat_deps/1, flat_pkgdeps/1, top_level_deps/1]).
  6. -export([create_app/4, create_empty_app/4, create_config/2]).
  7. -export([create_random_name/1, create_random_vsn/0]).
  8. %%%%%%%%%%%%%%
  9. %%% Public %%%
  10. %%%%%%%%%%%%%%
  11. %% @doc {@see init_rebar_state/2}
  12. init_rebar_state(Config) -> init_rebar_state(Config, "apps_dir1_").
  13. %% @doc Takes a common test config and a name (string) and sets up
  14. %% a basic OTP app directory with a pre-configured rebar state to
  15. %% run tests with.
  16. init_rebar_state(Config, Name) ->
  17. application:load(rebar),
  18. DataDir = ?config(priv_dir, Config),
  19. AppsDir = filename:join([DataDir, create_random_name(Name)]),
  20. CheckoutsDir = filename:join([AppsDir, "_checkouts"]),
  21. ok = ec_file:mkdir_p(AppsDir),
  22. ok = ec_file:mkdir_p(CheckoutsDir),
  23. Verbosity = rebar3:log_level(),
  24. rebar_log:init(command_line, Verbosity),
  25. State = rebar_state:new([{base_dir, filename:join([AppsDir, "_build"])}
  26. ,{root_dir, AppsDir}]),
  27. [{apps, AppsDir}, {checkouts, CheckoutsDir}, {state, State} | Config].
  28. %% @doc Takes common test config, a rebar config ([] if empty), a command to
  29. %% run ("install_deps", "compile", etc.), and a list of expected applications
  30. %% and/or dependencies to be present, and verifies whether they are all in
  31. %% place.
  32. %%
  33. %% The expectation list takes elements of the form:
  34. %% - `{app, Name :: string()}': checks that the app is properly built.
  35. %% - `{dep, Name :: string()}': checks that the dependency has been fetched.
  36. %% Ignores the build status of the dependency.
  37. %% - `{dep, Name :: string(), Vsn :: string()}': checks that the dependency
  38. %% has been fetched, and that a given version has been chosen. Useful to
  39. %% test for conflict resolution. Also ignores the build status of the
  40. %% dependency.
  41. %%
  42. %% This function assumes `init_rebar_state/1-2' has run before, in order to
  43. %% fetch the `apps' and `state' values from the CT config.
  44. run_and_check(Config, RebarConfig, Command, Expect) ->
  45. %% Assumes init_rebar_state has run first
  46. AppDir = ?config(apps, Config),
  47. State = ?config(state, Config),
  48. try
  49. Res = rebar3:run(rebar_state:new(State, RebarConfig, AppDir), Command),
  50. case Expect of
  51. {error, Reason} ->
  52. ?assertEqual({error, Reason}, Res);
  53. {ok, Expected} ->
  54. {ok, _} = Res,
  55. check_results(AppDir, Expected);
  56. return ->
  57. Res
  58. end
  59. catch
  60. rebar_abort when Expect =:= rebar_abort -> rebar_abort
  61. end.
  62. %% @doc Creates a dummy application including:
  63. %% - src/<file>.erl
  64. %% - src/<file>.app.src
  65. %% And returns a `rebar_app_info' object.
  66. create_app(AppDir, Name, Vsn, Deps) ->
  67. write_src_file(AppDir, Name),
  68. write_test_file(AppDir, Name),
  69. write_app_src_file(AppDir, Name, Vsn, Deps),
  70. rebar_app_info:new(Name, Vsn, AppDir, Deps).
  71. %% @doc Creates a dummy application including:
  72. %% - ebin/<file>.app
  73. %% And returns a `rebar_app_info' object.
  74. create_empty_app(AppDir, Name, Vsn, Deps) ->
  75. write_app_file(AppDir, Name, Vsn, Deps),
  76. rebar_app_info:new(Name, Vsn, AppDir, Deps).
  77. %% @doc Creates a rebar.config file. The function accepts a list of terms,
  78. %% each of which will be dumped as a consult file. For example, the list
  79. %% `[a, b, c]' will return the consult file `a. b. c.'.
  80. create_config(AppDir, Contents) ->
  81. Conf = filename:join([AppDir, "rebar.config"]),
  82. ok = filelib:ensure_dir(Conf),
  83. Config = lists:flatten([io_lib:fwrite("~p.~n", [Term]) || Term <- Contents]),
  84. ok = ec_file:write(Conf, Config),
  85. Conf.
  86. %% @doc Util to create a random variation of a given name.
  87. create_random_name(Name) ->
  88. random:seed(erlang:now()),
  89. Name ++ erlang:integer_to_list(random:uniform(1000000)).
  90. %% @doc Util to create a random variation of a given version.
  91. create_random_vsn() ->
  92. random:seed(erlang:now()),
  93. lists:flatten([erlang:integer_to_list(random:uniform(100)),
  94. ".", erlang:integer_to_list(random:uniform(100)),
  95. ".", erlang:integer_to_list(random:uniform(100))]).
  96. expand_deps(_, []) -> [];
  97. expand_deps(git, [{Name, Deps} | Rest]) ->
  98. Dep = {Name, ".*", {git, "https://example.org/user/"++Name++".git", "master"}},
  99. [{Dep, expand_deps(git, Deps)} | expand_deps(git, Rest)];
  100. expand_deps(git, [{Name, Vsn, Deps} | Rest]) ->
  101. Dep = {Name, Vsn, {git, "https://example.org/user/"++Name++".git", {tag, Vsn}}},
  102. [{Dep, expand_deps(git, Deps)} | expand_deps(git, Rest)];
  103. expand_deps(pkg, [{Name, Deps} | Rest]) ->
  104. Dep = {pkg, Name, "0.0.0"},
  105. [{Dep, expand_deps(pkg, Deps)} | expand_deps(pkg, Rest)];
  106. expand_deps(pkg, [{Name, Vsn, Deps} | Rest]) ->
  107. Dep = {pkg, Name, Vsn},
  108. [{Dep, expand_deps(pkg, Deps)} | expand_deps(pkg, Rest)].
  109. flat_deps([]) -> [];
  110. flat_deps([{{Name,_Vsn,Ref}, Deps} | Rest]) ->
  111. [{{Name,vsn_from_ref(Ref)}, top_level_deps(Deps)}]
  112. ++
  113. flat_deps(Deps)
  114. ++
  115. flat_deps(Rest).
  116. flat_pkgdeps([]) -> [];
  117. flat_pkgdeps([{{pkg, Name, Vsn}, Deps} | Rest]) ->
  118. [{{iolist_to_binary(Name),iolist_to_binary(Vsn)}, top_level_deps(Deps)}]
  119. ++
  120. flat_pkgdeps(Deps)
  121. ++
  122. flat_pkgdeps(Rest).
  123. vsn_from_ref({git, _, {_, Vsn}}) -> Vsn;
  124. vsn_from_ref({git, _, Vsn}) -> Vsn.
  125. top_level_deps([]) -> [];
  126. top_level_deps([{{pkg, Name, Vsn}, _} | Deps]) ->
  127. [{list_to_atom(Name), Vsn} | top_level_deps(Deps)];
  128. top_level_deps([{{Name, Vsn, Ref}, _} | Deps]) ->
  129. [{list_to_atom(Name), Vsn, Ref} | top_level_deps(Deps)].
  130. %%%%%%%%%%%%%%%
  131. %%% Helpers %%%
  132. %%%%%%%%%%%%%%%
  133. check_results(AppDir, Expected) ->
  134. BuildDirs = filelib:wildcard(filename:join([AppDir, "_build", "*", "lib"])),
  135. CheckoutsDir = filename:join([AppDir, "_checkouts"]),
  136. LockFile = filename:join([AppDir, "rebar.lock"]),
  137. Locks = lists:flatten(rebar_config:consult_file(LockFile)),
  138. InvalidApps = rebar_app_discover:find_apps(BuildDirs, invalid),
  139. ValidApps = rebar_app_discover:find_apps(BuildDirs, valid),
  140. InvalidDepsNames = [{ec_cnv:to_list(rebar_app_info:name(App)), App} || App <- InvalidApps],
  141. ValidDepsNames = [{ec_cnv:to_list(rebar_app_info:name(App)), App} || App <- ValidApps],
  142. Deps = rebar_app_discover:find_apps(BuildDirs, all),
  143. DepsNames = [{ec_cnv:to_list(rebar_app_info:name(App)), App} || App <- Deps],
  144. Checkouts = rebar_app_discover:find_apps([CheckoutsDir], all),
  145. CheckoutsNames = [{ec_cnv:to_list(rebar_app_info:name(App)), App} || App <- Checkouts],
  146. lists:foreach(
  147. fun({app, Name}) ->
  148. ct:pal("Name: ~p", [Name]),
  149. case lists:keyfind(Name, 1, DepsNames) of
  150. false ->
  151. error({app_not_found, Name});
  152. {Name, _App} ->
  153. ok
  154. end
  155. ; ({app, Name, invalid}) ->
  156. ct:pal("Name: ~p", [Name]),
  157. case lists:keyfind(Name, 1, InvalidDepsNames) of
  158. false ->
  159. error({app_not_found, Name});
  160. {Name, _App} ->
  161. ok
  162. end
  163. ; ({app, Name, valid}) ->
  164. ct:pal("Name: ~p", [Name]),
  165. case lists:keyfind(Name, 1, ValidDepsNames) of
  166. false ->
  167. error({app_not_found, Name});
  168. {Name, _App} ->
  169. ok
  170. end
  171. ; ({checkout, Name}) ->
  172. ct:pal("Name: ~p", [Name]),
  173. ?assertNotEqual(false, lists:keyfind(Name, 1, CheckoutsNames))
  174. ; ({dep, Name}) ->
  175. ct:pal("Name: ~p", [Name]),
  176. ?assertNotEqual(false, lists:keyfind(Name, 1, DepsNames))
  177. ; ({dep, Name, Vsn}) ->
  178. ct:pal("Name: ~p, Vsn: ~p", [Name, Vsn]),
  179. case lists:keyfind(Name, 1, DepsNames) of
  180. false ->
  181. error({dep_not_found, Name});
  182. {Name, App} ->
  183. ?assertEqual(iolist_to_binary(Vsn),
  184. iolist_to_binary(rebar_app_info:original_vsn(App)))
  185. end
  186. ; ({lock, Name}) ->
  187. ct:pal("Name: ~p", [Name]),
  188. ?assertNotEqual(false, lists:keyfind(iolist_to_binary(Name), 1, Locks))
  189. ; ({lock, Name, Vsn}) ->
  190. ct:pal("Name: ~p, Vsn: ~p", [Name, Vsn]),
  191. case lists:keyfind(iolist_to_binary(Name), 1, Locks) of
  192. false ->
  193. error({lock_not_found, Name});
  194. {_LockName, {pkg, _, LockVsn}, _} ->
  195. ?assertEqual(iolist_to_binary(Vsn),
  196. iolist_to_binary(LockVsn));
  197. {_LockName, {_, _, {ref, LockVsn}}, _} ->
  198. ?assertEqual(iolist_to_binary(Vsn),
  199. iolist_to_binary(LockVsn))
  200. end
  201. ; ({release, Name, Vsn}) ->
  202. ct:pal("Release: ~p-~s", [Name, Vsn]),
  203. {ok, Cwd} = file:get_cwd(),
  204. try
  205. file:set_cwd(AppDir),
  206. [ReleaseDir] = filelib:wildcard(filename:join([AppDir, "_build", "*", "rel"])),
  207. RelxState = rlx_state:new("", [], []),
  208. RelxState1 = rlx_state:base_output_dir(RelxState, ReleaseDir),
  209. {ok, RelxState2} = rlx_prv_app_discover:do(RelxState1),
  210. {ok, RelxState3} = rlx_prv_rel_discover:do(RelxState2),
  211. %% throws not_found if it doesn't exist
  212. rlx_state:get_realized_release(RelxState3, Name, Vsn)
  213. catch
  214. _ ->
  215. ct:fail(release_not_found)
  216. after
  217. file:set_cwd(Cwd)
  218. end
  219. ; ({tar, Name, Vsn}) ->
  220. ct:pal("Tarball: ~s-~s", [Name, Vsn]),
  221. Tarball = filename:join([AppDir, "_build", "rel", Name, Name++"-"++Vsn++".tar.gz"]),
  222. ?assertNotEqual([], filelib:is_file(Tarball))
  223. ; ({file, Filename}) ->
  224. ct:pal("Filename: ~s", [Filename]),
  225. ?assert(filelib:is_file(Filename))
  226. end, Expected).
  227. write_src_file(Dir, Name) ->
  228. Erl = filename:join([Dir, "src", "not_a_real_src_" ++ Name ++ ".erl"]),
  229. ok = filelib:ensure_dir(Erl),
  230. ok = ec_file:write(Erl, erl_src_file("not_a_real_src_" ++ Name ++ ".erl")).
  231. write_test_file(Dir, Name) ->
  232. Erl = filename:join([Dir, "test", "not_a_real_src_" ++ Name ++ "_tests.erl"]),
  233. ok = filelib:ensure_dir(Erl),
  234. ok = ec_file:write(Erl, erl_test_file("not_a_real_src_" ++ Name ++ ".erl")).
  235. write_app_file(Dir, Name, Version, Deps) ->
  236. Filename = filename:join([Dir, "ebin", Name ++ ".app"]),
  237. ok = filelib:ensure_dir(Filename),
  238. ok = ec_file:write_term(Filename, get_app_metadata(ec_cnv:to_list(Name), Version, Deps)).
  239. write_app_src_file(Dir, Name, Version, Deps) ->
  240. Filename = filename:join([Dir, "src", Name ++ ".app.src"]),
  241. ok = filelib:ensure_dir(Filename),
  242. ok = ec_file:write_term(Filename, get_app_metadata(ec_cnv:to_list(Name), Version, Deps)).
  243. erl_src_file(Name) ->
  244. io_lib:format("-module(~s).\n"
  245. "-export([main/0]).\n"
  246. "main() -> ok.\n"
  247. "-ifdef(TEST).\n"
  248. "-include_lib(\"eunit/include/eunit.hrl\").\n"
  249. "some_test_() -> ?_assertEqual(ok, main()).\n"
  250. "-endif.\n", [filename:basename(Name, ".erl")]).
  251. erl_test_file(Name) ->
  252. BaseName = filename:basename(Name, ".erl"),
  253. io_lib:format("-module(~s_tests).\n"
  254. "-compile(export_all).\n"
  255. "-ifndef(some_define).\n"
  256. "-define(some_define, false).\n"
  257. "-endif.\n"
  258. "-ifdef(TEST).\n"
  259. "-include_lib(\"eunit/include/eunit.hrl\").\n"
  260. "some_test_() -> ?_assertEqual(ok, ~s:main()).\n"
  261. "define_test_() -> ?_assertEqual(true, ?some_define).\n"
  262. "-endif.\n", [BaseName, BaseName]).
  263. get_app_metadata(Name, Vsn, Deps) ->
  264. {application, erlang:list_to_atom(Name),
  265. [{description, ""},
  266. {vsn, Vsn},
  267. {modules, []},
  268. {included_applications, []},
  269. {registered, []},
  270. {applications, Deps}]}.