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.

174 line
7.6 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. -include_lib("common_test/include/ct.hrl").
  13. -include_lib("eunit/include/eunit.hrl").
  14. -include_lib("kernel/include/file.hrl").
  15. %% ===================================================================
  16. %% common_test callbacks
  17. %% ===================================================================
  18. suite() ->
  19. [].
  20. init_per_suite(Config) ->
  21. Config.
  22. end_per_suite(_Config) ->
  23. ok.
  24. init_per_testcase(Case, Config) ->
  25. UpdConfig = rebar_test_utils:init_rebar_state(Config),
  26. AppDir = ?config(apps, UpdConfig),
  27. file:set_cwd(AppDir),
  28. Name = rebar_test_utils:create_random_name("xrefapp_"),
  29. Vsn = rebar_test_utils:create_random_vsn(),
  30. rebar_test_utils:create_empty_app(AppDir, Name, Vsn, [kernel, stdlib]),
  31. AppModules = [behaviour1, behaviour2, mymod, othermod],
  32. [write_src_file(AppDir, Name, Module, ignore_xref(Case)) || Module <- AppModules],
  33. RebarConfig = [{erl_opts, [debug_info]},
  34. {xref_checks, [deprecated_function_calls,deprecated_functions,
  35. undefined_function_calls,undefined_functions,
  36. exports_not_used,locals_not_used]}],
  37. [{app_name, Name},
  38. {rebar_config, RebarConfig} | UpdConfig].
  39. end_per_testcase(_, _Config) ->
  40. ok.
  41. all() ->
  42. [xref_test, xref_ignore_test].
  43. %% ===================================================================
  44. %% Test cases
  45. %% ===================================================================
  46. xref_test(Config) ->
  47. AppDir = ?config(apps, Config),
  48. State = ?config(state, Config),
  49. Name = ?config(app_name, Config),
  50. RebarConfig = ?config(rebar_config, Config),
  51. Result = rebar3:run(rebar_state:new(State, RebarConfig, AppDir), ["xref"]),
  52. verify_results(xref_test, Name, Result).
  53. xref_ignore_test(Config) ->
  54. AppDir = ?config(apps, Config),
  55. State = ?config(state, Config),
  56. Name = ?config(app_name, Config),
  57. RebarConfig = ?config(rebar_config, Config),
  58. Result = rebar3:run(rebar_state:new(State, RebarConfig, AppDir), ["xref"]),
  59. verify_results(xref_ignore_test, Name, Result).
  60. %% ===================================================================
  61. %% Helper functions
  62. %% ===================================================================
  63. ignore_xref(xref_ignore_test) ->
  64. true;
  65. ignore_xref(_) ->
  66. false.
  67. verify_results(TestCase, AppName, Results) ->
  68. {error, {rebar_prv_xref,
  69. {xref_issues, XrefResults, QueryResults}}} = Results,
  70. verify_test_results(TestCase, AppName, XrefResults, QueryResults).
  71. verify_test_results(xref_test, AppName, XrefResults, _QueryResults) ->
  72. AppModules = ["behaviour1", "behaviour2", "mymod", "othermod", "somemod"],
  73. [Behaviour1Mod, Behaviour2Mod, MyMod, OtherMod, SomeMod] =
  74. [list_to_atom(AppName ++ "_" ++ Mod) || Mod <- AppModules],
  75. UndefFuns = proplists:get_value(undefined_functions, XrefResults),
  76. UndefFunCalls = proplists:get_value(undefined_function_calls, XrefResults),
  77. LocalsNotUsed = proplists:get_value(locals_not_used, XrefResults),
  78. ExportsNotUsed = proplists:get_value(exports_not_used, XrefResults),
  79. DeprecatedFuns = proplists:get_value(deprecated_functions, XrefResults),
  80. DeprecatedFunCalls = proplists:get_value(deprecated_function_calls, XrefResults),
  81. ?assert(lists:member({SomeMod, notavailable, 1}, UndefFuns)),
  82. ?assert(lists:member({{OtherMod, somefunc, 0}, {SomeMod, notavailable, 1}},
  83. UndefFunCalls)),
  84. ?assert(lists:member({MyMod, fdeprecated, 0}, DeprecatedFuns)),
  85. ?assert(lists:member({{OtherMod, somefunc, 0}, {MyMod, fdeprecated, 0}},
  86. DeprecatedFunCalls)),
  87. ?assert(lists:member({MyMod, localfunc2, 0}, LocalsNotUsed)),
  88. ?assert(lists:member({Behaviour1Mod, behaviour_info, 1}, ExportsNotUsed)),
  89. ?assert(lists:member({Behaviour2Mod, behaviour_info, 1}, ExportsNotUsed)),
  90. ?assert(lists:member({MyMod, other2, 1}, ExportsNotUsed)),
  91. ?assert(lists:member({OtherMod, somefunc, 0}, ExportsNotUsed)),
  92. ?assertNot(lists:member({MyMod, bh1_a, 1}, ExportsNotUsed)),
  93. ?assertNot(lists:member({MyMod, bh1_b, 1}, ExportsNotUsed)),
  94. ?assertNot(lists:member({MyMod, bh2_a, 1}, ExportsNotUsed)),
  95. ?assertNot(lists:member({MyMod, bh2_b, 1}, ExportsNotUsed)),
  96. ok;
  97. verify_test_results(xref_ignore_test, AppName, XrefResults, _QueryResults) ->
  98. AppModules = ["behaviour1", "behaviour2", "mymod", "othermod", "somemod"],
  99. [_Behaviour1Mod, _Behaviour2Mod, _MyMod, _OtherMod, SomeMod] =
  100. [list_to_atom(AppName ++ "_" ++ Mod) || Mod <- AppModules],
  101. UndefFuns = proplists:get_value(undefined_functions, XrefResults),
  102. ?assertNot(lists:keymember(undefined_function_calls, 1, XrefResults)),
  103. ?assertNot(lists:keymember(locals_not_used, 1, XrefResults)),
  104. ?assertNot(lists:keymember(exports_not_used, 1, XrefResults)),
  105. ?assertNot(lists:keymember(deprecated_functions, 1, XrefResults)),
  106. ?assertNot(lists:keymember(deprecated_function_calls, 1, XrefResults)),
  107. ?assert(lists:member({SomeMod, notavailable, 1}, UndefFuns)),
  108. ok.
  109. write_src_file(Dir, AppName, Module, IgnoreXref) ->
  110. Erl = filename:join([Dir, "src", module_name(AppName, Module)]),
  111. ok = filelib:ensure_dir(Erl),
  112. ok = ec_file:write(Erl, get_module_body(Module, AppName, IgnoreXref)).
  113. module_name(AppName, Module) ->
  114. lists:flatten([AppName, "_", atom_to_list(Module), ".erl"]).
  115. get_module_body(behaviour1, AppName, IgnoreXref) ->
  116. ["-module(", AppName, "_behaviour1).\n",
  117. "-export([behaviour_info/1]).\n",
  118. ["-ignore_xref({behaviour_info,1}).\n" || X <- [IgnoreXref], X =:= true],
  119. "behaviour_info(callbacks) -> [{bh1_a,1},{bh1_b,1}];\n",
  120. "behaviour_info(_Other) -> undefined.\n"];
  121. get_module_body(behaviour2, AppName, IgnoreXref) ->
  122. ["-module(", AppName, "_behaviour2).\n",
  123. "-export([behaviour_info/1]).\n",
  124. ["-ignore_xref({behaviour_info,1}).\n" || X <- [IgnoreXref], X =:= true],
  125. "behaviour_info(callbacks) -> [{bh2_a,1},{bh2_b,1}];\n",
  126. "behaviour_info(_Other) -> undefined.\n"];
  127. get_module_body(mymod, AppName, IgnoreXref) ->
  128. ["-module(", AppName, "_mymod).\n",
  129. "-export([bh1_a/1,bh1_b/1,bh2_a/1,bh2_b/1,"
  130. "other1/1,other2/1,fdeprecated/0]).\n",
  131. ["-ignore_xref([{other2,1},{localfunc2,0},{fdeprecated,0}]).\n"
  132. || X <- [IgnoreXref], X =:= true],
  133. "-behaviour(", AppName, "_behaviour1).\n", % 2 behaviours
  134. "-behavior(", AppName, "_behaviour2).\n",
  135. "-deprecated({fdeprecated,0}).\n", % deprecated function
  136. "bh1_a(A) -> localfunc1(bh1_a, A).\n", % behaviour functions
  137. "bh1_b(A) -> localfunc1(bh1_b, A).\n",
  138. "bh2_a(A) -> localfunc1(bh2_a, A).\n",
  139. "bh2_b(A) -> localfunc1(bh2_b, A).\n",
  140. "other1(A) -> localfunc1(other1, A).\n", % regular exported functions
  141. "other2(A) -> localfunc1(other2, A).\n",
  142. "localfunc1(A, B) -> {A, B}.\n", % used local
  143. "localfunc2() -> ok.\n", % unused local
  144. "fdeprecated() -> ok.\n" % deprecated function
  145. ];
  146. get_module_body(othermod, AppName, IgnoreXref) ->
  147. ["-module(", AppName, "_othermod).\n",
  148. "-export([somefunc/0]).\n",
  149. [["-ignore_xref([{", AppName, "_somemod,notavailable,1},{somefunc,0}]).\n",
  150. "-ignore_xref({", AppName, "_mymod,fdeprecated,0}).\n"]
  151. || X <- [IgnoreXref], X =:= true],
  152. "somefunc() ->\n",
  153. " ", AppName, "_mymod:other1(arg),\n",
  154. " ", AppName, "_somemod:notavailable(arg),\n",
  155. " ", AppName, "_mymod:fdeprecated().\n"].