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.

254 lines
9.3 KiB

  1. %% -*- 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 '.test'
  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. [".test/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 suites=mysuite")
  69. end,
  70. fun teardown/1,
  71. [{"Cover reports are generated for module",
  72. assert_files_in("the temporary eunit directory",
  73. [".test/index.html",
  74. ".test/mysuite.COVER.html"])},
  75. {"Only production modules get coverage reports",
  76. assert_files_not_in("the temporary eunit directory",
  77. [".test/myapp_app.COVER.html",
  78. ".test/myapp_mymod.COVER.html",
  79. ".test/myapp_sup.COVER.html",
  80. ".test/myapp_mymod_tests.COVER.html"])}]}.
  81. expected_cover_generated_files() ->
  82. [".test/index.html",
  83. ".test/myapp_app.COVER.html",
  84. ".test/myapp_mymod.COVER.html",
  85. ".test/myapp_sup.COVER.html"].
  86. cover_coverage_test_() ->
  87. {"Coverage is accurately calculated",
  88. setup, fun() -> setup_cover_project(), rebar("-v eunit") end,
  89. fun teardown/1,
  90. [{"Modules that include the EUnit header can still have 100% coverage",
  91. %% cover notices the implicit EUnit test/0 func that never gets
  92. %% called during eunit:test(TestRepresentation), so NotCounted
  93. %% needs to be decremented in this case.
  94. assert_full_coverage("myapp_mymod")}]}.
  95. %% ====================================================================
  96. %% Environment and Setup Tests
  97. %% ====================================================================
  98. environment_test_() ->
  99. {"Sanity check the testing environment",
  100. setup, fun make_tmp_dir/0, fun remove_tmp_dir/1,
  101. [{"Ensure a test project can be created",
  102. ?_assert(filelib:is_dir(?TMP_DIR))},
  103. {"Ensure the rebar script can be found, copied, and run",
  104. [?_assert(filelib:is_regular(?REBAR_SCRIPT)),
  105. fun assert_rebar_runs/0]}]}.
  106. assert_rebar_runs() ->
  107. prepare_rebar_script(),
  108. ?assert(string:str(os:cmd(filename:nativename("./" ++ ?TMP_DIR ++ "rebar")),
  109. "No command to run specified!") =/= 0).
  110. basic_setup_test_() ->
  111. {"Create a simple project with a 'test' directory, a test, and a module",
  112. setup, fun setup_basic_project/0, fun teardown/1,
  113. %% Test the setup function
  114. assert_dirs_in("Basic Project",
  115. ["src", "ebin", "test"]) ++
  116. assert_files_in("Basic Project",
  117. ["test/myapp_mymod_tests.erl",
  118. "src/myapp_mymod.erl"])}.
  119. %% ====================================================================
  120. %% Setup and Teardown
  121. %% ====================================================================
  122. -define(myapp_mymod,
  123. ["-module(myapp_mymod).\n",
  124. "-export([myfunc/0]).\n",
  125. "-include_lib(\"eunit/include/eunit.hrl\").\n",
  126. "myfunc() -> ok.\n",
  127. "myprivate_test() -> ?assert(true).\n"]).
  128. -define(myapp_mymod_tests,
  129. ["-module(myapp_mymod_tests).\n",
  130. "-compile([export_all]).\n",
  131. "-include_lib(\"eunit/include/eunit.hrl\").\n",
  132. "myfunc_test() -> ?assertMatch(ok, myapp_mymod:myfunc()).\n"]).
  133. -define(mysuite,
  134. ["-module(mysuite).\n",
  135. "-export([all_test_/0]).\n",
  136. "-include_lib(\"eunit/include/eunit.hrl\").\n",
  137. "all_test_() -> [myapp_mymod_defined_in_mysuite_tests].\n"]).
  138. -define(myapp_mymod_defined_in_mysuite_tests,
  139. ["-module(myapp_mymod_defined_in_mysuite_tests).\n",
  140. "-compile([export_all]).\n",
  141. "-include_lib(\"eunit/include/eunit.hrl\").\n",
  142. "myfunc_test() -> ?assertMatch(ok, myapp_mymod:myfunc()).\n"]).
  143. make_tmp_dir() ->
  144. ok = file:make_dir(?TMP_DIR).
  145. setup_environment() ->
  146. ok = make_tmp_dir(),
  147. prepare_rebar_script(),
  148. ok = file:set_cwd(?TMP_DIR).
  149. setup_basic_project() ->
  150. setup_environment(),
  151. rebar("create-app appid=myapp"),
  152. ok = file:make_dir("ebin"),
  153. ok = file:make_dir("test"),
  154. ok = file:write_file("test/myapp_mymod_tests.erl", ?myapp_mymod_tests),
  155. ok = file:write_file("src/myapp_mymod.erl", ?myapp_mymod).
  156. setup_cover_project() ->
  157. setup_basic_project(),
  158. ok = file:write_file("rebar.config", "{cover_enabled, true}.\n").
  159. setup_cover_project_with_suite() ->
  160. setup_cover_project(),
  161. ok = file:write_file("test/mysuite.erl", ?mysuite),
  162. ok = file:write_file("test/myapp_mymod_defined_in_mysuite_tests.erl",
  163. ?myapp_mymod_defined_in_mysuite_tests).
  164. teardown(_) ->
  165. ok = file:set_cwd(".."),
  166. ok = remove_tmp_dir().
  167. remove_tmp_dir() ->
  168. remove_tmp_dir(arg_for_eunit).
  169. remove_tmp_dir(_) ->
  170. ok = rebar_file_utils:rm_rf(?TMP_DIR).
  171. %% ====================================================================
  172. %% Helper Functions
  173. %% ====================================================================
  174. prepare_rebar_script() ->
  175. Rebar = ?TMP_DIR ++ "rebar",
  176. {ok, _} = file:copy(?REBAR_SCRIPT, Rebar),
  177. case os:type() of
  178. {unix, _} ->
  179. [] = os:cmd("chmod u+x " ++ Rebar);
  180. {win32, _} ->
  181. {ok, _} = file:copy(?REBAR_SCRIPT ++ ".bat",
  182. ?TMP_DIR ++ "rebar.bat")
  183. end.
  184. rebar() ->
  185. rebar([]).
  186. rebar(Args) when is_list(Args) ->
  187. Out = os:cmd(filename:nativename("./rebar") ++ " " ++ Args),
  188. %% ?debugMsg("**** Begin"), ?debugMsg(Out), ?debugMsg("**** End"),
  189. Out.
  190. assert_dirs_in(Name, [Dir|T]) ->
  191. [{Name ++ " has directory: " ++ Dir, ?_assert(filelib:is_dir(Dir))} |
  192. assert_dirs_in(Name, T)];
  193. assert_dirs_in(_, []) -> [].
  194. assert_files_in(Name, [File|T]) ->
  195. [{Name ++ " has file: " ++ File, ?_assert(filelib:is_regular(File))} |
  196. assert_files_in(Name, T)];
  197. assert_files_in(_, []) -> [].
  198. assert_files_not_in(Name, [File|T]) ->
  199. [{Name ++ " does not have file: " ++ File,
  200. ?_assertNot(filelib:is_regular(File))} | assert_files_not_in(Name, T)];
  201. assert_files_not_in(_, []) -> [].
  202. assert_full_coverage(Mod) ->
  203. fun() ->
  204. {ok, F} = file:read_file(".test/index.html"),
  205. Result = [X || X <- string:tokens(binary_to_list(F), "\n"),
  206. string:str(X, Mod) =/= 0,
  207. string:str(X, "100%") =/= 0],
  208. ?assert(length(Result) =:= 1)
  209. end.