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.

604 line
26 KiB

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