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.

322 lines
13 KiB

  1. -module(rebar_cover_SUITE).
  2. -export([suite/0,
  3. init_per_suite/1,
  4. end_per_suite/1,
  5. init_per_testcase/2,
  6. all/0,
  7. flag_coverdata_written/1,
  8. config_coverdata_written/1,
  9. config_coverdata_overridden_name_written/1,
  10. basic_extra_src_dirs/1,
  11. release_extra_src_dirs/1,
  12. root_extra_src_dirs/1,
  13. index_written/1,
  14. flag_verbose/1,
  15. config_verbose/1,
  16. excl_mods_and_apps/1,
  17. coverdata_is_reset_on_write/1,
  18. flag_min_coverage/1,
  19. config_min_coverage/1]).
  20. -include_lib("common_test/include/ct.hrl").
  21. -include_lib("eunit/include/eunit.hrl").
  22. -include_lib("kernel/include/file.hrl").
  23. suite() ->
  24. [].
  25. init_per_suite(Config) ->
  26. Config.
  27. end_per_suite(_Config) ->
  28. ok.
  29. init_per_testcase(_, Config) ->
  30. rebar_test_utils:init_rebar_state(Config, "cover_").
  31. all() ->
  32. [flag_coverdata_written, config_coverdata_written,
  33. config_coverdata_overridden_name_written,
  34. basic_extra_src_dirs, release_extra_src_dirs,
  35. root_extra_src_dirs,
  36. index_written,
  37. flag_verbose, config_verbose,
  38. excl_mods_and_apps, coverdata_is_reset_on_write,
  39. flag_min_coverage, config_min_coverage].
  40. flag_coverdata_written(Config) ->
  41. AppDir = ?config(apps, Config),
  42. Name = rebar_test_utils:create_random_name("cover_"),
  43. Vsn = rebar_test_utils:create_random_vsn(),
  44. rebar_test_utils:create_eunit_app(AppDir, Name, Vsn, [kernel, stdlib]),
  45. RebarConfig = [{erl_opts, [{d, some_define}]}],
  46. rebar_test_utils:run_and_check(Config,
  47. RebarConfig,
  48. ["eunit", "--cover"],
  49. {ok, [{app, Name}]}),
  50. true = filelib:is_file(filename:join([AppDir, "_build", "test", "cover", "eunit.coverdata"])).
  51. config_coverdata_written(Config) ->
  52. AppDir = ?config(apps, Config),
  53. Name = rebar_test_utils:create_random_name("cover_"),
  54. Vsn = rebar_test_utils:create_random_vsn(),
  55. rebar_test_utils:create_eunit_app(AppDir, Name, Vsn, [kernel, stdlib]),
  56. RebarConfig = [{erl_opts, [{d, some_define}]}, {cover_enabled, true}],
  57. rebar_test_utils:run_and_check(Config,
  58. RebarConfig,
  59. ["eunit"],
  60. {ok, [{app, Name}]}),
  61. true = filelib:is_file(filename:join([AppDir, "_build", "test", "cover", "eunit.coverdata"])).
  62. config_coverdata_overridden_name_written(Config) ->
  63. AppDir = ?config(apps, Config),
  64. Name = rebar_test_utils:create_random_name("cover_"),
  65. Vsn = rebar_test_utils:create_random_vsn(),
  66. rebar_test_utils:create_eunit_app(AppDir, Name, Vsn, [kernel, stdlib]),
  67. RebarConfig = [{erl_opts, [{d, some_define}]}, {cover_enabled, true}],
  68. rebar_test_utils:run_and_check(Config,
  69. RebarConfig,
  70. ["eunit", "--cover_export_name=test_name"],
  71. {ok, [{app, Name}]}),
  72. true = filelib:is_file(filename:join([AppDir, "_build", "test", "cover", "test_name.coverdata"])).
  73. basic_extra_src_dirs(Config) ->
  74. AppDir = ?config(apps, Config),
  75. Name = rebar_test_utils:create_random_name("cover_extra_"),
  76. Vsn = rebar_test_utils:create_random_vsn(),
  77. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  78. ExtraSrc = io_lib:format("-module(~ts_extra).\n-export([ok/0]).\nok() -> ok.\n", [Name]),
  79. ok = filelib:ensure_dir(filename:join([AppDir, "extra", "dummy"])),
  80. ok = file:write_file(filename:join([AppDir, "extra", io_lib:format("~ts_extra.erl", [Name])]),
  81. ExtraSrc),
  82. RebarConfig = [{erl_opts, [{d, some_define}]}, {extra_src_dirs, ["extra"]}],
  83. rebar_test_utils:run_and_check(Config,
  84. RebarConfig,
  85. ["eunit", "--cover"],
  86. {ok, [{app, Name}]}),
  87. Mod = list_to_atom(Name),
  88. {file, _} = cover:is_compiled(Mod),
  89. ExtraMod = list_to_atom(lists:flatten(io_lib:format("~ts_extra", [Name]))),
  90. false = cover:is_compiled(ExtraMod).
  91. release_extra_src_dirs(Config) ->
  92. AppDir = ?config(apps, Config),
  93. Name1 = rebar_test_utils:create_random_name("relapp1_"),
  94. Vsn1 = rebar_test_utils:create_random_vsn(),
  95. rebar_test_utils:create_app(filename:join([AppDir, "apps", Name1]), Name1, Vsn1, [kernel, stdlib]),
  96. Name2 = rebar_test_utils:create_random_name("relapp2_"),
  97. Vsn2 = rebar_test_utils:create_random_vsn(),
  98. rebar_test_utils:create_app(filename:join([AppDir, "apps", Name2]), Name2, Vsn2, [kernel, stdlib]),
  99. ExtraOne = io_lib:format("-module(~ts_extra).\n-export([ok/0]).\nok() -> ok.\n", [Name1]),
  100. ok = filelib:ensure_dir(filename:join([AppDir, "apps", Name1, "extra", "dummy"])),
  101. ok = file:write_file(filename:join([AppDir, "apps", Name1, "extra",
  102. io_lib:format("~ts_extra.erl", [Name1])]),
  103. ExtraOne),
  104. ExtraTwo = io_lib:format("-module(~ts_extra).\n-export([ok/0]).\nok() -> ok.\n", [Name2]),
  105. ok = filelib:ensure_dir(filename:join([AppDir, "apps", Name2, "extra", "dummy"])),
  106. ok = file:write_file(filename:join([AppDir, "apps", Name2, "extra",
  107. io_lib:format("~ts_extra.erl", [Name2])]),
  108. ExtraTwo),
  109. RebarConfig = [{erl_opts, [{d, some_define}]}, {extra_src_dirs, ["extra"]}],
  110. rebar_test_utils:run_and_check(Config,
  111. RebarConfig,
  112. ["eunit", "--cover"],
  113. {ok, [{app, Name1}, {app, Name2}]}),
  114. Mod1 = list_to_atom(Name1),
  115. {file, _} = cover:is_compiled(Mod1),
  116. Mod2 = list_to_atom(Name2),
  117. {file, _} = cover:is_compiled(Mod2),
  118. ExtraMod1 = list_to_atom(lists:flatten(io_lib:format("~ts_extra", [Name1]))),
  119. false = cover:is_compiled(ExtraMod1),
  120. ExtraMod2 = list_to_atom(lists:flatten(io_lib:format("~ts_extra", [Name2]))),
  121. false = cover:is_compiled(ExtraMod2).
  122. root_extra_src_dirs(Config) ->
  123. AppDir = ?config(apps, Config),
  124. Name1 = rebar_test_utils:create_random_name("relapp1_"),
  125. Vsn1 = rebar_test_utils:create_random_vsn(),
  126. rebar_test_utils:create_app(filename:join([AppDir, "apps", Name1]), Name1, Vsn1, [kernel, stdlib]),
  127. Name2 = rebar_test_utils:create_random_name("relapp2_"),
  128. Vsn2 = rebar_test_utils:create_random_vsn(),
  129. rebar_test_utils:create_app(filename:join([AppDir, "apps", Name2]), Name2, Vsn2, [kernel, stdlib]),
  130. Extra = <<"-module(extra).\n-export([ok/0]).\nok() -> ok.\n">>,
  131. ok = filelib:ensure_dir(filename:join([AppDir, "extra", "dummy"])),
  132. ok = file:write_file(filename:join([AppDir, "extra", "extra.erl"]), Extra),
  133. RebarConfig = [{erl_opts, [{d, some_define}]}, {extra_src_dirs, ["extra"]}],
  134. rebar_test_utils:run_and_check(Config,
  135. RebarConfig,
  136. ["eunit", "--cover"],
  137. {ok, [{app, Name1}, {app, Name2}]}),
  138. Mod1 = list_to_atom(Name1),
  139. {file, _} = cover:is_compiled(Mod1),
  140. Mod2 = list_to_atom(Name2),
  141. {file, _} = cover:is_compiled(Mod2),
  142. false = cover:is_compiled(extra).
  143. index_written(Config) ->
  144. AppDir = ?config(apps, Config),
  145. Name = rebar_test_utils:create_random_name("cover_"),
  146. Vsn = rebar_test_utils:create_random_vsn(),
  147. rebar_test_utils:create_eunit_app(AppDir, Name, Vsn, [kernel, stdlib]),
  148. RebarConfig = [{erl_opts, [{d, some_define}]}],
  149. rebar_test_utils:run_and_check(Config,
  150. RebarConfig,
  151. ["do", "eunit", "--cover", ",", "cover"],
  152. {ok, [{app, Name}]}),
  153. true = filelib:is_file(filename:join([AppDir, "_build", "test", "cover", "index.html"])).
  154. flag_verbose(Config) ->
  155. AppDir = ?config(apps, Config),
  156. Name = rebar_test_utils:create_random_name("cover_"),
  157. Vsn = rebar_test_utils:create_random_vsn(),
  158. rebar_test_utils:create_eunit_app(AppDir, Name, Vsn, [kernel, stdlib]),
  159. RebarConfig = [{erl_opts, [{d, some_define}]}],
  160. rebar_test_utils:run_and_check(Config,
  161. RebarConfig,
  162. ["do", "eunit", "--cover", ",", "cover", "--verbose"],
  163. {ok, [{app, Name}]}),
  164. true = filelib:is_file(filename:join([AppDir, "_build", "test", "cover", "index.html"])).
  165. config_verbose(Config) ->
  166. AppDir = ?config(apps, Config),
  167. Name = rebar_test_utils:create_random_name("cover_"),
  168. Vsn = rebar_test_utils:create_random_vsn(),
  169. rebar_test_utils:create_eunit_app(AppDir, Name, Vsn, [kernel, stdlib]),
  170. RebarConfig = [{erl_opts, [{d, some_define}]}, {cover_opts, [verbose]}],
  171. rebar_test_utils:run_and_check(Config,
  172. RebarConfig,
  173. ["do", "eunit", "--cover", ",", "cover"],
  174. {ok, [{app, Name}]}),
  175. true = filelib:is_file(filename:join([AppDir, "_build", "test", "cover", "index.html"])).
  176. excl_mods_and_apps(Config) ->
  177. AppDir = ?config(apps, Config),
  178. Name1 = rebar_test_utils:create_random_name("relapp1_"),
  179. Vsn1 = rebar_test_utils:create_random_vsn(),
  180. rebar_test_utils:create_app(filename:join([AppDir, "apps", Name1]), Name1, Vsn1, [kernel, stdlib]),
  181. Name2 = rebar_test_utils:create_random_name("relapp2_"),
  182. Vsn2 = rebar_test_utils:create_random_vsn(),
  183. rebar_test_utils:create_app(filename:join([AppDir, "apps", Name2]), Name2, Vsn2, [kernel, stdlib]),
  184. Name3 = rebar_test_utils:create_random_name("excludeme_"),
  185. Vsn3 = rebar_test_utils:create_random_vsn(),
  186. rebar_test_utils:create_app(filename:join([AppDir, "apps", Name3]), Name3, Vsn3, [kernel, stdlib]),
  187. Mod1 = list_to_atom(Name1),
  188. Mod2 = list_to_atom(Name2),
  189. Mod3 = list_to_atom(Name3),
  190. RebarConfig = [{erl_opts, [{d, some_define}]},
  191. {cover_excl_mods, [Mod2]},
  192. {cover_excl_apps, [Name3]}],
  193. rebar_test_utils:run_and_check(Config,
  194. RebarConfig,
  195. ["eunit", "--cover"],
  196. {ok, [{app, Name1}, {app, Name2}, {app, Name3}]}),
  197. {file, _} = cover:is_compiled(Mod1),
  198. false = cover:is_compiled(Mod2),
  199. false = cover:is_compiled(Mod3).
  200. coverdata_is_reset_on_write(Config) ->
  201. AppDir = ?config(apps, Config),
  202. Name = rebar_test_utils:create_random_name("coverdata_is_reset_on_write_"),
  203. Vsn = rebar_test_utils:create_random_vsn(),
  204. rebar_test_utils:create_eunit_app(AppDir, Name, Vsn, [kernel, stdlib]),
  205. RebarConfig = [{erl_opts, [{d, some_define}]}, {cover_enabled, true}],
  206. rebar_test_utils:run_and_check(Config,
  207. RebarConfig,
  208. ["eunit"],
  209. {ok, [{app, Name}]}),
  210. Res = lists:map(fun(M) -> cover:analyse(M) end, cover:modules()),
  211. Ok = lists:foldl(fun({ok, R}, Acc) -> R ++ Acc end, [], Res),
  212. [] = lists:filter(fun({_, {0,_}}) -> false; (_) -> true end, Ok).
  213. flag_min_coverage(Config) ->
  214. AppDir = ?config(apps, Config),
  215. Name = rebar_test_utils:create_random_name("min_cover_"),
  216. Vsn = rebar_test_utils:create_random_vsn(),
  217. rebar_test_utils:create_eunit_app(AppDir, Name, Vsn, [kernel, stdlib]),
  218. RebarConfig = [{erl_opts, [{d, some_define}]}],
  219. ?assertMatch({ok, _},
  220. rebar_test_utils:run_and_check(
  221. Config, RebarConfig,
  222. ["do", "eunit", "--cover", ",", "cover", "--min_coverage=5"],
  223. return)),
  224. ?assertMatch({error,{rebar_prv_cover,{min_coverage_failed,{65,_}}}},
  225. rebar_test_utils:run_and_check(
  226. Config, RebarConfig,
  227. ["do", "eunit", "--cover", ",", "cover", "--min_coverage=65"],
  228. return)),
  229. ok.
  230. config_min_coverage(Config) ->
  231. AppDir = ?config(apps, Config),
  232. Name = rebar_test_utils:create_random_name("cover_"),
  233. Vsn = rebar_test_utils:create_random_vsn(),
  234. rebar_test_utils:create_eunit_app(AppDir, Name, Vsn, [kernel, stdlib]),
  235. RebarConfig1 = [{erl_opts, [{d, some_define}]}, {cover_opts, [{min_coverage,5}]}],
  236. ?assertMatch({ok, _},
  237. rebar_test_utils:run_and_check(
  238. Config, RebarConfig1,
  239. ["do", "eunit", "--cover", ",", "cover"],
  240. return)),
  241. RebarConfig2 = [{erl_opts, [{d, some_define}]}, {cover_opts, [{min_coverage,65}]}],
  242. ?assertMatch({error,{rebar_prv_cover,{min_coverage_failed,{65,_}}}},
  243. rebar_test_utils:run_and_check(
  244. Config, RebarConfig2,
  245. ["do", "eunit", "--cover", ",", "cover"],
  246. return)),
  247. ok.