Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

305 рядки
12 KiB

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