Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

426 строки
17 KiB

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