選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

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