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.

190 lines
8.4 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. {ok, OrigDir} = file:get_cwd(),
  28. file:set_cwd(AppDir),
  29. Name = rebar_test_utils:create_random_name("xrefapp_"),
  30. Vsn = rebar_test_utils:create_random_vsn(),
  31. rebar_test_utils:create_empty_app(AppDir, Name, Vsn, [kernel, stdlib]),
  32. AppModules = [behaviour1, behaviour2, mymod, othermod],
  33. [write_src_file(AppDir, Name, Module, ignore_xref(Case)) || Module <- AppModules],
  34. RebarConfig = [{erl_opts, [debug_info]},
  35. {xref_checks, [deprecated_function_calls,deprecated_functions,
  36. undefined_function_calls,undefined_functions,
  37. exports_not_used,locals_not_used]}],
  38. [{app_name, Name},
  39. {rebar_config, RebarConfig},
  40. {orig_dir, OrigDir} | UpdConfig].
  41. end_per_testcase(_, Config) ->
  42. ?debugMsg("End test case cleanup"),
  43. AppDir = ?config(apps, Config),
  44. OrigDir = ?config(orig_dir, Config),
  45. %% Code path cleanup because we set the CWD to the `AppDir' prior
  46. %% to launching rebar and these paths make it into the code path
  47. %% before the xref module executes so they don't get cleaned up
  48. %% automatically after the xref run. Only have to do this because
  49. %% we are about to remove the directory and there may be
  50. %% subsequent test cases that error out when the code path tries
  51. %% to include one of these soon-to-be nonexistent directories.
  52. true = code:del_path(AppDir ++ "/."),
  53. true = code:del_path(rebar_dir:ebin_dir()),
  54. file:set_cwd(OrigDir),
  55. ec_file:remove(AppDir, [recursive]),
  56. ok.
  57. all() ->
  58. [xref_test, xref_ignore_test].
  59. %% ===================================================================
  60. %% Test cases
  61. %% ===================================================================
  62. xref_test(Config) ->
  63. AppDir = ?config(apps, Config),
  64. State = ?config(state, Config),
  65. Name = ?config(app_name, Config),
  66. RebarConfig = ?config(rebar_config, Config),
  67. Result = rebar3:run(rebar_state:new(State, RebarConfig, AppDir), ["xref"]),
  68. verify_results(xref_test, Name, Result).
  69. xref_ignore_test(Config) ->
  70. AppDir = ?config(apps, Config),
  71. State = ?config(state, Config),
  72. Name = ?config(app_name, Config),
  73. RebarConfig = ?config(rebar_config, Config),
  74. Result = rebar3:run(rebar_state:new(State, RebarConfig, AppDir), ["xref"]),
  75. verify_results(xref_ignore_test, Name, Result).
  76. %% ===================================================================
  77. %% Helper functions
  78. %% ===================================================================
  79. ignore_xref(xref_ignore_test) ->
  80. true;
  81. ignore_xref(_) ->
  82. false.
  83. verify_results(TestCase, AppName, Results) ->
  84. {error, {rebar_prv_xref,
  85. {xref_issues, XrefResults, QueryResults}}} = Results,
  86. verify_test_results(TestCase, AppName, XrefResults, QueryResults).
  87. verify_test_results(xref_test, AppName, XrefResults, _QueryResults) ->
  88. AppModules = ["behaviour1", "behaviour2", "mymod", "othermod", "somemod"],
  89. [Behaviour1Mod, Behaviour2Mod, MyMod, OtherMod, SomeMod] =
  90. [list_to_atom(AppName ++ "_" ++ Mod) || Mod <- AppModules],
  91. UndefFuns = proplists:get_value(undefined_functions, XrefResults),
  92. UndefFunCalls = proplists:get_value(undefined_function_calls, XrefResults),
  93. LocalsNotUsed = proplists:get_value(locals_not_used, XrefResults),
  94. ExportsNotUsed = proplists:get_value(exports_not_used, XrefResults),
  95. DeprecatedFuns = proplists:get_value(deprecated_functions, XrefResults),
  96. DeprecatedFunCalls = proplists:get_value(deprecated_function_calls, XrefResults),
  97. ?assert(lists:member({SomeMod, notavailable, 1}, UndefFuns)),
  98. ?assert(lists:member({{OtherMod, somefunc, 0}, {SomeMod, notavailable, 1}},
  99. UndefFunCalls)),
  100. ?assert(lists:member({MyMod, fdeprecated, 0}, DeprecatedFuns)),
  101. ?assert(lists:member({{OtherMod, somefunc, 0}, {MyMod, fdeprecated, 0}},
  102. DeprecatedFunCalls)),
  103. ?assert(lists:member({MyMod, localfunc2, 0}, LocalsNotUsed)),
  104. ?assert(lists:member({Behaviour1Mod, behaviour_info, 1}, ExportsNotUsed)),
  105. ?assert(lists:member({Behaviour2Mod, behaviour_info, 1}, ExportsNotUsed)),
  106. ?assert(lists:member({MyMod, other2, 1}, ExportsNotUsed)),
  107. ?assert(lists:member({OtherMod, somefunc, 0}, ExportsNotUsed)),
  108. ?assertNot(lists:member({MyMod, bh1_a, 1}, ExportsNotUsed)),
  109. ?assertNot(lists:member({MyMod, bh1_b, 1}, ExportsNotUsed)),
  110. ?assertNot(lists:member({MyMod, bh2_a, 1}, ExportsNotUsed)),
  111. ?assertNot(lists:member({MyMod, bh2_b, 1}, ExportsNotUsed)),
  112. ok;
  113. verify_test_results(xref_ignore_test, AppName, XrefResults, _QueryResults) ->
  114. AppModules = ["behaviour1", "behaviour2", "mymod", "othermod", "somemod"],
  115. [Behaviour1Mod, Behaviour2Mod, MyMod, OtherMod, SomeMod] =
  116. [list_to_atom(AppName ++ "_" ++ Mod) || Mod <- AppModules],
  117. UndefFuns = proplists:get_value(undefined_functions, XrefResults),
  118. ?assertNot(lists:keymember(undefined_function_calls, 1, XrefResults)),
  119. ?assertNot(lists:keymember(locals_not_used, 1, XrefResults)),
  120. ?assertNot(lists:keymember(exports_not_used, 1, XrefResults)),
  121. ?assertNot(lists:keymember(deprecated_functions, 1, XrefResults)),
  122. ?assertNot(lists:keymember(deprecated_function_calls, 1, XrefResults)),
  123. ?assert(lists:member({SomeMod, notavailable, 1}, UndefFuns)),
  124. ok.
  125. write_src_file(Dir, AppName, Module, IgnoreXref) ->
  126. Erl = filename:join([Dir, "src", module_name(AppName, Module)]),
  127. ok = filelib:ensure_dir(Erl),
  128. ok = ec_file:write(Erl, get_module_body(Module, AppName, IgnoreXref)).
  129. module_name(AppName, Module) ->
  130. lists:flatten([AppName, "_", atom_to_list(Module), ".erl"]).
  131. get_module_body(behaviour1, AppName, IgnoreXref) ->
  132. ["-module(", AppName, "_behaviour1).\n",
  133. "-export([behaviour_info/1]).\n",
  134. ["-ignore_xref({behaviour_info,1}).\n" || X <- [IgnoreXref], X =:= true],
  135. "behaviour_info(callbacks) -> [{bh1_a,1},{bh1_b,1}];\n",
  136. "behaviour_info(_Other) -> undefined.\n"];
  137. get_module_body(behaviour2, AppName, IgnoreXref) ->
  138. ["-module(", AppName, "_behaviour2).\n",
  139. "-export([behaviour_info/1]).\n",
  140. ["-ignore_xref({behaviour_info,1}).\n" || X <- [IgnoreXref], X =:= true],
  141. "behaviour_info(callbacks) -> [{bh2_a,1},{bh2_b,1}];\n",
  142. "behaviour_info(_Other) -> undefined.\n"];
  143. get_module_body(mymod, AppName, IgnoreXref) ->
  144. ["-module(", AppName, "_mymod).\n",
  145. "-export([bh1_a/1,bh1_b/1,bh2_a/1,bh2_b/1,"
  146. "other1/1,other2/1,fdeprecated/0]).\n",
  147. ["-ignore_xref([{other2,1},{localfunc2,0},{fdeprecated,0}]).\n"
  148. || X <- [IgnoreXref], X =:= true],
  149. "-behaviour(", AppName, "_behaviour1).\n", % 2 behaviours
  150. "-behaviour(", AppName, "_behaviour2).\n",
  151. "-deprecated({fdeprecated,0}).\n", % deprecated function
  152. "bh1_a(A) -> localfunc1(bh1_a, A).\n", % behaviour functions
  153. "bh1_b(A) -> localfunc1(bh1_b, A).\n",
  154. "bh2_a(A) -> localfunc1(bh2_a, A).\n",
  155. "bh2_b(A) -> localfunc1(bh2_b, A).\n",
  156. "other1(A) -> localfunc1(other1, A).\n", % regular exported functions
  157. "other2(A) -> localfunc1(other2, A).\n",
  158. "localfunc1(A, B) -> {A, B}.\n", % used local
  159. "localfunc2() -> ok.\n", % unused local
  160. "fdeprecated() -> ok.\n" % deprecated function
  161. ];
  162. get_module_body(othermod, AppName, IgnoreXref) ->
  163. ["-module(", AppName, "_othermod).\n",
  164. "-export([somefunc/0]).\n",
  165. [["-ignore_xref([{", AppName, "_somemod,notavailable,1},{somefunc,0}]).\n",
  166. "-ignore_xref({", AppName, "_mymod,fdeprecated,0}).\n"]
  167. || X <- [IgnoreXref], X =:= true],
  168. "somefunc() ->\n",
  169. " ", AppName, "_mymod:other1(arg),\n",
  170. " ", AppName, "_somemod:notavailable(arg),\n",
  171. " ", AppName, "_mymod:fdeprecated().\n"].