Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

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