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.

171 lines
5.7 KiB

  1. %%% This suite can't run tests for built-in templates because
  2. %%% they require being escriptize and we currently don't support
  3. %%% this in here!
  4. -module(rebar_new_SUITE).
  5. -compile(export_all).
  6. -include_lib("common_test/include/ct.hrl").
  7. -include_lib("eunit/include/eunit.hrl").
  8. all() -> [app_git_user, app_hg_user, app_with_fallbacks,
  9. app_with_flags1, app_with_flags2, plugin_tpl].
  10. init_per_testcase(plugin_tpl, Config) ->
  11. application:load(rebar),
  12. DataDir = ?config(data_dir, Config),
  13. PrivDir = ?config(priv_dir, Config),
  14. Name = rebar_test_utils:create_random_name("plugin_tpl"),
  15. AppsDir = filename:join([PrivDir, rebar_test_utils:create_random_name(Name)]),
  16. ec_file:copy(filename:join([DataDir, "plugin_tpl"]), AppsDir, [recursive]),
  17. Verbosity = rebar3:log_level(),
  18. rebar_log:init(command_line, Verbosity),
  19. GlobalDir = filename:join([DataDir, "cache"]),
  20. State = rebar_state:new([{base_dir, filename:join([AppsDir, "_build"])}
  21. ,{global_rebar_dir, GlobalDir}
  22. ,{root_dir, AppsDir}]),
  23. mock_home_dir(DataDir),
  24. mock_empty_escript_templates(),
  25. [{apps, AppsDir}, {state, State}, {name, Name} | Config];
  26. init_per_testcase(Case, Config0) ->
  27. Config = rebar_test_utils:init_rebar_state(Config0),
  28. Name = rebar_test_utils:create_random_name(atom_to_list(Case)),
  29. Data = ?config(data_dir, Config),
  30. mock_home_dir(Data),
  31. mock_empty_escript_templates(),
  32. [{name, Name} | Config].
  33. end_per_testcase(_, Config) ->
  34. meck:unload(),
  35. Config.
  36. mock_home_dir(Path) ->
  37. meck:new(rebar_dir, [passthrough]),
  38. meck:expect(rebar_dir, template_dir, fun(_) -> Path end).
  39. mock_empty_escript_templates() ->
  40. %% Can't find escript templates unless we run
  41. %% from the escript, which obviously crashes these here tests.
  42. meck:new(rebar_utils, [passthrough]),
  43. meck:expect(rebar_utils, escript_foldl, fun(_,_,_) -> {ok, []} end).
  44. app_git_user(Config) ->
  45. meck:expect(rebar_utils, sh, fun("git config --global user.name", _) -> {ok, "gitname"};
  46. ("git config --global user.email", _) -> {ok, "git@email.com"}
  47. end),
  48. Name = ?config(name, Config),
  49. rebar_test_utils:run_and_check(
  50. Config, [],
  51. ["new", "test_app", Name, "author_name=some_name"],
  52. {ok, []}
  53. ),
  54. validate_files(
  55. Config, Name,
  56. [{"LICENSE", ["some_name", "git@email.com"]},
  57. {"README.md", [Name]},
  58. {".gitignore", []},
  59. {"rebar.config", []},
  60. {filename:join(["src", Name++".app.src"]), [Name]},
  61. {filename:join(["src", Name++"_sup.erl"]), [Name]},
  62. {filename:join(["src", Name++"_app.erl"]), [Name]}
  63. ]).
  64. app_with_fallbacks(Config) ->
  65. meck:expect(rebar_utils, sh, fun(_, _) -> {error, fallback} end),
  66. Name = ?config(name, Config),
  67. rebar_test_utils:run_and_check(
  68. Config, [],
  69. ["new", "test_app", Name],
  70. {ok, []}
  71. ),
  72. validate_files(
  73. Config, Name,
  74. [{"LICENSE", ["Anonymous", "anonymous@example.org"]},
  75. {"README.md", [Name]},
  76. {".gitignore", []},
  77. {"rebar.config", []},
  78. {filename:join(["src", Name++".app.src"]), [Name]},
  79. {filename:join(["src", Name++"_sup.erl"]), [Name]},
  80. {filename:join(["src", Name++"_app.erl"]), [Name]}
  81. ]).
  82. app_hg_user(Config) ->
  83. meck:expect(rebar_utils, sh, fun("hg showconfig ui.username", _) -> {ok, "hgname <hg@email.com>"};
  84. (_, _) -> {error, fallback}
  85. end),
  86. Name = ?config(name, Config),
  87. rebar_test_utils:run_and_check(
  88. Config, [],
  89. ["new", "test_app", Name],
  90. {ok, []}
  91. ),
  92. validate_files(
  93. Config, Name,
  94. [{"LICENSE", ["hgname", "hg@email.com"]},
  95. {"README.md", [Name]},
  96. {".gitignore", []},
  97. {"rebar.config", []},
  98. {filename:join(["src", Name++".app.src"]), [Name]},
  99. {filename:join(["src", Name++"_sup.erl"]), [Name]},
  100. {filename:join(["src", Name++"_app.erl"]), [Name]}
  101. ]).
  102. app_with_flags1(Config) ->
  103. Name = ?config(name, Config),
  104. rebar_test_utils:run_and_check(
  105. Config, [],
  106. ["new", "test_app", "-f", Name],
  107. {ok, []}
  108. ),
  109. validate_files(
  110. Config, Name,
  111. [{"LICENSE", []},
  112. {"README.md", []},
  113. {".gitignore", []},
  114. {"rebar.config", []},
  115. {filename:join(["src", Name++".app.src"]), [Name]},
  116. {filename:join(["src", Name++"_sup.erl"]), [Name]},
  117. {filename:join(["src", Name++"_app.erl"]), [Name]}
  118. ]).
  119. app_with_flags2(Config) ->
  120. Name = ?config(name, Config),
  121. rebar_test_utils:run_and_check(
  122. Config, [],
  123. ["new", "-f", "test_app", Name],
  124. {ok, []}
  125. ),
  126. validate_files(
  127. Config, Name,
  128. [{"LICENSE", []},
  129. {"README.md", []},
  130. {".gitignore", []},
  131. {"rebar.config", []},
  132. {filename:join(["src", Name++".app.src"]), [Name]},
  133. {filename:join(["src", Name++"_sup.erl"]), [Name]},
  134. {filename:join(["src", Name++"_app.erl"]), [Name]}
  135. ]).
  136. plugin_tpl(Config) ->
  137. Name = ?config(name, Config),
  138. rebar_test_utils:run_and_check(
  139. Config, [],
  140. ["new", "-f", "tpl", Name],
  141. {ok, []}
  142. ),
  143. Result = filename:join(["src", Name++".erl"]), % In CWD
  144. {ok, Bin} = file:read_file(Result),
  145. {match, _} = re:run(Bin, Name, [multiline,global]).
  146. validate_files(_Config, Name, Checks) ->
  147. [begin
  148. Path = filename:join([Name, File]),
  149. ct:pal("validating ~s for content", [Path]),
  150. {ok, Bin} = file:read_file(Path),
  151. [{match, _} = re:run(Bin, Pattern, [multiline,global])
  152. || Pattern <- Patterns]
  153. end || {File, Patterns} <- Checks],
  154. ok.