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.

256 lines
9.4 KiB

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