Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

229 řádky
10 KiB

  1. %% -*- erlang-indent-level: 4;indent-tabs-mode: nil -*-
  2. %% ex: ts=4 sw=4 et
  3. -module(rebar_xref_SUITE).
  4. -export([suite/0,
  5. init_per_suite/1,
  6. end_per_suite/1,
  7. init_per_testcase/2,
  8. end_per_testcase/2,
  9. all/0,
  10. xref_test/1,
  11. xref_ignore_test/1,
  12. xref_dep_hook/1,
  13. xref_undef_behaviour/1]).
  14. -include_lib("common_test/include/ct.hrl").
  15. -include_lib("eunit/include/eunit.hrl").
  16. -include_lib("kernel/include/file.hrl").
  17. %% ===================================================================
  18. %% common_test callbacks
  19. %% ===================================================================
  20. suite() ->
  21. [].
  22. init_per_suite(Config) ->
  23. Config.
  24. end_per_suite(_Config) ->
  25. ok.
  26. init_per_testcase(xref_dep_hook, Config) ->
  27. Src = filename:join([?config(data_dir, Config), "recursive"]),
  28. Dst = filename:join([?config(priv_dir, Config), "recursive"]),
  29. ok = rebar_file_utils:cp_r([Src], Dst),
  30. GlobalDir = filename:join([?config(priv_dir, Config), "cache"]),
  31. State = rebar_state:new([{base_dir, filename:join([Dst, "_build"])}
  32. ,{global_rebar_dir, GlobalDir}
  33. ,{root_dir, Dst}]),
  34. [{apps, Dst}, {state, State} | Config];
  35. init_per_testcase(Case, Config) ->
  36. UpdConfig = rebar_test_utils:init_rebar_state(Config),
  37. AppDir = ?config(apps, UpdConfig),
  38. file:set_cwd(AppDir),
  39. Name = rebar_test_utils:create_random_name("xrefapp_"),
  40. Vsn = rebar_test_utils:create_random_vsn(),
  41. rebar_test_utils:create_empty_app(AppDir, Name, Vsn, [kernel, stdlib]),
  42. AppModules = [behaviour1, behaviour2, mymod, othermod],
  43. [write_src_file(AppDir, Name, Module, ignore_xref(Case)) || Module <- AppModules],
  44. RebarConfig = [{erl_opts, [debug_info]},
  45. {xref_checks, [deprecated_function_calls,deprecated_functions,
  46. undefined_function_calls,undefined_functions,
  47. exports_not_used,locals_not_used]}],
  48. [{app_name, Name},
  49. {rebar_config, RebarConfig} | UpdConfig].
  50. end_per_testcase(_, _Config) ->
  51. ok.
  52. all() ->
  53. [xref_test, xref_ignore_test, xref_dep_hook, xref_undef_behaviour].
  54. %% ===================================================================
  55. %% Test cases
  56. %% ===================================================================
  57. xref_test(Config) ->
  58. AppDir = ?config(apps, Config),
  59. State = ?config(state, Config),
  60. Name = ?config(app_name, Config),
  61. RebarConfig = ?config(rebar_config, Config),
  62. Result = rebar3:run(rebar_state:new(State, RebarConfig, AppDir), ["xref"]),
  63. verify_results(xref_test, Name, Result).
  64. xref_ignore_test(Config) ->
  65. AppDir = ?config(apps, Config),
  66. State = ?config(state, Config),
  67. Name = ?config(app_name, Config),
  68. RebarConfig = ?config(rebar_config, Config),
  69. Result = rebar3:run(rebar_state:new(State, RebarConfig, AppDir), ["xref"]),
  70. verify_results(xref_ignore_test, Name, Result).
  71. xref_dep_hook(Config) ->
  72. rebar_test_utils:run_and_check(Config, [], ["compile"], {ok, []}).
  73. xref_undef_behaviour(Config) ->
  74. AppDir = ?config(apps, Config),
  75. State = ?config(state, Config),
  76. Name = ?config(app_name, Config),
  77. RebarConfig = ?config(rebar_config, Config),
  78. %% delete one of the behaviours, which should create new warnings
  79. delete_src_file(AppDir, Name, behaviour1),
  80. %% just ensure this does not crash
  81. Result = rebar3:run(rebar_state:new(State, RebarConfig, AppDir), ["xref"]),
  82. verify_results(xref_undef_behaviour, Name, Result).
  83. %% ===================================================================
  84. %% Helper functions
  85. %% ===================================================================
  86. ignore_xref(xref_ignore_test) ->
  87. true;
  88. ignore_xref(_) ->
  89. false.
  90. verify_results(TestCase, AppName, Results) ->
  91. {error, {rebar_prv_xref,
  92. {xref_issues, XrefResults, QueryResults}}} = Results,
  93. verify_test_results(TestCase, AppName, XrefResults, QueryResults).
  94. verify_test_results(xref_test, AppName, XrefResults, _QueryResults) ->
  95. AppModules = ["behaviour1", "behaviour2", "mymod", "othermod", "somemod"],
  96. [Behaviour1Mod, Behaviour2Mod, MyMod, OtherMod, SomeMod] =
  97. [list_to_atom(AppName ++ "_" ++ Mod) || Mod <- AppModules],
  98. UndefFuns = proplists:get_value(undefined_functions, XrefResults),
  99. UndefFunCalls = proplists:get_value(undefined_function_calls, XrefResults),
  100. LocalsNotUsed = proplists:get_value(locals_not_used, XrefResults),
  101. ExportsNotUsed = proplists:get_value(exports_not_used, XrefResults),
  102. DeprecatedFuns = proplists:get_value(deprecated_functions, XrefResults),
  103. DeprecatedFunCalls = proplists:get_value(deprecated_function_calls, XrefResults),
  104. ?assert(lists:member({SomeMod, notavailable, 1}, UndefFuns)),
  105. ?assert(lists:member({{OtherMod, somefunc, 0}, {SomeMod, notavailable, 1}},
  106. UndefFunCalls)),
  107. ?assert(lists:member({MyMod, fdeprecated, 0}, DeprecatedFuns)),
  108. ?assert(lists:member({{OtherMod, somefunc, 0}, {MyMod, fdeprecated, 0}},
  109. DeprecatedFunCalls)),
  110. ?assert(lists:member({MyMod, localfunc2, 0}, LocalsNotUsed)),
  111. ?assert(lists:member({Behaviour1Mod, behaviour_info, 1}, ExportsNotUsed)),
  112. ?assert(lists:member({Behaviour2Mod, behaviour_info, 1}, ExportsNotUsed)),
  113. ?assert(lists:member({MyMod, other2, 1}, ExportsNotUsed)),
  114. ?assert(lists:member({OtherMod, somefunc, 0}, ExportsNotUsed)),
  115. ?assertNot(lists:member({MyMod, bh1_a, 1}, ExportsNotUsed)),
  116. ?assertNot(lists:member({MyMod, bh1_b, 1}, ExportsNotUsed)),
  117. ?assertNot(lists:member({MyMod, bh2_a, 1}, ExportsNotUsed)),
  118. ?assertNot(lists:member({MyMod, bh2_b, 1}, ExportsNotUsed)),
  119. ok;
  120. verify_test_results(xref_undef_behaviour, AppName, XrefResults, _QueryResults) ->
  121. AppModules = ["behaviour2", "mymod", "othermod", "somemod"],
  122. [Behaviour2Mod, MyMod, OtherMod, SomeMod] =
  123. [list_to_atom(AppName ++ "_" ++ Mod) || Mod <- AppModules],
  124. UndefFuns = proplists:get_value(undefined_functions, XrefResults),
  125. UndefFunCalls = proplists:get_value(undefined_function_calls, XrefResults),
  126. LocalsNotUsed = proplists:get_value(locals_not_used, XrefResults),
  127. ExportsNotUsed = proplists:get_value(exports_not_used, XrefResults),
  128. DeprecatedFuns = proplists:get_value(deprecated_functions, XrefResults),
  129. DeprecatedFunCalls = proplists:get_value(deprecated_function_calls, XrefResults),
  130. ?assert(lists:member({SomeMod, notavailable, 1}, UndefFuns)),
  131. ?assert(lists:member({{OtherMod, somefunc, 0}, {SomeMod, notavailable, 1}},
  132. UndefFunCalls)),
  133. ?assert(lists:member({MyMod, fdeprecated, 0}, DeprecatedFuns)),
  134. ?assert(lists:member({{OtherMod, somefunc, 0}, {MyMod, fdeprecated, 0}},
  135. DeprecatedFunCalls)),
  136. ?assert(lists:member({MyMod, localfunc2, 0}, LocalsNotUsed)),
  137. ?assert(lists:member({Behaviour2Mod, behaviour_info, 1}, ExportsNotUsed)),
  138. ?assert(lists:member({MyMod, other2, 1}, ExportsNotUsed)),
  139. ?assert(lists:member({OtherMod, somefunc, 0}, ExportsNotUsed)),
  140. ?assert(lists:member({MyMod, bh1_a, 1}, ExportsNotUsed)),
  141. ?assert(lists:member({MyMod, bh1_b, 1}, ExportsNotUsed)),
  142. ?assertNot(lists:member({MyMod, bh2_a, 1}, ExportsNotUsed)),
  143. ?assertNot(lists:member({MyMod, bh2_b, 1}, ExportsNotUsed)),
  144. ok;
  145. verify_test_results(xref_ignore_test, AppName, XrefResults, _QueryResults) ->
  146. AppModules = ["behaviour1", "behaviour2", "mymod", "othermod", "somemod"],
  147. [_Behaviour1Mod, _Behaviour2Mod, _MyMod, _OtherMod, SomeMod] =
  148. [list_to_atom(AppName ++ "_" ++ Mod) || Mod <- AppModules],
  149. UndefFuns = proplists:get_value(undefined_functions, XrefResults),
  150. ?assertNot(lists:keymember(undefined_function_calls, 1, XrefResults)),
  151. ?assertNot(lists:keymember(locals_not_used, 1, XrefResults)),
  152. ?assertNot(lists:keymember(exports_not_used, 1, XrefResults)),
  153. ?assertNot(lists:keymember(deprecated_functions, 1, XrefResults)),
  154. ?assertNot(lists:keymember(deprecated_function_calls, 1, XrefResults)),
  155. ?assert(lists:member({SomeMod, notavailable, 1}, UndefFuns)),
  156. ok.
  157. write_src_file(Dir, AppName, Module, IgnoreXref) ->
  158. Erl = filename:join([Dir, "src", module_name(AppName, Module)]),
  159. ok = filelib:ensure_dir(Erl),
  160. ok = ec_file:write(Erl, get_module_body(Module, AppName, IgnoreXref)).
  161. delete_src_file(Dir, AppName, Module) ->
  162. Erl = filename:join([Dir, "src", module_name(AppName, Module)]),
  163. ok = file:delete(Erl).
  164. module_name(AppName, Module) ->
  165. lists:flatten([AppName, "_", atom_to_list(Module), ".erl"]).
  166. get_module_body(behaviour1, AppName, IgnoreXref) ->
  167. ["-module(", AppName, "_behaviour1).\n",
  168. "-export([behaviour_info/1]).\n",
  169. ["-ignore_xref({behaviour_info,1}).\n" || X <- [IgnoreXref], X =:= true],
  170. "behaviour_info(callbacks) -> [{bh1_a,1},{bh1_b,1}];\n",
  171. "behaviour_info(_Other) -> undefined.\n"];
  172. get_module_body(behaviour2, AppName, IgnoreXref) ->
  173. ["-module(", AppName, "_behaviour2).\n",
  174. "-export([behaviour_info/1]).\n",
  175. ["-ignore_xref({behaviour_info,1}).\n" || X <- [IgnoreXref], X =:= true],
  176. "behaviour_info(callbacks) -> [{bh2_a,1},{bh2_b,1}];\n",
  177. "behaviour_info(_Other) -> undefined.\n"];
  178. get_module_body(mymod, AppName, IgnoreXref) ->
  179. ["-module(", AppName, "_mymod).\n",
  180. "-export([bh1_a/1,bh1_b/1,bh2_a/1,bh2_b/1,"
  181. "other1/1,other2/1,fdeprecated/0]).\n",
  182. ["-ignore_xref([{other2,1},{localfunc2,0},{fdeprecated,0}]).\n"
  183. || X <- [IgnoreXref], X =:= true],
  184. "-behaviour(", AppName, "_behaviour1).\n", % 2 behaviours
  185. "-behavior(", AppName, "_behaviour2).\n",
  186. "-deprecated({fdeprecated,0}).\n", % deprecated function
  187. "bh1_a(A) -> localfunc1(bh1_a, A).\n", % behaviour functions
  188. "bh1_b(A) -> localfunc1(bh1_b, A).\n",
  189. "bh2_a(A) -> localfunc1(bh2_a, A).\n",
  190. "bh2_b(A) -> localfunc1(bh2_b, A).\n",
  191. "other1(A) -> localfunc1(other1, A).\n", % regular exported functions
  192. "other2(A) -> localfunc1(other2, A).\n",
  193. "localfunc1(A, B) -> {A, B}.\n", % used local
  194. "localfunc2() -> ok.\n", % unused local
  195. "fdeprecated() -> ok.\n" % deprecated function
  196. ];
  197. get_module_body(othermod, AppName, IgnoreXref) ->
  198. ["-module(", AppName, "_othermod).\n",
  199. "-export([somefunc/0]).\n",
  200. [["-ignore_xref([{", AppName, "_somemod,notavailable,1},{somefunc,0}]).\n",
  201. "-ignore_xref({", AppName, "_mymod,fdeprecated,0}).\n"]
  202. || X <- [IgnoreXref], X =:= true],
  203. "somefunc() ->\n",
  204. " ", AppName, "_mymod:other1(arg),\n",
  205. " ", AppName, "_somemod:notavailable(arg),\n",
  206. " ", AppName, "_mymod:fdeprecated().\n"].