Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

390 wiersze
15 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 a _tests suite",
  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. {"Ensure EUnit runs a specific test without a specified suite",
  140. setup,
  141. fun() ->
  142. setup_project_with_multiple_modules(),
  143. rebar("-v eunit tests=myprivate")
  144. end,
  145. fun teardown/1,
  146. fun(RebarOut) ->
  147. [{"Only selected suite tests are found and run",
  148. [?_assert(string:str(RebarOut,
  149. "myapp_mymod:myprivate_test/0") =/= 0),
  150. ?_assert(string:str(RebarOut,
  151. "myapp_mymod2:myprivate2_test/0")
  152. =/= 0)]},
  153. {"Selected suite tests is run once",
  154. ?_assert(string:str(RebarOut, "All 2 tests passed") =/= 0)}]
  155. end}].
  156. cover_test_() ->
  157. {"Ensure Cover runs with tests in a test dir and no defined suite",
  158. setup, fun() -> setup_cover_project(), rebar("-v eunit") end,
  159. fun teardown/1,
  160. [{"All cover reports are generated",
  161. assert_files_in("the temporary eunit directory",
  162. expected_cover_generated_files())},
  163. {"Only production modules get coverage reports",
  164. assert_files_not_in("the temporary eunit directory",
  165. [".eunit/myapp_mymod_tests.COVER.html"])}]}.
  166. cover_with_suite_test_() ->
  167. {"Ensure Cover runs with Tests in a test dir and a test suite",
  168. setup,
  169. fun() ->
  170. setup_cover_project_with_suite(),
  171. rebar("-v eunit suites=mysuite")
  172. end,
  173. fun teardown/1,
  174. [{"Cover reports are generated for module",
  175. assert_files_in("the temporary eunit directory",
  176. [".eunit/index.html",
  177. ".eunit/mysuite.COVER.html"])},
  178. {"Only production modules get coverage reports",
  179. assert_files_not_in("the temporary eunit directory",
  180. [".eunit/myapp_app.COVER.html",
  181. ".eunit/myapp_mymod.COVER.html",
  182. ".eunit/myapp_sup.COVER.html",
  183. ".eunit/myapp_mymod_tests.COVER.html"])}]}.
  184. expected_cover_generated_files() ->
  185. [".eunit/index.html",
  186. ".eunit/myapp_app.COVER.html",
  187. ".eunit/myapp_mymod.COVER.html",
  188. ".eunit/myapp_sup.COVER.html"].
  189. cover_coverage_test_() ->
  190. {"Coverage is accurately calculated",
  191. setup, fun() -> setup_cover_project(), rebar("-v eunit") end,
  192. fun teardown/1,
  193. [{"Modules that include the EUnit header can still have 100% coverage",
  194. %% cover notices the implicit EUnit test/0 func that never gets
  195. %% called during eunit:test(TestRepresentation), so NotCounted
  196. %% needs to be decremented in this case.
  197. assert_full_coverage("myapp_mymod")}]}.
  198. %% ====================================================================
  199. %% Environment and Setup Tests
  200. %% ====================================================================
  201. environment_test_() ->
  202. {"Sanity check the testing environment",
  203. setup, fun make_tmp_dir/0, fun remove_tmp_dir/1,
  204. [{"Ensure a test project can be created",
  205. ?_assert(filelib:is_dir(?TMP_DIR))},
  206. {"Ensure the rebar script can be found, copied, and run",
  207. [?_assert(filelib:is_regular(?REBAR_SCRIPT)),
  208. fun assert_rebar_runs/0]}]}.
  209. assert_rebar_runs() ->
  210. prepare_rebar_script(),
  211. ?assert(string:str(os:cmd(filename:nativename("./" ++ ?TMP_DIR ++ "rebar")),
  212. "No command to run specified!") =/= 0).
  213. basic_setup_test_() ->
  214. {"Create a simple project with a 'test' directory, a test, and a module",
  215. setup, fun setup_basic_project/0, fun teardown/1,
  216. %% Test the setup function
  217. assert_dirs_in("Basic Project",
  218. ["src", "ebin", "test"]) ++
  219. assert_files_in("Basic Project",
  220. ["test/myapp_mymod_tests.erl",
  221. "src/myapp_mymod.erl"])}.
  222. %% ====================================================================
  223. %% Setup and Teardown
  224. %% ====================================================================
  225. -define(myapp_mymod,
  226. ["-module(myapp_mymod).\n",
  227. "-export([myfunc/0]).\n",
  228. "-include_lib(\"eunit/include/eunit.hrl\").\n",
  229. "myfunc() -> ok.\n",
  230. "myprivate_test() -> ?assert(true).\n"]).
  231. -define(myapp_mymod_tests,
  232. ["-module(myapp_mymod_tests).\n",
  233. "-compile([export_all]).\n",
  234. "-include_lib(\"eunit/include/eunit.hrl\").\n",
  235. "myfunc_test() -> ?assertMatch(ok, myapp_mymod:myfunc()).\n"]).
  236. -define(myapp_mymod2,
  237. ["-module(myapp_mymod2).\n",
  238. "-export([myfunc2/0]).\n",
  239. "-include_lib(\"eunit/include/eunit.hrl\").\n",
  240. "myfunc2() -> ok.\n",
  241. "myprivate2_test() -> ?assert(true).\n",
  242. "common_name_test() -> ?assert(true).\n"]).
  243. -define(myapp_mymod2_tests,
  244. ["-module(myapp_mymod2_tests).\n",
  245. "-compile([export_all]).\n",
  246. "-include_lib(\"eunit/include/eunit.hrl\").\n",
  247. "myfunc2_test() -> ?assertMatch(ok, myapp_mymod2:myfunc2()).\n",
  248. "common_name_test() -> ?assert(true).\n"]).
  249. -define(mysuite,
  250. ["-module(mysuite).\n",
  251. "-export([all_test_/0]).\n",
  252. "-include_lib(\"eunit/include/eunit.hrl\").\n",
  253. "all_test_() -> [myapp_mymod_defined_in_mysuite_tests].\n"]).
  254. -define(myapp_mymod_defined_in_mysuite_tests,
  255. ["-module(myapp_mymod_defined_in_mysuite_tests).\n",
  256. "-compile([export_all]).\n",
  257. "-include_lib(\"eunit/include/eunit.hrl\").\n",
  258. "myfunc_test() -> ?assertMatch(ok, myapp_mymod:myfunc()).\n"]).
  259. make_tmp_dir() ->
  260. ok = file:make_dir(?TMP_DIR).
  261. setup_environment() ->
  262. ok = make_tmp_dir(),
  263. prepare_rebar_script(),
  264. ok = file:set_cwd(?TMP_DIR).
  265. setup_basic_project() ->
  266. setup_environment(),
  267. rebar("create-app appid=myapp"),
  268. ok = file:make_dir("ebin"),
  269. ok = file:make_dir("test"),
  270. ok = file:write_file("test/myapp_mymod_tests.erl", ?myapp_mymod_tests),
  271. ok = file:write_file("src/myapp_mymod.erl", ?myapp_mymod).
  272. setup_project_with_multiple_modules() ->
  273. setup_basic_project(),
  274. ok = file:write_file("test/myapp_mymod2_tests.erl", ?myapp_mymod2_tests),
  275. ok = file:write_file("src/myapp_mymod2.erl", ?myapp_mymod2).
  276. setup_cover_project() ->
  277. setup_basic_project(),
  278. ok = file:write_file("rebar.config", "{cover_enabled, true}.\n").
  279. setup_cover_project_with_suite() ->
  280. setup_cover_project(),
  281. ok = file:write_file("test/mysuite.erl", ?mysuite),
  282. ok = file:write_file("test/myapp_mymod_defined_in_mysuite_tests.erl",
  283. ?myapp_mymod_defined_in_mysuite_tests).
  284. teardown(_) ->
  285. ok = file:set_cwd(".."),
  286. ok = remove_tmp_dir().
  287. remove_tmp_dir() ->
  288. remove_tmp_dir(arg_for_eunit).
  289. remove_tmp_dir(_) ->
  290. ok = rebar_file_utils:rm_rf(?TMP_DIR).
  291. %% ====================================================================
  292. %% Helper Functions
  293. %% ====================================================================
  294. prepare_rebar_script() ->
  295. Rebar = ?TMP_DIR ++ "rebar",
  296. {ok, _} = file:copy(?REBAR_SCRIPT, Rebar),
  297. case os:type() of
  298. {unix, _} ->
  299. [] = os:cmd("chmod u+x " ++ Rebar);
  300. {win32, _} ->
  301. {ok, _} = file:copy(?REBAR_SCRIPT ++ ".bat",
  302. ?TMP_DIR ++ "rebar.bat")
  303. end.
  304. rebar() ->
  305. rebar([]).
  306. rebar(Args) when is_list(Args) ->
  307. Out = os:cmd(filename:nativename("./rebar") ++ " " ++ Args),
  308. %% ?debugMsg("**** Begin"), ?debugMsg(Out), ?debugMsg("**** End"),
  309. Out.
  310. assert_dirs_in(Name, [Dir|T]) ->
  311. [{Name ++ " has directory: " ++ Dir, ?_assert(filelib:is_dir(Dir))} |
  312. assert_dirs_in(Name, T)];
  313. assert_dirs_in(_, []) -> [].
  314. assert_files_in(Name, [File|T]) ->
  315. [{Name ++ " has file: " ++ File, ?_assert(filelib:is_regular(File))} |
  316. assert_files_in(Name, T)];
  317. assert_files_in(_, []) -> [].
  318. assert_files_not_in(Name, [File|T]) ->
  319. [{Name ++ " does not have file: " ++ File,
  320. ?_assertNot(filelib:is_regular(File))} | assert_files_not_in(Name, T)];
  321. assert_files_not_in(_, []) -> [].
  322. assert_full_coverage(Mod) ->
  323. fun() ->
  324. {ok, F} = file:read_file(".eunit/index.html"),
  325. Result = [X || X <- string:tokens(binary_to_list(F), "\n"),
  326. string:str(X, Mod) =/= 0,
  327. string:str(X, "100%") =/= 0],
  328. ?assert(length(Result) =:= 1)
  329. end.