Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

408 rindas
18 KiB

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