Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

229 righe
8.3 KiB

  1. %% -*- tab-width: 4;erlang-indent-level: 4;indent-tabs-mode: nil -*-
  2. %% ex: ts=4 sw=4 et
  3. %% -------------------------------------------------------------------
  4. %%
  5. %% rebar: Erlang Build Tools
  6. %%
  7. %% Copyright (c) 2009, 2010 Dave Smith (dizzyd@dizzyd.com)
  8. %%
  9. %% Permission is hereby granted, free of charge, to any person obtaining a copy
  10. %% of this software and associated documentation files (the "Software"), to deal
  11. %% in the Software without restriction, including without limitation the rights
  12. %% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. %% copies of the Software, and to permit persons to whom the Software is
  14. %% furnished to do so, subject to the following conditions:
  15. %%
  16. %% The above copyright notice and this permission notice shall be included in
  17. %% all copies or substantial portions of the Software.
  18. %%
  19. %% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. %% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. %% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. %% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. %% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. %% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. %% THE SOFTWARE.
  26. %% -------------------------------------------------------------------
  27. %% @author Chris Bernard <cebernard@gmail.com>
  28. %% @doc This tests functionality provided by the rebar command 'eunit'.
  29. %% @copyright 2009, 2010 Dave Smith
  30. %% -------------------------------------------------------------------
  31. -module(rebar_eunit_tests).
  32. -compile(export_all).
  33. -include_lib("eunit/include/eunit.hrl").
  34. %% Assuming this test is run inside the rebar 'eunit'
  35. %% command, the current working directory will be '.eunit'
  36. -define(REBAR_SCRIPT, "../rebar").
  37. -define(TMP_DIR, "tmp_eunit/").
  38. %% ====================================================================
  39. %% Rebar EUnit and Cover Tests
  40. %% ====================================================================
  41. eunit_test_() ->
  42. {"Ensure EUnit runs with tests in a 'test' dir and no defined suite",
  43. setup, fun() -> setup_basic_project(), rebar("-v eunit") end,
  44. fun teardown/1,
  45. fun(RebarOut) ->
  46. [{"Tests in 'test' directory are found and run",
  47. ?_assert(string:str(RebarOut, "myapp_mymod_tests:") =/= 0)},
  48. {"Tests in 'src' directory are found and run",
  49. ?_assert(string:str(RebarOut, "myapp_mymod:") =/= 0)},
  50. {"Tests are only run once",
  51. ?_assert(string:str(RebarOut, "All 2 tests passed") =/= 0)}]
  52. end}.
  53. cover_test_() ->
  54. {"Ensure Cover runs with tests in a test dir and no defined suite",
  55. setup, fun() -> setup_cover_project(), rebar("-v eunit") end,
  56. fun teardown/1,
  57. [{"All cover reports are generated",
  58. assert_files_in("the temporary eunit directory",
  59. expected_cover_generated_files())},
  60. {"Only production modules get coverage reports",
  61. assert_files_not_in("the temporary eunit directory",
  62. [".eunit/myapp_mymod_tests.COVER.html"])}]}.
  63. cover_with_suite_test_() ->
  64. {"Ensure Cover runs with Tests in a test dir and a test suite",
  65. setup,
  66. fun() ->
  67. setup_cover_project_with_suite(),
  68. rebar("-v eunit suite=mysuite")
  69. end,
  70. fun teardown/1,
  71. [{"All cover reports are generated",
  72. assert_files_in("the temporary eunit directory",
  73. expected_cover_generated_files())},
  74. {"Only production modules get coverage reports",
  75. assert_files_not_in("the temporary eunit directory",
  76. [".eunit/myapp_mymod_tests.COVER.html",
  77. ".eunit/mysuite"])}]}.
  78. expected_cover_generated_files() ->
  79. [".eunit/index.html",
  80. ".eunit/myapp_app.COVER.html",
  81. ".eunit/myapp_mymod.COVER.html",
  82. ".eunit/myapp_sup.COVER.html"].
  83. %% ====================================================================
  84. %% Environment and Setup Tests
  85. %% ====================================================================
  86. environment_test_() ->
  87. {"Sanity check the testing environment",
  88. setup, fun make_tmp_dir/0, fun remove_tmp_dir/1,
  89. [{"Ensure a test project can be created",
  90. ?_assert(filelib:is_dir(?TMP_DIR))},
  91. {"Ensure the rebar script can be found, copied, and run",
  92. [?_assert(filelib:is_file(?REBAR_SCRIPT)),
  93. fun assert_rebar_runs/0]}]}.
  94. assert_rebar_runs() ->
  95. prepare_rebar_script(),
  96. ?assert(string:str(os:cmd("./" ++ ?TMP_DIR ++ "rebar"), "Usage: rebar") =/= 0).
  97. basic_setup_test_() ->
  98. {"Create a simple project with a 'test' directory, a test, and a module",
  99. setup, fun setup_basic_project/0, fun teardown/1,
  100. %% Test the setup function
  101. assert_dirs_in("Basic Project",
  102. ["src", "ebin", "test"]) ++
  103. assert_files_in("Basic Project",
  104. ["test/myapp_mymod_tests.erl", "src/myapp_mymod.erl"])}.
  105. %% ====================================================================
  106. %% Setup and Teardown
  107. %% ====================================================================
  108. -define(myapp_mymod,
  109. ["-module(myapp_mymod).\n",
  110. "-export([myfunc/0]).\n",
  111. "-include_lib(\"eunit/include/eunit.hrl\").\n",
  112. "myfunc() -> ok.\n",
  113. "myprivate_test() -> ?assert(true).\n"]).
  114. -define(myapp_mymod_tests,
  115. ["-module(myapp_mymod_tests).\n",
  116. "-compile([export_all]).\n",
  117. "-include_lib(\"eunit/include/eunit.hrl\").\n",
  118. "myfunc_test() -> ?assertMatch(ok, myapp_mymod:myfunc()).\n"]).
  119. -define(mysuite,
  120. ["-module(mysuite).\n",
  121. "-export([all_test_/0]).\n",
  122. "-include_lib(\"eunit/include/eunit.hrl\").\n",
  123. "all_test_() -> [myapp_mymod_defined_in_mysuite_tests].\n"]).
  124. -define(myapp_mymod_defined_in_mysuite_tests,
  125. ["-module(myapp_mymod_defined_in_mysuite_tests).\n",
  126. "-compile([export_all]).\n",
  127. "-include_lib(\"eunit/include/eunit.hrl\").\n",
  128. "myfunc_test() -> ?assertMatch(ok, myapp_mymod:myfunc()).\n"]).
  129. make_tmp_dir() ->
  130. file:make_dir(?TMP_DIR).
  131. setup_environment() ->
  132. make_tmp_dir(),
  133. prepare_rebar_script(),
  134. file:set_cwd(?TMP_DIR).
  135. setup_basic_project() ->
  136. setup_environment(),
  137. rebar("create-app appid=myapp"),
  138. file:make_dir("test"),
  139. file:write_file("test/myapp_mymod_tests.erl", ?myapp_mymod_tests),
  140. file:write_file("src/myapp_mymod.erl", ?myapp_mymod).
  141. setup_cover_project() ->
  142. setup_basic_project(),
  143. file:write_file("rebar.config", "{cover_enabled, true}.\n").
  144. setup_cover_project_with_suite() ->
  145. setup_cover_project(),
  146. file:write_file("test/mysuite.erl", ?mysuite),
  147. file:write_file("test/myapp_mymod_defined_in_mysuite_tests.erl",
  148. ?myapp_mymod_defined_in_mysuite_tests).
  149. teardown(_) ->
  150. file:set_cwd(".."),
  151. remove_tmp_dir(),
  152. ok.
  153. remove_tmp_dir() ->
  154. remove_tmp_dir(arg_for_eunit).
  155. remove_tmp_dir(_) ->
  156. case os:type() of
  157. {unix, _} ->
  158. os:cmd("rm -rf " ++ ?TMP_DIR ++ " 2>/dev/null");
  159. {win32, _} ->
  160. %% os:cmd("rmdir /S /Q " ++ ?TMP_DIR ++ " 2>NUL")
  161. exit("Windows is not supported yet.")
  162. end.
  163. %% ====================================================================
  164. %% Helper Functions
  165. %% ====================================================================
  166. prepare_rebar_script() ->
  167. {ok, _} = file:copy(?REBAR_SCRIPT, ?TMP_DIR ++ "rebar"),
  168. [] = os:cmd("chmod u+x " ++ ?TMP_DIR ++ "rebar").
  169. rebar() ->
  170. rebar([]).
  171. rebar(Args) when is_list(Args) ->
  172. Out = os:cmd("./rebar " ++ Args),
  173. %?debugMsg("**** Begin"), ?debugMsg(Out), ?debugMsg("**** End"),
  174. Out.
  175. assert_dirs_in(Name, [Dir|T]) ->
  176. [{Name ++ " has directory: " ++ Dir, ?_assert(filelib:is_dir(Dir))} |
  177. assert_dirs_in(Name, T)];
  178. assert_dirs_in(_, []) -> [].
  179. assert_files_in(Name, [File|T]) ->
  180. [{Name ++ " has file: " ++ File, ?_assert(filelib:is_file(File))} |
  181. assert_files_in(Name, T)];
  182. assert_files_in(_, []) -> [].
  183. assert_files_not_in(Name, [File|T]) ->
  184. [{Name ++ " does not have file: " ++ File, ?_assertNot(filelib:is_file(File))} |
  185. assert_files_not_in(Name, T)];
  186. assert_files_not_in(_, []) -> [].