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.

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