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.

464 line
21 KiB

  1. -module(rebar_eunit_SUITE).
  2. -export([suite/0,
  3. init_per_suite/1,
  4. end_per_suite/1,
  5. init_per_testcase/2,
  6. all/0]).
  7. -export([test_basic_app/1,
  8. test_multi_app/1,
  9. test_profile/1,
  10. test_basic_exports/1,
  11. test_multi_exports/1,
  12. test_basic_defines/1,
  13. test_multi_defines/1,
  14. test_single_app_flag/1,
  15. test_multiple_app_flag/1,
  16. test_nonexistent_app_flag/1,
  17. test_single_suite_flag/1,
  18. test_suite_in_app_flag/1,
  19. test_suite_in_wrong_app_flag/1,
  20. test_nonexistent_suite_flag/1,
  21. test_single_file_flag/1,
  22. test_multiple_file_flag/1,
  23. test_nonexistent_file_flag/1]).
  24. -include_lib("common_test/include/ct.hrl").
  25. -include_lib("eunit/include/eunit.hrl").
  26. -include_lib("kernel/include/file.hrl").
  27. suite() ->
  28. [].
  29. init_per_suite(Config) ->
  30. Config.
  31. end_per_suite(_Config) ->
  32. ok.
  33. init_per_testcase(_, Config) ->
  34. rebar_test_utils:init_rebar_state(Config, "eunit_").
  35. all() ->
  36. [test_basic_app, test_multi_app, test_profile,
  37. test_basic_exports, test_multi_exports,
  38. test_basic_defines, test_multi_defines,
  39. test_single_app_flag, test_multiple_app_flag, test_nonexistent_app_flag,
  40. test_single_suite_flag, test_suite_in_app_flag,
  41. test_suite_in_wrong_app_flag, test_nonexistent_suite_flag,
  42. test_single_file_flag, test_multiple_file_flag, test_nonexistent_file_flag].
  43. test_basic_app(Config) ->
  44. AppDir = ?config(apps, Config),
  45. Name = rebar_test_utils:create_random_name("basic_"),
  46. Vsn = rebar_test_utils:create_random_vsn(),
  47. rebar_test_utils:create_eunit_app(AppDir, Name, Vsn, [kernel, stdlib]),
  48. RebarConfig = [{erl_opts, [{d, some_define}]}],
  49. rebar_test_utils:run_and_check(Config, RebarConfig, ["eunit"], {ok, [{app, Name}]}).
  50. test_multi_app(Config) ->
  51. AppDir = ?config(apps, Config),
  52. Name1 = rebar_test_utils:create_random_name("multi_app1_"),
  53. Vsn1 = rebar_test_utils:create_random_vsn(),
  54. rebar_test_utils:create_eunit_app(filename:join([AppDir,"apps",Name1]),
  55. Name1,
  56. Vsn1,
  57. [kernel, stdlib]),
  58. Name2 = rebar_test_utils:create_random_name("multi_app2_"),
  59. Vsn2 = rebar_test_utils:create_random_vsn(),
  60. rebar_test_utils:create_eunit_app(filename:join([AppDir,"apps",Name2]),
  61. Name2,
  62. Vsn2,
  63. [kernel, stdlib]),
  64. RebarConfig = [{erl_opts, [{d, some_define}]}],
  65. rebar_test_utils:run_and_check(Config,
  66. RebarConfig,
  67. ["eunit"],
  68. {ok, [{app, Name1}, {app, Name2}]}).
  69. test_profile(Config) ->
  70. AppDir = ?config(apps, Config),
  71. Name = rebar_test_utils:create_random_name("profile_"),
  72. Vsn = rebar_test_utils:create_random_vsn(),
  73. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  74. RebarConfig = [{erl_opts, [{d, some_define}]},
  75. {profiles, [{test, [{erl_opts, [debug_info]}]}]}],
  76. rebar_test_utils:run_and_check(Config,
  77. RebarConfig,
  78. ["as", "test", "eunit"],
  79. {ok, [{app, Name}]}).
  80. test_basic_exports(Config) ->
  81. AppDir = ?config(apps, Config),
  82. Name = rebar_test_utils:create_random_name("basic_exports_"),
  83. Vsn = rebar_test_utils:create_random_vsn(),
  84. rebar_test_utils:create_eunit_app(AppDir, Name, Vsn, [kernel, stdlib]),
  85. RebarConfig = [{erl_opts, [{d, some_define}]}],
  86. rebar_test_utils:run_and_check(Config,
  87. RebarConfig,
  88. ["eunit"],
  89. {ok, [{app, Name}]}),
  90. App = list_to_atom("not_a_real_src_" ++ Name),
  91. Suite = list_to_atom("not_a_real_src_" ++ Name ++ "_tests"),
  92. AppExports = App:module_info(exports),
  93. SuiteExports = Suite:module_info(exports),
  94. AppExpect = [{some_test_, 0}],
  95. SuiteExpect = [{some_test_, 0}, {define_test_, 0}],
  96. lists:foreach(fun(Expect) -> true = lists:member(Expect, AppExports) end, AppExpect),
  97. lists:foreach(fun(Expect) -> true = lists:member(Expect, SuiteExports) end, SuiteExpect).
  98. test_multi_exports(Config) ->
  99. AppDir = ?config(apps, Config),
  100. Name1 = rebar_test_utils:create_random_name("multi_exports_app1_"),
  101. Vsn1 = rebar_test_utils:create_random_vsn(),
  102. rebar_test_utils:create_eunit_app(filename:join([AppDir,"apps",Name1]),
  103. Name1,
  104. Vsn1,
  105. [kernel, stdlib]),
  106. Name2 = rebar_test_utils:create_random_name("multi_exports_app2_"),
  107. Vsn2 = rebar_test_utils:create_random_vsn(),
  108. rebar_test_utils:create_eunit_app(filename:join([AppDir,"apps",Name2]),
  109. Name2,
  110. Vsn2,
  111. [kernel, stdlib]),
  112. RebarConfig = [{erl_opts, [{d, some_define}]}],
  113. rebar_test_utils:run_and_check(Config,
  114. RebarConfig,
  115. ["eunit"],
  116. {ok, [{app, Name1}, {app, Name2}]}),
  117. App1 = list_to_atom("not_a_real_src_" ++ Name1),
  118. Suite1 = list_to_atom("not_a_real_src_" ++ Name1 ++ "_tests"),
  119. AppExports1 = App1:module_info(exports),
  120. SuiteExports1 = Suite1:module_info(exports),
  121. App2 = list_to_atom("not_a_real_src_" ++ Name2),
  122. Suite2 = list_to_atom("not_a_real_src_" ++ Name2 ++ "_tests"),
  123. AppExports2 = App2:module_info(exports),
  124. SuiteExports2 = Suite2:module_info(exports),
  125. AppExpect = [{some_test_, 0}],
  126. SuiteExpect = [{some_test_, 0}, {define_test_, 0}],
  127. lists:foreach(fun(Expect) -> true = lists:member(Expect, AppExports1) end, AppExpect),
  128. lists:foreach(fun(Expect) -> true = lists:member(Expect, SuiteExports1) end, SuiteExpect),
  129. lists:foreach(fun(Expect) -> true = lists:member(Expect, AppExports2) end, AppExpect),
  130. lists:foreach(fun(Expect) -> true = lists:member(Expect, SuiteExports2) end, SuiteExpect).
  131. test_basic_defines(Config) ->
  132. AppDir = ?config(apps, Config),
  133. Name = rebar_test_utils:create_random_name("basic_"),
  134. Vsn = rebar_test_utils:create_random_vsn(),
  135. rebar_test_utils:create_eunit_app(AppDir, Name, Vsn, [kernel, stdlib]),
  136. RebarConfig = [{erl_opts, [{d, some_define}]}],
  137. rebar_test_utils:run_and_check(Config, RebarConfig, ["eunit"], {ok, [{app, Name}]}),
  138. App = list_to_atom("not_a_real_src_" ++ Name),
  139. Suite = list_to_atom("not_a_real_src_" ++ Name ++ "_tests"),
  140. AppOpts = proplists:get_value(options, App:module_info(compile), []),
  141. SuiteOpts = proplists:get_value(options, Suite:module_info(compile), []),
  142. Expect = [{d, some_define}],
  143. lists:foreach(fun(E) -> true = lists:member(E, AppOpts) end, Expect),
  144. lists:foreach(fun(E) -> true = lists:member(E, SuiteOpts) end, Expect).
  145. test_multi_defines(Config) ->
  146. AppDir = ?config(apps, Config),
  147. Name1 = rebar_test_utils:create_random_name("multi_app1_"),
  148. Vsn1 = rebar_test_utils:create_random_vsn(),
  149. rebar_test_utils:create_eunit_app(filename:join([AppDir,"apps",Name1]),
  150. Name1,
  151. Vsn1,
  152. [kernel, stdlib]),
  153. Name2 = rebar_test_utils:create_random_name("multi_app2_"),
  154. Vsn2 = rebar_test_utils:create_random_vsn(),
  155. rebar_test_utils:create_eunit_app(filename:join([AppDir,"apps",Name2]),
  156. Name2,
  157. Vsn2,
  158. [kernel, stdlib]),
  159. RebarConfig = [{erl_opts, [{d, some_define}]}],
  160. rebar_test_utils:run_and_check(Config,
  161. RebarConfig,
  162. ["eunit"],
  163. {ok, [{app, Name1}, {app, Name2}]}),
  164. App1 = list_to_atom("not_a_real_src_" ++ Name1),
  165. Suite1 = list_to_atom("not_a_real_src_" ++ Name1 ++ "_tests"),
  166. AppOpts1 = proplists:get_value(options, App1:module_info(compile), []),
  167. SuiteOpts1 = proplists:get_value(options, Suite1:module_info(compile), []),
  168. App2 = list_to_atom("not_a_real_src_" ++ Name2),
  169. Suite2 = list_to_atom("not_a_real_src_" ++ Name2 ++ "_tests"),
  170. AppOpts2 = proplists:get_value(options, App2:module_info(compile), []),
  171. SuiteOpts2 = proplists:get_value(options, Suite2:module_info(compile), []),
  172. Expect = [{d, some_define}],
  173. lists:foreach(fun(E) -> true = lists:member(E, AppOpts1) end, Expect),
  174. lists:foreach(fun(E) -> true = lists:member(E, SuiteOpts1) end, Expect),
  175. lists:foreach(fun(E) -> true = lists:member(E, AppOpts2) end, Expect),
  176. lists:foreach(fun(E) -> true = lists:member(E, SuiteOpts2) end, Expect).
  177. test_single_app_flag(Config) ->
  178. AppDir = ?config(apps, Config),
  179. Name1 = rebar_test_utils:create_random_name("multi_exports_app1_"),
  180. Vsn1 = rebar_test_utils:create_random_vsn(),
  181. rebar_test_utils:create_eunit_app(filename:join([AppDir,"apps",Name1]),
  182. Name1,
  183. Vsn1,
  184. [kernel, stdlib]),
  185. Name2 = rebar_test_utils:create_random_name("multi_exports_app2_"),
  186. Vsn2 = rebar_test_utils:create_random_vsn(),
  187. rebar_test_utils:create_eunit_app(filename:join([AppDir,"apps",Name2]),
  188. Name2,
  189. Vsn2,
  190. [kernel, stdlib]),
  191. BareSuite = io_lib:format("-module(all_tests).\n"
  192. "-compile(export_all).\n"
  193. "-include_lib(\"eunit/include/eunit.hrl\").\n"
  194. "some_test_() -> ?_assert(true).\n"
  195. "define_test_() -> ?_assertEqual(true, ?some_define).\n", []),
  196. FileName = filename:join([AppDir, "test", "all_tests.erl"]),
  197. ok = filelib:ensure_dir(FileName),
  198. ok = ec_file:write(FileName, BareSuite),
  199. RebarConfig = [{erl_opts, [{d, some_define}]}],
  200. rebar_test_utils:run_and_check(Config,
  201. RebarConfig,
  202. ["eunit", "--app=" ++ Name1],
  203. {ok, [{app, Name1}, {app, Name2}]}),
  204. Suite1 = list_to_atom("not_a_real_src_" ++ Name1 ++ "_tests"),
  205. {module, Suite1} = code:ensure_loaded(Suite1),
  206. Suite2 = list_to_atom("not_a_real_src_" ++ Name2 ++ "_tests"),
  207. {error, nofile} = code:ensure_loaded(Suite2),
  208. {error, nofile} = code:ensure_loaded(all_tests).
  209. test_multiple_app_flag(Config) ->
  210. AppDir = ?config(apps, Config),
  211. Name1 = rebar_test_utils:create_random_name("multi_exports_app1_"),
  212. Vsn1 = rebar_test_utils:create_random_vsn(),
  213. rebar_test_utils:create_eunit_app(filename:join([AppDir,"apps",Name1]),
  214. Name1,
  215. Vsn1,
  216. [kernel, stdlib]),
  217. Name2 = rebar_test_utils:create_random_name("multi_exports_app2_"),
  218. Vsn2 = rebar_test_utils:create_random_vsn(),
  219. rebar_test_utils:create_eunit_app(filename:join([AppDir,"apps",Name2]),
  220. Name2,
  221. Vsn2,
  222. [kernel, stdlib]),
  223. BareSuite = io_lib:format("-module(all_tests).\n"
  224. "-compile(export_all).\n"
  225. "-include_lib(\"eunit/include/eunit.hrl\").\n"
  226. "some_test_() -> ?_assert(true).\n"
  227. "define_test_() -> ?_assertEqual(true, ?some_define).\n", []),
  228. FileName = filename:join([AppDir, "test", "all_tests.erl"]),
  229. ok = filelib:ensure_dir(FileName),
  230. ok = ec_file:write(FileName, BareSuite),
  231. RebarConfig = [{erl_opts, [{d, some_define}]}],
  232. rebar_test_utils:run_and_check(Config,
  233. RebarConfig,
  234. ["eunit", "--app=" ++ Name1 ++ "," ++ Name2],
  235. {ok, [{app, Name1}, {app, Name2}]}),
  236. Suite1 = list_to_atom("not_a_real_src_" ++ Name1 ++ "_tests"),
  237. {module, Suite1} = code:ensure_loaded(Suite1),
  238. Suite2 = list_to_atom("not_a_real_src_" ++ Name2 ++ "_tests"),
  239. {module, Suite2} = code:ensure_loaded(Suite2),
  240. {error, nofile} = code:ensure_loaded(all_tests).
  241. test_nonexistent_app_flag(Config) ->
  242. AppDir = ?config(apps, Config),
  243. Name1 = rebar_test_utils:create_random_name("multi_exports_app1_"),
  244. Vsn1 = rebar_test_utils:create_random_vsn(),
  245. rebar_test_utils:create_eunit_app(filename:join([AppDir,Name1]),
  246. Name1,
  247. Vsn1,
  248. [kernel, stdlib]),
  249. Name2 = rebar_test_utils:create_random_name("multi_exports_app2_"),
  250. Vsn2 = rebar_test_utils:create_random_vsn(),
  251. rebar_test_utils:create_eunit_app(filename:join([AppDir,"apps",Name2]),
  252. Name2,
  253. Vsn2,
  254. [kernel, stdlib]),
  255. RebarConfig = [{erl_opts, [{d, some_define}]}],
  256. {error, {_, Error}} = rebar_test_utils:run_and_check(Config,
  257. RebarConfig,
  258. ["eunit", "--app=not_a_real_app"],
  259. return),
  260. Error = {error_running_tests, "Application `not_a_real_app' not found in project."}.
  261. test_single_suite_flag(Config) ->
  262. AppDir = ?config(apps, Config),
  263. Name1 = rebar_test_utils:create_random_name("multi_exports_app1_"),
  264. Vsn1 = rebar_test_utils:create_random_vsn(),
  265. rebar_test_utils:create_eunit_app(filename:join([AppDir,"apps",Name1]),
  266. Name1,
  267. Vsn1,
  268. [kernel, stdlib]),
  269. Name2 = rebar_test_utils:create_random_name("multi_exports_app2_"),
  270. Vsn2 = rebar_test_utils:create_random_vsn(),
  271. rebar_test_utils:create_eunit_app(filename:join([AppDir,"apps",Name2]),
  272. Name2,
  273. Vsn2,
  274. [kernel, stdlib]),
  275. RebarConfig = [{erl_opts, [{d, some_define}]}],
  276. rebar_test_utils:run_and_check(Config,
  277. RebarConfig,
  278. ["eunit", "--suite=not_a_real_src_" ++ Name1],
  279. {ok, [{app, Name1}, {app, Name2}]}),
  280. Suite1 = list_to_atom("not_a_real_src_" ++ Name1 ++ "_tests"),
  281. {module, Suite1} = code:ensure_loaded(Suite1).
  282. test_suite_in_app_flag(Config) ->
  283. AppDir = ?config(apps, Config),
  284. Name1 = rebar_test_utils:create_random_name("multi_exports_app1_"),
  285. Vsn1 = rebar_test_utils:create_random_vsn(),
  286. rebar_test_utils:create_eunit_app(filename:join([AppDir,"apps",Name1]),
  287. Name1,
  288. Vsn1,
  289. [kernel, stdlib]),
  290. Name2 = rebar_test_utils:create_random_name("multi_exports_app2_"),
  291. Vsn2 = rebar_test_utils:create_random_vsn(),
  292. rebar_test_utils:create_eunit_app(filename:join([AppDir,"apps",Name2]),
  293. Name2,
  294. Vsn2,
  295. [kernel, stdlib]),
  296. RebarConfig = [{erl_opts, [{d, some_define}]}],
  297. rebar_test_utils:run_and_check(Config,
  298. RebarConfig,
  299. ["eunit",
  300. "--app=" ++ Name1,
  301. "--suite=not_a_real_src_" ++ Name1],
  302. {ok, [{app, Name1}, {app, Name2}]}),
  303. Suite1 = list_to_atom("not_a_real_src_" ++ Name1 ++ "_tests"),
  304. {module, Suite1} = code:ensure_loaded(Suite1),
  305. Suite2 = list_to_atom("not_a_real_src_" ++ Name2 ++ "_tests"),
  306. {error, nofile} = code:ensure_loaded(Suite2).
  307. test_suite_in_wrong_app_flag(Config) ->
  308. AppDir = ?config(apps, Config),
  309. Name1 = rebar_test_utils:create_random_name("multi_exports_app1_"),
  310. Vsn1 = rebar_test_utils:create_random_vsn(),
  311. rebar_test_utils:create_eunit_app(filename:join([AppDir,"apps",Name1]),
  312. Name1,
  313. Vsn1,
  314. [kernel, stdlib]),
  315. Name2 = rebar_test_utils:create_random_name("multi_exports_app2_"),
  316. Vsn2 = rebar_test_utils:create_random_vsn(),
  317. rebar_test_utils:create_eunit_app(filename:join([AppDir,"apps",Name2]),
  318. Name2,
  319. Vsn2,
  320. [kernel, stdlib]),
  321. RebarConfig = [{erl_opts, [{d, some_define}]}],
  322. {error, {_, Error}} = rebar_test_utils:run_and_check(Config,
  323. RebarConfig,
  324. ["eunit",
  325. "--app=" ++ Name1,
  326. "--suite=not_a_real_src_" ++ Name2],
  327. return),
  328. Error = {error_running_tests, "Module `not_a_real_src_" ++
  329. Name2 ++
  330. "' not found in applications."}.
  331. test_nonexistent_suite_flag(Config) ->
  332. AppDir = ?config(apps, Config),
  333. Name1 = rebar_test_utils:create_random_name("multi_exports_app1_"),
  334. Vsn1 = rebar_test_utils:create_random_vsn(),
  335. rebar_test_utils:create_eunit_app(filename:join([AppDir,"apps",Name1]),
  336. Name1,
  337. Vsn1,
  338. [kernel, stdlib]),
  339. Name2 = rebar_test_utils:create_random_name("multi_exports_app2_"),
  340. Vsn2 = rebar_test_utils:create_random_vsn(),
  341. rebar_test_utils:create_eunit_app(filename:join([AppDir,"apps",Name2]),
  342. Name2,
  343. Vsn2,
  344. [kernel, stdlib]),
  345. RebarConfig = [{erl_opts, [{d, some_define}]}],
  346. {error, {_, Error}} = rebar_test_utils:run_and_check(Config,
  347. RebarConfig,
  348. ["eunit", "--suite=not_a_real_module"],
  349. return),
  350. Error = {error_running_tests, "Module `not_a_real_module' not found in applications."}.
  351. test_single_file_flag(Config) ->
  352. AppDir = ?config(apps, Config),
  353. Name = rebar_test_utils:create_random_name("single_file_flag_app_"),
  354. Vsn = rebar_test_utils:create_random_vsn(),
  355. rebar_test_utils:create_eunit_app(AppDir, Name, Vsn, [kernel, stdlib]),
  356. RebarConfig = [{erl_opts, [{d, some_define}]}],
  357. rebar_test_utils:run_and_check(Config,
  358. RebarConfig,
  359. ["eunit", "--file=not_a_real_src_" ++ Name ++ "_tests.beam"],
  360. {ok, [{app, Name}]}),
  361. File = list_to_atom("not_a_real_src_" ++ Name ++ "_tests"),
  362. {module, File} = code:ensure_loaded(File).
  363. test_multiple_file_flag(Config) ->
  364. AppDir = ?config(apps, Config),
  365. Name = rebar_test_utils:create_random_name("multiple_file_flag_app_"),
  366. Vsn = rebar_test_utils:create_random_vsn(),
  367. rebar_test_utils:create_eunit_app(AppDir, Name, Vsn, [kernel, stdlib]),
  368. RebarConfig = [{erl_opts, [{d, some_define}]}],
  369. rebar_test_utils:run_and_check(Config,
  370. RebarConfig,
  371. ["eunit", "--file=not_a_real_src_" ++ Name ++ "_tests.beam,not_a_real_src_" ++ Name ++ ".beam"],
  372. {ok, [{app, Name}]}),
  373. File1 = list_to_atom("not_a_real_src_" ++ Name ++ "_tests"),
  374. {module, File1} = code:ensure_loaded(File1),
  375. File2 = list_to_atom("not_a_real_src_" ++ Name),
  376. {module, File2} = code:ensure_loaded(File2).
  377. test_nonexistent_file_flag(Config) ->
  378. AppDir = ?config(apps, Config),
  379. Name = rebar_test_utils:create_random_name("nonexistent_file_flag_app_"),
  380. Vsn = rebar_test_utils:create_random_vsn(),
  381. rebar_test_utils:create_eunit_app(AppDir,
  382. Name,
  383. Vsn,
  384. [kernel, stdlib]),
  385. RebarConfig = [{erl_opts, [{d, some_define}]}],
  386. {error, {rebar_prv_eunit, _Error}} = rebar_test_utils:run_and_check(Config,
  387. RebarConfig,
  388. ["eunit", "--file=" ++ filename:join(["some_path", "not_a_real_file.erl"])],
  389. return).