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.

608 regels
26 KiB

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