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.

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