Você não pode selecionar mais de 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.

162 linhas
6.6 KiB

10 anos atrás
  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([create_app/4, create_empty_app/4, create_config/2]).
  6. -export([create_random_name/1, create_random_vsn/0]).
  7. %%%%%%%%%%%%%%
  8. %%% Public %%%
  9. %%%%%%%%%%%%%%
  10. %% @doc {@see init_rebar_state/2}
  11. init_rebar_state(Config) -> init_rebar_state(Config, "apps_dir1_").
  12. %% @doc Takes a common test config and a name (string) and sets up
  13. %% a basic OTP app directory with a pre-configured rebar state to
  14. %% run tests with.
  15. init_rebar_state(Config, Name) ->
  16. application:load(rebar),
  17. DataDir = ?config(priv_dir, Config),
  18. AppsDir = filename:join([DataDir, create_random_name(Name)]),
  19. CheckoutsDir = filename:join([AppsDir, "_checkouts"]),
  20. ok = ec_file:mkdir_p(AppsDir),
  21. ok = ec_file:mkdir_p(CheckoutsDir),
  22. Verbosity = rebar3:log_level(),
  23. rebar_log:init(command_line, Verbosity),
  24. State = rebar_state:new([{base_dir, filename:join([AppsDir, "_build"])}]),
  25. [{apps, AppsDir}, {checkouts, CheckoutsDir}, {state, State} | Config].
  26. %% @doc Takes common test config, a rebar config ([] if empty), a command to
  27. %% run ("install_deps", "compile", etc.), and a list of expected applications
  28. %% and/or dependencies to be present, and verifies whether they are all in
  29. %% place.
  30. %%
  31. %% The expectation list takes elements of the form:
  32. %% - `{app, Name :: string()}': checks that the app is properly built.
  33. %% - `{dep, Name :: string()}': checks that the dependency has been fetched.
  34. %% Ignores the build status of the dependency.
  35. %% - `{dep, Name :: string(), Vsn :: string()}': checks that the dependency
  36. %% has been fetched, and that a given version has been chosen. Useful to
  37. %% test for conflict resolution. Also ignores the build status of the
  38. %% dependency.
  39. %%
  40. %% This function assumes `init_rebar_state/1-2' has run before, in order to
  41. %% fetch the `apps' and `state' values from the CT config.
  42. run_and_check(Config, RebarConfig, Command, Expect) ->
  43. %% Assumes init_rebar_state has run first
  44. AppDir = ?config(apps, Config),
  45. State = ?config(state, Config),
  46. Res = rebar3:run(rebar_state:new(State, RebarConfig, AppDir), Command),
  47. case Expect of
  48. {error, Reason} ->
  49. ?assertEqual({error, Reason}, Res);
  50. {ok, Expected} ->
  51. {ok, _} = Res,
  52. check_results(AppDir, Expected)
  53. end.
  54. %% @doc Creates a dummy application including:
  55. %% - src/<file>.erl
  56. %% - src/<file>.app.src
  57. %% And returns a `rebar_app_info' object.
  58. create_app(AppDir, Name, Vsn, Deps) ->
  59. write_src_file(AppDir, Name),
  60. write_app_src_file(AppDir, Name, Vsn, Deps),
  61. rebar_app_info:new(Name, Vsn, AppDir, Deps).
  62. %% @doc Creates a dummy application including:
  63. %% - ebin/<file>.app
  64. %% And returns a `rebar_app_info' object.
  65. create_empty_app(AppDir, Name, Vsn, Deps) ->
  66. write_app_file(AppDir, Name, Vsn, Deps),
  67. rebar_app_info:new(Name, Vsn, AppDir, Deps).
  68. %% @doc Creates a rebar.config file. The function accepts a list of terms,
  69. %% each of which will be dumped as a consult file. For example, the list
  70. %% `[a, b, c]' will return the consult file `a. b. c.'.
  71. create_config(AppDir, Contents) ->
  72. Conf = filename:join([AppDir, "rebar.config"]),
  73. ok = filelib:ensure_dir(Conf),
  74. Config = lists:flatten([io_lib:fwrite("~p.~n", [Term]) || Term <- Contents]),
  75. ok = ec_file:write(Conf, Config),
  76. Conf.
  77. %% @doc Util to create a random variation of a given name.
  78. create_random_name(Name) ->
  79. random:seed(erlang:now()),
  80. Name ++ erlang:integer_to_list(random:uniform(1000000)).
  81. %% @doc Util to create a random variation of a given version.
  82. create_random_vsn() ->
  83. random:seed(erlang:now()),
  84. lists:flatten([erlang:integer_to_list(random:uniform(100)),
  85. ".", erlang:integer_to_list(random:uniform(100)),
  86. ".", erlang:integer_to_list(random:uniform(100))]).
  87. %%%%%%%%%%%%%%%
  88. %%% Helpers %%%
  89. %%%%%%%%%%%%%%%
  90. check_results(AppDir, Expected) ->
  91. BuildDir = filename:join([AppDir, "_build", "lib"]),
  92. CheckoutsDir = filename:join([AppDir, "_checkouts"]),
  93. Apps = rebar_app_discover:find_apps([AppDir]),
  94. AppsNames = [{ec_cnv:to_list(rebar_app_info:name(App)), App} || App <- Apps],
  95. Deps = rebar_app_discover:find_apps([BuildDir], all),
  96. DepsNames = [{ec_cnv:to_list(rebar_app_info:name(App)), App} || App <- Deps],
  97. Checkouts = rebar_app_discover:find_apps([CheckoutsDir], all),
  98. CheckoutsNames = [{ec_cnv:to_list(rebar_app_info:name(App)), App} || App <- Checkouts],
  99. lists:foreach(
  100. fun({app, Name}) ->
  101. ct:pal("Name: ~p", [Name]),
  102. case lists:keyfind(Name, 1, AppsNames) of
  103. false ->
  104. error({app_not_found, Name});
  105. {Name, _App} ->
  106. ok
  107. end
  108. ; ({checkout, Name}) ->
  109. ct:pal("Name: ~p", [Name]),
  110. ?assertNotEqual(false, lists:keyfind(Name, 1, CheckoutsNames))
  111. ; ({dep, Name}) ->
  112. ct:pal("Name: ~p", [Name]),
  113. ?assertNotEqual(false, lists:keyfind(Name, 1, DepsNames))
  114. ; ({dep, Name, Vsn}) ->
  115. ct:pal("Name: ~p, Vsn: ~p", [Name, Vsn]),
  116. case lists:keyfind(Name, 1, DepsNames) of
  117. false ->
  118. error({dep_not_found, Name});
  119. {Name, App} ->
  120. ?assertEqual(iolist_to_binary(Vsn),
  121. iolist_to_binary(rebar_app_info:original_vsn(App)))
  122. end
  123. end, Expected).
  124. write_src_file(Dir, Name) ->
  125. Erl = filename:join([Dir, "src", "not_a_real_src" ++ Name ++ ".erl"]),
  126. ok = filelib:ensure_dir(Erl),
  127. ok = ec_file:write(Erl, erl_src_file("not_a_real_src" ++ Name ++ ".erl")).
  128. write_app_file(Dir, Name, Version, Deps) ->
  129. Filename = filename:join([Dir, "ebin", Name ++ ".app"]),
  130. ok = filelib:ensure_dir(Filename),
  131. ok = ec_file:write_term(Filename, get_app_metadata(ec_cnv:to_list(Name), Version, Deps)).
  132. write_app_src_file(Dir, Name, Version, Deps) ->
  133. Filename = filename:join([Dir, "src", Name ++ ".app.src"]),
  134. ok = filelib:ensure_dir(Filename),
  135. ok = ec_file:write_term(Filename, get_app_metadata(ec_cnv:to_list(Name), Version, Deps)).
  136. erl_src_file(Name) ->
  137. io_lib:format("-module(~s).\n"
  138. "-export([main/0]).\n"
  139. "main() -> ok.\n", [filename:basename(Name, ".erl")]).
  140. get_app_metadata(Name, Vsn, Deps) ->
  141. {application, erlang:list_to_atom(Name),
  142. [{description, ""},
  143. {vsn, Vsn},
  144. {modules, []},
  145. {included_applications, []},
  146. {registered, []},
  147. {applications, Deps}]}.