您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

641 行
27 KiB

  1. -module(rebar_eunit_SUITE).
  2. -export([all/0, groups/0]).
  3. -export([init_per_suite/1, end_per_suite/1]).
  4. -export([init_per_group/2, end_per_group/2]).
  5. -export([basic_app_compiles/1, basic_app_files/1]).
  6. -export([basic_app_exports/1, basic_app_testset/1]).
  7. -export([basic_app_eunit_macro/1]).
  8. -export([multi_app_compiles/1, multi_app_files/1]).
  9. -export([multi_app_exports/1, multi_app_testset/1]).
  10. -export([multi_app_eunit_macro/1]).
  11. -export([eunit_tests/1, eunit_opts/1, eunit_first_files/1]).
  12. -export([single_application_arg/1, multi_application_arg/1, missing_application_arg/1]).
  13. -export([single_module_arg/1, multi_module_arg/1, missing_module_arg/1]).
  14. -export([single_suite_arg/1, multi_suite_arg/1, missing_suite_arg/1]).
  15. -export([single_generator_arg/1, multi_generator_arg/1, missing_generator_arg/1]).
  16. -export([single_file_arg/1, multi_file_arg/1, missing_file_arg/1]).
  17. -export([single_dir_arg/1, multi_dir_arg/1, missing_dir_arg/1]).
  18. -export([multiple_arg_composition/1, multiple_arg_errors/1]).
  19. -export([misspecified_eunit_tests/1]).
  20. -export([misspecified_eunit_compile_opts/1]).
  21. -export([misspecified_eunit_first_files/1]).
  22. -export([alternate_test_regex/1]).
  23. -include_lib("common_test/include/ct.hrl").
  24. -include_lib("eunit/include/eunit.hrl").
  25. -include_lib("kernel/include/file.hrl").
  26. all() ->
  27. [{group, basic_app}, {group, multi_app}, {group, cmd_line_args},
  28. misspecified_eunit_tests,
  29. misspecified_eunit_compile_opts,
  30. misspecified_eunit_first_files,
  31. alternate_test_regex].
  32. groups() ->
  33. [{basic_app, [sequence], [basic_app_compiles, {group, basic_app_results}]},
  34. {basic_app_results, [], [basic_app_files,
  35. basic_app_exports,
  36. basic_app_testset,
  37. basic_app_eunit_macro]},
  38. {multi_app, [sequence], [multi_app_compiles, {group, multi_app_results}]},
  39. {multi_app_results, [], [multi_app_files,
  40. multi_app_exports,
  41. multi_app_testset,
  42. multi_app_eunit_macro]},
  43. {cmd_line_args, [], [eunit_tests, eunit_opts, eunit_first_files,
  44. single_application_arg, multi_application_arg, missing_application_arg,
  45. single_module_arg, multi_module_arg, missing_module_arg,
  46. single_suite_arg, multi_suite_arg, missing_suite_arg,
  47. single_generator_arg, multi_generator_arg, missing_generator_arg,
  48. single_file_arg, multi_file_arg, missing_file_arg,
  49. single_dir_arg, multi_dir_arg, missing_dir_arg,
  50. multiple_arg_composition, multiple_arg_errors]}].
  51. %% this just unzips the example apps used by tests to the priv dir for later use
  52. init_per_suite(Config) ->
  53. PrivDir = ?config(priv_dir, Config),
  54. DataDir = ?config(data_dir, Config),
  55. ok = ec_file:copy(filename:join([DataDir, "basic_app.zip"]), filename:join([PrivDir, "basic_app.zip"])),
  56. {ok, _} = zip:extract(filename:join([PrivDir, "basic_app.zip"]), [{cwd, PrivDir}]),
  57. ok = ec_file:copy(filename:join([DataDir, "multi_app.zip"]), filename:join([PrivDir, "multi_app.zip"])),
  58. {ok, _} = zip:extract(filename:join([PrivDir, "multi_app.zip"]), [{cwd, PrivDir}]),
  59. Config.
  60. end_per_suite(Config) -> Config.
  61. init_per_group(basic_app, Config) ->
  62. GroupState = rebar_test_utils:init_rebar_state(Config, "basic_app_"),
  63. AppDir = ?config(apps, GroupState),
  64. PrivDir = ?config(priv_dir, GroupState),
  65. AppDirs = ["src", "include", "test"],
  66. lists:foreach(fun(F) -> ec_file:copy(filename:join([PrivDir, "basic_app", F]),
  67. filename:join([AppDir, F]),
  68. [recursive]) end, AppDirs),
  69. RebarConfig = [{erl_opts, [{d, config_define}]}, {eunit_compile_opts, [{d, eunit_compile_define}]}],
  70. {ok, State} = rebar_test_utils:run_and_check(GroupState, RebarConfig, ["as", "test", "lock"], return),
  71. [{result, State}|GroupState];
  72. init_per_group(multi_app, Config) ->
  73. GroupState = rebar_test_utils:init_rebar_state(Config, "multi_app_"),
  74. AppDir = ?config(apps, GroupState),
  75. PrivDir = ?config(priv_dir, GroupState),
  76. AppDirs = ["apps", "test"],
  77. lists:foreach(fun(F) -> ec_file:copy(filename:join([PrivDir, "multi_app", F]),
  78. filename:join([AppDir, F]),
  79. [recursive]) end, AppDirs),
  80. RebarConfig = [{erl_opts, [{d, config_define}]}, {eunit_compile_opts, [{d, eunit_compile_define}]}],
  81. {ok, State} = rebar_test_utils:run_and_check(GroupState, RebarConfig, ["as", "test", "lock"], return),
  82. [{result, State}|GroupState];
  83. init_per_group(cmd_line_args, Config) ->
  84. GroupState = rebar_test_utils:init_rebar_state(Config, "cmd_line_args_"),
  85. AppDir = ?config(apps, GroupState),
  86. PrivDir = ?config(priv_dir, GroupState),
  87. AppDirs = ["apps", "test"],
  88. lists:foreach(fun(F) -> ec_file:copy(filename:join([PrivDir, "multi_app", F]),
  89. filename:join([AppDir, F]),
  90. [recursive]) end, AppDirs),
  91. RebarConfig = [{erl_opts, [{d, config_define}]},
  92. {eunit_compile_opts, [{d, eunit_compile_define}]},
  93. %% test set not supported by cmd line args
  94. {eunit_tests, [{test, multi_app_bar, sanity_test},
  95. {test, multi_app_baz, sanity_test}]},
  96. {eunit_opts, [verbose]},
  97. {eunit_first_files, [filename:join(["apps", "multi_app_bar", "test", "multi_app_bar_tests_helper.erl"]),
  98. filename:join(["apps", "multi_app_baz", "test", "multi_app_baz_tests_helper.erl"])]}],
  99. {ok, State} = rebar_test_utils:run_and_check(GroupState, RebarConfig, ["eunit"], return),
  100. [{result, State}|GroupState];
  101. init_per_group(_, Config) -> Config.
  102. end_per_group(_, Config) -> Config.
  103. %% === tests for a single application at the root of a project ===
  104. %% check that project compiles properly
  105. basic_app_compiles(Config) ->
  106. AppDir = ?config(apps, Config),
  107. State = ?config(result, Config),
  108. {ok, _} = rebar_prv_eunit:do(State),
  109. rebar_test_utils:check_results(AppDir, [{app, "basic_app"}], "*").
  110. %% check that all files expected to be present are present
  111. basic_app_files(Config) ->
  112. AppDir = ?config(apps, Config),
  113. lists:foreach(fun(F) -> true = ec_file:exists(filename:join([AppDir, "_build", "test", "lib", "basic_app", "ebin", F])) end,
  114. ["basic_app.app", "basic_app.beam"]),
  115. lists:foreach(fun(F) -> true = ec_file:exists(filename:join([AppDir, "_build", "test", "lib", "basic_app", "test", F])) end,
  116. ["basic_app_tests.beam", "basic_app_tests_helper.beam"]).
  117. %% check that the correct tests are exported from modules for project
  118. %% note that this implies `TEST` is set correctly
  119. basic_app_exports(_Config) ->
  120. Tests = fun(Mod) ->
  121. begin
  122. Path = code:which(Mod),
  123. {ok, {Mod, [{exports, Ex}]}} = beam_lib:chunks(Path, [exports]),
  124. true = lists:member({sanity_test, 0}, Ex)
  125. end
  126. end,
  127. Helpers = fun(Mod) ->
  128. begin
  129. Path = code:which(Mod),
  130. {ok, {Mod, [{exports, Ex}]}} = beam_lib:chunks(Path, [exports]),
  131. true = lists:member({help, 0}, Ex)
  132. end
  133. end,
  134. lists:foreach(Tests, [basic_app, basic_app_tests]),
  135. lists:foreach(Helpers, [basic_app_tests_helper]).
  136. %% check that the correct tests are schedule to run for project
  137. basic_app_testset(Config) ->
  138. Result = ?config(result, Config),
  139. Set = {ok, [{application, basic_app},
  140. {module, basic_app_tests_helper}]},
  141. Set = rebar_prv_eunit:prepare_tests(Result).
  142. basic_app_eunit_macro(_Config) ->
  143. Macro = fun(Mod) ->
  144. begin
  145. Path = code:which(Mod),
  146. {ok, {Mod, [{compile_info, CompileInfo}]}} = beam_lib:chunks(Path, [compile_info]),
  147. Opts = proplists:get_value(options, CompileInfo, []),
  148. true = lists:member({d, 'EUNIT'}, Opts)
  149. end
  150. end,
  151. lists:foreach(Macro, [basic_app, basic_app_tests, basic_app_tests_helper]).
  152. %% === tests for multiple applications in the `apps' directory of a project ===
  153. %% check that project compiles properly
  154. multi_app_compiles(Config) ->
  155. AppDir = ?config(apps, Config),
  156. State = ?config(result, Config),
  157. {ok, _} = rebar_prv_eunit:do(State),
  158. rebar_test_utils:check_results(AppDir, [{app, "multi_app_bar"}, {app, "multi_app_baz"}], "*").
  159. %% check that all files expected to be present are present
  160. multi_app_files(Config) ->
  161. AppDir = ?config(apps, Config),
  162. lists:foreach(fun(F) -> true = ec_file:exists(filename:join([AppDir, "_build", "test", "lib", "multi_app_bar", "ebin", F])) end,
  163. ["multi_app_bar.app", "multi_app_bar.beam"]),
  164. lists:foreach(fun(F) -> true = ec_file:exists(filename:join([AppDir, "_build", "test", "lib", "multi_app_baz", "ebin", F])) end,
  165. ["multi_app_baz.app", "multi_app_baz.beam"]),
  166. lists:foreach(fun(F) -> true = ec_file:exists(filename:join([AppDir, "_build", "test", "lib", "multi_app_bar", "test", F])) end,
  167. ["multi_app_bar_tests.beam", "multi_app_bar_tests_helper.beam"]),
  168. lists:foreach(fun(F) -> true = ec_file:exists(filename:join([AppDir, "_build", "test", "lib", "multi_app_baz", "test", F])) end,
  169. ["multi_app_baz_tests.beam", "multi_app_baz_tests_helper.beam"]),
  170. lists:foreach(fun(F) -> true = ec_file:exists(filename:join([AppDir, "_build", "test", "extras", "test", F])) end,
  171. ["multi_app_tests.beam", "multi_app_tests_helper.beam"]).
  172. %% check that the correct tests are exported from modules for project
  173. %% note that this implies `TEST` is set correctly
  174. multi_app_exports(_Config) ->
  175. Tests = fun(Mod) ->
  176. begin
  177. Ex = Mod:module_info(exports),
  178. true = lists:member({sanity_test, 0}, Ex)
  179. end
  180. end,
  181. Helpers = fun(Mod) ->
  182. begin
  183. Ex = Mod:module_info(exports),
  184. true = lists:member({help, 0}, Ex)
  185. end
  186. end,
  187. lists:foreach(Tests, [multi_app_bar, multi_app_bar_tests,
  188. multi_app_baz, multi_app_baz_tests,
  189. multi_app_tests]),
  190. lists:foreach(Helpers, [multi_app_bar_tests_helper, multi_app_baz_tests_helper, multi_app_tests_helper]).
  191. %% check that the correct tests are schedule to run for project
  192. multi_app_testset(Config) ->
  193. Result = ?config(result, Config),
  194. Set = {ok, [{application, multi_app_baz},
  195. {application, multi_app_bar},
  196. {module, multi_app_bar_tests_helper},
  197. {module, multi_app_baz_tests_helper},
  198. {module, multi_app_tests},
  199. {module, multi_app_tests_helper}]},
  200. Set = rebar_prv_eunit:prepare_tests(Result).
  201. multi_app_eunit_macro(_Config) ->
  202. Macro = fun(Mod) ->
  203. begin
  204. Path = code:which(Mod),
  205. {ok, {Mod, [{compile_info, CompileInfo}]}} = beam_lib:chunks(Path, [compile_info]),
  206. Opts = proplists:get_value(options, CompileInfo, []),
  207. true = lists:member({d, 'EUNIT'}, Opts)
  208. end
  209. end,
  210. lists:foreach(Macro, [multi_app_bar, multi_app_bar_tests,
  211. multi_app_baz, multi_app_baz_tests,
  212. multi_app_tests, multi_app_tests_helper,
  213. multi_app_bar_tests_helper, multi_app_baz_tests_helper]).
  214. %% === tests for command line arguments ===
  215. %% no explicit test for cmd line args taking precedence over the rebar.config since
  216. %% almost every single test implies it
  217. %% check tests in the rebar.config are run if no cmd line opts are specified
  218. eunit_tests(Config) ->
  219. State = ?config(result, Config),
  220. Expect = {ok, [{test, multi_app_bar, sanity_test}, {test, multi_app_baz, sanity_test}]},
  221. Expect = rebar_prv_eunit:prepare_tests(State).
  222. %% check eunit_opts from the rebar.config are respected
  223. eunit_opts(Config) ->
  224. State = ?config(result, Config),
  225. Apps = rebar_state:project_apps(State),
  226. lists:foreach(fun(App) -> [verbose] = rebar_app_info:get(App, eunit_opts) end,
  227. Apps).
  228. %% check eunit_first_files from the rebar.config are respected
  229. eunit_first_files(Config) ->
  230. State = ?config(result, Config),
  231. FirstFiles = [filename:join(["apps", "multi_app_bar", "test", "multi_app_bar_tests_helper.erl"]),
  232. filename:join(["apps", "multi_app_baz", "test", "multi_app_baz_tests_helper.erl"])],
  233. Apps = rebar_state:project_apps(State),
  234. lists:foreach(fun(App) -> FirstFiles = rebar_app_info:get(App, eunit_first_files) end,
  235. Apps).
  236. %% check that the --application cmd line opt generates the correct test set
  237. single_application_arg(Config) ->
  238. S = ?config(result, Config),
  239. {ok, Args} = getopt:parse(rebar_prv_eunit:eunit_opts(S), ["--application=multi_app_bar"]),
  240. State = rebar_state:command_parsed_args(S, Args),
  241. {ok, [{application, multi_app_bar}]} = rebar_prv_eunit:prepare_tests(State).
  242. multi_application_arg(Config) ->
  243. S = ?config(result, Config),
  244. {ok, Args} = getopt:parse(rebar_prv_eunit:eunit_opts(S), ["--application=multi_app_bar,multi_app_baz"]),
  245. State = rebar_state:command_parsed_args(S, Args),
  246. {ok, [{application, multi_app_bar}, {application, multi_app_baz}]} = rebar_prv_eunit:prepare_tests(State).
  247. %% check that an invalid --application cmd line opt generates an error
  248. missing_application_arg(Config) ->
  249. S = ?config(result, Config),
  250. {ok, Args} = getopt:parse(rebar_prv_eunit:eunit_opts(S), ["--application=missing_app"]),
  251. State = rebar_state:command_parsed_args(S, Args),
  252. Error = {error, {rebar_prv_eunit, {eunit_test_errors, ["Application `missing_app' not found in project."]}}},
  253. Error = rebar_prv_eunit:validate_tests(State, rebar_prv_eunit:prepare_tests(State)).
  254. %% check that the --module cmd line opt generates the correct test set
  255. single_module_arg(Config) ->
  256. AppDir = ?config(apps, Config),
  257. S = ?config(result, Config),
  258. %% necessary to fix paths
  259. Path = code:get_path(),
  260. code:add_paths([filename:join([AppDir, "_build", "test", "lib", "multi_app_bar", "ebin"])]),
  261. {ok, Args} = getopt:parse(rebar_prv_eunit:eunit_opts(S), ["--module=multi_app_bar"]),
  262. State = rebar_state:command_parsed_args(S, Args),
  263. {ok, [{module, multi_app_bar}]} = rebar_prv_eunit:prepare_tests(State),
  264. %% restore path
  265. code:set_path(Path).
  266. multi_module_arg(Config) ->
  267. AppDir = ?config(apps, Config),
  268. S = ?config(result, Config),
  269. %% necessary to fix paths
  270. Path = code:get_path(),
  271. code:add_paths([filename:join([AppDir, "_build", "test", "lib", "multi_app_bar", "ebin"])]),
  272. code:add_paths([filename:join([AppDir, "_build", "test", "lib", "multi_app_baz", "ebin"])]),
  273. {ok, Args} = getopt:parse(rebar_prv_eunit:eunit_opts(S), ["--module=multi_app_bar,multi_app_baz"]),
  274. State = rebar_state:command_parsed_args(S, Args),
  275. {ok, [{module, multi_app_bar}, {module, multi_app_baz}]} = rebar_prv_eunit:prepare_tests(State),
  276. %% restore path
  277. code:set_path(Path).
  278. %% check that an invalid --module cmd line opt generates an error
  279. missing_module_arg(Config) ->
  280. S = ?config(result, Config),
  281. {ok, Args} = getopt:parse(rebar_prv_eunit:eunit_opts(S), ["--module=missing_app"]),
  282. State = rebar_state:command_parsed_args(S, Args),
  283. T = rebar_prv_eunit:prepare_tests(State),
  284. Tests = rebar_prv_eunit:validate_tests(S, T),
  285. Error = {error, {rebar_prv_eunit, {eunit_test_errors, ["Module `missing_app' not found in project."]}}},
  286. Error = Tests.
  287. %% check that the --suite cmd line opt generates the correct test set
  288. single_suite_arg(Config) ->
  289. AppDir = ?config(apps, Config),
  290. S = ?config(result, Config),
  291. %% necessary to fix paths
  292. Path = code:get_path(),
  293. code:add_paths([filename:join([AppDir, "_build", "test", "lib", "multi_app_bar", "ebin"])]),
  294. {ok, Args} = getopt:parse(rebar_prv_eunit:eunit_opts(S), ["--suite=multi_app_bar"]),
  295. State = rebar_state:command_parsed_args(S, Args),
  296. {ok, [{module, multi_app_bar}]} = rebar_prv_eunit:prepare_tests(State),
  297. %% restore path
  298. code:set_path(Path).
  299. multi_suite_arg(Config) ->
  300. AppDir = ?config(apps, Config),
  301. S = ?config(result, Config),
  302. %% necessary to fix paths
  303. Path = code:get_path(),
  304. code:add_paths([filename:join([AppDir, "_build", "test", "lib", "multi_app_bar", "ebin"])]),
  305. code:add_paths([filename:join([AppDir, "_build", "test", "lib", "multi_app_baz", "ebin"])]),
  306. {ok, Args} = getopt:parse(rebar_prv_eunit:eunit_opts(S), ["--suite=multi_app_bar,multi_app_baz"]),
  307. State = rebar_state:command_parsed_args(S, Args),
  308. {ok, [{module, multi_app_bar}, {module, multi_app_baz}]} = rebar_prv_eunit:prepare_tests(State),
  309. %% restore path
  310. code:set_path(Path).
  311. %% check that an invalid --suite cmd line opt generates an error
  312. missing_suite_arg(Config) ->
  313. S = ?config(result, Config),
  314. {ok, Args} = getopt:parse(rebar_prv_eunit:eunit_opts(S), ["--suite=missing_app"]),
  315. State = rebar_state:command_parsed_args(S, Args),
  316. Error = {error, {rebar_prv_eunit, {eunit_test_errors, ["Module `missing_app' not found in project."]}}},
  317. Error = rebar_prv_eunit:validate_tests(State, rebar_prv_eunit:prepare_tests(State)).
  318. %% check that the --generator cmd line opt generates the correct test set
  319. single_generator_arg(Config) ->
  320. S = ?config(result, Config),
  321. {ok, Args} = getopt:parse(rebar_prv_eunit:eunit_opts(S), ["--generator=module_name:function_name"]),
  322. State = rebar_state:command_parsed_args(S, Args),
  323. {ok, [{generator, module_name, function_name}]} = rebar_prv_eunit:prepare_tests(State).
  324. multi_generator_arg(Config) ->
  325. S = ?config(result, Config),
  326. {ok, Args} = getopt:parse(rebar_prv_eunit:eunit_opts(S), ["--generator=module1:func1;func2,module2:func1;func2"]),
  327. State = rebar_state:command_parsed_args(S, Args),
  328. Generators = [{generator, module1, func1},
  329. {generator, module1, func2},
  330. {generator, module2, func1},
  331. {generator, module2, func2}],
  332. {ok, Generators} = rebar_prv_eunit:prepare_tests(State).
  333. %% check that an invalid --suite cmd line opt generates an error
  334. missing_generator_arg(Config) ->
  335. S = ?config(result, Config),
  336. {ok, Args} = getopt:parse(rebar_prv_eunit:eunit_opts(S), ["--generator=missing_module:func1"]),
  337. State = rebar_state:command_parsed_args(S, Args),
  338. Error = {error, {rebar_prv_eunit, {eunit_test_errors, ["Module `missing_module' not found in project."]}}},
  339. Error = rebar_prv_eunit:validate_tests(State, rebar_prv_eunit:prepare_tests(State)).
  340. %% check that the --file cmd line opt generates the correct test set
  341. single_file_arg(Config) ->
  342. S = ?config(result, Config),
  343. AppDir = ?config(apps, Config),
  344. Path = filename:join([AppDir, "_build", "test", "lib", "multi_app_bar", "ebin", "multi_app_bar.beam"]),
  345. {ok, Args} = getopt:parse(rebar_prv_eunit:eunit_opts(S), ["--file=" ++ Path]),
  346. State = rebar_state:command_parsed_args(S, Args),
  347. {ok, [{file, Path}]} = rebar_prv_eunit:prepare_tests(State).
  348. multi_file_arg(Config) ->
  349. S = ?config(result, Config),
  350. AppDir = ?config(apps, Config),
  351. BarPath = filename:join([AppDir, "_build", "test", "lib", "multi_app_bar", "ebin", "multi_app_bar.beam"]),
  352. BazPath = filename:join([AppDir, "_build", "test", "lib", "multi_app_baz", "ebin", "multi_app_baz.beam"]),
  353. {ok, Args} = getopt:parse(rebar_prv_eunit:eunit_opts(S), ["--file=" ++ BarPath ++ "," ++ BazPath]),
  354. State = rebar_state:command_parsed_args(S, Args),
  355. {ok, [{file, BarPath}, {file, BazPath}]} = rebar_prv_eunit:prepare_tests(State).
  356. %% check that an invalid --file cmd line opt generates an error
  357. missing_file_arg(Config) ->
  358. S = ?config(result, Config),
  359. AppDir = ?config(apps, Config),
  360. Path = filename:join([AppDir, "_build", "test", "lib", "missing_app", "ebin", "missing_app.beam"]),
  361. {ok, Args} = getopt:parse(rebar_prv_eunit:eunit_opts(S), ["--file=" ++ Path]),
  362. State = rebar_state:command_parsed_args(S, Args),
  363. Error = {error, {rebar_prv_eunit, {eunit_test_errors, ["File `" ++ Path ++"' not found."]}}},
  364. Error = rebar_prv_eunit:validate_tests(State, rebar_prv_eunit:prepare_tests(State)).
  365. %% check that the --dir cmd line opt generates the correct test set
  366. single_dir_arg(Config) ->
  367. S = ?config(result, Config),
  368. AppDir = ?config(apps, Config),
  369. Path = filename:join([AppDir, "_build", "test", "lib", "multi_app_bar", "ebin"]),
  370. {ok, Args} = getopt:parse(rebar_prv_eunit:eunit_opts(S), ["--dir=" ++ Path]),
  371. State = rebar_state:command_parsed_args(S, Args),
  372. {ok, [{dir, Path}]} = rebar_prv_eunit:prepare_tests(State).
  373. multi_dir_arg(Config) ->
  374. S = ?config(result, Config),
  375. AppDir = ?config(apps, Config),
  376. BarPath = filename:join([AppDir, "_build", "test", "lib", "multi_app_bar", "ebin"]),
  377. BazPath = filename:join([AppDir, "_build", "test", "lib", "multi_app_baz", "ebin"]),
  378. {ok, Args} = getopt:parse(rebar_prv_eunit:eunit_opts(S), ["--dir=" ++ BarPath ++ "," ++ BazPath]),
  379. State = rebar_state:command_parsed_args(S, Args),
  380. {ok, [{dir, BarPath}, {dir, BazPath}]} = rebar_prv_eunit:prepare_tests(State).
  381. %% check that an invalid --dir cmd line opt generates an error
  382. missing_dir_arg(Config) ->
  383. S = ?config(result, Config),
  384. AppDir = ?config(apps, Config),
  385. Path = filename:join([AppDir, "_build", "test", "lib", "missing_app", "ebin"]),
  386. {ok, Args} = getopt:parse(rebar_prv_eunit:eunit_opts(S), ["--dir=" ++ Path]),
  387. State = rebar_state:command_parsed_args(S, Args),
  388. Error = {error, {rebar_prv_eunit, {eunit_test_errors, ["Directory `" ++ Path ++"' not found."]}}},
  389. Error = rebar_prv_eunit:validate_tests(State, rebar_prv_eunit:prepare_tests(State)).
  390. %% check that multiple args are composed
  391. multiple_arg_composition(Config) ->
  392. S = ?config(result, Config),
  393. AppDir = ?config(apps, Config),
  394. %% necessary to fix paths
  395. Path = code:get_path(),
  396. code:add_paths([filename:join([AppDir, "_build", "test", "lib", "multi_app_bar", "ebin"])]),
  397. FilePath = filename:join([AppDir, "_build", "test", "lib", "multi_app_bar", "ebin", "multi_app_bar.beam"]),
  398. DirPath = filename:join([AppDir, "_build", "test", "lib", "multi_app_bar", "ebin"]),
  399. {ok, Args} = getopt:parse(rebar_prv_eunit:eunit_opts(S), ["--application=multi_app_bar",
  400. "--module=multi_app_bar",
  401. "--suite=multi_app_bar",
  402. "--file=" ++ FilePath,
  403. "--dir=" ++ DirPath]),
  404. State = rebar_state:command_parsed_args(S, Args),
  405. Expect = [{application, multi_app_bar},
  406. {dir, DirPath},
  407. {file, FilePath},
  408. {module, multi_app_bar},
  409. {module, multi_app_bar}],
  410. {ok, Expect} = rebar_prv_eunit:prepare_tests(State),
  411. %% restore path
  412. code:set_path(Path).
  413. %% check that multiple errors are reported
  414. multiple_arg_errors(Config) ->
  415. S = ?config(result, Config),
  416. AppDir = ?config(apps, Config),
  417. FilePath = filename:join([AppDir, "_build", "test", "lib", "missing_app", "ebin", "missing_app.beam"]),
  418. DirPath = filename:join([AppDir, "_build", "test", "lib", "missing_app", "ebin"]),
  419. {ok, Args} = getopt:parse(rebar_prv_eunit:eunit_opts(S), ["--application=missing_app",
  420. "--module=missing_app",
  421. "--suite=missing_app",
  422. "--file=" ++ FilePath,
  423. "--dir=" ++ DirPath]),
  424. State = rebar_state:command_parsed_args(S, Args),
  425. T = rebar_prv_eunit:prepare_tests(State),
  426. Tests = rebar_prv_eunit:validate_tests(S, T),
  427. Expect = ["Application `missing_app' not found in project.",
  428. "Directory `" ++ DirPath ++ "' not found.",
  429. "File `" ++ FilePath ++ "' not found.",
  430. "Module `missing_app' not found in project.",
  431. "Module `missing_app' not found in project."],
  432. {error, {rebar_prv_eunit, {eunit_test_errors, Expect}}} = Tests.
  433. misspecified_eunit_tests(Config) ->
  434. State = rebar_test_utils:init_rebar_state(Config, "basic_app_"),
  435. AppDir = ?config(apps, State),
  436. PrivDir = ?config(priv_dir, State),
  437. AppDirs = ["src", "include", "test"],
  438. lists:foreach(fun(F) -> ec_file:copy(filename:join([PrivDir, "basic_app", F]),
  439. filename:join([AppDir, F]),
  440. [recursive]) end, AppDirs),
  441. BaseConfig = [{erl_opts, [{d, config_define}]}, {eunit_compile_opts, [{d, eunit_compile_define}]}],
  442. RebarConfig = [{eunit_tests, {dir, "test"}}|BaseConfig],
  443. {error, {rebar_prv_eunit, Error}} = rebar_test_utils:run_and_check(State, RebarConfig, ["eunit"], return),
  444. {badconfig, {"Value `~p' of option `~p' must be a list", {{dir, "test"}, eunit_tests}}} = Error.
  445. misspecified_eunit_compile_opts(Config) ->
  446. State = rebar_test_utils:init_rebar_state(Config, "basic_app_"),
  447. AppDir = ?config(apps, State),
  448. PrivDir = ?config(priv_dir, State),
  449. AppDirs = ["src", "include", "test"],
  450. lists:foreach(fun(F) -> ec_file:copy(filename:join([PrivDir, "basic_app", F]),
  451. filename:join([AppDir, F]),
  452. [recursive]) end, AppDirs),
  453. RebarConfig = [{erl_opts, [{d, config_define}]}, {eunit_compile_opts, {d, eunit_compile_define}}],
  454. {error, {rebar_prv_eunit, Error}} = rebar_test_utils:run_and_check(State, RebarConfig, ["eunit"], return),
  455. {badconfig, {"Value `~p' of option `~p' must be a list", {{d, eunit_compile_define}, eunit_compile_opts}}} = Error.
  456. misspecified_eunit_first_files(Config) ->
  457. State = rebar_test_utils:init_rebar_state(Config, "basic_app_"),
  458. AppDir = ?config(apps, State),
  459. PrivDir = ?config(priv_dir, State),
  460. AppDirs = ["src", "include", "test"],
  461. lists:foreach(fun(F) -> ec_file:copy(filename:join([PrivDir, "basic_app", F]),
  462. filename:join([AppDir, F]),
  463. [recursive]) end, AppDirs),
  464. BaseConfig = [{erl_opts, [{d, config_define}]}, {eunit_compile_opts, [{d, eunit_compile_define}]}],
  465. RebarConfig = [{eunit_first_files, some_file}|BaseConfig],
  466. {error, {rebar_prv_eunit, Error}} = rebar_test_utils:run_and_check(State, RebarConfig, ["eunit"], return),
  467. {badconfig, {"Value `~p' of option `~p' must be a list", {some_file, eunit_first_files}}} = Error.
  468. alternate_test_regex(Config) ->
  469. State = rebar_test_utils:init_rebar_state(Config, "alternate_test_regex_"),
  470. AppDir = ?config(apps, State),
  471. PrivDir = ?config(priv_dir, State),
  472. AppDirs = ["src", "include", "test"],
  473. lists:foreach(fun(F) -> ec_file:copy(filename:join([PrivDir, "basic_app", F]),
  474. filename:join([AppDir, F]),
  475. [recursive]) end, AppDirs),
  476. BaseConfig = [{erl_opts, [{d, config_define}]}, {eunit_compile_opts, [{d, eunit_compile_define}]}],
  477. RebarConfig = [{eunit_test_regex, "basic_app_tests.erl"}|BaseConfig],
  478. {ok, S} = rebar_test_utils:run_and_check(State, RebarConfig, ["as", "test", "lock"], return),
  479. Set = {ok, [{application, basic_app},
  480. {module, basic_app_tests}]},
  481. Set = rebar_prv_eunit:prepare_tests(S).