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.

372 line
14 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 '.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. eunit_with_suites_and_tests_test_() ->
  54. [{"Ensure EUnit runs selected suites",
  55. setup, fun() ->
  56. setup_project_with_multiple_modules(),
  57. rebar("-v eunit suites=myapp_mymod2")
  58. end,
  59. fun teardown/1,
  60. fun(RebarOut) ->
  61. [{"Selected suite tests in 'test' directory are found and run",
  62. ?_assert(string:str(RebarOut, "myapp_mymod2_tests:") =/= 0)},
  63. {"Selected suite tests in 'src' directory are found and run",
  64. ?_assert(string:str(RebarOut, "myapp_mymod2:") =/= 0)},
  65. {"Unselected suite tests in 'test' directory are not run",
  66. ?_assert(string:str(RebarOut, "myapp_mymod_tests:") =:= 0)},
  67. {"Unselected suite tests in 'src' directory are not run",
  68. ?_assert(string:str(RebarOut, "myapp_mymod:") =:= 0)},
  69. {"Selected suite tests are only run once",
  70. ?_assert(string:str(RebarOut, "All 4 tests passed") =/= 0)}]
  71. end},
  72. {"Ensure EUnit runs selected _tests suites",
  73. setup, fun() ->
  74. setup_project_with_multiple_modules(),
  75. rebar("-v eunit suites=myapp_mymod2_tests")
  76. end,
  77. fun teardown/1,
  78. fun(RebarOut) ->
  79. [{"Selected suite tests in 'test' directory are found and run",
  80. ?_assert(string:str(RebarOut, "myapp_mymod2_tests:") =/= 0)},
  81. {"Selected suite tests in 'src' directory are not run",
  82. ?_assert(string:str(RebarOut, "myapp_mymod2:") =:= 0)},
  83. {"Unselected suite tests in 'test' directory are not run",
  84. ?_assert(string:str(RebarOut, "myapp_mymod_tests:") =:= 0)},
  85. {"Unselected suite tests in 'src' directory are not run",
  86. ?_assert(string:str(RebarOut, "myapp_mymod:") =:= 0)},
  87. {"Selected suite tests are only run once",
  88. ?_assert(string:str(RebarOut, "All 2 tests passed") =/= 0)}]
  89. end},
  90. {"Ensure EUnit runs a specific test defined in a selected suite",
  91. setup, fun() ->
  92. setup_project_with_multiple_modules(),
  93. rebar("-v eunit suites=myapp_mymod2 tests=myprivate2")
  94. end,
  95. fun teardown/1,
  96. fun(RebarOut) ->
  97. [{"Selected suite tests are found and run",
  98. ?_assert(string:str(RebarOut,
  99. "myapp_mymod2:myprivate2_test/0") =/= 0)},
  100. {"Selected suite tests is run once",
  101. ?_assert(string:str(RebarOut, "Test passed") =/= 0)}]
  102. end},
  103. {"Ensure EUnit runs specific tests defined in selected suites",
  104. setup, fun() ->
  105. setup_project_with_multiple_modules(),
  106. rebar("-v eunit suites=myapp_mymod,myapp_mymod2"
  107. " tests=myprivate,myfunc2")
  108. end,
  109. fun teardown/1,
  110. fun(RebarOut) ->
  111. [{"Selected suite tests are found and run",
  112. [?_assert(string:str(RebarOut,
  113. "myapp_mymod:myprivate_test/0") =/= 0),
  114. ?_assert(string:str(RebarOut,
  115. "myapp_mymod2:myprivate2_test/0") =/= 0),
  116. ?_assert(
  117. string:str(RebarOut,
  118. "myapp_mymod2_tests:myfunc2_test/0") =/= 0)]},
  119. {"Selected suite tests are run once",
  120. ?_assert(string:str(RebarOut, "All 3 tests passed") =/= 0)}]
  121. end},
  122. {"Ensure EUnit runs specific test in _tests suites",
  123. setup,
  124. fun() ->
  125. setup_project_with_multiple_modules(),
  126. rebar("-v eunit suites=myapp_mymod2_tests tests=common_name_test")
  127. end,
  128. fun teardown/1,
  129. fun(RebarOut) ->
  130. [{"Only selected suite tests are found and run",
  131. [?_assert(string:str(RebarOut,
  132. "myapp_mymod2:common_name_test/0") =:= 0),
  133. ?_assert(string:str(RebarOut,
  134. "myapp_mymod2_tests:common_name_test/0")
  135. =/= 0)]},
  136. {"Selected suite tests is run once",
  137. ?_assert(string:str(RebarOut, "Test passed") =/= 0)}]
  138. end}].
  139. cover_test_() ->
  140. {"Ensure Cover runs with tests in a test dir and no defined suite",
  141. setup, fun() -> setup_cover_project(), rebar("-v eunit") end,
  142. fun teardown/1,
  143. [{"All cover reports are generated",
  144. assert_files_in("the temporary eunit directory",
  145. expected_cover_generated_files())},
  146. {"Only production modules get coverage reports",
  147. assert_files_not_in("the temporary eunit directory",
  148. [".eunit/myapp_mymod_tests.COVER.html"])}]}.
  149. cover_with_suite_test_() ->
  150. {"Ensure Cover runs with Tests in a test dir and a test suite",
  151. setup,
  152. fun() ->
  153. setup_cover_project_with_suite(),
  154. rebar("-v eunit suites=mysuite")
  155. end,
  156. fun teardown/1,
  157. [{"Cover reports are generated for module",
  158. assert_files_in("the temporary eunit directory",
  159. [".eunit/index.html",
  160. ".eunit/mysuite.COVER.html"])},
  161. {"Only production modules get coverage reports",
  162. assert_files_not_in("the temporary eunit directory",
  163. [".eunit/myapp_app.COVER.html",
  164. ".eunit/myapp_mymod.COVER.html",
  165. ".eunit/myapp_sup.COVER.html",
  166. ".eunit/myapp_mymod_tests.COVER.html"])}]}.
  167. expected_cover_generated_files() ->
  168. [".eunit/index.html",
  169. ".eunit/myapp_app.COVER.html",
  170. ".eunit/myapp_mymod.COVER.html",
  171. ".eunit/myapp_sup.COVER.html"].
  172. cover_coverage_test_() ->
  173. {"Coverage is accurately calculated",
  174. setup, fun() -> setup_cover_project(), rebar("-v eunit") end,
  175. fun teardown/1,
  176. [{"Modules that include the EUnit header can still have 100% coverage",
  177. %% cover notices the implicit EUnit test/0 func that never gets
  178. %% called during eunit:test(TestRepresentation), so NotCounted
  179. %% needs to be decremented in this case.
  180. assert_full_coverage("myapp_mymod")}]}.
  181. %% ====================================================================
  182. %% Environment and Setup Tests
  183. %% ====================================================================
  184. environment_test_() ->
  185. {"Sanity check the testing environment",
  186. setup, fun make_tmp_dir/0, fun remove_tmp_dir/1,
  187. [{"Ensure a test project can be created",
  188. ?_assert(filelib:is_dir(?TMP_DIR))},
  189. {"Ensure the rebar script can be found, copied, and run",
  190. [?_assert(filelib:is_regular(?REBAR_SCRIPT)),
  191. fun assert_rebar_runs/0]}]}.
  192. assert_rebar_runs() ->
  193. prepare_rebar_script(),
  194. ?assert(string:str(os:cmd(filename:nativename("./" ++ ?TMP_DIR ++ "rebar")),
  195. "No command to run specified!") =/= 0).
  196. basic_setup_test_() ->
  197. {"Create a simple project with a 'test' directory, a test, and a module",
  198. setup, fun setup_basic_project/0, fun teardown/1,
  199. %% Test the setup function
  200. assert_dirs_in("Basic Project",
  201. ["src", "ebin", "test"]) ++
  202. assert_files_in("Basic Project",
  203. ["test/myapp_mymod_tests.erl",
  204. "src/myapp_mymod.erl"])}.
  205. %% ====================================================================
  206. %% Setup and Teardown
  207. %% ====================================================================
  208. -define(myapp_mymod,
  209. ["-module(myapp_mymod).\n",
  210. "-export([myfunc/0]).\n",
  211. "-include_lib(\"eunit/include/eunit.hrl\").\n",
  212. "myfunc() -> ok.\n",
  213. "myprivate_test() -> ?assert(true).\n"]).
  214. -define(myapp_mymod_tests,
  215. ["-module(myapp_mymod_tests).\n",
  216. "-compile([export_all]).\n",
  217. "-include_lib(\"eunit/include/eunit.hrl\").\n",
  218. "myfunc_test() -> ?assertMatch(ok, myapp_mymod:myfunc()).\n"]).
  219. -define(myapp_mymod2,
  220. ["-module(myapp_mymod2).\n",
  221. "-export([myfunc2/0]).\n",
  222. "-include_lib(\"eunit/include/eunit.hrl\").\n",
  223. "myfunc2() -> ok.\n",
  224. "myprivate2_test() -> ?assert(true).\n",
  225. "common_name_test() -> ?assert(true).\n"]).
  226. -define(myapp_mymod2_tests,
  227. ["-module(myapp_mymod2_tests).\n",
  228. "-compile([export_all]).\n",
  229. "-include_lib(\"eunit/include/eunit.hrl\").\n",
  230. "myfunc2_test() -> ?assertMatch(ok, myapp_mymod2:myfunc2()).\n",
  231. "common_name_test() -> ?assert(true).\n"]).
  232. -define(mysuite,
  233. ["-module(mysuite).\n",
  234. "-export([all_test_/0]).\n",
  235. "-include_lib(\"eunit/include/eunit.hrl\").\n",
  236. "all_test_() -> [myapp_mymod_defined_in_mysuite_tests].\n"]).
  237. -define(myapp_mymod_defined_in_mysuite_tests,
  238. ["-module(myapp_mymod_defined_in_mysuite_tests).\n",
  239. "-compile([export_all]).\n",
  240. "-include_lib(\"eunit/include/eunit.hrl\").\n",
  241. "myfunc_test() -> ?assertMatch(ok, myapp_mymod:myfunc()).\n"]).
  242. make_tmp_dir() ->
  243. ok = file:make_dir(?TMP_DIR).
  244. setup_environment() ->
  245. ok = make_tmp_dir(),
  246. prepare_rebar_script(),
  247. ok = file:set_cwd(?TMP_DIR).
  248. setup_basic_project() ->
  249. setup_environment(),
  250. rebar("create-app appid=myapp"),
  251. ok = file:make_dir("ebin"),
  252. ok = file:make_dir("test"),
  253. ok = file:write_file("test/myapp_mymod_tests.erl", ?myapp_mymod_tests),
  254. ok = file:write_file("src/myapp_mymod.erl", ?myapp_mymod).
  255. setup_project_with_multiple_modules() ->
  256. setup_basic_project(),
  257. ok = file:write_file("test/myapp_mymod2_tests.erl", ?myapp_mymod2_tests),
  258. ok = file:write_file("src/myapp_mymod2.erl", ?myapp_mymod2).
  259. setup_cover_project() ->
  260. setup_basic_project(),
  261. ok = file:write_file("rebar.config", "{cover_enabled, true}.\n").
  262. setup_cover_project_with_suite() ->
  263. setup_cover_project(),
  264. ok = file:write_file("test/mysuite.erl", ?mysuite),
  265. ok = file:write_file("test/myapp_mymod_defined_in_mysuite_tests.erl",
  266. ?myapp_mymod_defined_in_mysuite_tests).
  267. teardown(_) ->
  268. ok = file:set_cwd(".."),
  269. ok = remove_tmp_dir().
  270. remove_tmp_dir() ->
  271. remove_tmp_dir(arg_for_eunit).
  272. remove_tmp_dir(_) ->
  273. ok = rebar_file_utils:rm_rf(?TMP_DIR).
  274. %% ====================================================================
  275. %% Helper Functions
  276. %% ====================================================================
  277. prepare_rebar_script() ->
  278. Rebar = ?TMP_DIR ++ "rebar",
  279. {ok, _} = file:copy(?REBAR_SCRIPT, Rebar),
  280. case os:type() of
  281. {unix, _} ->
  282. [] = os:cmd("chmod u+x " ++ Rebar);
  283. {win32, _} ->
  284. {ok, _} = file:copy(?REBAR_SCRIPT ++ ".bat",
  285. ?TMP_DIR ++ "rebar.bat")
  286. end.
  287. rebar() ->
  288. rebar([]).
  289. rebar(Args) when is_list(Args) ->
  290. Out = os:cmd(filename:nativename("./rebar") ++ " " ++ Args),
  291. %% ?debugMsg("**** Begin"), ?debugMsg(Out), ?debugMsg("**** End"),
  292. Out.
  293. assert_dirs_in(Name, [Dir|T]) ->
  294. [{Name ++ " has directory: " ++ Dir, ?_assert(filelib:is_dir(Dir))} |
  295. assert_dirs_in(Name, T)];
  296. assert_dirs_in(_, []) -> [].
  297. assert_files_in(Name, [File|T]) ->
  298. [{Name ++ " has file: " ++ File, ?_assert(filelib:is_regular(File))} |
  299. assert_files_in(Name, T)];
  300. assert_files_in(_, []) -> [].
  301. assert_files_not_in(Name, [File|T]) ->
  302. [{Name ++ " does not have file: " ++ File,
  303. ?_assertNot(filelib:is_regular(File))} | assert_files_not_in(Name, T)];
  304. assert_files_not_in(_, []) -> [].
  305. assert_full_coverage(Mod) ->
  306. fun() ->
  307. {ok, F} = file:read_file(".eunit/index.html"),
  308. Result = [X || X <- string:tokens(binary_to_list(F), "\n"),
  309. string:str(X, Mod) =/= 0,
  310. string:str(X, "100%") =/= 0],
  311. ?assert(length(Result) =:= 1)
  312. end.