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.

142 line
4.5 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].
  10. init_per_testcase(Case, Config0) ->
  11. Config = rebar_test_utils:init_rebar_state(Config0),
  12. Name = rebar_test_utils:create_random_name(atom_to_list(Case)),
  13. Data = ?config(data_dir, Config),
  14. mock_home_dir(Data),
  15. mock_empty_escript_templates(),
  16. [{name, Name} | Config].
  17. end_per_testcase(_, Config) ->
  18. meck:unload(),
  19. Config.
  20. mock_home_dir(Path) ->
  21. meck:new(rebar_dir, [passthrough]),
  22. meck:expect(rebar_dir, template_dir, fun(_) -> Path end).
  23. mock_empty_escript_templates() ->
  24. %% Can't find escript templates unless we run
  25. %% from the escript, which obviously crashes these here tests.
  26. meck:new(rebar_utils, [passthrough]),
  27. meck:expect(rebar_utils, escript_foldl, fun(_,_,_) -> {ok, []} end).
  28. app_git_user(Config) ->
  29. meck:expect(rebar_utils, sh, fun("git config --global user.name", _) -> {ok, "gitname"};
  30. ("git config --global user.email", _) -> {ok, "git@email.com"}
  31. end),
  32. Name = ?config(name, Config),
  33. rebar_test_utils:run_and_check(
  34. Config, [],
  35. ["new", "test_app", Name, "author_name=some_name"],
  36. {ok, []}
  37. ),
  38. validate_files(
  39. Config, Name,
  40. [{"LICENSE", ["some_name", "git@email.com"]},
  41. {"README.md", [Name]},
  42. {".gitignore", []},
  43. {"rebar.config", []},
  44. {filename:join(["src", Name++".app.src"]), [Name]},
  45. {filename:join(["src", Name++"_sup.erl"]), [Name]},
  46. {filename:join(["src", Name++"_app.erl"]), [Name]}
  47. ]).
  48. app_with_fallbacks(Config) ->
  49. meck:expect(rebar_utils, sh, fun(_, _) -> {error, fallback} end),
  50. Name = ?config(name, Config),
  51. rebar_test_utils:run_and_check(
  52. Config, [],
  53. ["new", "test_app", Name],
  54. {ok, []}
  55. ),
  56. validate_files(
  57. Config, Name,
  58. [{"LICENSE", ["Anonymous", "anonymous@example.org"]},
  59. {"README.md", [Name]},
  60. {".gitignore", []},
  61. {"rebar.config", []},
  62. {filename:join(["src", Name++".app.src"]), [Name]},
  63. {filename:join(["src", Name++"_sup.erl"]), [Name]},
  64. {filename:join(["src", Name++"_app.erl"]), [Name]}
  65. ]).
  66. app_hg_user(Config) ->
  67. meck:expect(rebar_utils, sh, fun("hg showconfig ui.username", _) -> {ok, "hgname <hg@email.com>"};
  68. (_, _) -> {error, fallback}
  69. end),
  70. Name = ?config(name, Config),
  71. rebar_test_utils:run_and_check(
  72. Config, [],
  73. ["new", "test_app", Name],
  74. {ok, []}
  75. ),
  76. validate_files(
  77. Config, Name,
  78. [{"LICENSE", ["hgname", "hg@email.com"]},
  79. {"README.md", [Name]},
  80. {".gitignore", []},
  81. {"rebar.config", []},
  82. {filename:join(["src", Name++".app.src"]), [Name]},
  83. {filename:join(["src", Name++"_sup.erl"]), [Name]},
  84. {filename:join(["src", Name++"_app.erl"]), [Name]}
  85. ]).
  86. app_with_flags1(Config) ->
  87. Name = ?config(name, Config),
  88. rebar_test_utils:run_and_check(
  89. Config, [],
  90. ["new", "test_app", "-f", Name],
  91. {ok, []}
  92. ),
  93. validate_files(
  94. Config, Name,
  95. [{"LICENSE", []},
  96. {"README.md", []},
  97. {".gitignore", []},
  98. {"rebar.config", []},
  99. {filename:join(["src", Name++".app.src"]), [Name]},
  100. {filename:join(["src", Name++"_sup.erl"]), [Name]},
  101. {filename:join(["src", Name++"_app.erl"]), [Name]}
  102. ]).
  103. app_with_flags2(Config) ->
  104. Name = ?config(name, Config),
  105. rebar_test_utils:run_and_check(
  106. Config, [],
  107. ["new", "-f", "test_app", Name],
  108. {ok, []}
  109. ),
  110. validate_files(
  111. Config, Name,
  112. [{"LICENSE", []},
  113. {"README.md", []},
  114. {".gitignore", []},
  115. {"rebar.config", []},
  116. {filename:join(["src", Name++".app.src"]), [Name]},
  117. {filename:join(["src", Name++"_sup.erl"]), [Name]},
  118. {filename:join(["src", Name++"_app.erl"]), [Name]}
  119. ]).
  120. validate_files(_Config, Name, Checks) ->
  121. [begin
  122. Path = filename:join([Name, File]),
  123. {ok, Bin} = file:read_file(Path),
  124. [{match, _} = re:run(Bin, Pattern, [multiline,global])
  125. || Pattern <- Patterns]
  126. end || {File, Patterns} <- Checks],
  127. ok.